--- title: Content Security Policy description: Prevent unwanted content from being injected in your app. links: - label: Enabled icon: i-heroicons-check-badge color: green size: xs --- Content Security Policy (CSP) helps prevent unwanted content from being injected/loaded into your webpages. This can mitigate cross-site scripting (XSS) vulnerabilities, clickjacking, formjacking, malicious frames, unwanted trackers, and other web client-side attacks. ::callout{icon="i-heroicons-light-bulb"} Read more about this header [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP). :: ## Usage This header is enabled by default but you can change its behavior like following. ```ts export default defineNuxtConfig({ // Global security: { headers: { contentSecurityPolicy: , }, }, // Per route routeRules: { '/custom-route': { security: { headers: { contentSecurityPolicy: , }, }, } } }) ``` You can also disable this header by `contentSecurityPolicy: false`. ## Default value By default, Nuxt Security will set the following value for this header: ```http Content-Security-Policy: base-uri 'none'; font-src 'self' https: data:; form-action 'self'; img-src 'self' data:; object-src 'none'; script-src-attr 'none'; style-src 'self' https: 'unsafe-inline'; script-src 'self' https: 'unsafe-inline' 'strict-dynamic' 'nonce-{{nonce}}'; upgrade-insecure-requests; ``` ## Available values The `contentSecurityPolicy` header can be configured with following values. ```ts contentSecurityPolicy: { 'child-src'?: CSPSourceValue[] false; 'connect-src'?: CSPSourceValue[] | false; 'default-src'?: CSPSourceValue[] | false; 'font-src'?: CSPSourceValue[] | false; 'frame-src'?: CSPSourceValue[] | false; 'img-src'?: CSPSourceValue[] | false; 'manifest-src'?: CSPSourceValue[] | false; 'media-src'?: CSPSourceValue[] | false; 'object-src'?: CSPSourceValue[] | false; 'prefetch-src'?: CSPSourceValue[] | false; 'script-src'?: CSPSourceValue[] | false; 'script-src-elem'?: CSPSourceValue[] | false; 'script-src-attr'?: CSPSourceValue[] | false; 'style-src'?: CSPSourceValue[] | false; 'style-src-elem'?: CSPSourceValue[] | false; 'style-src-attr'?: CSPSourceValue[] | false; 'worker-src'?: CSPSourceValue[] | false; 'base-uri'?: CSPSourceValue[] | false; 'sandbox'?: CSPSandboxValue[] | false; 'form-action'?: CSPSourceValue[] | false; 'frame-ancestors'?: ("'self'" | "'none'" | string)[] | false; 'report-uri'?: string[] | false; 'report-to'?: string | false; 'require-trusted-types-for'?: string | false; 'trusted-types'?: string[] | string | false; 'upgrade-insecure-requests'?: boolean; } | false ``` `CSPSourceValue` type ```ts type CSPSourceValue = | "'self'" | "'unsafe-eval'" | "'wasm-unsafe-eval'" | "'unsafe-hashes'" | "'unsafe-inline'" | "'none'" | "'strict-dynamic'" | "'report-sample'" | "'nonce='" | string; ``` `CSPSandboxValue` type ```ts type CSPSandboxValue = | 'allow-downloads' | 'allow-downloads-without-user-activation' | 'allow-forms' | 'allow-modals' | 'allow-orientation-lock' | 'allow-pointer-lock' | 'allow-popups' | 'allow-popups-to-escape-sandbox' | 'allow-presentation' | 'allow-same-origin' | 'allow-scripts' | 'allow-storage-access-by-user-activation' | 'allow-top-navigation' | 'allow-top-navigation-by-user-activation' | 'allow-top-navigation-to-custom-protocols'; ``` ## Report-Only Mode You can enable CSP in report-only mode to monitor violations without blocking any content. This is useful for testing CSP policies before enforcing them. ```ts export default defineNuxtConfig({ security: { contentSecurityPolicyReportOnly: true, headers: { contentSecurityPolicy: { 'script-src': ["'self'", "'strict-dynamic'"], 'report-uri': ['/api/csp-report'] } } } }) ``` When `contentSecurityPolicyReportOnly: true` is set, the `Content-Security-Policy-Report-Only` header will be used instead of `Content-Security-Policy`. ## Strict CSP Nuxt Security helps you increase the security of your site by enabling **Strict CSP** support for both SSR and SSG applications. For further reading about Strict CSP and how to handle specific cases, please consult our [Adanced Section about Strict CSP](/advanced/strict-csp) - For SSR applications, Nuxt Security implements strict CSP via nonces. A one-time cryptographically-generated random nonce is generated at runtime by the server for each request of a page. - For SSG applications, Nuxt Security implements strict CSP via hashes. At static build-time, Nuxt Security computes the SHA hashes of the elements that are allowed to execute on your site. By default, Strict CSP will be enabled on your site. The following default configuration options are used: ```ts export default defineNuxtConfig({ security: { nonce: true, // Enables HTML nonce support in SSR mode ssg: { meta: true, // Enables CSP as a meta tag in SSG mode hashScripts: true, // Enables CSP hash support for scripts in SSG mode hashStyles: false // Disables CSP hash support for styles in SSG mode (recommended) }, headers: { contentSecurityPolicy: { 'script-src': [ "'self'", // Fallback value, will be ignored by most modern browsers (level 3) "https:", // Fallback value, will be ignored by most modern browsers (level 3) "'unsafe-inline'", // Fallback value, will be ignored by almost any browser (level 2) "'strict-dynamic'", // Strict CSP via 'strict-dynamic', supported by most modern browsers (level 3) "'nonce-{{nonce}}'" // Enables CSP nonce support for scripts in SSR mode, supported by almost any browser (level 2) ], 'style-src': [ "'self'", // Enables loading of stylesheets hosted on same origin "https:", // For increased security, replace by the specific hosting domain or file name of your external stylesheets "'unsafe-inline'" // Recommended default for most Nuxt apps ], 'base-uri': ["'none'"], 'img-src': ["'self'", "data:"], // Add relevant https://... sources if you load images from external sources 'font-src': ["'self'", "https:", "data:"], // For increased security, replace by the specific sources for fonts 'object-src': ["'none'"], 'script-src-attr': ["'none'"], 'upgrade-insecure-requests': true } }, sri: true } }) ``` ## Server Side Rendering (SSR) Nuxt Security provides first-class support of SSR apps via 'strict-dynamic' and nonces. In SSR mode, Strict CSP is enabled when you set the `nonce` option and the `"'nonce-{{nonce}}'"` placeholders: ```ts export default defineNuxtConfig({ // Global security: { nonce: true, // Enables HTML nonce support in SSR mode headers: { contentSecurityPolicy: { 'script-src': [ "'strict-dynamic'", // Modify with your custom CSP sources "'nonce-{{nonce}}'" // Enables CSP nonce support for scripts in SSR mode, supported by almost any browser (level 2) ] } }, }, // Per route routeRules: { '/custom-route': { security: { nonce: false, headers: { contentSecurityPolicy: { 'script-src': "self 'unsafe-inline'" }, }, }, } } }) ``` - `nonce`: Set this option to `true` to parse all `