Skip to content
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
1 change: 1 addition & 0 deletions api/tough-cookie.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class Cookie {
static sameSiteCanonical: {
readonly strict: "Strict";
readonly lax: "Lax";
readonly none: "None";
};
// @internal (undocumented)
static sameSiteLevel: {
Expand Down
31 changes: 31 additions & 0 deletions lib/__tests__/sameSite.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand Down
8 changes: 7 additions & 1 deletion lib/cookie/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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}`
}
Expand Down Expand Up @@ -995,6 +1000,7 @@ export class Cookie {
static sameSiteCanonical = {
strict: 'Strict',
lax: 'Lax',
none: 'None',
} as const

/**
Expand Down
Loading