csrf.adoc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. [[webflux-csrf]]
  2. = Cross Site Request Forgery (CSRF) for WebFlux Environments
  3. This section discusses Spring Security's xref:features/exploits/csrf.adoc#csrf[Cross Site Request Forgery (CSRF)] support for WebFlux environments.
  4. [[webflux-csrf-using]]
  5. == Using Spring Security CSRF Protection
  6. The steps to using Spring Security's CSRF protection are outlined below:
  7. * <<webflux-csrf-idempotent,Use proper HTTP verbs>>
  8. * <<webflux-csrf-configure,Configure CSRF Protection>>
  9. * <<webflux-csrf-include,Include the CSRF Token>>
  10. [[webflux-csrf-idempotent]]
  11. === Use proper HTTP verbs
  12. The first step to protecting against CSRF attacks is to ensure 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. [[webflux-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. Below are a few common customizations.
  19. [[webflux-csrf-configure-custom-repository]]
  20. ==== Custom CsrfTokenRepository
  21. By default Spring Security stores the expected CSRF token in the `WebSession` using `WebSessionServerCsrfTokenRepository`.
  22. There can be cases where users will want to configure a custom `ServerCsrfTokenRepository`.
  23. For example, it might be desirable to persist the `CsrfToken` in a cookie to <<webflux-csrf-include-ajax-auto,support a JavaScript based application>>.
  24. By default the `CookieServerCsrfTokenRepository` will write to a cookie named `XSRF-TOKEN` and read 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 `CookieServerCsrfTokenRepository` in Java Configuration using:
  27. .Store CSRF Token in a Cookie
  28. [tabs]
  29. ======
  30. Java::
  31. +
  32. [source,java,role="primary"]
  33. -----
  34. @Bean
  35. public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  36. http
  37. // ...
  38. .csrf(csrf -> csrf.csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse()))
  39. return http.build();
  40. }
  41. -----
  42. Kotlin::
  43. +
  44. [source,kotlin,role="secondary"]
  45. -----
  46. @Bean
  47. fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  48. return http {
  49. // ...
  50. csrf {
  51. csrfTokenRepository = CookieServerCsrfTokenRepository.withHttpOnlyFalse()
  52. }
  53. }
  54. }
  55. -----
  56. ======
  57. [NOTE]
  58. ====
  59. The sample explicitly sets `cookieHttpOnly=false`.
  60. This is necessary to allow JavaScript (i.e. AngularJS) to read it.
  61. If you do not need the ability to read the cookie with JavaScript directly, it is recommended to omit `cookieHttpOnly=false` (by using `new CookieServerCsrfTokenRepository()` instead) to improve security.
  62. ====
  63. [[webflux-csrf-configure-disable]]
  64. ==== Disable CSRF Protection
  65. CSRF protection is enabled by default.
  66. However, it is simple to disable CSRF protection if it xref:features/exploits/csrf.adoc#csrf-when[makes sense for your application].
  67. The Java configuration below will disable CSRF protection.
  68. .Disable CSRF Configuration
  69. [tabs]
  70. ======
  71. Java::
  72. +
  73. [source,java,role="primary"]
  74. ----
  75. @Bean
  76. public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  77. http
  78. // ...
  79. .csrf(csrf -> csrf.disable()))
  80. return http.build();
  81. }
  82. ----
  83. Kotlin::
  84. +
  85. [source,kotlin,role="secondary"]
  86. -----
  87. @Bean
  88. fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  89. return http {
  90. // ...
  91. csrf {
  92. disable()
  93. }
  94. }
  95. }
  96. -----
  97. ======
  98. [[webflux-csrf-configure-request-handler]]
  99. ==== Configure ServerCsrfTokenRequestHandler
  100. Spring Security's https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/server/csrf/CsrfWebFilter.html[`CsrfWebFilter`] exposes a https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/csrf/CsrfToken.html[`Mono<CsrfToken>`] as a `ServerWebExchange` attribute named `org.springframework.security.web.server.csrf.CsrfToken` with the help of a https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/server/csrf/ServerCsrfTokenRequestHandler.html[`ServerCsrfTokenRequestHandler`].
  101. The default implementation is `ServerCsrfTokenRequestAttributeHandler`.
  102. An alternate implementation `XorServerCsrfTokenRequestAttributeHandler` is available to provide protection for BREACH (see https://github.com/spring-projects/spring-security/issues/4001[gh-4001]).
  103. You can configure `XorServerCsrfTokenRequestAttributeHandler` using the following Java configuration:
  104. .Configure BREACH protection
  105. [tabs]
  106. ======
  107. Java::
  108. +
  109. [source,java,role="primary"]
  110. -----
  111. @Bean
  112. public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  113. http
  114. // ...
  115. .csrf(csrf -> csrf
  116. .csrfTokenRequestHandler(new XorServerCsrfTokenRequestAttributeHandler())
  117. )
  118. return http.build();
  119. }
  120. -----
  121. Kotlin::
  122. +
  123. [source,kotlin,role="secondary"]
  124. -----
  125. @Bean
  126. fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  127. return http {
  128. // ...
  129. csrf {
  130. csrfTokenRequestHandler = XorServerCsrfTokenRequestAttributeHandler()
  131. }
  132. }
  133. }
  134. -----
  135. ======
  136. [[webflux-csrf-include]]
  137. === Include the CSRF Token
  138. In order 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.
  139. 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.
  140. <<webflux-csrf-configure-request-handler,We've seen>> that the `Mono<CsrfToken>` is exposed as a `ServerWebExchange` attribute.
  141. This means that any view technology can access the `Mono<CsrfToken>` to expose the expected token as either a <<webflux-csrf-include-form-attr,form>> or <<webflux-csrf-include-ajax-meta,meta tag>>.
  142. [[webflux-csrf-include-subscribe]]
  143. If your view technology does not provide a simple way to subscribe to the `Mono<CsrfToken>`, a common pattern is to use Spring's `@ControllerAdvice` to expose the `CsrfToken` directly.
  144. For example, the following code will place the `CsrfToken` on the default attribute name (`_csrf`) used by Spring Security's <<webflux-csrf-include-form-auto,CsrfRequestDataValueProcessor>> to automatically include the CSRF token as a hidden input.
  145. .`CsrfToken` as `@ModelAttribute`
  146. [tabs]
  147. ======
  148. Java::
  149. +
  150. [source,java,role="primary"]
  151. ----
  152. @ControllerAdvice
  153. public class SecurityControllerAdvice {
  154. @ModelAttribute
  155. Mono<CsrfToken> csrfToken(ServerWebExchange exchange) {
  156. Mono<CsrfToken> csrfToken = exchange.getAttribute(CsrfToken.class.getName());
  157. return csrfToken.doOnSuccess(token -> exchange.getAttributes()
  158. .put(CsrfRequestDataValueProcessor.DEFAULT_CSRF_ATTR_NAME, token));
  159. }
  160. }
  161. ----
  162. Kotlin::
  163. +
  164. [source,kotlin,role="secondary"]
  165. ----
  166. @ControllerAdvice
  167. class SecurityControllerAdvice {
  168. @ModelAttribute
  169. fun csrfToken(exchange: ServerWebExchange): Mono<CsrfToken> {
  170. val csrfToken: Mono<CsrfToken>? = exchange.getAttribute(CsrfToken::class.java.name)
  171. return csrfToken!!.doOnSuccess { token ->
  172. exchange.attributes[CsrfRequestDataValueProcessor.DEFAULT_CSRF_ATTR_NAME] = token
  173. }
  174. }
  175. }
  176. ----
  177. ======
  178. Fortunately, Thymeleaf provides <<webflux-csrf-include-form-auto,integration>> that works without any additional work.
  179. [[webflux-csrf-include-form]]
  180. ==== Form URL Encoded
  181. In order to post an HTML form the CSRF token must be included in the form as a hidden input.
  182. For example, the rendered HTML might look like:
  183. .CSRF Token HTML
  184. [source,html]
  185. ----
  186. <input type="hidden"
  187. name="_csrf"
  188. value="4bfd1575-3ad1-4d21-96c7-4ef2d9f86721"/>
  189. ----
  190. Next we will discuss various ways of including the CSRF token in a form as a hidden input.
  191. [[webflux-csrf-include-form-auto]]
  192. ===== Automatic CSRF Token Inclusion
  193. Spring Security's CSRF support provides integration with Spring's https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/reactive/result/view/RequestDataValueProcessor.html[RequestDataValueProcessor] via its https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/reactive/result/view/CsrfRequestDataValueProcessor.html[CsrfRequestDataValueProcessor].
  194. In order for `CsrfRequestDataValueProcessor` to work, the `Mono<CsrfToken>` must be subscribed to and the `CsrfToken` must be <<webflux-csrf-include-subscribe,exposed as an attribute>> that matches https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/reactive/result/view/CsrfRequestDataValueProcessor.html#DEFAULT_CSRF_ATTR_NAME[DEFAULT_CSRF_ATTR_NAME].
  195. Fortunately, Thymeleaf https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#integration-with-requestdatavalueprocessor[provides support] to take care of all the boilerplate for you by integrating with `RequestDataValueProcessor` to ensure that forms that have an unsafe HTTP method (i.e. post) will automatically include the actual CSRF token.
  196. [[webflux-csrf-include-form-attr]]
  197. ===== CsrfToken Request Attribute
  198. If the <<webflux-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 `Mono<CsrfToken>` <<webflux-csrf-include,is exposed>> as a `ServerWebExchange` attribute named `org.springframework.security.web.server.csrf.CsrfToken`.
  199. The Thymeleaf sample below assumes that you <<webflux-csrf-include-subscribe,expose>> the `CsrfToken` on an attribute named `_csrf`.
  200. .CSRF Token in Form with Request Attribute
  201. [source,html]
  202. ----
  203. <form th:action="@{/logout}"
  204. method="post">
  205. <input type="submit"
  206. value="Log out" />
  207. <input type="hidden"
  208. th:name="${_csrf.parameterName}"
  209. th:value="${_csrf.token}"/>
  210. </form>
  211. ----
  212. [[webflux-csrf-include-ajax]]
  213. ==== Ajax and JSON Requests
  214. If you are using JSON, then it is not possible to submit the CSRF token within an HTTP parameter.
  215. Instead you can submit the token within a HTTP header.
  216. In the following sections we will discuss various ways of including the CSRF token as an HTTP request header in JavaScript based applications.
  217. [[webflux-csrf-include-ajax-auto]]
  218. ===== Automatic Inclusion
  219. Spring Security can easily be <<webflux-csrf-configure-custom-repository,configured>> to store the expected CSRF token in a cookie.
  220. By storing the expected CSRF in a cookie, JavaScript frameworks like https://docs.angularjs.org/api/ng/service/$http#cross-site-request-forgery-xsrf-protection[AngularJS] will automatically include the actual CSRF token in the HTTP request headers.
  221. [[webflux-csrf-include-ajax-meta]]
  222. ===== Meta tags
  223. An alternative pattern to <<webflux-csrf-include-form-auto,exposing the CSRF in a cookie>> is to include the CSRF token within your `meta` tags.
  224. The HTML might look something like this:
  225. .CSRF meta tag HTML
  226. [source,html]
  227. ----
  228. <html>
  229. <head>
  230. <meta name="_csrf" content="4bfd1575-3ad1-4d21-96c7-4ef2d9f86721"/>
  231. <meta name="_csrf_header" content="X-CSRF-TOKEN"/>
  232. <!-- ... -->
  233. </head>
  234. <!-- ... -->
  235. ----
  236. Once the meta tags contained the CSRF token, the JavaScript code would read the meta tags and include the CSRF token as a header.
  237. If you were using jQuery, this could be done with the following:
  238. .AJAX send CSRF Token
  239. [source,javascript]
  240. ----
  241. $(function () {
  242. var token = $("meta[name='_csrf']").attr("content");
  243. var header = $("meta[name='_csrf_header']").attr("content");
  244. $(document).ajaxSend(function(e, xhr, options) {
  245. xhr.setRequestHeader(header, token);
  246. });
  247. });
  248. ----
  249. The sample below assumes that you <<webflux-csrf-include-subscribe,expose>> the `CsrfToken` on an attribute named `_csrf`.
  250. An example of doing this with Thymeleaf is shown below:
  251. .CSRF meta tag JSP
  252. [source,html]
  253. ----
  254. <html>
  255. <head>
  256. <meta name="_csrf" th:content="${_csrf.token}"/>
  257. <!-- default header name is X-CSRF-TOKEN -->
  258. <meta name="_csrf_header" th:content="${_csrf.headerName}"/>
  259. <!-- ... -->
  260. </head>
  261. <!-- ... -->
  262. ----
  263. [[webflux-csrf-considerations]]
  264. == CSRF Considerations
  265. There are a few special considerations to consider when implementing protection against CSRF attacks.
  266. This section discusses those considerations as it pertains to WebFlux environments.
  267. Refer to xref:features/exploits/csrf.adoc#csrf-considerations[CSRF Considerations] for a more general discussion.
  268. [[webflux-considerations-csrf-login]]
  269. === Logging In
  270. 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.
  271. Spring Security's WebFlux support does this out of the box.
  272. [[webflux-considerations-csrf-logout]]
  273. === Logging Out
  274. It is important to xref:features/exploits/csrf.adoc#csrf-considerations-logout[require CSRF for log out] requests to protect against forging log out attempts.
  275. By default Spring Security's `LogoutWebFilter` only processes HTTP post requests.
  276. This ensures that log out requires a CSRF token and that a malicious user cannot forcibly log out your users.
  277. The easiest approach is to use a form to log out.
  278. If you really want a link, you can use JavaScript to have the link perform a POST (i.e. maybe on a hidden form).
  279. 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.
  280. If you really want to use HTTP GET with logout you can do so, but remember this is generally not recommended.
  281. For example, the following Java Configuration will perform logout with the URL `/logout` is requested with any HTTP method:
  282. // FIXME: This should be a link to log out documentation
  283. .Log out with HTTP GET
  284. [tabs]
  285. ======
  286. Java::
  287. +
  288. [source,java,role="primary"]
  289. ----
  290. @Bean
  291. public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  292. http
  293. // ...
  294. .logout(logout -> logout.requiresLogout(new PathPatternParserServerWebExchangeMatcher("/logout")))
  295. return http.build();
  296. }
  297. ----
  298. Kotlin::
  299. +
  300. [source,kotlin,role="secondary"]
  301. ----
  302. @Bean
  303. fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  304. return http {
  305. // ...
  306. logout {
  307. requiresLogout = PathPatternParserServerWebExchangeMatcher("/logout")
  308. }
  309. }
  310. }
  311. ----
  312. ======
  313. [[webflux-considerations-csrf-timeouts]]
  314. === CSRF and Session Timeouts
  315. By default Spring Security stores the CSRF token in the `WebSession`.
  316. This can lead to a situation where the session expires which means there is not an expected CSRF token to validate against.
  317. We've already discussed xref:features/exploits/csrf.adoc#csrf-considerations-login[general solutions] to session timeouts.
  318. This section discusses the specifics of CSRF timeouts as it pertains to the WebFlux support.
  319. It is simple to change storage of the expected CSRF token to be in a cookie.
  320. For details, refer to the <<webflux-csrf-configure-custom-repository>> section.
  321. // FIXME: We should add a custom AccessDeniedHandler section in the reference and update the links above
  322. // FIXME: We need a WebFlux multipart body vs action story. WebFlux always has multipart enabled.
  323. [[webflux-csrf-considerations-multipart]]
  324. === Multipart (file upload)
  325. 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.
  326. This section discusses how to implement placing the CSRF token in the <<webflux-csrf-considerations-multipart-body,body>> and <<webflux-csrf-considerations-multipart-url,url>> within a WebFlux application.
  327. [NOTE]
  328. ====
  329. More information about using multipart forms with Spring can be found within the https://docs.spring.io/spring/docs/5.2.x/spring-framework-reference/web-reactive.html#webflux-multipart[Multipart Data] section of the Spring reference.
  330. ====
  331. [[webflux-csrf-considerations-multipart-body]]
  332. ==== Place CSRF Token in the Body
  333. We have xref:features/exploits/csrf.adoc#csrf-considerations-multipart[already discussed] the trade-offs of placing the CSRF token in the body.
  334. In a WebFlux application, this can be configured with the following configuration:
  335. .Enable obtaining CSRF token from multipart/form-data
  336. [tabs]
  337. ======
  338. Java::
  339. +
  340. [source,java,role="primary"]
  341. ----
  342. @Bean
  343. public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  344. http
  345. // ...
  346. .csrf(csrf -> csrf.tokenFromMultipartDataEnabled(true))
  347. return http.build();
  348. }
  349. ----
  350. Kotlin::
  351. +
  352. [source,kotlin,role="secondary"]
  353. ----
  354. @Bean
  355. fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  356. return http {
  357. // ...
  358. csrf {
  359. tokenFromMultipartDataEnabled = true
  360. }
  361. }
  362. }
  363. ----
  364. ======
  365. [[webflux-csrf-considerations-multipart-url]]
  366. ==== Include CSRF Token in URL
  367. We have xref:features/exploits/csrf.adoc#csrf-considerations-multipart[already discussed] the trade-offs of placing the CSRF token in the URL.
  368. Since the `CsrfToken` is exposed as an `ServerHttpRequest` <<webflux-csrf-include,request attribute>>, we can use that to create an `action` with the CSRF token in it.
  369. An example with Thymeleaf is shown below:
  370. .CSRF Token in Action
  371. [source,html]
  372. ----
  373. <form method="post"
  374. th:action="@{/upload(${_csrf.parameterName}=${_csrf.token})}"
  375. enctype="multipart/form-data">
  376. ----
  377. [[webflux-csrf-considerations-override-method]]
  378. === HiddenHttpMethodFilter
  379. We have xref:features/exploits/csrf.adoc#csrf-considerations-override-method[already discussed] overriding the HTTP method.
  380. In a Spring WebFlux application, overriding the HTTP method is done using https://docs.spring.io/spring-framework/docs/5.2.x/javadoc-api/org/springframework/web/filter/reactive/HiddenHttpMethodFilter.html[HiddenHttpMethodFilter].