Koba is a Kitura middleware for setting HTTP security headers to help mitigate vulnerabilities and protect against attackers. It contains a strict default configuration and a policy builder for designing and overriding security header values.
Secure Headers
Security Headers are HTTP response headers that, when set, can enhance the security of your web application by enabling browser security policies.
Prevent cross-site injections Default Value:script-src 'self'; object-src 'self'(not included by default)
Expect-CT
Enforcement of Certificate Transparency
Default Value:max-age=0(not included by default)
Feature-Policy
Disable browser features and APIs Default Value:accelerometer 'none'; ambient-light-sensor 'none'; autoplay 'none'; camera 'none'; encrypted-media 'none'; fullscreen 'none'; geolocation 'none'; gyroscope 'none'; magnetometer 'none'; microphone 'none'; midi 'none'; payment 'none'; picture-in-picture 'none'; speaker 'none'; sync-xhr 'none'; usb 'none'; vr 'none'(not included by default)
Strict-Transport-Security (HSTS)
Ensure application communication is sent over HTTPS Default Value:max-age=86400; includeSubDomains
Referrer-Policy
Enable full referrer if same origin, remove path for cross origin and disable referrer in unsupported browsers Default Value:no-referrer, strict-origin-when-cross-origin
X-Content-Type-Options (XCTO)
Prevent MIME-sniffing Default Value:nosniff
X-Frame-Options (XFO)
Disable framing from different origins (clickjacking defense) Default Value:SAMEORIGIN
The **Strict-Transport-Security (HSTS)** header will tell the browser to *only* utilize secure HTTPS connections for the domain, and in the default configuration, including all subdomains. The HSTS header requires trusted certificates and users will unable to connect to the site if using self-signed or expired certificates. The browser will honor the HSTS header for the time directed in the max-age attribute *(default = 2 years)*, and setting the max-age to 0 will disable an already set HSTS header. Use `hsts: nil` in the KobaConfig to not include the HSTS header.
The **Content-Security-Policy (CSP)** header can break functionality and can (and should) be carefully constructed, use the `csp: CSP().default()` in the KobaConfig to enable default values.
You can toggle the setting of headers with default values by passing an object with Header().default() or nil to remove the header. You can override default values by passing Header().set("custom") for a custom header value or using the policy builder for the following options:
Koba
Koba is a Kitura middleware for setting HTTP security headers to help mitigate vulnerabilities and protect against attackers. It contains a strict default configuration and a policy builder for designing and overriding security header values.
Secure Headers
Security Headers are HTTP response headers that, when set, can enhance the security of your web application by enabling browser security policies.
You can assess the security of your HTTP response headers at securityheaders.com or at the Mozilla Observatory
Recommendations used by Koba and more information regarding security headers can be found at the OWASP Secure Headers Project and Mozilla Web Security
Cache-control
Prevent cacheable HTTPS response
Default Value:
no-cache, no-store, must-revalidate, max-age=0
Content-Security-Policy (CSP)
Prevent cross-site injections
Default Value:
script-src 'self'; object-src 'self'
(not included by default)Expect-CT
Enforcement of Certificate Transparency Default Value:
max-age=0
(not included by default)Feature-Policy
Disable browser features and APIs
Default Value:
accelerometer 'none'; ambient-light-sensor 'none'; autoplay 'none'; camera 'none'; encrypted-media 'none'; fullscreen 'none'; geolocation 'none'; gyroscope 'none'; magnetometer 'none'; microphone 'none'; midi 'none'; payment 'none'; picture-in-picture 'none'; speaker 'none'; sync-xhr 'none'; usb 'none'; vr 'none'
(not included by default)Strict-Transport-Security (HSTS)
Ensure application communication is sent over HTTPS
Default Value:
max-age=86400; includeSubDomains
Referrer-Policy
Enable full referrer if same origin, remove path for cross origin and disable referrer in unsupported browsers
Default Value:
no-referrer, strict-origin-when-cross-origin
X-Content-Type-Options (XCTO)
Prevent MIME-sniffing
Default Value:
nosniff
X-Frame-Options (XFO)
Disable framing from different origins (clickjacking defense)
Default Value:
SAMEORIGIN
X-XSS-Protection (XXP)
Enable browser cross-site scripting filters
Default Value:
1; mode=block
Important information
Usage
Add Koba to your Package.swift
Add to dependencies
Add to target dependencies
Import package
Default configuration:
Default HTTP response headers:
Options
You can toggle the setting of headers with default values by passing an object with
Header().default()
ornil
to remove the header. You can override default values by passingHeader().set("custom")
for a custom header value or using the policy builder for the following options:Example:
Policy Builder
Helpers
Koba.Source
data:
mediastream:
https:
blob:
filesystem:
'none'
'self'
'src'
'strict-dynamic'
'unsafe-eval'
'unsafe-inline'
*
Koba.Time
300
86400
604800
2592000
31536000
63072000
Example
CacheControl()
Directives: private(), public(), immutable(), maxAge(seconds), maxStale(seconds), minFresh(seconds), mustRevalidate(), noCache(), noStore(), noTransform(), onlyIfCached(), proxyRevalidate(), sMaxage(seconds), staleIfError(seconds), staleWhileRevalidate(seconds)
Example:
Resources: Cache-Control | MDN
CSP()
Directives: baseUri(sources), blockAllMixedContent(), connectSrc(sources), defaultSrc(sources), fontSrc(sources), formAction(sources), frameAncestors(sources), frameSrc(sources), imgSrc(sources), manifestSrc(sources), mediaSrc(sources), objectSrc(sources), pluginTypes(types), reportTo(ReportTo), reportUri(uri), requireSriFor(values), sandbox(values), scriptSrc(sources), styleSrc(sources), upgradeInsecureRequests(), workerSrc(sources)
You can check the effectiveness of your CSP Policy at the CSP Evaluator
Example:
Reporting
Content-Security-Policy-Report-Only
Using
reportOnly()
will change the header toContent-Security-Policy-Report-Only
Example:
report-to
report-uri
Resources: CSP Cheat Sheet | Scott Helme , Content-Security-Policy | MDN , Content Security Policy Cheat Sheet | OWASP , Content Security Policy CSP Reference & Examples, Reporting: The Reporting API | Google Developers, CSP: report-uri | MDN, report-to - HTTP | MDN
ExpectCT()
Directives: maxAge(seconds), enforce(), reportUri(uri)
Example:
Resources: Expect-CT | MDN
FeaturePolicy()
Directives: accelerometer(allowlist), ambientLightSensor(allowlist), autoplay(allowlist), camera(allowlist), documentDomain(allowlist), encryptedMedia(allowlist), fullscreen(allowlist), geolocation(allowlist), gyroscope(allowlist), magnetometer(allowlist), microphone(allowlist), midi(allowlist), payment(allowlist), pictureInPicture(allowlist), speaker(allowlist), syncXhr(allowlist), usb(allowlist), vibrate(allowlist), vr(allowlist)
Example:
Resources: A new security header: Feature Policy | Scott Helme , Feature-Policy | MDN , Introduction to Feature Policy | Google Developers
HSTS()
Directives: includeSubdomains(), maxAge(seconds), preload()
Example:
Resources: Strict-Transport-Security | MDN , HTTP Strict Transport Security Cheat Sheet | OWASP
ReferrerPolicy()
Directives:, noReferrer(), noReferrerWhenDowngrade(), origin(), originWhenCrossOrigin(), sameOrigin(), strictOrigin(), strictOriginWhenCrossOrigin(), unsafeUrl()
Resources: A new security header: Referrer Policy | Scott Helme , Referrer-Policy | MDN
XCTO()
Examples:
Resources: X-Content-Type-Options - HTTP | MDN
XFO()
Directives: allowFrom(), deny(), sameorigin()
Examples:
Resources: X-Frame-Options | MDN
XXP()
Directives: disabled(), enabled(), enabledBlock(), enabledReport(uri)
Examples:
Resources: X-XSS-Protection | MDN
Contributing
Send a pull request, create an issue or discuss with me (@cak) on the the Kitura Slack.
Miscellaneous
Kob’a (ko’-bah) is the Hebrew word for helmet, a nod to Helmet.js.
Looking for security headers for Vapor? Check out Vapor Security Headers.
Resources: