When hosting multiple Drupal sites on a shared domain, users are unable to log into more than one site because all the sites use the same session cookie name. The cookie can only contain the session of the last site they log into.

The session cookie prefix should be configurable in settings.php for upstream processing by a reverse proxy, and a database hash should be included in the cookie name so even sites that do not set a custom cookie prefix will not overwrite another site's session cookie. The current prefix is SESS for HTTP connections and SSESS for secure-only cookies. Our fix allows a configurable prefix to be added before the SESS or SSESS prefix.

Issue fork drupal-1361742

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

erikwebb’s picture

erikwebb’s picture

Status: Active » Needs review
erikwebb’s picture

cashwilliams’s picture

+1

ultimateboy’s picture

ultimateboy’s picture

Issue summary: View changes

Updated wording to mention secure cookies, rather than HTTPS.

Status: Needs review » Needs work

The last submitted patch, 1: allow-configurable-cookie-prefix-1361742-1.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

botanic_spark’s picture

Issue summary: View changes
StatusFileSize
new1.68 KB

I refactored the patch so that it's applicable to 8.9 and D9

botanic_spark’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 16: allow-configurable-cookie-prefix-1361742-16.patch, failed testing. View results

BS Pavan’s picture

Status: Needs work » Needs review
StatusFileSize
new2.47 KB
new1.97 KB

Fixed fail test cases.

rockthedrop’s picture

Status: Needs review » Needs work

The last submitted patch, 20: allow-configurable-cookie-prefix-1361742-19.patch, failed testing. View results

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
darren oh’s picture

Another use for a custom cookie prefix is to prevent sites that don't have their subdomains set up hierarchically from overwriting each other's cookies. Example:

site.a.example.com and site.b.example.com are different locales for the same site. They use .example.com as their cookie domain.
anothersite.a.example.com and anothersite.b.example.com also share the .example.com cookie domain, and they overwrite the other site's cookies.

darren oh’s picture

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ambient.impact’s picture

I'm glad to see some movement on this issue, but please don't prefix "S" or anything else in a secure context, as it defeats the main reason I and possibly many others need this configurability: the __Secure- and __Host- prefixes which allow a higher level of security in all modern browsers.

To quote the MDN article on the Set-Cookie header:

Note: Some <cookie-name> have a specific semantic:

__Secure- prefix: Cookies with names starting with __Secure- (dash is part of the prefix)
must be set with the secure flag from a secure page (HTTPS).

__Host- prefix: Cookies with names starting with __Host- must be set with the secure flag, must be from a secure page (HTTPS), must not have a domain specified (and therefore, are not sent to subdomains), and the path must be /.

To quote Tough Cookies by Scott Helme:

Another great addition to help create tough cookies is the cookie prefix. This approach was devised to "smuggle cookie state to the server within the confines of the existing Cookie request header syntax". In other words, by implementing a new security feature without requiring changes/patches to servers/applications we are going to see much higher adoption a lot more quickly. There are 2 different prefixes that you can add to your cookie.

darren oh’s picture

darren oh’s picture

Version: 9.5.x-dev » 10.1.x-dev

darren oh’s picture

Status: Needs work » Needs review
ambient.impact’s picture

Version: 10.1.x-dev » 9.5.x-dev
Status: Needs review » Needs work

Just tested this out and it works great for my use case as described above! My only suggestion is to rephrase the docblock slightly as the wording feels off. Current one:

/**
 * Drupal creates a cookie name consisting of a static prefix and a site-
 * specific name that is automatically created. When hosting multiple sites
 * under a single domain, it may be desirable to add a unique prefix for sites
 * to easily filter these in reverse proxies or utilities.
 */

I suggest this, which adds a first line to match other docblocks and also rewrites the first sentence to flow a bit better and not use "create" twice in a way that felt ambiguous:

/**
 * Session cookie prefix:
 *
 * Drupal creates a session cookie name consisting of a static prefix and a
 * site-specific name. When hosting multiple sites under a single domain, it
 * may be desirable to add a unique prefix for sites to easily filter these in
 * reverse proxies or utilities.
 */
ambient.impact’s picture

Version: 9.5.x-dev » 10.1.x-dev
Status: Needs work » Needs review

Whoops, did not mean to change these.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests, +Needs framework manager review

Tagging for framework manager reviews for their thoughts.

Either way think this will require a test case for the new feature.

Also MR has failures.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

poker10’s picture

Thanks for working on this @Darren Oh!

I would say we need to split the patch, because it seems to me that it is combining two separate changes:

Adding a cookie prefix:

@@ -66,13 +67,14 @@ public function getOptions(Request $request) {
+    $prefix = Settings::get('session_cookie_prefix', '');
     ...
-    $prefix = $request->isSecure() ? 'SSESS' : 'SESS';
+    $prefix .= $request->isSecure() ? 'SSESS' : 'SESS';

Fixing a problem with overwriting site cookies:

@@ -102,6 +104,10 @@ protected function getUnprefixedName(Request $request) {
+    // Add hash salt to prevent sites on shared domains with separate databases
+    // from overwriting each other's cookies.
+    $session_name .= Settings::get('hash_salt', '');

I think the second change (seems like a bug-fix?) should be in a separate issue. Or the issue title + summary needs to be updated and explained why there are these two changes together. Thanks!

darren oh’s picture

Title: Allow configurable cookie prefix » Fix cookie conflicts on shared domains
Issue summary: View changes
darren oh’s picture

Issue summary: View changes

poker10, I have updated the issue description. Both changes are addressing the same bug. The configurable prefix was the original solution, but it requires site owners to take action to prevent the bug. I added the database hash as a more reliable solution that does not require action from site owners. I believe both solutions are required because the same bug affects reverse proxies that filter cookies, and the database hash does not fix that.

darren oh’s picture

Issue summary: View changes
darren oh’s picture

Status: Needs work » Needs review
damienmckenna’s picture

Status: Needs review » Needs work

This could be tested by leaving the prefix blank, logging into the site, changing the prefix, reloading the site and confirming that the session is no longer logged in, then removing the prefix and confirming the original session is logged in again; might be a little fiddly to do, but it'd be simpler than trying to create concurrent installs of core.

And I suspect the test coverage failures are related to the addition of the hash_salt setting to the session name, maybe this should be optional and it should just use the prefix?

sadikyalcin’s picture

What's the state with this? I see the last activity was 2 years ago. I've got the same issue right now. This change should be in core IMO.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

martygraphie’s picture

Status: Needs work » Needs review
StatusFileSize
new4.4 KB

Hi,
I am attaching a proposed patch for this issue, including a unit test to validate the behavior.

The implementation has been tested successfully on a Drupal 11 instance and appears to work correctly.

For maintainability reasons and to ease compatibility with future Drupal core updates, I am proposing a version of the patch that does not rely on the hash salt, along with dedicated test coverage.

I was not able to update the issue repository, which is still based on an older Drupal version (Drupal 10), so I am providing the patch file directly.
Marc