-
-
Notifications
You must be signed in to change notification settings - Fork 993
Add sameSite 'auto' support to match secure 'auto' pattern #1087
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
base: master
Are you sure you want to change the base?
Conversation
bjohansebas
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Overall, adding that option looks good to me, I think this would be good to have for v1.
By the way, I added more tests to better verify its behavior with different configurations
| it('should not set cookie when insecure', function (done) { | ||
| request(this.server) | ||
| .get('/') | ||
| .set('X-Secure', 'false') | ||
| .expect(shouldNotHaveHeader('Set-Cookie')) | ||
| .expect(200, 'false', done) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The behavior here is interesting and cannot be changed since it would be a breaking change. However, because secure is set to true in the configuration, during cookie creation, which happens on this line
Line 161 in 2cd6561
| req.session.cookie = new Cookie(cookieOptions); |
Line 235 in 2cd6561
| if (req.session.cookie.secure && !issecure(req, trustProxy)) { |
| if (cookieOptions.secure === 'auto') { | ||
| req.session.cookie.secure = issecure(req, trustProxy); | ||
| } | ||
|
|
||
| if (cookieOptions.sameSite === 'auto') { | ||
| req.session.cookie.sameSite = issecure(req, trustProxy) ? 'none' : 'lax'; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why call the function twice when once will do?
| if (cookieOptions.secure === 'auto') { | |
| req.session.cookie.secure = issecure(req, trustProxy); | |
| } | |
| if (cookieOptions.sameSite === 'auto') { | |
| req.session.cookie.sameSite = issecure(req, trustProxy) ? 'none' : 'lax'; | |
| } | |
| const isSecure = issecure(req, trustProxy); | |
| if (cookieOptions.secure === 'auto') { | |
| req.session.cookie.secure = isSecure; | |
| } | |
| if (cookieOptions.sameSite === 'auto') { | |
| req.session.cookie.sameSite = isSecure ? 'none' : 'lax'; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially implemented each check independently without considering that both checks rely on the same issecure() call.
I've updated the code to cache the result in a const isSecure variable and reuse it for both the secure and sameSite auto configurations. This avoids the redundant function call you pointed out.
Thanks for the review!
f745109 to
6df617c
Compare
This PR adds support for
sameSite: 'auto'option that automatically sets the SameSite cookie attribute based on connection security, similar to the existingsecure: 'auto'feature.When the connection is secure (HTTPS), SameSite is set to 'None' to enable cross-site usage, and when insecure (HTTP), it's set to 'Lax' for better security. This solves real-world scenarios like SAML authentication where the connection security isn't known at configuration time.
The implementation follows the same pattern as
secure: 'auto', uses the existingissecure()function, and includes comprehensive test coverage with no breaking changes.Fixes #1081