Skip to content

Commit

Permalink
Merge pull request #30 from grahamburgsma/allow-no-xss
Browse files Browse the repository at this point in the history
Allow setting no XSS header
  • Loading branch information
0xTim authored Nov 3, 2024
2 parents e1369d2 + eaa50ed commit 0a25315
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
8 changes: 6 additions & 2 deletions Sources/VaporSecurityHeaders/SecurityHeaders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ public struct SecurityHeaders {
init(contentTypeConfiguration: ContentTypeOptionsConfiguration = ContentTypeOptionsConfiguration(option: .nosniff),
contentSecurityPolicyConfiguration: ContentSecurityPolicyConfiguration = ContentSecurityPolicyConfiguration(value: ContentSecurityPolicy().defaultSrc(sources: CSPKeywords.`self`)),
frameOptionsConfiguration: FrameOptionsConfiguration = FrameOptionsConfiguration(option: .deny),
xssProtectionConfiguration: XSSProtectionConfiguration = XSSProtectionConfiguration(),
xssProtectionConfiguration: XSSProtectionConfiguration? = XSSProtectionConfiguration(),
hstsConfiguration: StrictTransportSecurityConfiguration? = nil,
serverConfiguration: ServerConfiguration? = nil,
contentSecurityPolicyReportOnlyConfiguration: ContentSecurityPolicyReportOnlyConfiguration? = nil,
referrerPolicyConfiguration: ReferrerPolicyConfiguration? = nil) {
configurations = [contentTypeConfiguration, contentSecurityPolicyConfiguration, frameOptionsConfiguration, xssProtectionConfiguration]
configurations = [contentTypeConfiguration, contentSecurityPolicyConfiguration, frameOptionsConfiguration]

if let xssProtectionConfiguration {
configurations.append(xssProtectionConfiguration)
}

if let hstsConfiguration = hstsConfiguration {
configurations.append(hstsConfiguration)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/VaporSecurityHeaders/SecurityHeadersFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class SecurityHeadersFactory {
var contentTypeOptions = ContentTypeOptionsConfiguration(option: .nosniff)
var contentSecurityPolicy = ContentSecurityPolicyConfiguration(value: ContentSecurityPolicy().defaultSrc(sources: CSPKeywords.`self`))
var frameOptions = FrameOptionsConfiguration(option: .deny)
var xssProtection = XSSProtectionConfiguration()
var xssProtection: XSSProtectionConfiguration? = XSSProtectionConfiguration()
var hsts: StrictTransportSecurityConfiguration?
var server: ServerConfiguration?
var referrerPolicy: ReferrerPolicyConfiguration?
Expand Down Expand Up @@ -33,7 +33,7 @@ public class SecurityHeadersFactory {
return self
}

@discardableResult public func with(XSSProtection configuration: XSSProtectionConfiguration) -> SecurityHeadersFactory {
@discardableResult public func with(XSSProtection configuration: XSSProtectionConfiguration?) -> SecurityHeadersFactory {
xssProtection = configuration
return self
}
Expand Down
7 changes: 7 additions & 0 deletions Tests/VaporSecurityHeadersTests/HeaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ class HeaderTests: XCTestCase {

XCTAssertEqual("0", response.headers[.xssProtection].first)
}

func testHeaderWithXssProtectionDisabled() throws {
let factory = SecurityHeadersFactory().with(XSSProtection: nil)
let response = try makeTestResponse(for: request, securityHeadersToAdd: factory)

XCTAssertNil(response.headers[.xssProtection].first)
}

func testHeaderWithHSTSwithMaxAge() throws {
let hstsConfig = StrictTransportSecurityConfiguration(maxAge: 30)
Expand Down

0 comments on commit 0a25315

Please sign in to comment.