-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: document -elem and -attr CSP directives #14196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ematipico
wants to merge
4
commits into
main
Choose a base branch
from
feat/docs-csp-elem-attrs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+145
−5
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6ddf276
feat: document -elem and -attr CSP directives
ematipico f31c229
Update src/content/docs/en/reference/api-reference.mdx
ematipico 0e44905
Apply suggestions from code review
ematipico 4a9b66e
Update src/content/docs/en/reference/api-reference.mdx
ematipico File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1086,6 +1086,14 @@ You can customize the `<meta>` 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 `<meta>` 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 a warning if it detects such a situation. | ||
|
|
||
| To fix the issue, move all the resources and hashes to the specific directives. | ||
| ::: | ||
|
|
||
|
|
||
| #### `csp.insertDirective()` | ||
|
|
||
|
|
@@ -1122,7 +1130,7 @@ content=" | |
|
|
||
| <p> | ||
|
|
||
| **Type:** `(resource: string) => void`<br /> | ||
| **Type:** `(resource: string | { resource: string; kind: 'element' | 'attribute' | 'default' }) => void`<br /> | ||
| <Since v="6.0.0" /> | ||
| </p> | ||
|
|
||
|
|
@@ -1146,11 +1154,34 @@ content=" | |
| > | ||
| ``` | ||
|
|
||
| <p><Since v="7.1.0" /></p> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, nice catch. I forgot about this pattern when I answered on Discord. I think it makes sense now that I have the content right in front of me! |
||
|
|
||
| You can also scope a source to a more specific directive by passing an object with a `kind` property. Use `'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 `<meta>` element for this individual page will add your source to the `style-src-attr` directive, which applies only to inline `style` attributes: | ||
|
|
||
| ```html | ||
| <meta | ||
| http-equiv="content-security-policy" | ||
| content=" | ||
| script-src 'self'; | ||
| style-src 'self'; | ||
| style-src-attr 'unsafe-inline'; | ||
| " | ||
| > | ||
| ``` | ||
|
|
||
| #### `csp.insertStyleHash()` | ||
|
|
||
| <p> | ||
|
|
||
| **Type:** `(hash: CspHash) => void`<br /> | ||
| **Type:** `(hash: CspHash | { hash: CspHash; kind: 'element' | 'attribute' | 'default' }) => void`<br /> | ||
| <Since v="6.0.0" /> | ||
| </p> | ||
|
|
||
|
|
@@ -1174,11 +1205,34 @@ content=" | |
| > | ||
| ``` | ||
|
|
||
| <p><Since v="7.1.0" /></p> | ||
|
|
||
| You can also scope a hash to a more specific directive by passing an object with a `kind` property. Use `'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 `<meta>` 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 `<style>` and `<link>` elements: | ||
|
|
||
| ```html | ||
| <meta | ||
| http-equiv="content-security-policy" | ||
| content=" | ||
| script-src 'self' 'sha256-somehash'; | ||
| style-src 'self'; | ||
| style-src-elem 'self' 'sha256-somehash' 'sha512-styleHash'; | ||
| " | ||
| > | ||
| ``` | ||
|
|
||
| #### `csp.insertScriptResource()` | ||
|
|
||
| <p> | ||
|
|
||
| **Type:** `(resource: string) => void`<br /> | ||
| **Type:** `(resource: string | { resource: string; kind: 'element' | 'attribute' | 'default' }) => void`<br /> | ||
| <Since v="6.0.0" /> | ||
| </p> | ||
|
|
||
|
|
@@ -1202,11 +1256,34 @@ content=" | |
| > | ||
| ``` | ||
|
|
||
| <p><Since v="7.1.0" /></p> | ||
|
|
||
| You can also scope a source to a more specific directive by passing an object with a `kind` property. Use `'element'` for `script-src-elem`, `'attribute'` for `script-src-attr`, or `'default'` (the same as a bare string) for `script-src`. | ||
|
|
||
| ```astro title="src/pages/index.astro" | ||
| --- | ||
| Astro.csp?.insertScriptResource({ resource: "https://scripts.cdn.example.com", kind: "element" }); | ||
| --- | ||
| ``` | ||
|
|
||
| After the build, the `<meta>` element for this individual page will add your source to the `script-src-elem` directive, which governs `<script>` elements: | ||
|
|
||
| ```html | ||
| <meta | ||
| http-equiv="content-security-policy" | ||
| content=" | ||
| script-src 'self'; | ||
| script-src-elem https://scripts.cdn.example.com; | ||
| style-src 'self'; | ||
| " | ||
| > | ||
| ``` | ||
|
|
||
| #### `csp.insertScriptHash()` | ||
|
|
||
| <p> | ||
|
|
||
| **Type:** `(hash: CspHash) => void`<br /> | ||
| **Type:** `(hash: CspHash | { hash: CspHash; kind: 'element' | 'attribute' | 'default' }) => void`<br /> | ||
| <Since v="6.0.0" /> | ||
| </p> | ||
|
|
||
|
|
@@ -1224,7 +1301,30 @@ After the build, the `<meta>` element for this individual page will add your has | |
| <meta | ||
| http-equiv="content-security-policy" | ||
| content=" | ||
| script-src 'self' 'sha256-somehash' 'sha512-styleHash'; | ||
| script-src 'self' 'sha256-somehash' 'sha512-scriptHash'; | ||
| style-src 'self' 'sha256-somehash'; | ||
| " | ||
| > | ||
| ``` | ||
|
|
||
| <p><Since v="7.1.0" /></p> | ||
|
|
||
| You can also scope a hash to a more specific directive by passing an object with a `kind` property. Use `'element'` for `script-src-elem`, `'attribute'` for `script-src-attr`, or `'default'` (the same as a bare hash) for `script-src`. | ||
|
|
||
| ```astro title="src/pages/index.astro" | ||
| --- | ||
| Astro.csp?.insertScriptHash({ hash: "sha512-scriptHash", kind: "element" }); | ||
| --- | ||
| ``` | ||
|
|
||
| After the build, the `<meta>` element for this individual page will add your hash to the `script-src-elem` directive. Astro's automatically generated script hashes also move there, since `script-src-elem` takes precedence over `script-src` for `<script>` elements: | ||
|
|
||
| ```html | ||
| <meta | ||
| http-equiv="content-security-policy" | ||
| content=" | ||
| script-src 'self'; | ||
| script-src-elem 'self' 'sha256-somehash' 'sha512-scriptHash'; | ||
| style-src 'self' 'sha256-somehash'; | ||
| " | ||
| > | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.