Set SameSite on session cookies

Created on 10 June 2020, over 4 years ago
Updated 14 March 2023, over 1 year ago

On a new Drupal 9 install, the session cookie of a logged in user, does not have a "SameSite" attribute at all.

This attribute should at least be explicitly set to Lax.

As far as I am concerned though, I would set the value to Strict, at least if I have a security sensitive site/application.

So, my question is: Is there a valid reason for not setting this cookie attribute at all?

Furthermore, is there a valid reason for not setting this cookie attribute to the default secure option of "Strict" (with the ability to be relaxed/configured maybe through a setting or in settings.php)?

I know that this issue is also Symphony related. I am not super familiar with the core, so I am trying to start an educated discussion on the topic and include better informed persons too.

Possible usefull links:
https://www.drupal.org/project/drupal/issues/3027872 β†’
https://www.drupal.org/forum/support/module-development-and-code-questio... β†’

Release notes snippet

Drupal now adds 'Samesite: Lax' as a session cookie attribute by default β†’ . This is configurable in default.services.yml

πŸ“Œ Task
Status

Fixed

Version

10.1 ✨

Component
BaseΒ  β†’

Last updated about 1 hour ago

Created by

πŸ‡¬πŸ‡·Greece vagelis-prokopiou

Live updates comments and jobs are added and updated live.
  • Security improvements

    It makes Drupal less vulnerable to abuse or misuse. Note, this is the preferred tag, though the Security tag has a large body of issues tagged to it. Do NOT publicly disclose security vulnerabilities; contact the security team instead. Anyone (whether security team or not) can apply this tag to security improvements that do not directly present a vulnerability e.g. hardening an API to add filtering to reduce a common mistake in contributed modules.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    "Undefined" or "not set"?

  • "Undefined" in this context seems to imply something random could happen. I would prefer "not set" or "no value".

  • πŸ‡§πŸ‡¬Bulgaria pfrenssen Sofia

    Fixed coding standards and remarks from comments #77-80 in the MR. Here is a backport of the latest iteration to D9.5.

  • πŸ‡²πŸ‡ΎMalaysia muaz91 MY

    Test out the patch from #81. it help out the SameSite cookie problem for me. +1 for RTBC

  • πŸ‡§πŸ‡¬Bulgaria pfrenssen Sofia

    Last test failure was unrelated. Rebased on latest 10.1.x.

  • πŸ‡ΊπŸ‡ΈUnited States m-simmons

    Unfortunately, #81 does not apply to Drupal 9.5.3.

  • πŸ‡·πŸ‡ΊRussia Chi

    Nitpick. This issue is for Drupal 10 which means we can use PHP 8.1 syntax here.

    - $valid = in_array($samesite, ['Lax', 'Strict'], TRUE) || ($samesite === 'None' && $request_object->isSecure());
    + $valid = match ($samesite) {'Lax', 'Strict' => TRUE, 'None' =>  $request_object->isSecure(), default => FALSE};
    

    The latter could be split into multiple lines.

  • Status changed to Needs review almost 2 years ago
  • πŸ‡§πŸ‡¬Bulgaria pfrenssen Sofia
    • Addressed remark from #85, yay for match expressions!
    • The requirement check was accidentally nested inside the PHP version check, fixed that.
    • Updated documentation.
    • Attaching new D9.5.x patch to address #84
  • Status changed to RTBC over 1 year ago
  • πŸ‡¨πŸ‡­Switzerland znerol

    Thanks!

    • catch β†’ committed a3bddae4 on 10.1.x
      Issue #3150614 by pfrenssen, cilefen, murilohp, FinnishFlash, mpp,...
  • Status changed to Fixed over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom catch

    OK the test coverage looks neat and the docs are fine. Looks like a lot of browsers, if not all, are defaulting to 'Lax' rather than none, so this shouldn't be disruptive, but better to get this into 10.1.x early.

    I'm slightly weary about offering 'Strict' given Berdir's comments that it would break everything, but... maybe there's a use case and we probably want to allow configuration between 'Lax' and 'None' anyway.

    I added a release note since this changes scaffold files.

  • The change record looks incorrect.

  • πŸ‡¨πŸ‡­Switzerland znerol

    I think the change record was correct before. Commit a3bddae4 doesn't change the default - neither for existing nor for new sites. In order to change the default value for the session.cookie_samesite, it is necessary to set the container parameter in core/core.services.yml. The commit only touches sites/default/default.services.yml.

    The following change is necessary in order to influence the default behavior (for new and existing sites):

    diff --git a/core/core.services.yml b/core/core.services.yml
    index 1c5d787c57..3beafcd932 100644
    --- a/core/core.services.yml
    +++ b/core/core.services.yml
    @@ -9,6 +9,7 @@ parameters:
         gc_divisor: 100
         gc_maxlifetime: 200000
         cookie_lifetime: 2000000
    +    cookie_samesite: Lax
         sid_length: 48
         sid_bits_per_character: 6
       twig.config:
    
  • I'll reverted the changes but it says things like "Drupal core will begin to warn if the session cookie SameSite is not set to Lax or Strict" which I do not think are true.

  • πŸ‡¨πŸ‡­Switzerland znerol

    Drupal core will begin to warn if the session cookie SameSite is not set to Lax or Strict

    That's in fact what happens. php core/scripts/drupal quick-start, then navigate to /admin/reports/status#warning, this is what I get:

  • πŸ‡¨πŸ‡­Switzerland znerol

    I've updated the CR with the correct way to fix the warning.

  • Again, "Drupal core will begin to warn if the session cookie SameSite is not set to Lax or Strict" is untrue. I've looked at the commit. "None" is an allowed value under one condition.

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    @znerol - it's a bit trickier than that... completely new sites where sites/default/services.yml gets created from sites/default/default.services.yml will have it set to Lax and will not have a warning.

    I do think we need a quick follow-up though. I think we should add cookie_samesite: Lax to core.services.yml in it's session.storage.options so that quickstart sites have the correct default.

  • πŸ‡¨πŸ‡­Switzerland znerol

    @znerol - it's a bit trickier than that... completely new sites where sites/default/services.yml gets created from sites/default/default.services.yml will have it set to Lax and will not have a warning.

    I tried the web installer as well as the quick-start script. Neither of them copies default.services.yml to services.yml. Both of them will copy default.settings.php to settings.php though. So the install behavior is different for services.yml and settings.php. I wonder whether we should fix that inconsistency.

  • πŸ‡¨πŸ‡­Switzerland znerol

    So the install behavior is different for services.yml and settings.php.

    It looks like this has a purpose, see πŸ“Œ Stop creating services.yml by default Fixed .

    • catch β†’ committed e6c4507c on 10.1.x
      Revert "Issue #3150614 by pfrenssen, cilefen, murilohp, FinnishFlash,...
  • Status changed to Needs work over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom catch

    Let's do #97 here, I was also under the mistaken impression that default.services.yml kicks in for newly installed sites without manual copying.

    Reverted so all the changes can go back in togher.

  • First commit to issue fork.
  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡ΎUruguay rpayanm

    I added the #97's suggestion, please review.

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    πŸ“Œ Stop creating services.yml by default Fixed makes this really tricky. Because it means sites don't have a sites/default/services.yml - that means that the whole "existing sites can stay as they are and new sites get the new value" assumption won't work.

    That said, I think the current approach is fine given https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/Sam... - it says

    Cookies without SameSite default to SameSite=Lax

    so defaulting to Lax makes sense as that is what most modern browsers are doing anyway.

    I think the CR will need to document how to unset it in case someone is relying on the current behaviour and we need to call this out in the release notes.

  • Status changed to RTBC over 1 year ago
  • πŸ‡¨πŸ‡­Switzerland znerol

    Thanks @rpayanm, the SameSite attribute is now present on the session cookie for all sites. Going to revert the CR to @cilefen version and add instructions on how to modify the default.

  • Status changed to Fixed over 1 year ago
  • πŸ‡³πŸ‡±Netherlands ralphvdhoudt

    Rerolled patch against 9.4

  • πŸ‡³πŸ‡±Netherlands ralphvdhoudt

    Rerolled patch against 9.4.10

  • πŸ‡³πŸ‡±Netherlands ralphvdhoudt

    Previous patch for 9.4 had patch for sites/default/default.services.yml which is scaffolded

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024