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

ENH Add samesite attribute to cookies. #10335

Merged

Conversation

GuySartorelli
Copy link
Member

@GuySartorelli GuySartorelli commented May 26, 2022

Sets a default samesite attribute on all cookies. Using the Lax value means that we're just setting an explicit value that is identical to the implicit value that was already being used.

This PR provides a way to configure both the default value for all cookies and for session cookies separately, as well as giving an extension hook if a specific value is desired for a given cookie.

The extension hook should be removed in the next major in favour of adding a new parameter like what was proposed in #9842 - I've avoided doing that here because the cookie backend is explicitly extensible (with an interface and docs and everything) so adding new parameters here would constitute a breaking change.

The following links may be useful background:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
https://www.php.net/manual/en/function.setcookie.php
https://www.php.net/manual/en/function.session-set-cookie-params.php

Parent issue:

Related issue:

@GuySartorelli
Copy link
Member Author

Leaving in draft for now because I still need to add unit tests.

@GuySartorelli
Copy link
Member Author

GuySartorelli commented May 27, 2022

Actually.... I don't think there is a way to test this... none of the unit tests current test against any of the properties of cookies other than the values and whether they're set at all. I don't think there's a way we can test if the cookie's samesite attribute is set correctly.

Marking as ready for review. But if anyone has ideas for how we could test this, that'd be awesome.

Edit: Steve has pointed out that it should be possible to test with a FunctionalTest - giving that a go.

@GuySartorelli GuySartorelli marked this pull request as ready for review May 27, 2022 01:10
@GuySartorelli GuySartorelli marked this pull request as draft May 29, 2022 22:40
@GuySartorelli
Copy link
Member Author

GuySartorelli commented May 29, 2022

After giving this a go it looks like I can't test cookie attributes in a FunctionalTest - the cookies are set using setcookie() directly, rather than adding them to the HTTPResponse object, which means we aren't fully including them in mock requests. The cookie values are being set in mock requests by pulling them out of the cookie jar and setting the $_COOKIE env variable. So we are mocking cookie values but not the other attributes in cookie headers.

Changing the way we add cookies to make them more fully mockable would definitely be a valuable enhancement but is out of scope for this PR. I've created an issue (#10338) for that.

@GuySartorelli GuySartorelli marked this pull request as ready for review May 29, 2022 23:48
src/Control/CookieJar.php Outdated Show resolved Hide resolved
src/Control/Session.php Outdated Show resolved Hide resolved
src/Control/Session.php Outdated Show resolved Hide resolved
src/Control/Session.php Outdated Show resolved Hide resolved
src/Control/CookieJar.php Outdated Show resolved Hide resolved
@GuySartorelli GuySartorelli force-pushed the pulls/4/samesite-cookies branch 3 times, most recently from 5088070 to 3168d3b Compare May 30, 2022 04:32
@GuySartorelli
Copy link
Member Author

@emteknetnz Requested changes made.

src/Control/CookieJar.php Show resolved Hide resolved
src/Control/Session.php Outdated Show resolved Hide resolved
docs/en/04_Changelogs/4.12.0.md Outdated Show resolved Hide resolved
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.

Dismiss this stale review

@emteknetnz emteknetnz dismissed their stale review May 30, 2022 22:18

Incorrect testing

src/Control/Session.php Show resolved Hide resolved
src/Control/Session.php Outdated Show resolved Hide resolved
src/Control/CookieJar.php Show resolved Hide resolved
src/Control/Session.php Show resolved Hide resolved
src/Control/Session.php Outdated Show resolved Hide resolved
src/Control/Cookie.php Show resolved Hide resolved
src/Control/Cookie.php Outdated Show resolved Hide resolved
src/Control/Cookie.php Outdated Show resolved Hide resolved
src/Control/Cookie.php Show resolved Hide resolved
src/Control/CookieJar.php Outdated Show resolved Hide resolved
src/Control/CookieJar.php Outdated Show resolved Hide resolved
src/Control/Session.php Outdated Show resolved Hide resolved
tests/php/Control/CookieTest.php Show resolved Hide resolved
tests/php/Control/CookieTest.php Show resolved Hide resolved
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.

Test locally, works great. Thanks for implementing all the feedback!

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.

Just noticed there's a failing unit test - happy to merge once this is fixed

Co-authored-by: pine3ree <pine3ree@gmail.com>
@GuySartorelli
Copy link
Member Author

It turns out the $this->once() counts as an assertion (that the method will be called once).
Changed to $this->never() and removed the @doesNotPerformAssertions annotation.

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.

Merge on green

@GuySartorelli GuySartorelli merged commit fa35567 into silverstripe:4 Jun 2, 2022
@GuySartorelli GuySartorelli deleted the pulls/4/samesite-cookies branch June 2, 2022 00:49
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.

No support for SameSite cookie directive
2 participants