Skip to content

Commit

Permalink
Merge pull request #11 from Charliekenney23/add-X-XSS-Protection-repo…
Browse files Browse the repository at this point in the history
…rt-header-config

add X-XSS-Protection report header config
  • Loading branch information
0xTim committed Mar 28, 2018
2 parents 46b921c + 042a0f7 commit 8cd8e2e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -175,6 +175,12 @@ To just enable the protection:
let xssProtectionConfig = XssProtectionConfiguration(option: .enable)
```

To sanitize the page and report the violation:

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

Or to disable:

```swift
Expand Down
Expand Up @@ -6,6 +6,7 @@ public struct XSSProtectionConfiguration: SecurityHeaderConfiguration {
case disable
case enable
case block
case report(uri: String)
}

private let option: Options
Expand All @@ -22,6 +23,8 @@ public struct XSSProtectionConfiguration: SecurityHeaderConfiguration {
response.headers[HeaderKey.xXssProtection] = "1"
case .block:
response.headers[HeaderKey.xXssProtection] = "1; mode=block"
case .report(let uri):
response.headers[HeaderKey.xXssProtection] = "1; report=\(uri)"
}
}
}
10 changes: 10 additions & 0 deletions Tests/VaporSecurityHeadersTests/HeaderTests.swift
Expand Up @@ -23,6 +23,7 @@ class HeaderTests: XCTestCase {
("testHeaderWithXssProtectionDisable", testHeaderWithXssProtectionDisable),
("testHeaderWithXssProtectionEnable", testHeaderWithXssProtectionEnable),
("testHeaderWithXssProtectionBlock", testHeaderWithXssProtectionBlock),
("testHeaderWithXssProtectionReport", testHeaderWithXssProtectionReport),
("testHeaderWithHSTSwithMaxAge", testHeaderWithHSTSwithMaxAge),
("testHeadersWithHSTSwithSubdomains", testHeadersWithHSTSwithSubdomains),
("testHeadersWithHSTSwithPreload", testHeadersWithHSTSwithPreload),
Expand Down Expand Up @@ -214,6 +215,15 @@ class HeaderTests: XCTestCase {
XCTAssertEqual("1; mode=block", response.headers[HeaderKey.xXssProtection])
}

func testHeaderWithXssProtectionReport() throws {
let xssProtectionConfig = XSSProtectionConfiguration(option: .report(uri: "https://test.com"))
let factory = SecurityHeadersFactory().with(XSSProtection: xssProtectionConfig)
let drop = try makeTestDroplet(securityHeadersToAdd: factory)
let response = try drop.respond(to: request)

XCTAssertEqual("1; report=https://test.com", response.headers[HeaderKey.xXssProtection])
}

func testHeaderWithHSTSwithMaxAge() throws {
let hstsConfig = StrictTransportSecurityConfiguration(maxAge: 30)
let factory = SecurityHeadersFactory().with(strictTransportSecurity: hstsConfig)
Expand Down

0 comments on commit 8cd8e2e

Please sign in to comment.