Skip to content

Commit

Permalink
Add cookie.priority
Browse files Browse the repository at this point in the history
This adds cookie.priority by passing this property through to
cookie (which requires us to upgrade cookie to >=0.5.0).

The priority option is not fully standardized, but has been
proposed and in-use since 2016.

See also: jshttp/cookie#138
Fixes: #884
  • Loading branch information
mlucool authored and dougwilson committed Dec 12, 2023
1 parent d21a69f commit 0d83077
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ as once the cookie is set on HTTPS, it will no longer be visible over HTTP. This
is useful when the Express `"trust proxy"` setting is properly setup to simplify
development vs production configuration.

##### cookie.priority

Specifies the `string` to be the value for the [`Priority` `Set-Cookie` attribute][rfc-west-cookie-priority-00-4.1].

- `'low'` will set the `Priority` attribute to `Low`.
- `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
- `'high'` will set the `Priority` attribute to `High`.

More information about the different priority levels can be found in
[the specification][rfc-west-cookie-priority-00-4.1].

**note** This is an attribute that has not yet been fully standardized, and may change in the future.
This also means many clients may ignore this attribute until they understand it.

##### genid

Function to call to generate a new session ID. Provide a function that returns
Expand Down Expand Up @@ -975,6 +989,7 @@ On Windows, use the corresponding command;
[MIT](LICENSE)

[rfc-6265bis-03-4.1.2.7]: https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7
[rfc-west-cookie-priority-00-4.1]: https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1
[ci-image]: https://badgen.net/github/checks/expressjs/session/master?label=ci
[ci-url]: https://github.com/expressjs/session/actions?query=workflow%3Aci
[coveralls-image]: https://badgen.net/coveralls/c/github/expressjs/session/master
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"repository": "expressjs/session",
"license": "MIT",
"dependencies": {
"cookie": "0.4.2",
"cookie": "0.5.0",
"cookie-signature": "1.0.6",
"debug": "2.6.9",
"depd": "~2.0.0",
Expand Down
2 changes: 2 additions & 0 deletions session/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var Cookie = module.exports = function Cookie(options) {
this.path = '/';
this.maxAge = null;
this.httpOnly = true;
this.priority = 'medium';

if (options) {
if (typeof options !== 'object') {
Expand Down Expand Up @@ -123,6 +124,7 @@ Cookie.prototype = {
, domain: this.domain
, path: this.path
, sameSite: this.sameSite
, priority: this.priority
}
},

Expand Down
12 changes: 12 additions & 0 deletions test/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ describe('new Cookie()', function () {
assert.strictEqual(cookie.maxAge, null)
})

it('should default priority to medium', function () {
var cookie = new Cookie()
assert.strictEqual(cookie.priority, 'medium')
})

describe('with options', function () {
it('should create a new cookie object', function () {
assert.strictEqual(typeof new Cookie({}), 'object')
Expand Down Expand Up @@ -105,6 +110,13 @@ describe('new Cookie()', function () {
assert.throws(function() { new Cookie({ maxAge: true }) }, /maxAge/)
assert.throws(function() { new Cookie({ maxAge: function () {} }) }, /maxAge/)
})

it('should set priority', function () {
var maxAge = 60000
var cookie = new Cookie({ priority: 'high' })

assert.strictEqual(cookie.priority, 'high')
})
})

describe('path', function () {
Expand Down
3 changes: 2 additions & 1 deletion test/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -1878,7 +1878,7 @@ describe('session()', function(){
})

it('should override defaults', function(done){
var server = createServer({ cookie: { path: '/admin', httpOnly: false, secure: true, maxAge: 5000 } }, function (req, res) {
var server = createServer({ cookie: { path: '/admin', httpOnly: false, secure: true, maxAge: 5000, priority: 'high' } }, function (req, res) {
req.session.cookie.secure = false
res.end()
})
Expand All @@ -1889,6 +1889,7 @@ describe('session()', function(){
.expect(shouldSetCookieWithoutAttribute('connect.sid', 'HttpOnly'))
.expect(shouldSetCookieWithAttributeAndValue('connect.sid', 'Path', '/admin'))
.expect(shouldSetCookieWithoutAttribute('connect.sid', 'Secure'))
.expect(shouldSetCookieWithAttributeAndValue('connect.sid', 'Priority', 'High'))
.expect(200, done)
})

Expand Down

0 comments on commit 0d83077

Please sign in to comment.