Experimental CSP Level 3 directives
Это содержимое пока не доступно на вашем языке.
Type: boolean
Default: false
astro@6.2.0
Бета
This experimental feature enables support for CSP Level 3 granular directives: script-src-elem, script-src-attr, style-src-elem, and style-src-attr.
These directives let you apply separate Content Security Policies to inline <script> and <style> elements versus event-handler attributes (e.g. onclick) and the style attribute, giving you finer-grained control than the base script-src and style-src directives alone.
To enable this feature, set experimental.cspLevel3 to true in your Astro config along with a security.csp configuration:
import { defineConfig } from "astro/config"
export default defineConfig({ experimental: { cspLevel3: true, }, security: { csp: true, },})With CSP Level 3 enabled, you can configure the new scriptElemDirective and styleElemDirective options in your security.csp configuration, and add the corresponding script-src-attr and style-src-attr values via the existing directives array:
import { defineConfig } from "astro/config"
export default defineConfig({ experimental: { cspLevel3: true, }, security: { csp: { scriptElemDirective: { hashes: ['sha256-abc123'], resources: ['https://scripts.cdn.example.com/'], }, styleElemDirective: { hashes: ['sha256-def456'], resources: ['https://styles.cdn.example.com/'], }, directives: [ "script-src-attr 'none'", "style-src-attr 'unsafe-inline'", ], }, },})You can also inject Level 3 resources and hashes at runtime via Astro.csp:
---Astro.csp.insertScriptElemResource('https://scripts.cdn.example.com/');Astro.csp.insertStyleElemHash('sha256-abc123');---
<html> <head><title>My Page</title></head> <body> <h1>Hello</h1> </body></html>Configuration
Section titled “Configuration”scriptElemDirective
Section titled “scriptElemDirective”Type: { hashes?: string[], resources?: string[] }
A configuration object for the script-src-elem directive. This controls which inline <script> elements are allowed to execute.
scriptElemDirective.hashes
Section titled “scriptElemDirective.hashes”Type: string[]
Default: []
An array of CSP hashes (e.g. sha256-..., sha384-..., sha512-...) to add to the script-src-elem directive. These hashes authorize specific inline script content.
scriptElemDirective.resources
Section titled “scriptElemDirective.resources”Type: string[]
Default: []
An array of allowed source URLs for the script-src-elem directive.
styleElemDirective
Section titled “styleElemDirective”Type: { hashes?: string[], resources?: string[] }
A configuration object for the style-src-elem directive. This controls which inline <style> elements are allowed.
styleElemDirective.hashes
Section titled “styleElemDirective.hashes”Type: string[]
Default: []
An array of CSP hashes to add to the style-src-elem directive.
styleElemDirective.resources
Section titled “styleElemDirective.resources”Type: string[]
Default: []
An array of allowed source URLs for the style-src-elem directive.
Attribute directives
Section titled “Attribute directives”The script-src-attr and style-src-attr directives can be set using the existing directives array:
script-src-attrcontrols event-handler attributes likeonclick,onload, etc.style-src-attrcontrols thestyleattribute on HTML elements.
export default defineConfig({ experimental: { cspLevel3: true, }, security: { csp: { directives: [ "script-src-attr 'none'", "style-src-attr 'unsafe-inline'", ], }, },})Runtime API
Section titled “Runtime API”When experimental.cspLevel3 is enabled, four additional methods are available on the Astro.csp object:
insertScriptElemResource(resource)— adds a source URL to thescript-src-elemdirectiveinsertScriptElemHash(hash)— adds a hash to thescript-src-elemdirectiveinsertStyleElemResource(resource)— adds a source URL to thestyle-src-elemdirectiveinsertStyleElemHash(hash)— adds a hash to thestyle-src-elemdirective
These methods are also available on the context.csp object in middleware:
export function onRequest(context, next) { context.csp.insertScriptElemResource('https://scripts.cdn.example.com/'); return next();}How it works
Section titled “How it works”When CSP Level 3 directives are enabled, Astro adds the script-src-elem and style-src-elem directives to the CSP <meta> tag alongside the existing script-src and style-src directives.
Auto-generated hashes from script-src and style-src are automatically inherited into script-src-elem and style-src-elem when you have configured hash or resource values for the corresponding Level 3 directive. This means you do not need to duplicate your hashes manually.
The script-src-attr and style-src-attr directives are not auto-populated and must be configured explicitly via the directives array.