Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/content/docs/en/reference/experimental-flags/csp.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,47 @@ After the build, the `<meta>` element will instead apply your sources to the `st
</head>
```

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`.

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.

```js title="astro.config.mjs"
import { defineConfig } from 'astro/config';

export default defineConfig({
experimental: {
csp: {
styleDirective: {
resources: [
"'unsafe-inline'"
]
},
scriptDirective: {
resources: [
"'unsafe-inline'"
]
}
}
}
});
```

After the build, `style-src` and `script-src` directives will contain the `'unsafe-line'` resource:

```html
<head>
<meta
http-equiv="content-security-policy"
content="
script-src 'unsafe-line';
style-src 'unsafe-line';
"
>
</head>
```



#### `hashes`

<p>
Expand Down