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

fix: partitioned and autoChips should support different paths #55

Merged
merged 2 commits into from
Jul 6, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/pkg.pr.new.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Publish Any Commit
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: npm install

- name: Build
run: npm run prepublishOnly --if-present

- run: npx pkg-pr-new publish
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@types/mocha": "10",
"egg-bin": "6",
"eslint": "8",
"eslint-config-egg": "13",
"eslint-config-egg": "14",
"tshy": "1",
"tshy-after": "1",
"typescript": "5"
Expand Down
30 changes: 26 additions & 4 deletions src/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@ export class Cookies {
opts.overwrite = false;
headers = ignoreCookiesByName(headers, name);
}
const removeCookieOpts = Object.assign({}, opts, {
const removeCookieOpts = {
...opts,
partitioned: false,
});
};
const removeUnpartitionedCookie = new Cookie(name, '', removeCookieOpts);
// if user not set secure, reset secure to ctx.secure
if (opts.secure === undefined) {
Expand All @@ -186,7 +187,8 @@ export class Cookies {
// signed
if (signed) {
removeUnpartitionedCookie.name += '.sig';
headers = ignoreCookiesByName(headers, removeUnpartitionedCookie.name);
headers = ignoreCookiesByNameAndPath(headers,
removeUnpartitionedCookie.name, removeUnpartitionedCookie.attrs.path);
headers = pushCookie(headers, removeUnpartitionedCookie);
}
} else if (autoChips) {
Expand All @@ -205,7 +207,8 @@ export class Cookies {
if (signed) {
newPartitionedCookie.value = value && this.keys.sign(newPartitionedCookie.toString());
newPartitionedCookie.name += '.sig';
headers = ignoreCookiesByName(headers, newPartitionedCookie.name);
headers = ignoreCookiesByNameAndPath(headers,
newPartitionedCookie.name, newPartitionedCookie.attrs.path);
headers = pushCookie(headers, newPartitionedCookie);
}
}
Expand Down Expand Up @@ -309,3 +312,22 @@ function ignoreCookiesByName(cookies: string[], name: string) {
const prefix = `${name}=`;
return cookies.filter(c => !c.startsWith(prefix));
}

function ignoreCookiesByNameAndPath(cookies: string[], name: string, path: string | null | undefined) {
if (!path) {
return ignoreCookiesByName(cookies, name);
}
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
const prefix = `${name}=`;
// foo=hello; path=/path1; samesite=none
const includedPath = `; path=${path};`;
// foo=hello; path=/path1
const endsWithPath = `; path=${path}`;
return cookies.filter(c => {
if (c.startsWith(prefix)) {
if (c.includes(includedPath) || c.endsWith(endsWithPath)) {
return false;
}
}
return true;
});
}
113 changes: 113 additions & 0 deletions test/cookies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,37 @@ describe('test/cookies.test.ts', () => {
assert.equal(headers[3], 'foo.sig=ZWbaA4bWk8ByBuYVgfmJ2DMvhhS3sOctMbfXAQ2vnwI; path=/; samesite=none; secure; httponly');
});

it('should ignore remove unpartitioned property with different paths', () => {
const cookies = Cookies({
secure: true,
headers: {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.3945.29 Safari/537.36',
},
}, { secure: true }, { autoChips: true, partitioned: false, removeUnpartitioned: true, sameSite: 'None' });
const opts: CookieSetOptions = {
signed: 1,
};
cookies.set('foo', 'hello', opts);
cookies.set('foo', 'hello', {
...opts,
path: '/path2',
});

assert(opts.signed === 1);
assert(opts.secure === undefined);
const headers = cookies.ctx.response.headers['set-cookie'];
assert.deepEqual(headers, [
'_CHIPS-foo=hello; path=/; samesite=none; secure; httponly; partitioned',
'_CHIPS-foo.sig=G4Idm9Wdp_vfCnUbOpQG284o22SgTe88SUmG6QW1ylk; path=/; samesite=none; secure; httponly; partitioned',
'foo=hello; path=/; samesite=none; secure; httponly',
'foo.sig=ZWbaA4bWk8ByBuYVgfmJ2DMvhhS3sOctMbfXAQ2vnwI; path=/; samesite=none; secure; httponly',
'_CHIPS-foo=hello; path=/path2; samesite=none; secure; httponly; partitioned',
'_CHIPS-foo.sig=G4Idm9Wdp_vfCnUbOpQG284o22SgTe88SUmG6QW1ylk; path=/path2; samesite=none; secure; httponly; partitioned',
'foo=hello; path=/path2; samesite=none; secure; httponly',
'foo.sig=ZWbaA4bWk8ByBuYVgfmJ2DMvhhS3sOctMbfXAQ2vnwI; path=/path2; samesite=none; secure; httponly',
]);
});

it('should ignore remove unpartitioned property when autoChips = true and signed = false', () => {
const cookies = Cookies({
secure: true,
Expand All @@ -917,6 +948,88 @@ describe('test/cookies.test.ts', () => {
assert.equal(headers[1], 'foo=hello; path=/; samesite=none; secure; httponly');
});

it('should work on unpartitioned = true and partitioned = true with different paths', () => {
const cookies = Cookies({
secure: true,
headers: {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.3945.29 Safari/537.36',
},
}, { secure: true }, { partitioned: true, removeUnpartitioned: true, sameSite: 'None' });
cookies.set('foo', 'hello', {
signed: 1,
});
cookies.set('foo', 'hello', {
signed: 1,
path: '/path1',
});
cookies.set('foo', 'hello', {
signed: 1,
path: '/path2',
});
cookies.set('foo', 'hello', {
signed: 1,
path: '/path3',
});

const headers = cookies.ctx.response.headers['set-cookie'];
assert.deepEqual(headers, [
'foo=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; samesite=none; secure; httponly',
'foo.sig=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; samesite=none; secure; httponly',
'foo=hello; path=/; samesite=none; secure; httponly; partitioned',
'foo.sig=ZWbaA4bWk8ByBuYVgfmJ2DMvhhS3sOctMbfXAQ2vnwI; path=/; samesite=none; secure; httponly; partitioned',
'foo=; path=/path1; expires=Thu, 01 Jan 1970 00:00:00 GMT; samesite=none; secure; httponly',
'foo.sig=; path=/path1; expires=Thu, 01 Jan 1970 00:00:00 GMT; samesite=none; secure; httponly',
'foo=hello; path=/path1; samesite=none; secure; httponly; partitioned',
'foo.sig=ZWbaA4bWk8ByBuYVgfmJ2DMvhhS3sOctMbfXAQ2vnwI; path=/path1; samesite=none; secure; httponly; partitioned',
'foo=; path=/path2; expires=Thu, 01 Jan 1970 00:00:00 GMT; samesite=none; secure; httponly',
'foo.sig=; path=/path2; expires=Thu, 01 Jan 1970 00:00:00 GMT; samesite=none; secure; httponly',
'foo=hello; path=/path2; samesite=none; secure; httponly; partitioned',
'foo.sig=ZWbaA4bWk8ByBuYVgfmJ2DMvhhS3sOctMbfXAQ2vnwI; path=/path2; samesite=none; secure; httponly; partitioned',
'foo=; path=/path3; expires=Thu, 01 Jan 1970 00:00:00 GMT; samesite=none; secure; httponly',
'foo.sig=; path=/path3; expires=Thu, 01 Jan 1970 00:00:00 GMT; samesite=none; secure; httponly',
'foo=hello; path=/path3; samesite=none; secure; httponly; partitioned',
'foo.sig=ZWbaA4bWk8ByBuYVgfmJ2DMvhhS3sOctMbfXAQ2vnwI; path=/path3; samesite=none; secure; httponly; partitioned',
]);
});

it('should work on unpartitioned = true and partitioned = true with different null path', () => {
const cookies = Cookies({
secure: true,
headers: {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.3945.29 Safari/537.36',
},
}, { secure: true }, { partitioned: true, removeUnpartitioned: true, sameSite: 'None' });
cookies.set('foo', 'hello', {
signed: 1,
});
cookies.set('foo', 'hello', {
signed: 1,
path: '/path1',
});
cookies.set('foo', 'hello', {
signed: 1,
path: '/path2',
});
cookies.set('foo', 'hello', {
signed: 1,
path: null,
});

const headers = cookies.ctx.response.headers['set-cookie'];
assert.deepEqual(headers, [
'foo=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; samesite=none; secure; httponly',
'foo=hello; path=/; samesite=none; secure; httponly; partitioned',
'foo=; path=/path1; expires=Thu, 01 Jan 1970 00:00:00 GMT; samesite=none; secure; httponly',
'foo=hello; path=/path1; samesite=none; secure; httponly; partitioned',
'foo=; path=/path2; expires=Thu, 01 Jan 1970 00:00:00 GMT; samesite=none; secure; httponly',
'foo=hello; path=/path2; samesite=none; secure; httponly; partitioned',
'foo=; expires=Thu, 01 Jan 1970 00:00:00 GMT; samesite=none; secure; httponly',
'foo.sig=; expires=Thu, 01 Jan 1970 00:00:00 GMT; samesite=none; secure; httponly',
'foo=hello; samesite=none; secure; httponly; partitioned',
'foo.sig=ZWbaA4bWk8ByBuYVgfmJ2DMvhhS3sOctMbfXAQ2vnwI; samesite=none; secure; httponly; partitioned',
]);
});

it('should work with overwrite = true', () => {
const cookies = Cookies({
secure: true,
Expand Down
Loading