features-exploits-headers.md 22.8 KB
Newer Older
茶陵後's avatar
茶陵後 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
# Security HTTP Response Headers

|   |This portion of the documentation discusses the general topic of Security HTTP Response Headers.<br/>Refer to the relevant sections for specific information on Security HTTP Response Headers [servlet](../../servlet/exploits/headers.html#servlet-headers) and [WebFlux](../../reactive/exploits/headers.html#webflux-headers) based applications.|
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

There are many [HTTP response headers](https://owasp.org/www-project-secure-headers/#div-headers) that can be used to increase the security of web applications.
This section is dedicated to the various HTTP response headers that Spring Security provides explicit support for.
If necessary, Spring Security can also be configured to provide [custom headers](#headers-custom).

## Default Security Headers

|   |Refer to the relevant sections to see how to customize the defaults for both [servlet](../../servlet/exploits/headers.html#servlet-headers-default) and [webflux](../../reactive/exploits/headers.html#webflux-headers-default) based applications.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

Spring Security provides a default set of security related HTTP response headers to provide secure defaults.

The default for Spring Security is to include the following headers:

Example 1. Default Security HTTP Response Headers

```
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Content-Type-Options: nosniff
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
```

|   |Strict-Transport-Security is only added on HTTPS requests|
|---|---------------------------------------------------------|

If the defaults do not meet your needs, you can easily remove, modify, or add headers from these defaults.
For additional details on each of these headers, refer to the corresponding sections:

* [Cache Control](#headers-cache-control)

* [Content Type Options](#headers-content-type-options)

* [HTTP Strict Transport Security](#headers-hsts)

* [X-Frame-Options](#headers-frame-options)

* [X-XSS-Protection](#headers-xss-protection)

## Cache Control

|   |Refer to the relevant sections to see how to customize the defaults for both [servlet](../../servlet/exploits/headers.html#servlet-headers-cache-control) and [webflux](../../reactive/exploits/headers.html#webflux-headers-cache-control) based applications.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

Spring Security’s default is to disable caching to protect user’s content.

If a user authenticates to view sensitive information and then logs out, we don’t want a malicious user to be able to click the back button to view the sensitive information.
The cache control headers that are sent by default are:

Example 2. Default Cache Control HTTP Response Headers

```
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
```

In order to be secure by default, Spring Security adds these headers by default.
However, if your application provides its own cache control headers Spring Security will back out of the way.
This allows for applications to ensure that static resources like CSS and JavaScript can be cached.

## Content Type Options

|   |Refer to the relevant sections to see how to customize the defaults for both [servlet](../../servlet/exploits/headers.html#servlet-headers-content-type-options) and [webflux](../../reactive/exploits/headers.html#webflux-headers-content-type-options) based applications.|
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

Historically browsers, including Internet Explorer, would try to guess the content type of a request using [content sniffing](https://en.wikipedia.org/wiki/Content_sniffing).
This allowed browsers to improve the user experience by guessing the content type on resources that had not specified the content type.
For example, if a browser encountered a JavaScript file that did not have the content type specified, it would be able to guess the content type and then run it.

|   |There are many additional things one should do (i.e. only display the document in a distinct domain, ensure Content-Type header is set, sanitize the document, etc) when allowing content to be uploaded.<br/>However, these measures are out of the scope of what Spring Security provides.<br/>It is also important to point out when disabling content sniffing, you must specify the content type in order for things to work properly.|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

The problem with content sniffing is that this allowed malicious users to use polyglots (i.e. a file that is valid as multiple content types) to perform XSS attacks.
For example, some sites may allow users to submit a valid postscript document to a website and view it.
A malicious user might create a [postscript document that is also a valid JavaScript file](http://webblaze.cs.berkeley.edu/papers/barth-caballero-song.pdf) and perform a XSS attack with it.

Spring Security disables content sniffing by default by adding the following header to HTTP responses:

Example 3. nosniff HTTP Response Header

```
X-Content-Type-Options: nosniff
```

## 

|   |Refer to the relevant sections to see how to customize the defaults for both [servlet](../../servlet/exploits/headers.html#servlet-headers-hsts) and [webflux](../../reactive/exploits/headers.html#webflux-headers-hsts) based applications.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

When you type in your bank’s website, do you enter mybank.example.com or do you enter [https://mybank.example.com](https://mybank.example.com)?
If you omit the https protocol, you are potentially vulnerable to [Man in the Middle attacks](https://en.wikipedia.org/wiki/Man-in-the-middle_attack).
Even if the website performs a redirect to [https://mybank.example.com](https://mybank.example.com) a malicious user could intercept the initial HTTP request and manipulate the response (e.g. redirect to [https://mibank.example.com](https://mibank.example.com) and steal their credentials).

Many users omit the https protocol and this is why [HTTP Strict Transport Security (HSTS)](https://tools.ietf.org/html/rfc6797) was created.
Once mybank.example.com is added as a [HSTS host](https://tools.ietf.org/html/rfc6797#section-5.1), a browser can know ahead of time that any request to mybank.example.com should be interpreted as [https://mybank.example.com](https://mybank.example.com).
This greatly reduces the possibility of a Man in the Middle attack occurring.

|   |In accordance with [RFC6797](https://tools.ietf.org/html/rfc6797#section-7.2), the HSTS header is only injected into HTTPS responses.<br/>In order for the browser to acknowledge the header, the browser must first trust the CA that signed the SSL certificate used to make the connection (not just the SSL certificate).|
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

One way for a site to be marked as a HSTS host is to have the host preloaded into the browser.
Another is to add the `Strict-Transport-Security` header to the response.
For example, Spring Security’s default behavior is to add the following header which instructs the browser to treat the domain as an HSTS host for a year (there are approximately 31536000 seconds in a year):

Example 4. Strict Transport Security HTTP Response Header

```
Strict-Transport-Security: max-age=31536000 ; includeSubDomains ; preload
```

The optional `includeSubDomains` directive instructs the browser that subdomains (e.g. secure.mybank.example.com) should also be treated as an HSTS domain.

The optional `preload` directive instructs the browser that domain should be preloaded in browser as HSTS domain.
For more details on HSTS preload please see [https://hstspreload.org](https://hstspreload.org).

## 

|   |In order to remain passive Spring Security still provides [support for HPKP in servlet environments](../../servlet/exploits/headers.html#servlet-headers-hpkp), but for the reasons listed above HPKP is no longer recommended by the security team.|
|---|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

[HTTP Public Key Pinning (HPKP)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Public_Key_Pinning) specifies to a web client which public key to use with certain web server to prevent Man in the Middle (MITM) attacks with forged certificates.
When used correctly, HPKP could add additional layers of protection against compromised certificates.
However, due to the complexity of HPKP many experts no longer recommend using it and [Chrome has even removed support](https://www.chromestatus.com/feature/5903385005916160) for it.

For additional details around why HPKP is no longer recommended read [Is HTTP Public Key Pinning Dead?](https://blog.qualys.com/ssllabs/2016/09/06/is-http-public-key-pinning-dead) and [I’m giving up on HPKP](https://scotthelme.co.uk/im-giving-up-on-hpkp/).

## X-Frame-Options

|   |Refer to the relevant sections to see how to customize the defaults for both [servlet](../../servlet/exploits/headers.html#servlet-headers-frame-options) and [webflux](../../reactive/exploits/headers.html#webflux-headers-frame-options) based applications.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

Allowing your website to be added to a frame can be a security issue.
For example, using clever CSS styling users could be tricked into clicking on something that they were not intending.
For example, a user that is logged into their bank might click a button that grants access to other users.
This sort of attack is known as [Clickjacking](https://en.wikipedia.org/wiki/Clickjacking).

|   |Another modern approach to dealing with clickjacking is to use [Content Security Policy (CSP)](#headers-csp).|
|---|-------------------------------------------------------------------------------------------------------------|

There are a number ways to mitigate clickjacking attacks.
For example, to protect legacy browsers from clickjacking attacks you can use [frame breaking code](https://www.owasp.org/index.php/Clickjacking_Defense_Cheat_Sheet#Best-for-now_Legacy_Browser_Frame_Breaking_Script).
While not perfect, the frame breaking code is the best you can do for the legacy browsers.

A more modern approach to address clickjacking is to use [X-Frame-Options](https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options) header.
By default Spring Security disables rendering pages within an iframe using with the following header:

```
X-Frame-Options: DENY
```

## X-XSS-Protection

|   |Refer to the relevant sections to see how to customize the defaults for both [servlet](../../servlet/exploits/headers.html#servlet-headers-xss-protection) and [webflux](../../reactive/exploits/headers.html#webflux-headers-xss-protection) based applications.|
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

Some browsers have built in support for filtering out [reflected XSS attacks](https://www.owasp.org/index.php/Testing_for_Reflected_Cross_site_scripting_(OWASP-DV-001)).
This is by no means foolproof, but does assist in XSS protection.

The filtering is typically enabled by default, so adding the header typically just ensures it is enabled and instructs the browser what to do when a XSS attack is detected.
For example, the filter might try to change the content in the least invasive way to still render everything.
At times, this type of replacement can become a [XSS vulnerability in itself](https://hackademix.net/2009/11/21/ies-xss-filter-creates-xss-vulnerabilities/).
Instead, it is best to block the content rather than attempt to fix it.
By default Spring Security blocks the content using the following header:

```
X-XSS-Protection: 1; mode=block
```

## 

|   |Refer to the relevant sections to see how to configure both [servlet](../../servlet/exploits/headers.html#servlet-headers-csp) and [webflux](../../reactive/exploits/headers.html#webflux-headers-csp) based applications.|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

[Content Security Policy (CSP)](https://www.w3.org/TR/CSP2/) is a mechanism that web applications can leverage to mitigate content injection vulnerabilities, such as cross-site scripting (XSS).
CSP is a declarative policy that provides a facility for web application authors to declare and ultimately inform the client (user-agent) about the sources from which the web application expects to load resources.

|   |Content Security Policy is not intended to solve all content injection vulnerabilities.<br/>Instead, CSP can be leveraged to help reduce the harm caused by content injection attacks.<br/>As a first line of defense, web application authors should validate their input and encode their output.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

A web application may employ the use of CSP by including one of the following HTTP headers in the response:

* `Content-Security-Policy`

* `Content-Security-Policy-Report-Only`

Each of these headers are used as a mechanism to deliver a security policy to the client.
A security policy contains a set of security policy directives, each responsible for declaring the restrictions for a particular resource representation.

For example, a web application can declare that it expects to load scripts from specific, trusted sources, by including the following header in the response:

Example 5. Content Security Policy Example

```
Content-Security-Policy: script-src https://trustedscripts.example.com
```

An attempt to load a script from another source other than what is declared in the `script-src` directive will be blocked by the user-agent.
Additionally, if the [report-uri](https://www.w3.org/TR/CSP2/#directive-report-uri) directive is declared in the security policy, then the violation will be reported by the user-agent to the declared URL.

For example, if a web application violates the declared security policy, the following response header will instruct the user-agent to send violation reports to the URL specified in the policy’s `report-uri` directive.

Example 6. Content Security Policy with report-uri

```
Content-Security-Policy: script-src https://trustedscripts.example.com; report-uri /csp-report-endpoint/
```

[Violation reports](https://www.w3.org/TR/CSP2/#violation-reports) are standard JSON structures that can be captured either by the web application’s own API or by a publicly hosted CSP violation reporting service, such as, [https://report-uri.com/](https://report-uri.com/).

The `Content-Security-Policy-Report-Only` header provides the capability for web application authors and administrators to monitor security policies, rather than enforce them.
This header is typically used when experimenting and/or developing security policies for a site.
When a policy is deemed effective, it can be enforced by using the `Content-Security-Policy` header field instead.

Given the following response header, the policy declares that scripts may be loaded from one of two possible sources.

Example 7. Content Security Policy Report Only

```
Content-Security-Policy-Report-Only: script-src 'self' https://trustedscripts.example.com; report-uri /csp-report-endpoint/
```

If the site violates this policy, by attempting to load a script from *evil.com*, the user-agent will send a violation report to the declared URL specified by the *report-uri* directive, but still allow the violating resource to load nevertheless.

Applying Content Security Policy to a web application is often a non-trivial undertaking.
The following resources may provide further assistance in developing effective security policies for your site.

[An Introduction to Content Security Policy](https://www.html5rocks.com/en/tutorials/security/content-security-policy/)

[CSP Guide - Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/Web/Security/CSP)

[W3C Candidate Recommendation](https://www.w3.org/TR/CSP2/)

## Referrer Policy

|   |Refer to the relevant sections to see how to configure both [servlet](../../servlet/exploits/headers.html#servlet-headers-referrer) and [webflux](../../reactive/exploits/headers.html#webflux-headers-referrer) based applications.|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

[Referrer Policy](https://www.w3.org/TR/referrer-policy) is a mechanism that web applications can leverage to manage the referrer field, which contains the last
page the user was on.

Spring Security’s approach is to use [Referrer Policy](https://www.w3.org/TR/referrer-policy/) header, which provides different [policies](https://www.w3.org/TR/referrer-policy/#referrer-policies):

Example 8. Referrer Policy Example

```
Referrer-Policy: same-origin
```

The Referrer-Policy response header instructs the browser to let the destination knows the source where the user was previously.

## Feature Policy

|   |Refer to the relevant sections to see how to configure both [servlet](../../servlet/exploits/headers.html#servlet-headers-feature) and [webflux](../../reactive/exploits/headers.html#webflux-headers-feature) based applications.|
|---|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

[Feature Policy](https://wicg.github.io/feature-policy/) is a mechanism that allows web developers to selectively enable, disable, and modify the behavior of certain APIs and web features in the browser.

Example 9. Feature Policy Example

```
Feature-Policy: geolocation 'self'
```

With Feature Policy, developers can opt-in to a set of "policies" for the browser to enforce on specific features used throughout your site.
These policies restrict what APIs the site can access or modify the browser’s default behavior for certain features.

## Permissions Policy

|   |Refer to the relevant sections to see how to configure both [servlet](../../servlet/exploits/headers.html#servlet-headers-permissions) and [webflux](../../reactive/exploits/headers.html#webflux-headers-permissions) based applications.|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

[Permissions Policy](https://w3c.github.io/webappsec-permissions-policy/) is a mechanism that allows web developers to selectively enable, disable, and modify the behavior of certain APIs and web features in the browser.

Example 10. Permissions Policy Example

```
Permissions-Policy: geolocation=(self)
```

With Permissions Policy, developers can opt-in to a set of "policies" for the browser to enforce on specific features used throughout your site.
These policies restrict what APIs the site can access or modify the browser’s default behavior for certain features.

## Clear Site Data

|   |Refer to the relevant sections to see how to configure both [servlet](../../servlet/exploits/headers.html#servlet-headers-clear-site-data) and [webflux](../../reactive/exploits/headers.html#webflux-headers-clear-site-data) based applications.|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

[Clear Site Data](https://www.w3.org/TR/clear-site-data/) is a mechanism by which any browser-side data - cookies, local storage, and the like - can be removed when an HTTP response contains this header:

```
Clear-Site-Data: "cache", "cookies", "storage", "executionContexts"
```

This is a nice clean-up action to perform on logout.

## Custom Headers

|   |Refer to the relevant section to see how to configure [servlet](../../servlet/exploits/headers.html#servlet-headers-custom) based applications.|
|---|-----------------------------------------------------------------------------------------------------------------------------------------------|

Spring Security has mechanisms to make it convenient to add the more common security headers to your application.
However, it also provides hooks to enable adding custom headers.