Skip to content

Commit 586dda4

Browse files
authored
Merge pull request #6426 from erikjanwestendorp/csp
Add CSP health check article
2 parents 8c57713 + c76a178 commit 586dda4

File tree

8 files changed

+110
-2
lines changed

8 files changed

+110
-2
lines changed

14/umbraco-cms/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@
208208
* [Health Check](extending/health-check/README.md)
209209
* [Health Check Guides](extending/health-check/guides/README.md)
210210
* [Click-Jacking Protection](extending/health-check/guides/clickjackingprotection.md)
211+
* [Content Content Security Policy (CSP)](extending/health-check/guides/contentsecuritypolicy.md)
211212
* [Content/MIME Sniffing Protection](extending/health-check/guides/contentsniffingprotection.md)
212213
* [Cross-site scripting Protection (X-XSS-Protection header)](extending/health-check/guides/crosssitescriptingprotection.md)
213214
* [Debug Compilation Mode](extending/health-check/guides/debugcompilationmode.md)

14/umbraco-cms/extending/health-check/guides/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Below is a list of guides for Health Checks in Umbraco.
44

55
## [Click jack protection](clickjackingprotection.md)
66

7+
## [Content Security Policy](contentsecuritypolicy.md)
8+
79
## [Content sniffing protection](contentsniffingprotection.md)
810

911
## [Cross-site scripting protection](crosssitescriptingprotection.md)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Content Security Policy (CSP)
2+
3+
_This check verifies if your site has a Content Security Policy (CSP) header to defend against Cross-Site Scripting (XSS) and data injection attacks._
4+
5+
## How to fix this health check
6+
This health check can be fixed by adding a header before the response is started.
7+
8+
Preferable you use a security library like [NWebSec](https://docs.nwebsec.com/).
9+
10+
### Adding a Content Security Policy (CSP) using NWebSec
11+
12+
If you take a NuGet dependency on [NWebsec.AspNetCore.Middleware/](https://www.nuget.org/packages/NWebsec.AspNetCore.Middleware/), you can use third extension methods on `IApplicationBuilder`.
13+
14+
```csharp
15+
...
16+
WebApplication app = builder.Build();
17+
app.UseCsp(options => options
18+
.ImageSources(s => s
19+
.Self()
20+
.CustomSources(
21+
"our.umbraco.com data:",
22+
"dashboard.umbraco.com"))
23+
.DefaultSources(s => s
24+
.Self()
25+
.CustomSources(
26+
"our.umbraco.com",
27+
"marketplace.umbraco.com"))
28+
.ScriptSources(s => s
29+
.Self())
30+
.StyleSources(s => s
31+
.Self())
32+
.FontSources(s => s
33+
.Self())
34+
.ConnectSources(s => s
35+
.Self())
36+
.FrameSources(s => s
37+
.Self()));
38+
```
39+
40+
### Adding a Content Security Policy (CSP) using manual middleware
41+
42+
Avoid third-party library dependencies by using custom middleware added to the request pipeline as shown below.
43+
44+
```csharp
45+
app.Use(async (context, next) =>
46+
{
47+
context.Response.Headers.Append("Content-Security-Policy", "img-src 'self' our.umbraco.com data: dashboard.umbraco.com; default-src 'self' our.umbraco.com marketplace.umbraco.com; script-src 'self'; style-src 'unsafe-inline' 'self'; font-src 'self'; connect-src 'self'; frame-src 'self'; ");
48+
await next();
49+
});
50+
```

14/umbraco-cms/extending/health-check/guides/crosssitescriptingprotection.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Health check: Cross-site scripting Protection (X-XSS-Protection header)
22

33
{% hint style="warning" %}
4-
This header is non-standard and should not be used.
4+
This header is non-standard and should not be used. Instead, it is recommended to use a [Content Security Policy (CSP)](./contentsecuritypolicy.md) header.
55

66
For more information about the X-XSS-Protection header, and why it should not be used, see [MDN web docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection).
77
{% endhint %}

15/umbraco-cms/SUMMARY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
## Extending
4343

4444
* [Build on Umbraco functionality](extending/build-on-umbraco-functionality.md)
45+
* [Health Check](extending/health-check/README.md)
46+
* [Health Check Guides](extending/health-check/guides/README.md)
47+
* [Content Content Security Policy (CSP)](extending/health-check/guides/contentsecuritypolicy.md)
4548

4649
## Reference
4750

15/umbraco-cms/extending/health-check/guides/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Below is a list of guides for Health Checks in Umbraco.
44

55
## [Click jack protection](clickjackingprotection.md)
66

7+
## [Content Security Policy](contentsecuritypolicy.md)
8+
79
## [Content sniffing protection](contentsniffingprotection.md)
810

911
## [Cross-site scripting protection](crosssitescriptingprotection.md)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Content Security Policy (CSP)
2+
3+
_This check verifies if your site has a Content Security Policy (CSP) header to defend against Cross-Site Scripting (XSS) and data injection attacks._
4+
5+
## How to fix this health check
6+
This health check can be fixed by adding a header before the response is started.
7+
8+
Preferable you use a security library like [NWebSec](https://docs.nwebsec.com/).
9+
10+
### Adding a Content Security Policy (CSP) using NWebSec
11+
12+
If you take a NuGet dependency on [NWebsec.AspNetCore.Middleware/](https://www.nuget.org/packages/NWebsec.AspNetCore.Middleware/), you can use third extension methods on `IApplicationBuilder`.
13+
14+
```csharp
15+
...
16+
WebApplication app = builder.Build();
17+
app.UseCsp(options => options
18+
.ImageSources(s => s
19+
.Self()
20+
.CustomSources(
21+
"our.umbraco.com data:",
22+
"dashboard.umbraco.com"))
23+
.DefaultSources(s => s
24+
.Self()
25+
.CustomSources(
26+
"our.umbraco.com",
27+
"marketplace.umbraco.com"))
28+
.ScriptSources(s => s
29+
.Self())
30+
.StyleSources(s => s
31+
.Self())
32+
.FontSources(s => s
33+
.Self())
34+
.ConnectSources(s => s
35+
.Self())
36+
.FrameSources(s => s
37+
.Self()));
38+
```
39+
40+
### Adding a Content Security Policy (CSP) using manual middleware
41+
42+
Avoid third-party library dependencies by using custom middleware added to the request pipeline as shown below.
43+
44+
```csharp
45+
app.Use(async (context, next) =>
46+
{
47+
context.Response.Headers.Append("Content-Security-Policy", "img-src 'self' our.umbraco.com data: dashboard.umbraco.com; default-src 'self' our.umbraco.com marketplace.umbraco.com; script-src 'self'; style-src 'unsafe-inline' 'self'; font-src 'self'; connect-src 'self'; frame-src 'self'; ");
48+
await next();
49+
});
50+
```

15/umbraco-cms/extending/health-check/guides/crosssitescriptingprotection.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Health check: Cross-site scripting Protection (X-XSS-Protection header)
22

33
{% hint style="warning" %}
4-
This header is non-standard and should not be used.
4+
This header is non-standard and should not be used. Instead, it is recommended to use a [Content Security Policy (CSP)](./contentsecuritypolicy.md) header.
55

66
For more information about the X-XSS-Protection header, and why it should not be used, see [MDN web docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection).
77
{% endhint %}

0 commit comments

Comments
 (0)