From 6ddf276edf2f8c9cf9d10c7fa3085b4cfb0205c7 Mon Sep 17 00:00:00 2001 From: ematipico Date: Thu, 2 Jul 2026 17:39:14 +0100 Subject: [PATCH 1/4] feat: document -elem and -attr CSP directives --- .../docs/en/reference/api-reference.mdx | 110 +++++++++++++++++- .../en/reference/integrations-reference.mdx | 40 +++++++ 2 files changed, 145 insertions(+), 5 deletions(-) diff --git a/src/content/docs/en/reference/api-reference.mdx b/src/content/docs/en/reference/api-reference.mdx index e75b24c83e28e..987250a45ea5a 100644 --- a/src/content/docs/en/reference/api-reference.mdx +++ b/src/content/docs/en/reference/api-reference.mdx @@ -1086,6 +1086,14 @@ You can customize the `` element per page from the `Astro` global inside ` When resources are inserted multiple times or from multiple sources (e.g. defined in your [`csp` config](/en/reference/configuration-reference/#securitycsp) and added using the following CSP runtime APIs, Astro will merge and deduplicate all resources to create your `` element. +:::caution[Scoping to more specific directives] +When you scope a source or hash to a more specific directive with the `kind` option (`'element'` or `'attribute'`), browsers use that directive instead of the generic `script-src`/`style-src` for its scope, and do not fall back to the generic directive. + +Astro automatically moves the hashes it generates for your inline scripts and styles onto the more specific directive so they keep working. However, it does **not** move your generic (`'default'`) resources. If you combine generic resources with more specific resources or hashes for the same directive family, the generic resources will not apply to the specific scope. Astro logs some warning if it detects in case a situation such as this one happens. + + To fix the issue, you have move all the resources and hashes to the specific directives. +::: + #### `csp.insertDirective()` @@ -1122,7 +1130,7 @@ content="

-**Type:** `(resource: string) => void`
+**Type:** `(resource: string | { resource: string; kind: 'element' | 'attribute' | 'default' }) => void`

@@ -1146,11 +1154,34 @@ content=" > ``` +

+ +You can also scope a source to a more specific directive by passing an object with a `kind`: `'element'` for `style-src-elem`, `'attribute'` for `style-src-attr`, or `'default'` (the same as a bare string) for `style-src`. + +```astro title="src/pages/index.astro" +--- +Astro.csp?.insertStyleResource({ resource: "'unsafe-inline'", kind: "attribute" }); +--- +``` + +After the build, the `` element for this individual page will add your source to the `style-src-attr` directive, which applies only to inline `style` attributes: + +```html + +``` + #### `csp.insertStyleHash()`

-**Type:** `(hash: CspHash) => void`
+**Type:** `(hash: CspHash | { hash: CspHash; kind: 'element' | 'attribute' | 'default' }) => void`

@@ -1174,11 +1205,34 @@ content=" > ``` +

+ +You can also scope a hash to a more specific directive by passing an object with a `kind`: `'element'` for `style-src-elem`, `'attribute'` for `style-src-attr`, or `'default'` (the same as a bare hash) for `style-src`. + +```astro title="src/pages/index.astro" +--- +Astro.csp?.insertStyleHash({ hash: "sha512-styleHash", kind: "element" }); +--- +``` + +After the build, the `` element for this individual page will add your hash to the `style-src-elem` directive. Astro's automatically generated style hashes also move there, since `style-src-elem` takes precedence over `style-src` for `