Skip to content

Commit 5a80e8e

Browse files
authored
Update Blazor CSP Guidance
Reflects changes in dotnet/aspnetcore#36771 / dotnet/aspnetcore#34428. Created dotnet/aspnetcore#36805 to track removal of the inline script which requires the `sha256-v8v3RKRPmN4odZ1CWM5gw80QKPCCWMcpNeOmimNL2AA=` hash.
1 parent ea286af commit 5a80e8e

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

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

+8-12
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ Minimally, specify the following directives and sources for Blazor apps. Add add
4141
* Specify `self` to indicate that the app's origin, including the scheme and port number, is a valid source.
4242
* In a Blazor WebAssembly app:
4343
* Specify hashes to permit required scripts to load.
44-
* Specify `unsafe-eval` to use `eval()` and methods for creating code from strings.
44+
* Specify `unsafe-eval` to permit the Blazor WebAssembly mono runtime to function.
4545
* In a Blazor Server app, specify hashes to permit required scripts to load.
4646
* [style-src](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/style-src): Indicates valid sources for stylesheets.
4747
* Specify the `https://stackpath.bootstrapcdn.com/` host source for Bootstrap stylesheets.
4848
* Specify `self` to indicate that the app's origin, including the scheme and port number, is a valid source.
49-
* Specify `unsafe-inline` to allow the use of inline styles. The inline declaration is required for the UI in Blazor Server apps for reconnecting the client and server after the initial request. In a future release, inline styling might be removed so that `unsafe-inline` is no longer required.
49+
* Specify `unsafe-inline` to allow the use of inline styles.
5050
* [upgrade-insecure-requests](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/upgrade-insecure-requests): Indicates that content URLs from insecure (HTTP) sources should be acquired securely over HTTPS.
5151

5252
The preceding directives are supported by all browsers except Microsoft Internet Explorer.
@@ -80,16 +80,15 @@ In the `<head>` content of the `wwwroot/index.html` host page, apply the directi
8080
default-src 'self';
8181
img-src data: https:;
8282
object-src 'none';
83-
script-src https://stackpath.bootstrapcdn.com/
84-
'self'
83+
script-src 'self'
8584
'sha256-v8v3RKRPmN4odZ1CWM5gw80QKPCCWMcpNeOmimNL2AA='
8685
'unsafe-eval';
87-
style-src https://stackpath.bootstrapcdn.com/
88-
'self'
89-
'unsafe-inline';
86+
style-src 'self';
9087
upgrade-insecure-requests;">
9188
```
9289

90+
Note `sha256-v8v3RKRPmN4odZ1CWM5gw80QKPCCWMcpNeOmimNL2AA=` represents the hash of an [inline](https://github.com/dotnet/aspnetcore/blob/57501251222b199597b9ac16888f362a69eb13c1/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts#L212) script which is used for Blazor WebAssembly. This may be removed in the future.
91+
9392
Add additional `script-src` and `style-src` hashes as required by the app. During development, use an online tool or browser developer tools to have the hashes calculated for you. For example, the following browser tools console error reports the hash for a required script not covered by the policy:
9493

9594
> Refused to execute inline script because it violates the following Content Security Policy directive: " ... ". Either the 'unsafe-inline' keyword, a hash ('sha256-v8v3RKRPmN4odZ1CWM5gw80QKPCCWMcpNeOmimNL2AA='), or a nonce ('nonce-...') is required to enable inline execution.
@@ -107,11 +106,8 @@ In the `<head>` content of the `Pages/_Layout.cshtml` host page, apply the direc
107106
default-src 'self';
108107
img-src data: https:;
109108
object-src 'none';
110-
script-src https://stackpath.bootstrapcdn.com/
111-
'self';
112-
style-src https://stackpath.bootstrapcdn.com/
113-
'self'
114-
'unsafe-inline';
109+
script-src 'self';
110+
style-src 'self';
115111
upgrade-insecure-requests;">
116112
```
117113

0 commit comments

Comments
 (0)