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 none as a valid sameSite option #111

Merged
merged 1 commit into from Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions HISTORY.md
Expand Up @@ -3,6 +3,7 @@ unreleased

* Fix check for default `secure` option behavior
* Fix `maxAge` option preventing cookie deletion
* Support `"none"` in `sameSite` option
* deps: depd@~2.0.0
- Replace internal `eval` usage with `Function` constructor
- Use instance methods on `process` to check for listeners
Expand Down
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -26,7 +26,7 @@ var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
* RegExp to match Same-Site cookie attribute value.
*/

var sameSiteRegExp = /^(?:lax|strict)$/i
var SAME_SITE_REGEXP = /^(?:lax|none|strict)$/i

function Cookies(request, response, options) {
if (!(this instanceof Cookies)) return new Cookies(request, response, options)
Expand Down Expand Up @@ -146,7 +146,7 @@ function Cookie(name, value, attrs) {
throw new TypeError('option domain is invalid');
}

if (this.sameSite && this.sameSite !== true && !sameSiteRegExp.test(this.sameSite)) {
if (this.sameSite && this.sameSite !== true && !SAME_SITE_REGEXP.test(this.sameSite)) {
throw new TypeError('option sameSite is invalid')
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/cookie.js
Expand Up @@ -95,6 +95,13 @@ describe('new Cookie(name, value, [options])', function () {
})
})

describe('when set to "none"', function () {
it('should set "samesite=none" attribute in header', function () {
var cookie = new cookies.Cookie('foo', 'bar', { sameSite: 'none' })
assert.equal(cookie.toHeader(), 'foo=bar; path=/; samesite=none; httponly')
})
})

describe('when set to "strict"', function () {
it('should set "samesite=strict" attribute in header', function () {
var cookie = new cookies.Cookie('foo', 'bar', { sameSite: 'strict' })
Expand Down