Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Add support for samesite attribute to session cookies #9842

Closed

Conversation

madmatt
Copy link
Member

@madmatt madmatt commented Feb 3, 2021

Relates to #9440.

Expected behaviour

Silverstripe CMS should support the new SameSite cookie attribute for all cookies, as well as session cookies.

Observed behaviour

Silverstripe CMS doesn't properly support this attribute. There are two issues:

  1. The Cookie::set() logic doesn't have support for the SameSite attribute.
  2. Even if the above is fixed, Silverstripe CMS has some logic that sets the session cookie on every HTTP request, which means that until this fix is in place, you can set the samesite attribute via ini_set() but it only works for the very first Set-Cookie header - from then on the framework sets the cookie without the flag.

I think this fix is backwards compatible. It allows the attribute to be set, but only on PHP 7.3+ because it can't be set on older versions of PHP than that (without manually setting HTTP headers).

This doesn't take on any of the complexity that #9548 discussed, just the simplest possible thing to ensure that the samesite attribute is consistently set on session cookies.

@@ -329,10 +329,27 @@ public function start(HTTPRequest $request)

session_start();

$sameSite = null;
$sessionParams = session_get_cookie_params();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work? It's calling session_set_cookie_params above without samesite.
From my testing, the above call removes the samesite, and the below Cookie::set will only get called if you also have a non-zero $timeout configured.

Copy link
Member

@emteknetnz emteknetnz Jan 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@madmatt matt-in-a-hatt had an unanswered question about this above

Copy link
Member

@emteknetnz emteknetnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some changes to make now that the minimum version of php in framework in 7.3

) {
// if headers aren't sent, we can set the cookie
if (!headers_sent($file, $line)) {
return setcookie($name, $value, $expiry, $path, $domain, $secure, $httpOnly);
if (version_compare(PHP_VERSION, '7.3.0') >= 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this check, minimum version of framework is currently 7.3

]
);
} else {
// We can't set the SameSite cookie, if it isn't the default value of null then throw an error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this block

@@ -329,10 +329,27 @@ public function start(HTTPRequest $request)

session_start();

$sameSite = null;
$sessionParams = session_get_cookie_params();
Copy link
Member

@emteknetnz emteknetnz Jan 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@madmatt matt-in-a-hatt had an unanswered question about this above

$sameSite = null;
$sessionParams = session_get_cookie_params();

// If samesite param is set, then we can pass it into the Cookie::set call below as we are on >= PHP 7.3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove 7.3 check

@NikxDa
Copy link
Contributor

NikxDa commented Feb 20, 2022

@madmatt @emteknetnz @matt-in-a-hat It'd be great if this PR could be fixed up so it can be merged. Incorporating it requires very little changes but makes SilverStripe a lot more flexible in headless environments :)

Happy to fork it and open a new PR, but I just thought I'd give this a push because it looks like it's nearly there.

@GuySartorelli
Copy link
Member

@NikxDa Doesn't look like there's any movement here - are you still interested in forking and opening a new PR for this?

@GuySartorelli
Copy link
Member

Closing in favour of #10335 which avoids breaking changes this PR would introduce.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants