Support cookie SameSite=None option on nodejs - #8652
Conversation
Allows cookies to be set with SameSite=None in addition to "Lax" and "Strict". As per https://web.dev/samesite-cookies-explained/, this option is only valid if the cookie is set as Secure.
|
There's an existing failing test (at least on my machine)
This error occurs in the I'm not sure why the Lint check failed, I use an ESLint extension in VSCode that's supposed to auto format everything according to the repo's rules. The error doesn't seem related to my change. |
n2o1988
left a comment
There was a problem hiding this comment.
A couple of clarifications
| * | ||
| * | ||
| * @type {(!Date|number|undefined)} | ||
| * @type {(string|undefined)} |
There was a problem hiding this comment.
I think this was a copy-paste mistake in the original PR.
| await driver.manage().addCookie(cookie) | ||
| await assert.doesNotReject( | ||
| async () => await driver.manage().addCookie(cookie) | ||
| ) |
There was a problem hiding this comment.
Other test cases for SameSite take a different approach to test this: they set the cookie and read it straight after.
In case of None, the cookie has to be secure, and I found this means the getCookie call would not return any cookie (since the test page in localhost is not served via https I presume?).
I figured the best way to test this is to test that it does not throw an exception. In fact, an invalid combination (see below tests) would cause the underlying driver to raise an exception.
This sounds sufficient to me but let me know if this is not enough.
I did make sure that this works in our real test suites (where the app is served on https).
There was a problem hiding this comment.
I think that this is fine
| } | ||
| ) | ||
|
|
||
| ignore(env.browsers(Browser.FIREFOX, Browser.IE, Browser.SAFARI)).it( |
There was a problem hiding this comment.
Shouldn't these tests work in Firefox?
There was a problem hiding this comment.
I mirrored the original tests, didn't want to change too much. I'll double-check. Maybe it wasn't an option at the time of the original implementation but it is now.
There was a problem hiding this comment.
You're right, updated.
| await driver.manage().addCookie(cookie) | ||
| await assert.doesNotReject( | ||
| async () => await driver.manage().addCookie(cookie) | ||
| ) |
There was a problem hiding this comment.
I think that this is fine
| } | ||
| ) | ||
|
|
||
| ignore(env.browsers(Browser.FIREFOX, Browser.IE, Browser.SAFARI)).it( |
There was a problem hiding this comment.
Shouldn't this work in Firefox?
| @@ -1177,9 +1177,15 @@ class Options { | |||
| expiry = Math.floor(date.getTime() / 1000) | |||
| } | |||
|
|
|||
There was a problem hiding this comment.
Based on the related webdriver specification
Defaults to None if omitted when adding a cookie.
We should set sameSite cookie by default as "None" when omitted.
| if (!sameSite) { | |
| sameSite = 'None' | |
| } |
There was a problem hiding this comment.
I'm not too sure we should do that given that:
- no other Selenium binding is doing that: all seem to leverage on the underlying driver/browser implementation for the default value
- I think it's the Browsers' responsibility to define the default behavior. For instance, in Chrome 85 that's exactly what changed: default is now
Laxfor Chrome and soon for Firefox too. That change was the reason I opened this PR in the first place. - with the new security proposal,
sameSite='None'should always be accompanied bySecure=true, so we'd need to take that into account - changing that would be a breaking change (nothing against it, but I'd like the maintainers' opinion)
Interested to hear thoughts from @AutomatedTester
|
|
||
| if (sameSite === 'None' && !secure) { | ||
| throw new error.InvalidArgumentError( | ||
| `Invalid cookie configuration: SameSite=None must be Secure` |
There was a problem hiding this comment.
It helps further understanding that None is a string and we keep a consistent sameSite formatting
| `Invalid cookie configuration: SameSite=None must be Secure` | |
| 'Invalid cookie configuration: sameSite="None" must be Secure' |
There was a problem hiding this comment.
I was following the cookie header format (e.g. SameSite=None; Secure; ).
I can change that if you feel strongly about it, but then it should be secure: true instead of Secure.
|
@AutomatedTester any other thoughts on this one? |
|
This looks like it is about ready to land. There are linting errors. Could you fix those please and I will get it landed. |
@AutomatedTester the linting errors seem unrelated to my change. It also seems like this "Super-Linter" has conflicting rules with the ones declared in the .eslintrc.js config
|
|
@n2o1988 I just ran the linter again on |
|
@diemol maybe I'm doing this wrong. My linting config says everything is good, locally (at least according to |
|
@n2o1988 I did not do it locally, I only ran the GitHub action job again on |
|
@diemol it's kinda hard to spot what's wrong in this case. If you look at the output of the linter, it shows a whole bunch of things I haven't changed, and the rules it's using are conflicting with the rules specified by ESLint I never touched those lines.. :| |
|
Let me have a look at the linter stuff since I set it up |
|
@AutomatedTester any updates on merging this PR? |
|
We're sort of in a hurry to get this released, otherwise we'll have to go the way of pinning our own fork (which I'd like to avoid). |
|
Up: any updates on merging this PR @AutomatedTester @diemol ? |
|
Merging this, thank you for your contribution @n2o1988! However, I cannot say when the next alpha release will happen. |
|
Thanks @diemol ! |

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it
Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.
Description
Allows cookies to be added with
SameSiteset to "None" in addition to "Lax" and "Strict", as per https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite.This option is only valid if the cookie is
Secure(see https://web.dev/samesite-cookies-explained/#samesitenone-must-be-secure and proposal).Motivation and Context
Follow up to #7901
Recent updates to Chrome 85 (and soon landing on Firefox) changed the default from
NonetoLax, requiring us to explicitly setsameSite: 'None', which the original implementation does not allow (not sure why, maybeNonewasn't an option, back then?).This also mirrors what the Java bindings support: https://github.com/SeleniumHQ/selenium/blob/trunk/java/client/src/org/openqa/selenium/Cookie.java#L111
Other bindings (such as Python) have the same issue and should be updated too (I'm not a Python dev though).
Types of changes
Checklist