2
0

csrf.adoc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. [[servlet-csrf]]
  2. = Cross Site Request Forgery (CSRF) for Servlet Environments
  3. This section discusses Spring Security's xref:features/exploits/csrf.adoc#csrf[Cross Site Request Forgery (CSRF)] support for servlet environments.
  4. [[servlet-csrf-using]]
  5. == Using Spring Security CSRF Protection
  6. The steps to using Spring Security's CSRF protection are outlined below:
  7. * <<servlet-csrf-idempotent>>
  8. * <<servlet-csrf-configure>>
  9. * <<servlet-csrf-include>>
  10. [[servlet-csrf-idempotent]]
  11. === Use proper HTTP verbs
  12. The first step to protecting against CSRF attacks is to ensure that your website uses proper HTTP verbs.
  13. This is covered in detail in xref:features/exploits/csrf.adoc#csrf-protection-idempotent[Safe Methods Must be Idempotent].
  14. [[servlet-csrf-configure]]
  15. === Configure CSRF Protection
  16. The next step is to configure Spring Security's CSRF protection within your application.
  17. Spring Security's CSRF protection is enabled by default, but you may need to customize the configuration.
  18. The next few sections cover a few common customizations.
  19. [[servlet-csrf-configure-custom-repository]]
  20. ==== Custom CsrfTokenRepository
  21. By default, Spring Security stores the expected CSRF token in the `HttpSession` by using `HttpSessionCsrfTokenRepository`.
  22. There can be cases where users want to configure a custom `CsrfTokenRepository`.
  23. For example, it might be desirable to persist the `CsrfToken` in a cookie to <<servlet-csrf-include-ajax-auto,support a JavaScript-based application>>.
  24. By default, the `CookieCsrfTokenRepository` writes to a cookie named `XSRF-TOKEN` and reads it from a header named `X-XSRF-TOKEN` or the HTTP parameter `_csrf`.
  25. These defaults come from https://docs.angularjs.org/api/ng/service/$http#cross-site-request-forgery-xsrf-protection[AngularJS].
  26. You can configure `CookieCsrfTokenRepository` in XML byusing the following:
  27. .Store CSRF Token in a Cookie with XML Configuration
  28. ====
  29. [source,xml]
  30. ----
  31. <http>
  32. <!-- ... -->
  33. <csrf token-repository-ref="tokenRepository"/>
  34. </http>
  35. <b:bean id="tokenRepository"
  36. class="org.springframework.security.web.csrf.CookieCsrfTokenRepository"
  37. p:cookieHttpOnly="false"/>
  38. ----
  39. ====
  40. [NOTE]
  41. ====
  42. The sample explicitly sets `cookieHttpOnly=false`.
  43. This is necessary to allow JavaScript (such as AngularJS) to read it.
  44. If you do not need the ability to read the cookie with JavaScript directly, we recommend omitting `cookieHttpOnly=false` to improve security.
  45. ====
  46. You can configure `CookieCsrfTokenRepository` in Java or Kotlin configuration by using:
  47. .Store CSRF Token in a Cookie
  48. ====
  49. .Java
  50. [source,java,role="primary"]
  51. ----
  52. @Configuration
  53. @EnableWebSecurity
  54. public class WebSecurityConfig {
  55. @Bean
  56. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  57. http
  58. .csrf(csrf -> csrf
  59. .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
  60. );
  61. return http.build();
  62. }
  63. }
  64. ----
  65. .Kotlin
  66. [source,kotlin,role="secondary"]
  67. ----
  68. @Configuration
  69. @EnableWebSecurity
  70. class SecurityConfig {
  71. @Bean
  72. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  73. http {
  74. csrf {
  75. csrfTokenRepository = CookieCsrfTokenRepository.withHttpOnlyFalse()
  76. }
  77. }
  78. return http.build()
  79. }
  80. }
  81. ----
  82. ====
  83. [NOTE]
  84. ====
  85. The sample explicitly sets `cookieHttpOnly=false`.
  86. This is necessary to let JavaScript (such as AngularJS) read it.
  87. If you do not need the ability to read the cookie with JavaScript directly, we recommend omitting `cookieHttpOnly=false` (by using `new CookieCsrfTokenRepository()` instead) to improve security.
  88. ====
  89. [[servlet-csrf-configure-disable]]
  90. ==== Disable CSRF Protection
  91. By default, CSRF protection is enabled.
  92. However, you can disable CSRF protection if it xref:features/exploits/csrf.adoc#csrf-when[makes sense for your application].
  93. The following XML configuration disables CSRF protection:
  94. .Disable CSRF XML Configuration
  95. ====
  96. [source,xml]
  97. ----
  98. <http>
  99. <!-- ... -->
  100. <csrf disabled="true"/>
  101. </http>
  102. ----
  103. ====
  104. The following Java or Kotlin configuration disables CSRF protection:
  105. .Disable CSRF
  106. ====
  107. .Java
  108. [source,java,role="primary"]
  109. ----
  110. @Configuration
  111. @EnableWebSecurity
  112. public class WebSecurityConfig {
  113. @Bean
  114. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  115. http
  116. .csrf(csrf -> csrf.disable());
  117. return http.build();
  118. }
  119. }
  120. ----
  121. .Kotlin
  122. [source,kotlin,role="secondary"]
  123. ----
  124. @Configuration
  125. @EnableWebSecurity
  126. class SecurityConfig {
  127. @Bean
  128. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  129. http {
  130. csrf {
  131. disable()
  132. }
  133. }
  134. return http.build()
  135. }
  136. }
  137. ----
  138. ====
  139. [[servlet-csrf-include]]
  140. === Include the CSRF Token
  141. For the xref:features/exploits/csrf.adoc#csrf-protection-stp[synchronizer token pattern] to protect against CSRF attacks, we must include the actual CSRF token in the HTTP request.
  142. This must be included in a part of the request (a form parameter, an HTTP header, or other part) that is not automatically included in the HTTP request by the browser.
  143. Spring Security's https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/csrf/CsrfFilter.html[`CsrfFilter`] exposes a https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/csrf/CsrfToken.html[`CsrfToken`] as an `HttpServletRequest` attribute named `_csrf`.
  144. This means that any view technology can access the `CsrfToken` to expose the expected token as either a <<servlet-csrf-include-form-attr,form>> or <<servlet-csrf-include-ajax-meta-attr,meta tag>>.
  145. Fortunately, there are integrations listed later in this chapter that make including the token in <<servlet-csrf-include-form,form>> and <<servlet-csrf-include-ajax,ajax>> requests even easier.
  146. [[servlet-csrf-include-form]]
  147. ==== Form URL Encoded
  148. To post an HTML form, the CSRF token must be included in the form as a hidden input.
  149. For example, the rendered HTML might look like:
  150. .CSRF Token HTML
  151. ====
  152. [source,html]
  153. ----
  154. <input type="hidden"
  155. name="_csrf"
  156. value="4bfd1575-3ad1-4d21-96c7-4ef2d9f86721"/>
  157. ----
  158. ====
  159. Next, we discuss various ways of including the CSRF token in a form as a hidden input.
  160. [[servlet-csrf-include-form-auto]]
  161. ===== Automatic CSRF Token Inclusion
  162. Spring Security's CSRF support provides integration with Spring's https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/support/RequestDataValueProcessor.html[`RequestDataValueProcessor`] through its https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/servlet/support/csrf/CsrfRequestDataValueProcessor.html[`CsrfRequestDataValueProcessor`].
  163. This means that, if you use https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-view-jsp-formtaglib[Spring’s form tag library], https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#integration-with-requestdatavalueprocessor[Thymeleaf], or any other view technology that integrates with `RequestDataValueProcessor`, then forms that have an unsafe HTTP method (such as post) automatically include the actual CSRF token.
  164. [[servlet-csrf-include-form-tag]]
  165. ===== csrfInput Tag
  166. If you use JSPs, you can use https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-view-jsp-formtaglib[Spring’s form tag library].
  167. However, if that is not an option, you can also include the token with the xref:servlet/integrations/jsp-taglibs.adoc#taglibs-csrfinput[csrfInput] tag.
  168. [[servlet-csrf-include-form-attr]]
  169. ===== CsrfToken Request Attribute
  170. If the <<servlet-csrf-include,other options>> for including the actual CSRF token in the request do not work, you can take advantage of the fact that the `CsrfToken` <<servlet-csrf-include,is exposed>> as an `HttpServletRequest` attribute named `_csrf`.
  171. The following example does this with a JSP:
  172. .CSRF Token in Form with Request Attribute
  173. ====
  174. [source,xml]
  175. ----
  176. <c:url var="logoutUrl" value="/logout"/>
  177. <form action="${logoutUrl}"
  178. method="post">
  179. <input type="submit"
  180. value="Log out" />
  181. <input type="hidden"
  182. name="${_csrf.parameterName}"
  183. value="${_csrf.token}"/>
  184. </form>
  185. ----
  186. ====
  187. [[servlet-csrf-include-ajax]]
  188. ==== Ajax and JSON Requests
  189. If you use JSON, you cannot submit the CSRF token within an HTTP parameter.
  190. Instead, you can submit the token within a HTTP header.
  191. The following sections discuss various ways of including the CSRF token as an HTTP request header in JavaScript based applications.
  192. [[servlet-csrf-include-ajax-auto]]
  193. ===== Automatic Inclusion
  194. You can <<servlet-csrf-configure-custom-repository,configure>> Spring Security to store the expected CSRF token in a cookie.
  195. By storing the expected CSRF in a cookie, JavaScript frameworks, such as https://docs.angularjs.org/api/ng/service/$http#cross-site-request-forgery-xsrf-protection[AngularJS], automatically include the actual CSRF token in the HTTP request headers.
  196. [[servlet-csrf-include-ajax-meta]]
  197. ===== Meta Tags
  198. An alternative pattern to <<servlet-csrf-include-form-auto,exposing the CSRF in a cookie>> is to include the CSRF token within your `meta` tags.
  199. The HTML might look something like this:
  200. .CSRF meta tag HTML
  201. ====
  202. [source,html]
  203. ----
  204. <html>
  205. <head>
  206. <meta name="_csrf" content="4bfd1575-3ad1-4d21-96c7-4ef2d9f86721"/>
  207. <meta name="_csrf_header" content="X-CSRF-TOKEN"/>
  208. <!-- ... -->
  209. </head>
  210. <!-- ... -->
  211. ----
  212. ====
  213. Once the meta tags contain the CSRF token, the JavaScript code can read the meta tags and include the CSRF token as a header.
  214. If you use jQuery, you can do this with the following code:
  215. .AJAX send CSRF Token
  216. ====
  217. [source,javascript]
  218. ----
  219. $(function () {
  220. var token = $("meta[name='_csrf']").attr("content");
  221. var header = $("meta[name='_csrf_header']").attr("content");
  222. $(document).ajaxSend(function(e, xhr, options) {
  223. xhr.setRequestHeader(header, token);
  224. });
  225. });
  226. ----
  227. ====
  228. [[servlet-csrf-include-ajax-meta-tag]]
  229. ====== csrfMeta tag
  230. If you use JSPs, one way to write the CSRF token to the `meta` tags is by using the xref:servlet/integrations/jsp-taglibs.adoc#taglibs-csrfmeta[csrfMeta] tag.
  231. [[servlet-csrf-include-ajax-meta-attr]]
  232. ====== CsrfToken Request Attribute
  233. If the <<servlet-csrf-include,other options>> for including the actual CSRF token in the request do not work, you can take advantage of the fact that the `CsrfToken` <<servlet-csrf-include,is exposed>> as an `HttpServletRequest` attribute named `_csrf`.
  234. The following example does this with a JSP:
  235. .CSRF meta tag JSP
  236. ====
  237. [source,html]
  238. ----
  239. <html>
  240. <head>
  241. <meta name="_csrf" content="${_csrf.token}"/>
  242. <!-- default header name is X-CSRF-TOKEN -->
  243. <meta name="_csrf_header" content="${_csrf.headerName}"/>
  244. <!-- ... -->
  245. </head>
  246. <!-- ... -->
  247. ----
  248. ====
  249. [[servlet-csrf-considerations]]
  250. == CSRF Considerations
  251. There are a few special considerations to consider when implementing protection against CSRF attacks.
  252. This section discusses those considerations as they pertain to servlet environments.
  253. See xref:features/exploits/csrf.adoc#csrf-considerations[CSRF Considerations] for a more general discussion.
  254. [[servlet-considerations-csrf-login]]
  255. === Logging In
  256. It is important to xref:features/exploits/csrf.adoc#csrf-considerations-login[require CSRF for log in] requests to protect against forging log in attempts.
  257. Spring Security's servlet support does this out of the box.
  258. [[servlet-considerations-csrf-logout]]
  259. === Logging Out
  260. It is important to xref:features/exploits/csrf.adoc#csrf-considerations-logout[require CSRF for log out] requests to protect against forging logout attempts.
  261. If CSRF protection is enabled (the default), Spring Security's `LogoutFilter` to only process HTTP POST.
  262. This ensures that logging out requires a CSRF token and that a malicious user cannot forcibly log out your users.
  263. The easiest approach is to use a form to log out.
  264. If you really want a link, you can use JavaScript to have the link perform a POST (maybe on a hidden form).
  265. For browsers with JavaScript that is disabled, you can optionally have the link take the user to a log out confirmation page that performs the POST.
  266. If you really want to use HTTP GET with logout, you can do so. However, remember that this is generally not recommended.
  267. For example, the following Java Configuration logs out when the `/logout` URL is requested with any HTTP method:
  268. .Log out with any HTTP method
  269. ====
  270. .Java
  271. [source,java,role="primary"]
  272. ----
  273. @Configuration
  274. @EnableWebSecurity
  275. public class WebSecurityConfig {
  276. @Bean
  277. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  278. http
  279. .logout(logout -> logout
  280. .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
  281. );
  282. return http.build();
  283. }
  284. }
  285. ----
  286. .Kotlin
  287. [source,kotlin,role="secondary"]
  288. ----
  289. @Configuration
  290. @EnableWebSecurity
  291. class SecurityConfig {
  292. @Bean
  293. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  294. http {
  295. logout {
  296. logoutRequestMatcher = AntPathRequestMatcher("/logout")
  297. }
  298. }
  299. return http.build()
  300. }
  301. }
  302. ----
  303. ====
  304. [[servlet-considerations-csrf-timeouts]]
  305. === CSRF and Session Timeouts
  306. By default, Spring Security stores the CSRF token in the `HttpSession`.
  307. This can lead to a situation where the session expires, leaving no CSRF token to validate against.
  308. We have already discussed xref:features/exploits/csrf.adoc#csrf-considerations-login[general solutions] to session timeouts.
  309. This section discusses the specifics of CSRF timeouts as it pertains to the servlet support.
  310. You can change the storage of the CSRF token to be in a cookie.
  311. For details, see the <<servlet-csrf-configure-custom-repository>> section.
  312. If a token does expire, you might want to customize how it is handled by specifying a custom `AccessDeniedHandler`.
  313. The custom `AccessDeniedHandler` can process the `InvalidCsrfTokenException` any way you like.
  314. For an example of how to customize the `AccessDeniedHandler`, see the provided links for both xref:servlet/appendix/namespace/http.adoc#nsa-access-denied-handler[xml] and {gh-url}/config/src/test/java/org/springframework/security/config/annotation/web/configurers/NamespaceHttpServerAccessDeniedHandlerTests.java#L64[Java configuration].
  315. // FIXME: We should add a custom AccessDeniedHandler section in the reference and update the links above
  316. [[servlet-csrf-considerations-multipart]]
  317. === Multipart (file upload)
  318. We have xref:features/exploits/csrf.adoc#csrf-considerations-multipart[already discussed] how protecting multipart requests (file uploads) from CSRF attacks causes a https://en.wikipedia.org/wiki/Chicken_or_the_egg[chicken and the egg] problem.
  319. This section discusses how to implement placing the CSRF token in the <<servlet-csrf-considerations-multipart-body,body>> and <<servlet-csrf-considerations-multipart-url,url>> within a servlet application.
  320. [NOTE]
  321. ====
  322. You can find more information about using multipart forms with Spring in the https://docs.spring.io/spring/docs/5.2.x/spring-framework-reference/web.html#mvc-multipart[1.1.11. Multipart Resolver] section of the Spring reference and the https://docs.spring.io/spring/docs/5.2.x/javadoc-api/org/springframework/web/multipart/support/MultipartFilter.html[`MultipartFilter` javadoc].
  323. ====
  324. [[servlet-csrf-considerations-multipart-body]]
  325. ==== Place CSRF Token in the Body
  326. We have xref:features/exploits/csrf.adoc#csrf-considerations-multipart-body[already discussed] the tradeoffs of placing the CSRF token in the body.
  327. In this section, we discuss how to configure Spring Security to read the CSRF from the body.
  328. To read the CSRF token from the body, the `MultipartFilter` is specified before the Spring Security filter.
  329. 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.
  330. However, only authorized users can submit a file that is processed by your application.
  331. In general, this is the recommended approach because the temporary file upload should have a negligible impact on most servers.
  332. // FIXME: Document Spring Boot
  333. To ensure that `MultipartFilter` is specified before the Spring Security filter with XML configuration, you can ensure the `<filter-mapping>` element of the `MultipartFilter` is placed before the `springSecurityFilterChain` within the `web.xml` file:
  334. .Initializer MultipartFilter
  335. ====
  336. .Java
  337. [source,java,role="primary"]
  338. ----
  339. public class SecurityApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
  340. @Override
  341. protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
  342. insertFilters(servletContext, new MultipartFilter());
  343. }
  344. }
  345. ----
  346. .Kotlin
  347. [source,kotlin,role="secondary"]
  348. ----
  349. class SecurityApplicationInitializer : AbstractSecurityWebApplicationInitializer() {
  350. override fun beforeSpringSecurityFilterChain(servletContext: ServletContext?) {
  351. insertFilters(servletContext, MultipartFilter())
  352. }
  353. }
  354. ----
  355. ====
  356. 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:
  357. .web.xml - MultipartFilter
  358. ====
  359. [source,xml]
  360. ----
  361. <filter>
  362. <filter-name>MultipartFilter</filter-name>
  363. <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
  364. </filter>
  365. <filter>
  366. <filter-name>springSecurityFilterChain</filter-name>
  367. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  368. </filter>
  369. <filter-mapping>
  370. <filter-name>MultipartFilter</filter-name>
  371. <url-pattern>/*</url-pattern>
  372. </filter-mapping>
  373. <filter-mapping>
  374. <filter-name>springSecurityFilterChain</filter-name>
  375. <url-pattern>/*</url-pattern>
  376. </filter-mapping>
  377. ----
  378. ====
  379. [[servlet-csrf-considerations-multipart-url]]
  380. ==== Include a CSRF Token in a URL
  381. If letting unauthorized users 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.
  382. Since the `CsrfToken` is exposed as an `HttpServletRequest` <<servlet-csrf-include,request attribute>>, we can use that to create an `action` with the CSRF token in it.
  383. The following example does this with a JSP:
  384. .CSRF Token in Action
  385. ====
  386. [source,html]
  387. ----
  388. <form method="post"
  389. action="./upload?${_csrf.parameterName}=${_csrf.token}"
  390. enctype="multipart/form-data">
  391. ----
  392. ====
  393. [[servlet-csrf-considerations-override-method]]
  394. === HiddenHttpMethodFilter
  395. We have xref:features/exploits/csrf.adoc#csrf-considerations-multipart-body[already discussed] the trade-offs of placing the CSRF token in the body.
  396. In Spring's Servlet support, overriding the HTTP method is done by using https://docs.spring.io/spring-framework/docs/5.2.x/javadoc-api/org/springframework/web/filter/reactive/HiddenHttpMethodFilter.html[`HiddenHttpMethodFilter`].
  397. You can find more information in the https://docs.spring.io/spring/docs/5.2.x/spring-framework-reference/web.html#mvc-rest-method-conversion[HTTP Method Conversion] section of the reference documentation.