From d97f368de3eb8dca110820093633b47cee0aa2d9 Mon Sep 17 00:00:00 2001 From: Shubham Padkonde Date: Wed, 16 Sep 2026 10:23:40 +0530 Subject: [PATCH 1/2] Preserve explicit SameSite=None in cookie headers Fixes #378. --- api/tough-cookie.api.md | 1 + lib/__tests__/sameSite.spec.ts | 31 +++++++++++++++++++++++++++++++ lib/cookie/cookie.ts | 8 +++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/api/tough-cookie.api.md b/api/tough-cookie.api.md index e63b1186..b7f97a62 100644 --- a/api/tough-cookie.api.md +++ b/api/tough-cookie.api.md @@ -44,6 +44,7 @@ export class Cookie { static sameSiteCanonical: { readonly strict: "Strict"; readonly lax: "Lax"; + readonly none: "None"; }; // @internal (undocumented) static sameSiteLevel: { diff --git a/lib/__tests__/sameSite.spec.ts b/lib/__tests__/sameSite.spec.ts index 869bb3d5..c1e24dc4 100644 --- a/lib/__tests__/sameSite.spec.ts +++ b/lib/__tests__/sameSite.spec.ts @@ -198,6 +198,37 @@ describe('Same-Site Cookies', function () { expect(lax.toString()).toBe('lax=okay; SameSite=Lax') }) + it('preserves explicit SameSite=None when parsing and serializing', () => { + const cookie = parse('cross=allowed; Secure; SameSite=nOnE') + const serialized = cookie.toString() + expect(serialized).toBe('cross=allowed; Secure; SameSite=None') + expect(parse(serialized).sameSite).toBe('none') + }) + + it.each(['none', 'None', 'NONE', 'nOnE'])( + 'canonicalizes an explicitly constructed SameSite=%s cookie', + (sameSite) => { + const cookie = new Cookie({ + key: 'cross', + value: 'allowed', + secure: true, + sameSite, + }) + expect(cookie.toString()).toBe('cross=allowed; Secure; SameSite=None') + }, + ) + + it('preserves explicit SameSite=None in CookieJar Set-Cookie headers', async () => { + const secureUrl = 'https://example.com/' + await cookieJar.setCookie( + 'cross=allowed; Path=/; Secure; SameSite=None', + secureUrl, + ) + expect(await cookieJar.getSetCookieStrings(secureUrl)).toEqual([ + 'cross=allowed; Path=/; Secure; SameSite=None', + ]) + }) + it('omit if same-site was not specified', () => { expect(normal.toString()).toBe('normal=whatever') }) diff --git a/lib/cookie/cookie.ts b/lib/cookie/cookie.ts index 7b01b3d2..b1402858 100644 --- a/lib/cookie/cookie.ts +++ b/lib/cookie/cookie.ts @@ -767,7 +767,7 @@ export class Cookie { if (this.httpOnly) { str += '; HttpOnly' } - if (this.sameSite && this.sameSite !== 'none') { + if (this.sameSite) { if ( this.sameSite.toLowerCase() === Cookie.sameSiteCanonical.lax.toLowerCase() @@ -778,6 +778,11 @@ export class Cookie { Cookie.sameSiteCanonical.strict.toLowerCase() ) { str += `; SameSite=${Cookie.sameSiteCanonical.strict}` + } else if ( + this.sameSite.toLowerCase() === + Cookie.sameSiteCanonical.none.toLowerCase() + ) { + str += `; SameSite=${Cookie.sameSiteCanonical.none}` } else { str += `; SameSite=${this.sameSite}` } @@ -992,6 +997,7 @@ export class Cookie { static sameSiteCanonical = { strict: 'Strict', lax: 'Lax', + none: 'None', } as const /** From 1993fc0a57c9302b6c6c215b2299a13aee4bf8e2 Mon Sep 17 00:00:00 2001 From: Shubham Padkonde Date: Wed, 16 Sep 2026 18:16:55 +0530 Subject: [PATCH 2/2] Re-run CLA check