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
4 changes: 2 additions & 2 deletions api/docs/tough-cookie.cookiejar.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ Description

</td><td>

string
[PrefixSecurityMode](./tough-cookie.prefixsecuritymode.md)


</td><td>

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)<!-- -->.


</td></tr>
Expand Down
4 changes: 2 additions & 2 deletions api/docs/tough-cookie.cookiejar.prefixsecurity.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```
4 changes: 2 additions & 2 deletions api/docs/tough-cookie.createcookiejaroptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ Defaults to `false` if not specified.

</td><td>

'strict' \| 'silent' \| 'unsafe-disabled' \| undefined
[PrefixSecurityMode](./tough-cookie.prefixsecuritymode.md) \| undefined


</td><td>

_(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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```
13 changes: 13 additions & 0 deletions api/docs/tough-cookie.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,19 @@ Description
The inverse of NonNullable<T>.


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

[PrefixSecurityMode](./tough-cookie.prefixsecuritymode.md)


</td><td>

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.


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

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

[Home](./index.md) &gt; [tough-cookie](./tough-cookie.md) &gt; [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)

7 changes: 5 additions & 2 deletions api/tough-cookie.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class CookieJar {
_importCookies(serialized: unknown, callback: Callback<CookieJar>): void;
// @internal (undocumented)
_importCookiesSync(serialized: unknown): void;
readonly prefixSecurity: string;
readonly prefixSecurity: PrefixSecurityMode;
removeAllCookies(callback: ErrorCallback): void;
removeAllCookies(): Promise<void>;
removeAllCookiesSync(): void;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
40 changes: 38 additions & 2 deletions lib/__tests__/cookiePrefixes.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<PrefixSecurityMode>()
expectTypeOf<PrefixSecurityMode>().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')
})
})
})
12 changes: 12 additions & 0 deletions lib/cookie/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}|:)|
Expand Down
13 changes: 6 additions & 7 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,
type PrefixSecurityMode,
SerializedCookieJar,
} from './constants.js'
import { defaultPath } from './defaultPath.js'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
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 PrefixSecurityMode,
type SerializedCookie,
type SerializedCookieJar,
} from './constants.js'
Expand Down
Loading