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

Accept common cookie attributes when deleting a cookie #10671

Merged
merged 4 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/shaggy-cats-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
fshafiee marked this conversation as resolved.
Show resolved Hide resolved
---

Accept standard cookie attributes when deleting a cookie
fshafiee marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 13 additions & 8 deletions packages/astro/src/core/cookies/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface AstroCookieGetOptions {
decode?: (value: string) => string;
}

type AstroCookieDeleteOptions = Pick<AstroCookieSetOptions, 'domain' | 'path'>;
type AstroCookieDeleteOptions = Omit<CookieSerializeOptions, 'expires' | 'maxAge'>;
fshafiee marked this conversation as resolved.
Show resolved Hide resolved

interface AstroCookieInterface {
value: string;
Expand Down Expand Up @@ -78,17 +78,22 @@ class AstroCookies implements AstroCookiesInterface {
* @param options Options related to this deletion, such as the path of the cookie.
*/
delete(key: string, options?: AstroCookieDeleteOptions): void {
/**
* The `@ts-expect-error` is necessary because `maxAge` and `expires` properties
* must not appear in the AstroCookieDeleteOptions type.
*/
const {
// @ts-expect-error
maxAge: _ignoredMaxAge,
// @ts-expect-error
expires: _ignoredExpires,
...sanitizedOptions
} = options || {};
const serializeOptions: CookieSerializeOptions = {
expires: DELETED_EXPIRATION,
...sanitizedOptions,
};

if (options?.domain) {
serializeOptions.domain = options.domain;
}
if (options?.path) {
serializeOptions.path = options.path;
}

// Set-Cookie: token=deleted; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT
this.#ensureOutgoingMap().set(key, [
DELETED_VALUE,
Expand Down
39 changes: 35 additions & 4 deletions packages/astro/test/units/cookies/delete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,57 @@ describe('astro/src/core/cookies', () => {
assert.equal(cookies.has('foo'), false);
});

it('can provide a path', () => {
it('deletes a cookie with attributes', () => {
let req = new Request('http://example.com/');
let cookies = new AstroCookies(req);

cookies.delete('foo', {
domain: 'example.com',
path: '/subpath/',
priority: 'high',
secure: true,
httpOnly: true,
sameSite: 'strict',
});

let headers = Array.from(cookies.headers());
assert.equal(headers.length, 1);
assert.equal(/foo=deleted/.test(headers[0]), true);
assert.equal(/Expires=Thu, 01 Jan 1970 00:00:00 GMT/.test(headers[0]), true);
assert.equal(/Domain=example.com/.test(headers[0]), true);
assert.equal(/Path=\/subpath\//.test(headers[0]), true);
assert.equal(/Priority=High/.test(headers[0]), true);
assert.equal(/Secure/.test(headers[0]), true);
assert.equal(/HttpOnly/.test(headers[0]), true);
assert.equal(/SameSite=Strict/.test(headers[0]), true);
});

it('ignores expires option', () => {
let req = new Request('http://example.com/');
let cookies = new AstroCookies(req);

cookies.delete('foo', {
expires: new Date(),
});

let headers = Array.from(cookies.headers());
assert.equal(headers.length, 1);
assert.equal(/foo=deleted/.test(headers[0]), true);
assert.equal(/Expires=Thu, 01 Jan 1970 00:00:00 GMT/.test(headers[0]), true);
});

it('can provide a domain', () => {
it('ignores maxAge option', () => {
let req = new Request('http://example.com/');
let cookies = new AstroCookies(req);

cookies.delete('foo', {
domain: '.example.com',
maxAge: 60,
});

let headers = Array.from(cookies.headers());
assert.equal(headers.length, 1);
assert.equal(/Domain=\.example\.com/.test(headers[0]), true);
assert.equal(/foo=deleted/.test(headers[0]), true);
assert.equal(/Expires=Thu, 01 Jan 1970 00:00:00 GMT/.test(headers[0]), true);
});
});
});
Loading