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

Default cookie's SameSite attribute to lax #2495

Merged
merged 4 commits into from Sep 24, 2020
Merged

Conversation

t-ae
Copy link
Contributor

@t-ae t-ae commented Sep 18, 2020

Set a cookie's default SameSite attribute to lax.

This prevents warnings in browsers and stops functionality working when following redirects, cross site link or when browsers assume the attribute to be None, which requires Secure attribute(HTTPS only).

#2495

Copy link
Member

@0xTim 0xTim left a comment

Choose a reason for hiding this comment

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

Looks good for the most part, though I think we can simplify it lots. It also contains a breaking change that we need to fix.

If you could write a test to ensure that the cookie gets a lax same site policy by default that would be 👌

@@ -166,14 +166,16 @@ public struct HTTPCookies: ExpressibleByDictionaryLiteral {
isHTTPOnly: Bool = false,
sameSite: SameSitePolicy? = nil
) {
let sameSitePolicy = sameSite ?? .lax
Copy link
Member

Choose a reason for hiding this comment

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

We don't need this line, we can change the default parameter on the line above

@@ -139,7 +139,7 @@ public struct HTTPCookies: ExpressibleByDictionaryLiteral {
/// A cookie which can only be sent in requests originating from the same origin as the target domain.
///
/// This restriction mitigates attacks such as cross-site request forgery (XSRF).
public var sameSite: SameSitePolicy?
public var sameSite: SameSitePolicy
Copy link
Member

Choose a reason for hiding this comment

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

Making this non-optional is a breaking-change since anyone looking at this would have compiler errors after updating. However, I think we can still work around this. Leave this as optional but in the initialiser it will default to lax. We should also still give people the option to set it to nil as well

self.string = string
self.expires = expires
self.maxAge = maxAge
self.domain = domain
self.path = path
self.isSecure = isSecure
self.isSecure = isSecure || sameSitePolicy == .none //samesite none requires secure attribute to be set
Copy link
Member

Choose a reason for hiding this comment

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

This should be noted in the comment block for the initialiser since it might catch some people out. If you could link to the same site spec where this is mandated in the new comment that would be great

Also, small nit, place the comment on the line above rather than at the end of the line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I read IETF draft but could not find description about the relation of SameSite=None and Secure
https://tools.ietf.org/id/draft-ietf-httpbis-rfc6265bis-03.html

That can be seen in Mozilla's documentation so I added link for this page.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite

@@ -84,7 +84,7 @@ struct HTTPSetCookie {
guard let parameter = directive.parameter else {
return nil
}
self.value.sameSite = HTTPCookies.SameSitePolicy(rawValue: .init(parameter))
self.value.sameSite = HTTPCookies.SameSitePolicy(rawValue: .init(parameter)) ?? .lax
Copy link
Member

Choose a reason for hiding this comment

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

Again, I think we should still give people the option to set it to nil, and if we default to lax in the initialiser that should fix the underlying issue

@t-ae
Copy link
Contributor Author

t-ae commented Sep 24, 2020

@0xTim
Thank you for your comments.

If you could write a test to ensure that the cookie gets a lax same site policy by default that would be 👌

I added default sameSite == .lax check for existing test case.
https://github.com/vapor/vapor/pull/2495/files#diff-08ea427e384e3eb3f34472175a23bc6fR164

Copy link
Member

@0xTim 0xTim left a comment

Choose a reason for hiding this comment

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

This LGTM

@0xTim 0xTim added the semver-patch Internal changes only label Sep 24, 2020
@0xTim
Copy link
Member

0xTim commented Sep 24, 2020

@t-ae final thing, can you change the comment to be more 'release note-y'? These form the release notes for the automated release. See the contributing guidelines here for some guidance, or have a look at previous releases for inspiration

@0xTim
Copy link
Member

0xTim commented Sep 24, 2020

PS I'll merge this as soon as that's done and CI is green

@t-ae
Copy link
Contributor Author

t-ae commented Sep 24, 2020

@0xTim
Updated (Not sure if it's enough).

@0xTim 0xTim changed the title Default SameSite attribute=lax Default cookie's SameSite attribute to lax Sep 24, 2020
@0xTim 0xTim merged commit 236c616 into vapor:master Sep 24, 2020
@tanner0101
Copy link
Member

These changes are now available in 4.29.3

@t-ae t-ae deleted the same-site-lax branch September 24, 2020 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-patch Internal changes only
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants