Skip to content

Commit e2ee8cd

Browse files
authored
[11.0 P1] Update CSP guidance for inline JS removal (#36686)
1 parent b20b382 commit e2ee8cd

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

aspnetcore/blazor/security/content-security-policy.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ The following directives and sources are commonly used for Blazor apps. Add addi
5151
* Specify `self` to indicate that the app's origin, including the scheme and port number, is a valid source.
5252
* In a client-side Blazor app:
5353
* Specify [`wasm-unsafe-eval`](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_webassembly_execution) to permit the client-side Blazor Mono runtime to function.
54-
* Specify any additional hashes to permit your required *non-framework scripts* to load. For example, specify [`unsafe-hashes`](https://developer.mozilla.org/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/script-src#unsafe_hashes) with a hash of `sha256-qnHnQs7NjQNHHNYv/I9cW+I62HzDJjbnyS/OFzqlix0=` to permit the inline JavaScript for the navigation toggler in the `NavMenu` component.
54+
* Specify any additional hashes with the [`unsafe-hashes`](https://developer.mozilla.org/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/script-src#unsafe_hashes) source expression to permit your required *non-framework inline scripts* to load.
5555
* In a server-side Blazor app, specify hashes to permit required scripts to load.
5656
* [`style-src`](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/style-src): Indicates valid sources for stylesheets.
5757
* Specify `self` to indicate that the app's origin, including the scheme and port number, is a valid source.
@@ -191,7 +191,27 @@ For more information, see [CSP: frame-ancestors (MDN documentation)](https://dev
191191

192192
The following example is a starting point for further development. At the top of [`<head>` content](xref:blazor/project-structure#location-of-head-and-body-content), apply the directives described in the [*Policy directives*](#policy-directives) section, along with any other directives that your app specification requires.
193193

194-
:::moniker range=">= aspnetcore-8.0"
194+
:::moniker range=">= aspnetcore-11.0"
195+
196+
For Blazor Web Apps or Blazor Server apps:
197+
198+
```html
199+
<meta http-equiv="Content-Security-Policy" content="
200+
base-uri 'self';
201+
default-src 'self';
202+
img-src data: https:;
203+
object-src 'none';
204+
script-src 'self' 'wasm-unsafe-eval';
205+
style-src https:;
206+
connect-src 'self' http: ws: wss:;
207+
upgrade-insecure-requests;" />
208+
```
209+
210+
Blazor Web Apps have an `ImportMap` component in `<head>` content that renders an inline import map `<script>` tag. To modify the policy to permit the import map to load, see the [Resolving CSP violations with Subresource Integrity (SRI) or a cryptographic nonce](#resolving-csp-violations-with-subresource-integrity-sri-or-a-cryptographic-nonce) section.
211+
212+
:::moniker-end
213+
214+
:::moniker range=">= aspnetcore-8.0 < aspnetcore-11.0"
195215

196216
For Blazor Web Apps or Blazor Server apps:
197217

0 commit comments

Comments
 (0)