"sha256-QOxqn7EUzb3ydF9SALJoJGWSvywW9R0AfTDSenB83Z8=" csp.ts
Declarations #
29 declarations
COLOR_SCHEME_SCRIPT_HASH #
create_csp_directives #
(options?: CreateCspDirectivesOptions): CspDirectives This is designed for compatibility with SvelteKit
and maps to the KitConfig directives option.
The goal is to provide an ergonomic, modern, and safe API
for Content Security Policy (CSP) creation
that's simple to write and audit, and isn't error-prone.
Things like validation and rendering to a string are out of scope and left to SvelteKit.
options
{}returns
CspDirectives CreateCspDirectivesOptions #
CreateCspDirectivesOptions directives
Override or transform specific directives.
Returning null or undefined from a transform function will remove the directive.
{
[K in CspDirective]?:
| CspDirectiveValue<K> // Static value replacement
| null // Removes the directive
// Transform function returning one of the previous types
| ((value: CspDirectiveValue<K>) => CspDirectiveValue<K> | null);
}trusted_sources
Sources to include based on their trust levels.
Array<CspSourceSpec>value_defaults
Override default values for specific directives,
merging with value_defaults_base (or replacing if that directive is null in the base).
Partial<typeof csp_directive_value_defaults>value_defaults_base
Base values for directive defaults.
Set to null or {} to start with no defaults.
Defaults to csp_directive_value_defaults.
Partial<typeof csp_directive_value_defaults> | nullrequired_trust_defaults
Override trust requirements for specific directives,
merging with required_trust_defaults_base (or replacing if that directive is null in the base).
Partial<typeof csp_directive_required_trust_defaults>required_trust_defaults_base
Base values for directive trust requirements.
Set to null or {} to start with no trust requirements.
Defaults to csp_directive_required_trust_defaults.
Partial<typeof csp_directive_required_trust_defaults> | nullcsp_directive_required_trust_defaults #
Record<keyof CspDirectives, "low" | "medium" | "high" | null> Sources that meet this trust requirement are included for it by default.
If null, no trusted sources are added to the directive automatically.
Directives that don't support sources or default to ['none'] are null.
Feedback is welcome, please see the issues - https://github.com/ryanatkn/fuz/issues
csp_directive_spec_by_name #
Map<keyof CspDirectives, CspDirectiveSpec> csp_directive_specs #
CspDirectiveSpec[] Static data descriptors for the CSP directives. Fuz excludes deprecated directives, so those are intentionally omitted, but any newer missing directives are bugs.
Could be moved to csp.ts but is currently here to keep that module smaller.
see also
csp_directive_value_defaults #
Record<keyof CspDirectives, boolean | (CspSource | CspActionSource)[] | CspSources | CspFrameSource[] | string[] | "script"[] | ("allow-downloads-without-user-activation" | ... 11 more ... | "allow-top-navigation-by-user-activation")[] | null> The base CSP directive defaults.
Prioritizes safety but loosens around media and styles, relying on defense-in-depth.
Customizable via CreateCspDirectivesOptions.defaults.
csp_trust_level_value #
Record<"low" | "medium" | "high", number | undefined> Numeric values for CSP trust levels, csp_trust_levels.
Lower is less trusted.
Includes undefined in the type for safety.
csp_trust_levels #
readonly ["low", "medium", "high"] CspActionSource #
CspActionSource CspBaseSource #
CspBaseSource CspCryptoSource #
`nonce-${string}` | `sha256-${string}` | `sha384-${string}` | `sha512-${string}` CspDirective #
keyof CspDirectives CspDirectives #
CspDirectives sandbox
Array<
| 'allow-downloads-without-user-activation'
| 'allow-forms'
| 'allow-modals'
| 'allow-orientation-lock'
| 'allow-pointer-lock'
| 'allow-popups'
| 'allow-popups-to-escape-sandbox'
| 'allow-presentation'
| 'allow-same-origin'
| 'allow-scripts'
| 'allow-storage-access-by-user-activation'
| 'allow-top-navigation'
| 'allow-top-navigation-by-user-activation'
>CspDirectiveSpec #
CspDirectiveSpec name
fallback
Array<CspDirective> | nullfallback_of
Array<CspDirective> | nullCspDirectiveValue #
CspDirectiveValue<T> generics
T
CspFrameSource #
CspFrameSource CspHostNameScheme #
CspHostNameScheme CspHostProtocolSchemes #
CspHostProtocolSchemes CspHostSource #
`${string}.${string}` | "localhost" | `${string}.${string}:${number}` | `${string}.${string}:*` | `localhost:${number}` | "localhost:*" | `${string}://${string}.${string}` | `${string}://${string}.${string}:${number}` | `${string}://${string}.${string}:*` | `${string}://localhost` | `${string}://localhost:${number}`... CspPortScheme #
CspPortScheme CspSchemeSource #
CspSchemeSource CspSource #
CspSource CspSources #
CspSources CspSourceSpec #
CspSourceSpec source
trust
directives
Array<CspDirective>CspTrustLevel #
"low" | "medium" | "high" Trust levels for CSP sources.
With the base defaults, trust levels roughly correspond to:
- low – Passive resources only (no script execution, no styling or UI control).
Examples: img-src, font-src.
- medium – Content that may affect layout, styling, or embed external browsing contexts,
but cannot directly run code in the page's JS execution environment or
perform other high-risk actions. Examples: style-src, frame-src, frame-ancestors.
- high – Sources that can execute code in the page's context or open powerful network
channels. Examples: script-src, connect-src, child-src.
- null – No trust. This is used for directives that don't support sources.
is_csp_trusted #
(required_trust: "low" | "medium" | "high" | null | undefined, granted_trust: "low" | "medium" | "high" | null | undefined): boolean Determines if a granted trust level is sufficient to satisfy a required trust level.
Trust levels have the following hierarchy: - 'high' sources can be used in high, medium, and low trust directives (highest privilege) - 'medium' sources can be used in medium and low trust directives - 'low' sources can only be used in low trust directives (lowest privilege)
required_trust
"low" | "medium" | "high" | null | undefinedgranted_trust
"low" | "medium" | "high" | null | undefinedreturns
boolean parse_csp_directive #
(directive: unknown): keyof CspDirectives | null directive
unknownreturns
keyof CspDirectives | null parse_csp_trust_level #
(trust: unknown): "low" | "medium" | "high" | null Validates and extracts a CSP trust level from an unknown value.
trust
unknownreturns
"low" | "medium" | "high" | null