You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -35,29 +35,21 @@ These headers will *help* prevent cross-site scripting attacks, SSL downgrade at
35
35
36
36
# Usage
37
37
38
-
To use Vapor Security Headers, just add the middleware to your `Config` and then to your `droplet.json`. Vapor Security Headers makes this easy to do with a `builder` function on the factory:
38
+
To use Vapor Security Headers, just register the middleware with your services and add it to your `MiddlewareConfig`. Vapor Security Headers makes this easy to do with a `builder` function on the factory. In `configure.swift` add:
39
39
40
40
```swift
41
-
let config =Config()
41
+
services.register(securityHeadersToAdd.build())
42
42
let securityHeadersFactory =SecurityHeadersFactory()
The default factory will add default values to your site for Content-Security-Policy, X-XSS-Protection, X-Frame-Options and X-Content-Type-Options.
48
51
49
-
***Note:*** You should ensure you set the security headers as the first middleware in your `droplet.json` to make sure the headers get added to all responses:
50
-
51
-
```json
52
-
{
53
-
...
54
-
"middleware": [
55
-
"security-headers",
56
-
...
57
-
],
58
-
...
59
-
}
60
-
```
52
+
***Note:*** You should ensure you set the security headers as the first middleware in your `MiddlewareConfig` to make sure the headers get added to all responses
61
53
62
54
If you want to add your own values, it is easy to do using the factory. For instance, to add a content security policy configuration, just do:
63
55
@@ -72,7 +64,7 @@ You will need to add it as a dependency in your `Package.swift` file:
@@ -140,14 +132,22 @@ Check out [https://report-uri.io/](https://report-uri.io/) for a free tool to se
140
132
141
133
### Page Specific CSP
142
134
143
-
Vapor Security Headers also supports setting the CSP on a route or request basis. If the middleware has been added to the Droplet, you can override the CSP for a request. This allows you to have a strict default CSP, but allow content from extra sources when required, such as only allowing the Javascript for blog comments on the blog page. Create a separate `ContentSecurityPolicyConfiguration` and then add it to the request. For example, inside a route handler, you could do:
135
+
Vapor Security Headers also supports setting the CSP on a route or request basis. If the middleware has been added to the `MiddlewareConfig`, you can override the CSP for a request. This allows you to have a strict default CSP, but allow content from extra sources when required, such as only allowing the Javascript for blog comments on the blog page. Create a separate `ContentSecurityPolicyConfiguration` and then add it to the request. For example, inside a route handler, you could do:
144
136
145
137
```swift
146
138
let pageSpecificCSPVaue ="default-src 'none'; script-src https://comments.disqus.com;"
147
139
let pageSpecificCSP =ContentSecurityPolicyConfiguration(value: pageSpecificCSPValue)
148
140
request.contentSecurityPolicy= pageSpecificCSP
149
141
```
150
142
143
+
You must also enable the `CSPRequestConfiguration` service for this to work. In `configure.swift` add:
144
+
145
+
```swift
146
+
services.register { _in
147
+
returnCSPRequestConfiguration()
148
+
}
149
+
```
150
+
151
151
## Content-Security-Policy-Report-Only
152
152
153
153
Content-Security-Policy-Report-Only works in exactly the same way as Content-Security-Policy except that any violations will not block content, but they will be reported back to you. This is extremely useful for testing a CSP before rolling it out over your site. You can run both side by side - so for example have a fairly simply policy under Content-Security-Policy but test a more restrictive policy over Content-Security-Policy-Report-Only. The great thing about this is that your users do all your testing for you!
@@ -175,7 +175,7 @@ To just enable the protection:
175
175
let xssProtectionConfig =XssProtectionConfiguration(option: .enable)
176
176
```
177
177
178
-
To sanitize the page and report the violation:
178
+
To sanitise the page and report the violation:
179
179
180
180
```swift
181
181
let xssProtectionConfig =XssProtectionConfiguration(option: .report("https://report-uri.com"))
0 commit comments