servlet-exploits-csrf.md 18.5 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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
# Cross Site Request Forgery (CSRF) for Servlet Environments

This section discusses Spring Security’s [Cross Site Request Forgery (CSRF)](../../features/exploits/csrf.html#csrf) support for servlet environments.

## Using Spring Security CSRF Protection

The steps to using Spring Security’s CSRF protection are outlined below:

* [Use proper HTTP verbs](#servlet-csrf-idempotent)

* [Configure CSRF Protection](#servlet-csrf-configure)

* [Include the CSRF Token](#servlet-csrf-include)

### Use proper HTTP verbs

The first step to protecting against CSRF attacks is to ensure your website uses proper HTTP verbs.
This is covered in detail in [Safe Methods Must be Idempotent](../../features/exploits/csrf.html#csrf-protection-idempotent).

### Configure CSRF Protection

The next step is to configure Spring Security’s CSRF protection within your application.
Spring Security’s CSRF protection is enabled by default, but you may need to customize the configuration.
Below are a few common customizations.

#### Custom CsrfTokenRepository

By default Spring Security stores the expected CSRF token in the `HttpSession` using `HttpSessionCsrfTokenRepository`.
There can be cases where users will want to configure a custom `CsrfTokenRepository`.
For example, it might be desirable to persist the `CsrfToken` in a cookie to [support a JavaScript based application](#servlet-csrf-include-ajax-auto).

By default the `CookieCsrfTokenRepository` will write to a cookie named `XSRF-TOKEN` and read it from a header named `X-XSRF-TOKEN` or the HTTP parameter `_csrf`.
These defaults come from [AngularJS](https://docs.angularjs.org/api/ng/service/$http#cross-site-request-forgery-xsrf-protection)

You can configure `CookieCsrfTokenRepository` in XML using the following:

Example 1. Store CSRF Token in a Cookie with XML Configuration

```
<http>
	<!-- ... -->
	<csrf token-repository-ref="tokenRepository"/>
</http>
<b:bean id="tokenRepository"
	class="org.springframework.security.web.csrf.CookieCsrfTokenRepository"
	p:cookieHttpOnly="false"/>
```

|   |The sample explicitly sets `cookieHttpOnly=false`.<br/>This is necessary to allow JavaScript (i.e. AngularJS) to read it.<br/>If you do not need the ability to read the cookie with JavaScript directly, it is recommended to omit `cookieHttpOnly=false` to improve security.|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

You can configure `CookieCsrfTokenRepository` in Java Configuration using:

Example 2. Store CSRF Token in a Cookie

Java

```
@EnableWebSecurity
public class WebSecurityConfig extends
		WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) {
		http
			.csrf(csrf -> csrf
				.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
			);
	}
}
```

Kotlin

```
@EnableWebSecurity
class SecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
       http {
            csrf {
                csrfTokenRepository = CookieCsrfTokenRepository.withHttpOnlyFalse()
            }
        }
    }
}
```

|   |The sample explicitly sets `cookieHttpOnly=false`.<br/>This is necessary to allow JavaScript (i.e. AngularJS) to read it.<br/>If you do not need the ability to read the cookie with JavaScript directly, it is recommended to omit `cookieHttpOnly=false` (by using `new CookieCsrfTokenRepository()` instead) to improve security.|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

#### Disable CSRF Protection

CSRF protection is enabled by default.
However, it is simple to disable CSRF protection if it [makes sense for your application](../../features/exploits/csrf.html#csrf-when).

The XML configuration below will disable CSRF protection.

Example 3. Disable CSRF XML Configuration

```
<http>
	<!-- ... -->
	<csrf disabled="true"/>
</http>
```

The Java configuration below will disable CSRF protection.

Example 4. Disable CSRF

Java

```
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends
		WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) {
		http
			.csrf(csrf -> csrf.disable());
	}
}
```

Kotlin

```
@Configuration
@EnableWebSecurity
class SecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
       http {
            csrf {
                disable()
            }
        }
    }
}
```

### Include the CSRF Token

In order for the [synchronizer token pattern](../../features/exploits/csrf.html#csrf-protection-stp) to protect against CSRF attacks, we must include the actual CSRF token in the HTTP request.
This must be included in a part of the request (i.e. form parameter, HTTP header, etc) that is not automatically included in the HTTP request by the browser.

Spring Security’s [CsrfFilter](https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/csrf/CsrfFilter.html) exposes a [CsrfToken](https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/csrf/CsrfToken.html) as an `HttpServletRequest` attribute named `_csrf`.
This means that any view technology can access the `CsrfToken` to expose the expected token as either a [form](#servlet-csrf-include-form-attr) or [meta tag](#servlet-csrf-include-ajax-meta-attr).
Fortunately, there are integrations listed below that make including the token in [form](#servlet-csrf-include-form) and [ajax](#servlet-csrf-include-ajax) requests even easier.

#### Form URL Encoded

In order to post an HTML form the CSRF token must be included in the form as a hidden input.
For example, the rendered HTML might look like:

Example 5. CSRF Token HTML

```
<input type="hidden"
	name="_csrf"
	value="4bfd1575-3ad1-4d21-96c7-4ef2d9f86721"/>
```

Next we will discuss various ways of including the CSRF token in a form as a hidden input.

##### Automatic CSRF Token Inclusion

Spring Security’s CSRF support provides integration with Spring’s [RequestDataValueProcessor](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/support/RequestDataValueProcessor.html) via its [CsrfRequestDataValueProcessor](https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/servlet/support/csrf/CsrfRequestDataValueProcessor.html).
This means that if you leverage [Spring’s form tag library](https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-view-jsp-formtaglib), [Thymeleaf](https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#integration-with-requestdatavalueprocessor), or any other view technology that integrates with `RequestDataValueProcessor`, then forms that have an unsafe HTTP method (i.e. post) will automatically include the actual CSRF token.

##### csrfInput Tag

If you are using JSPs, then you can use [Spring’s form tag library](https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-view-jsp-formtaglib).
However, if that is not an option, you can also easily include the token with the [csrfInput](../integrations/jsp-taglibs.html#taglibs-csrfinput) tag.

##### CsrfToken Request Attribute

If the [other options](#servlet-csrf-include) for including the actual CSRF token in the request do not work, you can take advantage of the fact that the `CsrfToken` [is exposed](#servlet-csrf-include) as an `HttpServletRequest` attribute named `_csrf`.

An example of doing this with a JSP is shown below:

Example 6. CSRF Token in Form with Request Attribute

```
<c:url var="logoutUrl" value="/logout"/>
<form action="${logoutUrl}"
	method="post">
<input type="submit"
	value="Log out" />
<input type="hidden"
	name="${_csrf.parameterName}"
	value="${_csrf.token}"/>
</form>
```

#### Ajax and JSON Requests

If you are using JSON, then it is not possible to submit the CSRF token within an HTTP parameter.
Instead you can submit the token within a HTTP header.

In the following sections we will discuss various ways of including the CSRF token as an HTTP request header in JavaScript based applications.

##### Automatic Inclusion

Spring Security can easily be [configured](#servlet-csrf-configure-custom-repository) to store the expected CSRF token in a cookie.
By storing the expected CSRF in a cookie, JavaScript frameworks like [AngularJS](https://docs.angularjs.org/api/ng/service/$http#cross-site-request-forgery-xsrf-protection) will automatically include the actual CSRF token in the HTTP request headers.

##### Meta tags

An alternative pattern to [exposing the CSRF in a cookie](#servlet-csrf-include-form-auto) is to include the CSRF token within your `meta` tags.
The HTML might look something like this:

Example 7. CSRF meta tag HTML

```
<html>
<head>
	<meta name="_csrf" content="4bfd1575-3ad1-4d21-96c7-4ef2d9f86721"/>
	<meta name="_csrf_header" content="X-CSRF-TOKEN"/>
	<!-- ... -->
</head>
<!-- ... -->
```

Once the meta tags contained the CSRF token, the JavaScript code would read the meta tags and include the CSRF token as a header.
If you were using jQuery, this could be done with the following:

Example 8. AJAX send CSRF Token

```
$(function () {
	var token = $("meta[name='_csrf']").attr("content");
	var header = $("meta[name='_csrf_header']").attr("content");
	$(document).ajaxSend(function(e, xhr, options) {
		xhr.setRequestHeader(header, token);
	});
});
```

###### csrfMeta tag

If you are using JSPs a simple way to write the CSRF token to the `meta` tags is by leveraging the [csrfMeta](../integrations/jsp-taglibs.html#taglibs-csrfmeta) tag.

###### CsrfToken Request Attribute

If the [other options](#servlet-csrf-include) for including the actual CSRF token in the request do not work, you can take advantage of the fact that the `CsrfToken` [is exposed](#servlet-csrf-include) as an `HttpServletRequest` attribute named `_csrf`.
An example of doing this with a JSP is shown below:

Example 9. CSRF meta tag JSP

```
<html>
<head>
	<meta name="_csrf" content="${_csrf.token}"/>
	<!-- default header name is X-CSRF-TOKEN -->
	<meta name="_csrf_header" content="${_csrf.headerName}"/>
	<!-- ... -->
</head>
<!-- ... -->
```

## CSRF Considerations

There are a few special considerations to consider when implementing protection against CSRF attacks.
This section discusses those considerations as it pertains to servlet environments.
Refer to [CSRF Considerations](../../features/exploits/csrf.html#csrf-considerations) for a more general discussion.

### Logging In

It is important to [require CSRF for log in](../../features/exploits/csrf.html#csrf-considerations-login) requests to protect against forging log in attempts.
Spring Security’s servlet support does this out of the box.

### Logging Out

It is important to [require CSRF for log out](../../features/exploits/csrf.html#csrf-considerations-logout) requests to protect against forging log out attempts.
If CSRF protection is enabled (default), Spring Security’s `LogoutFilter` to only process HTTP POST.
This ensures that log out requires a CSRF token and that a malicious user cannot forcibly log out your users.

The easiest approach is to use a form to log out.
If you really want a link, you can use JavaScript to have the link perform a POST (i.e. maybe on a hidden form).
For browsers with JavaScript that is disabled, you can optionally have the link take the user to a log out confirmation page that will perform the POST.

If you really want to use HTTP GET with logout you can do so, but remember this is generally not recommended.
For example, the following Java Configuration will perform logout with the URL `/logout` is requested with any HTTP method:

Example 10. Log out with HTTP GET

Java

```
@EnableWebSecurity
public class WebSecurityConfig extends
		WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) {
		http
			.logout(logout -> logout
				.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
			);
	}
}
```

Kotlin

```
@EnableWebSecurity
class SecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
       http {
            logout {
                logoutRequestMatcher = AntPathRequestMatcher("/logout")
            }
        }
    }
}
```

### CSRF and Session Timeouts

By default Spring Security stores the CSRF token in the `HttpSession`.
This can lead to a situation where the session expires which means there is not an expected CSRF token to validate against.

We’ve already discussed [general solutions](../../features/exploits/csrf.html#csrf-considerations-login) to session timeouts.
This section discusses the specifics of CSRF timeouts as it pertains to the servlet support.

It is simple to change storage of the expected CSRF token to be in a cookie.
For details, refer to the [Custom CsrfTokenRepository](#servlet-csrf-configure-custom-repository) section.

If a token does expire, you might want to customize how it is handled by specifying a custom `AccessDeniedHandler`.
The custom `AccessDeniedHandler` can process the `InvalidCsrfTokenException` any way you like.
For an example of how to customize the `AccessDeniedHandler` refer to the provided links for both [xml](../appendix/namespace/http.html#nsa-access-denied-handler) and [Java configuration](https://github.com/spring-projects/spring-security/tree/5.6.2/config/src/test/java/org/springframework/security/config/annotation/web/configurers/NamespaceHttpServerAccessDeniedHandlerTests.java#L64).

### 

We have [already discussed](../../features/exploits/csrf.html#csrf-considerations-multipart) how protecting multipart requests (file uploads) from CSRF attacks causes a [chicken and the egg](https://en.wikipedia.org/wiki/Chicken_or_the_egg) problem.
This section discusses how to implement placing the CSRF token in the [body](#servlet-csrf-considerations-multipart-body) and [url](#servlet-csrf-considerations-multipart-url) within a servlet application.

|   |More information about using multipart forms with Spring can be found within the [1.1.11. Multipart Resolver](https://docs.spring.io/spring/docs/5.2.x/spring-framework-reference/web.html#mvc-multipart) section of the Spring reference and the [MultipartFilter javadoc](https://docs.spring.io/spring/docs/5.2.x/javadoc-api/org/springframework/web/multipart/support/MultipartFilter.html).|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

#### Place CSRF Token in the Body

We have [already discussed](../../features/exploits/csrf.html#csrf-considerations-multipart-body) the tradeoffs of placing the CSRF token in the body.
In this section we will discuss how to configure Spring Security to read the CSRF from the body.

In order to read the CSRF token from the body, the `MultipartFilter` is specified before the Spring Security filter.
Specifying the `MultipartFilter` before the Spring Security filter means that there is no authorization for invoking the `MultipartFilter` which means anyone can place temporary files on your server.
However, only authorized users will be able to submit a File that is processed by your application.
In general, this is the recommended approach because the temporary file upload should have a negligible impact on most servers.

To ensure `MultipartFilter` is specified before the Spring Security filter with java configuration, users can override beforeSpringSecurityFilterChain as shown below:

Example 11. Initializer MultipartFilter

Java

```
public class SecurityApplicationInitializer extends AbstractSecurityWebApplicationInitializer {

	@Override
	protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
		insertFilters(servletContext, new MultipartFilter());
	}
}
```

Kotlin

```
class SecurityApplicationInitializer : AbstractSecurityWebApplicationInitializer() {
    override fun beforeSpringSecurityFilterChain(servletContext: ServletContext?) {
        insertFilters(servletContext, MultipartFilter())
    }
}
```

To ensure `MultipartFilter` is specified before the Spring Security filter with XML configuration, users can ensure the \<filter-mapping\> element of the `MultipartFilter` is placed before the springSecurityFilterChain within the web.xml as shown below:

Example 12. web.xml - MultipartFilter

```
<filter>
	<filter-name>MultipartFilter</filter-name>
	<filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>
<filter>
	<filter-name>springSecurityFilterChain</filter-name>
	<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
	<filter-name>MultipartFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
	<filter-name>springSecurityFilterChain</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>
```

#### Include CSRF Token in URL

If allowing unauthorized users to upload temporary files is not acceptable, an alternative is to place the `MultipartFilter` after the Spring Security filter and include the CSRF as a query parameter in the action attribute of the form.
Since the `CsrfToken` is exposed as an `HttpServletRequest` [request attribute](#servlet-csrf-include), we can use that to create an `action` with the CSRF token in it.
An example with a jsp is shown below

Example 13. CSRF Token in Action

```
<form method="post"
	action="./upload?${_csrf.parameterName}=${_csrf.token}"
	enctype="multipart/form-data">
```

### HiddenHttpMethodFilter

We have [already discussed](../../features/exploits/csrf.html#csrf-considerations-multipart-body) the trade-offs of placing the CSRF token in the body.

In Spring’s Servlet support, overriding the HTTP method is done using [HiddenHttpMethodFilter](https://docs.spring.io/spring-framework/docs/5.2.x/javadoc-api/org/springframework/web/filter/reactive/HiddenHttpMethodFilter.html).
More information can be found in [HTTP Method Conversion](https://docs.spring.io/spring/docs/5.2.x/spring-framework-reference/web.html#mvc-rest-method-conversion) section of the reference documentation.

[Protection Against Exploits](index.html)[Security HTTP Response Headers](headers.html)