Skip to content

Commit e7d1b49

Browse files
csp: improve docs (#12812)
Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
1 parent 814a620 commit e7d1b49

File tree

1 file changed

+44
-1
lines changed
  • src/content/docs/en/reference/experimental-flags

1 file changed

+44
-1
lines changed

src/content/docs/en/reference/experimental-flags/csp.mdx

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ These properties are added to all pages and **completely override Astro's defaul
146146
<Since v="5.9.0" />
147147
</p>
148148

149-
A list of valid sources for the `script-src` and `style-src` directives.
149+
A list of valid sources for the `script-src` and `style-src` directives, including [values for subclasses](#adding-values-for-subclasses).
150150

151151
The `script-src` and `style-src` directives are handled by Astro by default, and use the `'self'` resource. This means that scripts and styles can only be downloaded by the current host (usually the current website).
152152

@@ -188,6 +188,49 @@ After the build, the `<meta>` element will instead apply your sources to the `st
188188
</head>
189189
```
190190

191+
### Adding values for subclasses
192+
193+
You can also use this field to add valid values that belong to [`script-src-attr`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/script-src-attr), [`script-src-elem`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/script-src-elem), [`style-src-attr`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/style-src-attr) and [`style-src-elem`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/style-src-elem), such as `unsafe-hashes` and `unsafe-inline`.
194+
195+
For example, if you have an external library that adds scripts or inline styles to some HTML elements of the page, you can add `unsafe-inline` to tell the browser that they are safe to render.
196+
197+
```js title="astro.config.mjs"
198+
import { defineConfig } from 'astro/config';
199+
200+
export default defineConfig({
201+
experimental: {
202+
csp: {
203+
styleDirective: {
204+
resources: [
205+
"'unsafe-inline'"
206+
]
207+
},
208+
scriptDirective: {
209+
resources: [
210+
"'unsafe-inline'"
211+
]
212+
}
213+
}
214+
}
215+
});
216+
```
217+
218+
After the build, `style-src` and `script-src` directives will contain the `'unsafe-line'` resource:
219+
220+
```html
221+
<head>
222+
<meta
223+
http-equiv="content-security-policy"
224+
content="
225+
script-src 'unsafe-line';
226+
style-src 'unsafe-line';
227+
"
228+
>
229+
</head>
230+
```
231+
232+
233+
191234
#### `hashes`
192235

193236
<p>

0 commit comments

Comments
 (0)