diff --git a/api/docs/tough-cookie.cookiejar.md b/api/docs/tough-cookie.cookiejar.md index 3d691955..f8d40b0b 100644 --- a/api/docs/tough-cookie.cookiejar.md +++ b/api/docs/tough-cookie.cookiejar.md @@ -83,12 +83,12 @@ Description -string +[PrefixSecurityMode](./tough-cookie.prefixsecuritymode.md) -The configured [PrefixSecurityEnum](./tough-cookie.prefixsecurityenum.md) value for the [CookieJar](./tough-cookie.cookiejar.md). +The configured [PrefixSecurityMode](./tough-cookie.prefixsecuritymode.md) value for the [CookieJar](./tough-cookie.cookiejar.md). diff --git a/api/docs/tough-cookie.cookiejar.prefixsecurity.md b/api/docs/tough-cookie.cookiejar.prefixsecurity.md index 6d0ab52d..3336be7c 100644 --- a/api/docs/tough-cookie.cookiejar.prefixsecurity.md +++ b/api/docs/tough-cookie.cookiejar.prefixsecurity.md @@ -4,10 +4,10 @@ ## CookieJar.prefixSecurity property -The configured [PrefixSecurityEnum](./tough-cookie.prefixsecurityenum.md) value for the [CookieJar](./tough-cookie.cookiejar.md). +The configured [PrefixSecurityMode](./tough-cookie.prefixsecuritymode.md) value for the [CookieJar](./tough-cookie.cookiejar.md). **Signature:** ```typescript -readonly prefixSecurity: string; +readonly prefixSecurity: PrefixSecurityMode; ``` diff --git a/api/docs/tough-cookie.createcookiejaroptions.md b/api/docs/tough-cookie.createcookiejaroptions.md index 70141bec..6fcc66d4 100644 --- a/api/docs/tough-cookie.createcookiejaroptions.md +++ b/api/docs/tough-cookie.createcookiejaroptions.md @@ -110,12 +110,12 @@ Defaults to `false` if not specified. -'strict' \| 'silent' \| 'unsafe-disabled' \| undefined +[PrefixSecurityMode](./tough-cookie.prefixsecuritymode.md) \| undefined -_(Optional)_ Controls how cookie prefixes are handled. See [PrefixSecurityEnum](./tough-cookie.prefixsecurityenum.md). +_(Optional)_ Controls how cookie prefixes are handled. See [PrefixSecurityMode](./tough-cookie.prefixsecuritymode.md). Defaults to `silent` if not specified. diff --git a/api/docs/tough-cookie.createcookiejaroptions.prefixsecurity.md b/api/docs/tough-cookie.createcookiejaroptions.prefixsecurity.md index b8c98ca9..2343c48d 100644 --- a/api/docs/tough-cookie.createcookiejaroptions.prefixsecurity.md +++ b/api/docs/tough-cookie.createcookiejaroptions.prefixsecurity.md @@ -4,12 +4,12 @@ ## CreateCookieJarOptions.prefixSecurity property -Controls how cookie prefixes are handled. See [PrefixSecurityEnum](./tough-cookie.prefixsecurityenum.md). +Controls how cookie prefixes are handled. See [PrefixSecurityMode](./tough-cookie.prefixsecuritymode.md). Defaults to `silent` if not specified. **Signature:** ```typescript -prefixSecurity?: 'strict' | 'silent' | 'unsafe-disabled' | undefined; +prefixSecurity?: PrefixSecurityMode | undefined; ``` diff --git a/api/docs/tough-cookie.md b/api/docs/tough-cookie.md index e09715c3..37fce5d7 100644 --- a/api/docs/tough-cookie.md +++ b/api/docs/tough-cookie.md @@ -415,6 +415,19 @@ Description The inverse of NonNullable. + + + +[PrefixSecurityMode](./tough-cookie.prefixsecuritymode.md) + + + + +Prefix security enforcement mode for a CookieJar per RFC 6265bis. + +- `'silent'` - Enable cookie prefix checking but silently ignores the cookie if conditions are not met. Default. - `'strict'` - Enables cookie prefix checking and will raise an error if conditions are not met. - `'unsafe-disabled'` - Disables cookie prefix checking. + + diff --git a/api/docs/tough-cookie.prefixsecuritymode.md b/api/docs/tough-cookie.prefixsecuritymode.md new file mode 100644 index 00000000..e2edb6b6 --- /dev/null +++ b/api/docs/tough-cookie.prefixsecuritymode.md @@ -0,0 +1,17 @@ + + +[Home](./index.md) > [tough-cookie](./tough-cookie.md) > [PrefixSecurityMode](./tough-cookie.prefixsecuritymode.md) + +## PrefixSecurityMode type + +Prefix security enforcement mode for a CookieJar per RFC 6265bis. + +- `'silent'` - Enable cookie prefix checking but silently ignores the cookie if conditions are not met. Default. - `'strict'` - Enables cookie prefix checking and will raise an error if conditions are not met. - `'unsafe-disabled'` - Disables cookie prefix checking. + +**Signature:** + +```typescript +type PrefixSecurityMode = (typeof PrefixSecurityEnum)[keyof typeof PrefixSecurityEnum]; +``` +**References:** [PrefixSecurityEnum](./tough-cookie.prefixsecurityenum.md) + diff --git a/api/tough-cookie.api.md b/api/tough-cookie.api.md index ef73b3d2..9e73914b 100644 --- a/api/tough-cookie.api.md +++ b/api/tough-cookie.api.md @@ -105,7 +105,7 @@ export class CookieJar { _importCookies(serialized: unknown, callback: Callback): void; // @internal (undocumented) _importCookiesSync(serialized: unknown): void; - readonly prefixSecurity: string; + readonly prefixSecurity: PrefixSecurityMode; removeAllCookies(callback: ErrorCallback): void; removeAllCookies(): Promise; removeAllCookiesSync(): void; @@ -127,7 +127,7 @@ export interface CreateCookieJarOptions { allowSecureOnLocal?: boolean | undefined; allowSpecialUseDomain?: boolean | undefined; looseMode?: boolean | undefined; - prefixSecurity?: 'strict' | 'silent' | 'unsafe-disabled' | undefined; + prefixSecurity?: PrefixSecurityMode | undefined; rejectPublicSuffixes?: boolean | undefined; } @@ -252,6 +252,9 @@ export const PrefixSecurityEnum: { readonly DISABLED: "unsafe-disabled"; }; +// @public +export type PrefixSecurityMode = (typeof PrefixSecurityEnum)[keyof typeof PrefixSecurityEnum]; + // @public export type SerializedCookie = { key?: string; diff --git a/lib/__tests__/cookiePrefixes.spec.ts b/lib/__tests__/cookiePrefixes.spec.ts index db8be401..339e1303 100644 --- a/lib/__tests__/cookiePrefixes.spec.ts +++ b/lib/__tests__/cookiePrefixes.spec.ts @@ -1,5 +1,8 @@ -import { beforeEach, describe, expect, it } from 'vitest' -import { PrefixSecurityEnum } from '../cookie/constants.js' +import { beforeEach, describe, expect, expectTypeOf, it } from 'vitest' +import { + PrefixSecurityEnum, + type PrefixSecurityMode, +} from '../cookie/constants.js' import { CookieJar } from '../cookie/cookieJar.js' let cookieJar: CookieJar @@ -226,4 +229,37 @@ describe('When `prefixSecurity` is enabled for `CookieJar`', () => { }) }) }) + + describe('PrefixSecurityMode type and configuration', () => { + it('types CookieJar.prefixSecurity as PrefixSecurityMode', () => { + const jar = new CookieJar() + expect(jar.prefixSecurity).toBe('silent') + expectTypeOf(jar.prefixSecurity).toEqualTypeOf() + expectTypeOf().toEqualTypeOf< + 'silent' | 'strict' | 'unsafe-disabled' + >() + }) + + it('defaults to silent mode when option is omitted', () => { + const jar = new CookieJar() + expect(jar.prefixSecurity).toBe('silent') + }) + + it('normalizes valid PrefixSecurityMode options case-insensitively', () => { + const strictJar = new CookieJar(null, { prefixSecurity: 'strict' }) + expect(strictJar.prefixSecurity).toBe('strict') + + const disabledJar = new CookieJar(null, { + prefixSecurity: 'unsafe-disabled', + }) + expect(disabledJar.prefixSecurity).toBe('unsafe-disabled') + }) + + it('falls back to silent mode for unrecognized string values', () => { + const jar = new CookieJar(null, { + prefixSecurity: 'unknown-mode' as unknown as PrefixSecurityMode, + }) + expect(jar.prefixSecurity).toBe('silent') + }) + }) }) diff --git a/lib/cookie/constants.ts b/lib/cookie/constants.ts index 49d8de5e..9e492b3b 100644 --- a/lib/cookie/constants.ts +++ b/lib/cookie/constants.ts @@ -18,6 +18,18 @@ export const PrefixSecurityEnum = { } as const Object.freeze(PrefixSecurityEnum) +/** + * Prefix security enforcement mode for a CookieJar per RFC 6265bis. + * + * - `'silent'` - Enable cookie prefix checking but silently ignores the cookie if conditions are not met. Default. + * - `'strict'` - Enables cookie prefix checking and will raise an error if conditions are not met. + * - `'unsafe-disabled'` - Disables cookie prefix checking. + * + * @public + */ +export type PrefixSecurityMode = + (typeof PrefixSecurityEnum)[keyof typeof PrefixSecurityEnum] + const IP_V6_REGEX = ` \\[?(?: (?:[a-fA-F\\d]{1,4}:){7}(?:[a-fA-F\\d]{1,4}|:)| diff --git a/lib/cookie/cookieJar.ts b/lib/cookie/cookieJar.ts index 5f539524..046b28be 100644 --- a/lib/cookie/cookieJar.ts +++ b/lib/cookie/cookieJar.ts @@ -18,6 +18,7 @@ import { IP_V4_REGEX_OBJECT, IP_V6_REGEX_OBJECT, PrefixSecurityEnum, + type PrefixSecurityMode, SerializedCookieJar, } from './constants.js' import { defaultPath } from './defaultPath.js' @@ -172,11 +173,11 @@ export interface CreateCookieJarOptions { */ looseMode?: boolean | undefined /** - * Controls how cookie prefixes are handled. See {@link PrefixSecurityEnum}. + * Controls how cookie prefixes are handled. See {@link PrefixSecurityMode}. * * Defaults to `silent` if not specified. */ - prefixSecurity?: 'strict' | 'silent' | 'unsafe-disabled' | undefined + prefixSecurity?: PrefixSecurityMode | undefined /** * Accepts {@link https://datatracker.ietf.org/doc/html/rfc6761 | special-use domains } such as `local`. * This is not in the standard, but is used sometimes on the web and is accepted by most browsers. It is @@ -302,11 +303,9 @@ function isHostPrefixConditionMet(cookie: Cookie): boolean { ) } -type PrefixSecurityValue = - (typeof PrefixSecurityEnum)[keyof typeof PrefixSecurityEnum] function getNormalizedPrefixSecurity( prefixSecurity: string, -): PrefixSecurityValue { +): PrefixSecurityMode { const normalizedPrefixSecurity = prefixSecurity.toLowerCase() /* The three supported options */ switch (normalizedPrefixSecurity) { @@ -338,9 +337,9 @@ export class CookieJar { readonly store: Store /** - * The configured {@link PrefixSecurityEnum} value for the {@link CookieJar}. + * The configured {@link PrefixSecurityMode} value for the {@link CookieJar}. */ - readonly prefixSecurity: string + readonly prefixSecurity: PrefixSecurityMode /** * Creates a new `CookieJar` instance. diff --git a/lib/cookie/index.ts b/lib/cookie/index.ts index e41e0b71..6973979a 100644 --- a/lib/cookie/index.ts +++ b/lib/cookie/index.ts @@ -12,6 +12,7 @@ export { type Callback, type ErrorCallback, type Nullable } from '../utils.js' export { canonicalDomain } from './canonicalDomain.js' export { PrefixSecurityEnum, + type PrefixSecurityMode, type SerializedCookie, type SerializedCookieJar, } from './constants.js'