Skip to content

Commit 8ac6087

Browse files
committed
Update README for Vapor 3
1 parent 9868745 commit 8ac6087

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<br>
44
<br>
55
<a href="https://swift.org">
6-
<img src="http://img.shields.io/badge/Swift-4-brightgreen.svg" alt="Language">
6+
<img src="http://img.shields.io/badge/Swift-4.1-brightgreen.svg" alt="Language">
77
</a>
88
<a href="https://travis-ci.org/brokenhandsio/VaporSecurityHeaders">
99
<img src="https://travis-ci.org/brokenhandsio/VaporSecurityHeaders.svg?branch=master" alt="Build Status">
@@ -35,29 +35,21 @@ These headers will *help* prevent cross-site scripting attacks, SSL downgrade at
3535

3636
# Usage
3737

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:
3939

4040
```swift
41-
let config = Config()
41+
services.register(securityHeadersToAdd.build())
4242
let securityHeadersFactory = SecurityHeadersFactory()
43-
config.addConfigurable(middleware: securityHeadersFactory.builder(), name: "security-headers"))
44-
let drop = Droplet(config)
43+
44+
var middlewareConfig = MiddlewareConfig()
45+
middlewareConfig.use(SecurityHeaders.self)
46+
// ...
47+
services.register(middlewareConfig)
4548
```
4649

4750
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.
4851

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
6153

6254
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:
6355

@@ -72,7 +64,7 @@ You will need to add it as a dependency in your `Package.swift` file:
7264
```swift
7365
dependencies: [
7466
...,
75-
.package(url: "https://github.com/brokenhandsio/VaporSecurityHeaders.git", from: "1.1.0")
67+
.package(url: "https://github.com/brokenhandsio/VaporSecurityHeaders.git", from: "2.0.0")
7668
]
7769
```
7870

@@ -140,14 +132,22 @@ Check out [https://report-uri.io/](https://report-uri.io/) for a free tool to se
140132

141133
### Page Specific CSP
142134

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:
144136

145137
```swift
146138
let pageSpecificCSPVaue = "default-src 'none'; script-src https://comments.disqus.com;"
147139
let pageSpecificCSP = ContentSecurityPolicyConfiguration(value: pageSpecificCSPValue)
148140
request.contentSecurityPolicy = pageSpecificCSP
149141
```
150142

143+
You must also enable the `CSPRequestConfiguration` service for this to work. In `configure.swift` add:
144+
145+
```swift
146+
services.register { _ in
147+
return CSPRequestConfiguration()
148+
}
149+
```
150+
151151
## Content-Security-Policy-Report-Only
152152

153153
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:
175175
let xssProtectionConfig = XssProtectionConfiguration(option: .enable)
176176
```
177177

178-
To sanitize the page and report the violation:
178+
To sanitise the page and report the violation:
179179

180180
```swift
181181
let xssProtectionConfig = XssProtectionConfiguration(option: .report("https://report-uri.com"))

0 commit comments

Comments
 (0)