Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion api/docs/tough-cookie.cookie.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ A boolean flag indicating if a cookie had no 'Path' attribute and the default pa

</td><td>

string \| undefined
[SameSiteLevel](./tough-cookie.samesitelevel.md) \| undefined


</td><td>
Expand Down
2 changes: 1 addition & 1 deletion api/docs/tough-cookie.cookie.samesite.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ The 'SameSite' attribute of a cookie as defined in RFC6265bis (See [RFC6265bis (
**Signature:**

```typescript
sameSite: string | undefined;
sameSite: SameSiteLevel | undefined;
```
2 changes: 1 addition & 1 deletion api/docs/tough-cookie.createcookieoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ _(Optional)_ A boolean flag indicating if a cookie had no 'Path' attribute and t

</td><td>

string \| undefined
[SameSiteLevel](./tough-cookie.samesitelevel.md) \| undefined


</td><td>
Expand Down
2 changes: 1 addition & 1 deletion api/docs/tough-cookie.createcookieoptions.samesite.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ The 'SameSite' attribute of a cookie as defined in RFC6265bis (See [RFC6265bis (
**Signature:**

```typescript
sameSite?: string | undefined;
sameSite?: SameSiteLevel | undefined;
```
2 changes: 1 addition & 1 deletion api/docs/tough-cookie.getcookiesoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Defaults to `true` if not provided.

</td><td>

'none' \| 'lax' \| 'strict' \| undefined
[SameSiteLevel](./tough-cookie.samesitelevel.md) \| undefined


</td><td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Defaults to `undefined` if not provided.
**Signature:**

```typescript
sameSiteContext?: 'none' | 'lax' | 'strict' | undefined;
sameSiteContext?: SameSiteLevel | undefined;
```

## Remarks
Expand Down
11 changes: 11 additions & 0 deletions api/docs/tough-cookie.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,17 @@ Description
The inverse of NonNullable<T>.


</td></tr>
<tr><td>

[SameSiteLevel](./tough-cookie.samesitelevel.md)


</td><td>

The normalized values of the SameSite cookie attribute and request context.


</td></tr>
<tr><td>

Expand Down
13 changes: 13 additions & 0 deletions api/docs/tough-cookie.samesitelevel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [tough-cookie](./tough-cookie.md) &gt; [SameSiteLevel](./tough-cookie.samesitelevel.md)

## SameSiteLevel type

The normalized values of the SameSite cookie attribute and request context.

**Signature:**

```typescript
type SameSiteLevel = 'strict' | 'lax' | 'none';
```
2 changes: 1 addition & 1 deletion api/docs/tough-cookie.setcookieoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Defaults to `Date.now()` if not provided.

</td><td>

'strict' \| 'lax' \| 'none' \| undefined
[SameSiteLevel](./tough-cookie.samesitelevel.md) \| undefined


</td><td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Defaults to `undefined` if not provided.
**Signature:**

```typescript
sameSiteContext?: 'strict' | 'lax' | 'none' | undefined;
sameSiteContext?: SameSiteLevel | undefined;
```

## Remarks
Expand Down
11 changes: 7 additions & 4 deletions api/tough-cookie.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Cookie {
static parse(str: string, options?: ParseCookieOptions): Cookie | undefined;
path: string | null;
pathIsDefault: boolean | null;
sameSite: string | undefined;
sameSite: SameSiteLevel | undefined;
// @internal (undocumented)
static sameSiteCanonical: {
readonly strict: "Strict";
Expand Down Expand Up @@ -144,7 +144,7 @@ export interface CreateCookieOptions {
maxAge?: number | 'Infinity' | '-Infinity' | null;
path?: string | null;
pathIsDefault?: boolean | null;
sameSite?: string | undefined;
sameSite?: SameSiteLevel | undefined;
secure?: boolean;
value?: string;
}
Expand Down Expand Up @@ -172,7 +172,7 @@ export interface GetCookiesOptions {
allPaths?: boolean | undefined;
expire?: boolean | undefined;
http?: boolean | undefined;
sameSiteContext?: 'none' | 'lax' | 'strict' | undefined;
sameSiteContext?: SameSiteLevel | undefined;
sort?: boolean | undefined;
}

Expand Down Expand Up @@ -252,6 +252,9 @@ export const PrefixSecurityEnum: {
readonly DISABLED: "unsafe-disabled";
};

// @public
export type SameSiteLevel = 'strict' | 'lax' | 'none';

// @public
export type SerializedCookie = {
key?: string;
Expand All @@ -274,7 +277,7 @@ export interface SetCookieOptions {
ignoreError?: boolean | undefined;
loose?: boolean | undefined;
now?: Date | undefined;
sameSiteContext?: 'strict' | 'lax' | 'none' | undefined;
sameSiteContext?: SameSiteLevel | undefined;
}

// @public
Expand Down
2 changes: 2 additions & 0 deletions lib/__tests__/sameSite.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ describe('Same-Site Cookies', function () {

describe('Canonicalized Strings', () => {
it('garbage in = garbage out', () => {
// @ts-expect-error deliberately test invalid input from JavaScript callers
garbage.sameSite = 'GaRbAGe'
expect(garbage.toString()).toBe(
'garbageIn=treatedAsNone; SameSite=GaRbAGe',
Expand Down Expand Up @@ -212,6 +213,7 @@ describe('Same-Site Cookies', function () {
key: 'cross',
value: 'allowed',
secure: true,
// @ts-expect-error exercise mixed-case values from JavaScript callers
sameSite,
})
expect(cookie.toString()).toBe('cross=allowed; Secure; SameSite=None')
Expand Down
54 changes: 54 additions & 0 deletions lib/__tests__/sameSiteTypes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { describe, expect, expectTypeOf, it } from 'vitest'
import {
Cookie,
type CreateCookieOptions,
type GetCookiesOptions,
type SameSiteLevel,
type SetCookieOptions,
} from '../cookie/index.js'

describe('SameSite types', () => {
// eslint-disable-next-line vitest/expect-expect -- expectTypeOf assertions are checked by _lint:types
it('shares the public type between cookies and request contexts', () => {
expectTypeOf<SameSiteLevel>().toEqualTypeOf<'strict' | 'lax' | 'none'>()
expectTypeOf<Cookie['sameSite']>().toEqualTypeOf<
SameSiteLevel | undefined
>()
expectTypeOf<CreateCookieOptions['sameSite']>().toEqualTypeOf<
SameSiteLevel | undefined
>()
expectTypeOf<GetCookiesOptions['sameSiteContext']>().toEqualTypeOf<
SameSiteLevel | undefined
>()
expectTypeOf<SetCookieOptions['sameSiteContext']>().toEqualTypeOf<
SameSiteLevel | undefined
>()
})

it.each<SameSiteLevel>(['strict', 'lax', 'none'])(
'accepts and round-trips %s',
(sameSite) => {
const cookie = new Cookie({ key: 'session', value: 'value', sameSite })
expect(Cookie.fromJSON(cookie.toJSON())?.sameSite).toBe(sameSite)
},
)

it('rejects invalid constructor values and property assignments', () => {
// @ts-expect-error only the three lowercase SameSite values are valid
const cookie = new Cookie({ sameSite: 'invalid' })
// @ts-expect-error header casing must be normalized before assignment
cookie.sameSite = 'Strict'
// Type narrowing does not add runtime validation for JavaScript callers.
expect(cookie.sameSite).toBe('Strict')
})

it('preserves legacy serialized values at runtime', () => {
const cookie = Cookie.fromJSON({
key: 'session',
value: 'value',
sameSite: 'GaRbAGe',
})
expect(cookie?.sameSite).toBe('GaRbAGe')
expect(cookie?.toString()).toBe('session=value; SameSite=GaRbAGe')
})
})
6 changes: 6 additions & 0 deletions lib/cookie/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* The normalized values of the SameSite cookie attribute and request context.
* @public
*/
export type SameSiteLevel = 'strict' | 'lax' | 'none'

/**
* Cookie prefixes are a way to indicate that a given cookie was set with a set of attributes simply by inspecting the
* first few characters of the cookie's name. These are defined in {@link https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-13#section-4.1.3 | RFC6265bis - Section 4.1.3}.
Expand Down
12 changes: 9 additions & 3 deletions lib/cookie/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { canonicalDomain } from './canonicalDomain.js'
import {
IP_V4_REGEX_OBJECT,
IP_V6_REGEX_OBJECT,
type SameSiteLevel,
type SerializedCookie,
} from './constants.js'

Expand Down Expand Up @@ -311,11 +312,16 @@ function fromJSON(str: unknown): Cookie | undefined {
switch (prop) {
case 'key':
case 'value':
case 'sameSite':
if (typeof val === 'string') {
c[prop] = val
}
break
case 'sameSite':
if (typeof val === 'string') {
// Preserve legacy serialized values without changing runtime behavior.
c.sameSite = val as SameSiteLevel
}
break
case 'expires':
case 'creation':
case 'lastAccessed':
Expand Down Expand Up @@ -403,7 +409,7 @@ export interface CreateCookieOptions {
/** {@inheritDoc Cookie.lastAccessed} */
lastAccessed?: Date | 'Infinity' | null
/** {@inheritDoc Cookie.sameSite} */
sameSite?: string | undefined
sameSite?: SameSiteLevel | undefined
}

const cookieDefaults = {
Expand Down Expand Up @@ -513,7 +519,7 @@ export class Cookie {
* The 'SameSite' attribute of a cookie as defined in RFC6265bis
* (See {@link https://www.ietf.org/archive/id/draft-ietf-httpbis-rfc6265bis-13.html#section-5.2 | RFC6265bis (v13) Section 5.2 }).
*/
sameSite: string | undefined
sameSite: SameSiteLevel | undefined

/**
* Create a new Cookie instance.
Expand Down
6 changes: 3 additions & 3 deletions lib/cookie/cookieJar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
IP_V4_REGEX_OBJECT,
IP_V6_REGEX_OBJECT,
PrefixSecurityEnum,
SameSiteLevel,
SerializedCookieJar,
} from './constants.js'
import { defaultPath } from './defaultPath.js'
Expand Down Expand Up @@ -65,7 +66,7 @@ export interface SetCookieOptions {
* - It is highly recommended that you read {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-02##section-8.8 | RFC6265bis - Section 8.8}
* which discusses security considerations and defence on SameSite cookies in depth.
*/
sameSiteContext?: 'strict' | 'lax' | 'none' | undefined
sameSiteContext?: SameSiteLevel | undefined
/**
* Silently ignore things like parse errors and invalid domains. Store errors aren't ignored by this option.
*
Expand Down Expand Up @@ -145,7 +146,7 @@ export interface GetCookiesOptions {
* - It is highly recommended that you read {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-02##section-8.8 | RFC6265bis - Section 8.8}
* which discusses security considerations and defence on SameSite cookies in depth.
*/
sameSiteContext?: 'none' | 'lax' | 'strict' | undefined
sameSiteContext?: SameSiteLevel | undefined
/**
* Flag to indicate if the returned cookies should be sorted or not.
*
Expand Down Expand Up @@ -253,7 +254,6 @@ function getCookieContext(url: unknown): UrlContext {
}
}

type SameSiteLevel = keyof (typeof Cookie)['sameSiteLevel']
function checkSameSiteContext(value: string): SameSiteLevel | undefined {
const context = value.toLowerCase()
if (context === 'none' || context === 'lax' || context === 'strict') {
Expand Down
1 change: 1 addition & 0 deletions lib/cookie/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export { type Callback, type ErrorCallback, type Nullable } from '../utils.js'
export { canonicalDomain } from './canonicalDomain.js'
export {
PrefixSecurityEnum,
type SameSiteLevel,
type SerializedCookie,
type SerializedCookieJar,
} from './constants.js'
Expand Down
Loading