Skip to content

Commit

Permalink
feat: add support for all cookie options
Browse files Browse the repository at this point in the history
The "cookie" options can now be an object, which will be forwarded to
the "cookie" module.

The previous syntax is still valid:

```
new Server({
  cookieName: "test",
  cookieHttpOnly: false,
  cookiePath: "/custom"
})
```

but the new syntax add support for all options:

```
new Server({
  cookie: {
    name: "test",
    httpOnly: false,
    path: "/custom"
    sameSite: "lax"
  }
})
```

Reference: https://github.com/jshttp/cookie#options-1

Backported from master: a374471
  • Loading branch information
darrachequesne committed Dec 30, 2020
1 parent 5ad2736 commit 19cc582
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
16 changes: 10 additions & 6 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,16 @@ Server.prototype.handshake = function (transportName, req) {

if (false !== this.cookie) {
transport.on('headers', function (headers) {
headers['Set-Cookie'] = cookieMod.serialize(self.cookie, id,
{
path: self.cookiePath,
httpOnly: self.cookiePath ? self.cookieHttpOnly : false,
sameSite: true
});
if (typeof self.cookie === 'object') {
headers['Set-Cookie'] = cookieMod.serialize(self.cookie.name, id, self.cookie);
} else {
headers['Set-Cookie'] = cookieMod.serialize(self.cookie, id,
{
path: self.cookiePath,
httpOnly: self.cookiePath ? self.cookieHttpOnly : false,
sameSite: true
});
}
});
}

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"dependencies": {
"accepts": "~1.3.4",
"base64id": "2.0.0",
"cookie": "0.3.1",
"cookie": "~0.4.1",
"debug": "~4.1.0",
"engine.io-parser": "~2.2.0",
"ws": "~7.4.2"
Expand Down
19 changes: 19 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ describe('server', function () {
});
});

it('should forward all cookie options', function (done) {
listen({ cookie: {
name: 'woot',
path: '/test',
httpOnly: true,
sameSite: 'lax'
}}, function (port) {
request.get('http://localhost:%d/engine.io/default/'.s(port))
.query({ transport: 'polling', b64: 1 })
.end(function (err, res) {
expect(err).to.be(null);
// hack-obtain sid
var sid = res.text.match(/"sid":"([^"]+)"/)[1];
expect(res.headers['set-cookie'][0]).to.be('woot=' + sid + '; Path=/test; HttpOnly; SameSite=Lax');
done();
});
});
});

it('should send the io cookie custom name', function (done) {
listen({ cookie: 'woot' }, function (port) {
request.get('http://localhost:%d/engine.io/default/'.s(port))
Expand Down

0 comments on commit 19cc582

Please sign in to comment.