Skip to content

Commit

Permalink
Fixes #26 - [Feature Request] Support of phasing out third-party cookies
Browse files Browse the repository at this point in the history
Added support for cookie attributes: Domain, Path, Max-Age, Partitioned.

Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed Nov 16, 2023
1 parent b262a34 commit 4ebe4e6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cometd-nodejs-server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,12 @@ export interface Options {
sweepPeriod?: number;
timeout?: number;
// HTTP options.
browserCookieDomain?: string;
browserCookieHttpOnly?: boolean;
browserCookieMaxAge?: number;
browserCookieName?: string;
browserCookiePartitioned?: boolean;
browserCookiePath?: string;
browserCookieSameSite?: 'Strict' | 'Lax' | 'None';
browserCookieSecure?: boolean;
duplicateMetaConnectHttpResponseCode?: number;
Expand Down
15 changes: 15 additions & 0 deletions cometd-nodejs-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,18 @@ module.exports = (() => {

function _generateCookie(cookieName, cookieValue) {
let result = cookieName + '=' + cookieValue;
const domain = _self.option('browserCookieDomain');
if (domain) {
result += '; Domain=' + domain;
}
const path = _self.option('browserCookiePath');
if (path) {
result += '; Path=' + path;
}
const maxAge = _self.option('browserCookieMaxAge');
if (maxAge) {
result += '; Max-Age=' + maxAge;
}
if (_self.option('browserCookieHttpOnly') === true) {
result += '; HttpOnly';
}
Expand All @@ -368,6 +380,9 @@ module.exports = (() => {
if (sameSite) {
result += '; SameSite=' + sameSite;
}
if (_self.option('browserCookiePartitioned') === true) {
result += '; Partitioned';
}
return result;
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
"test": "mocha --exit --require ts-node/register --extension ts"
},
"devDependencies": {
"@types/mocha": ">=10.0.0",
"@types/node": ">=16.0.0",
"cometd": "^7.0.6",
"cometd-nodejs-client": "^1.4.0",
"mocha": ">=10.2.0",
"typescript": ">=5.0.0",
"ts-node": ">=10.9.0",
"@types/node": ">=16.0.0",
"@types/mocha": ">=10.0.0"
"typescript": ">=5.0.0"
}
}
35 changes: 35 additions & 0 deletions test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,41 @@ describe('server', () => {
'}]');
});

it('supports Partitioned cookie attribute', done => {
_server.options.browserCookiePath = '/';
_server.options.browserCookieSecure = true;
_server.options.browserCookiePartitioned = true;

http.request(newRequest(), r => {
receiveResponse(r, replies => {
const reply = replies[0];
assert.strictEqual(reply.successful, true);
const headers = r.headers;
for (let name in headers) {
if (headers.hasOwnProperty(name)) {
if (/^set-cookie$/i.test(name)) {
const values = headers[name] || [];
for (let i = 0; i < values.length; ++i) {
let value = values[i];
if (value.indexOf('Partitioned') >= 0 &&
value.indexOf('Path=/') >= 0 &&
value.indexOf('Secure') >= 0) {
done();
return;
}
}
}
}
}
done(new Error('missing Partitioned cookie attribute'));
});
}).end('[{' +
'"channel": "/meta/handshake",' +
'"version": "1.0",' +
'"supportedConnectionTypes": ["long-polling"]' +
'}]');
});

it('resends messages when connection is broken', done => {
const serverAck = require('../ack-extension');
_server.addExtension(new serverAck.AcknowledgedMessagesExtension());
Expand Down

0 comments on commit 4ebe4e6

Please sign in to comment.