|
@@ -1,7 +1,7 @@
|
|
|
[[webflux-csrf]]
|
|
|
= Cross Site Request Forgery (CSRF) for WebFlux Environments
|
|
|
|
|
|
-This section discusses Spring Security's xref:overview/features/exploits/csrf.adoc#csrf[Cross Site Request Forgery (CSRF)] support for WebFlux environments.
|
|
|
+This section discusses Spring Security's xref:features/exploits/csrf.adoc#csrf[Cross Site Request Forgery (CSRF)] support for WebFlux environments.
|
|
|
|
|
|
[[webflux-csrf-using]]
|
|
|
== Using Spring Security CSRF Protection
|
|
@@ -14,7 +14,7 @@ The steps to using Spring Security's CSRF protection are outlined below:
|
|
|
[[webflux-csrf-idempotent]]
|
|
|
=== 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 xref:overview/features/exploits/csrf.adoc#csrf-protection-idempotent[Safe Methods Must be Idempotent].
|
|
|
+This is covered in detail in xref:features/exploits/csrf.adoc#csrf-protection-idempotent[Safe Methods Must be Idempotent].
|
|
|
|
|
|
[[webflux-csrf-configure]]
|
|
|
=== Configure CSRF Protection
|
|
@@ -73,7 +73,7 @@ If you do not need the ability to read the cookie with JavaScript directly, it i
|
|
|
[[webflux-csrf-configure-disable]]
|
|
|
==== Disable CSRF Protection
|
|
|
CSRF protection is enabled by default.
|
|
|
-However, it is simple to disable CSRF protection if it xref:overview/features/exploits/csrf.adoc#csrf-when[makes sense for your application].
|
|
|
+However, it is simple to disable CSRF protection if it xref:features/exploits/csrf.adoc#csrf-when[makes sense for your application].
|
|
|
|
|
|
The Java configuration below will disable CSRF protection.
|
|
|
|
|
@@ -109,7 +109,7 @@ fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain
|
|
|
[[webflux-csrf-include]]
|
|
|
=== Include the CSRF Token
|
|
|
|
|
|
-In order for the xref:overview/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.
|
|
|
+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.
|
|
|
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 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`.
|
|
@@ -272,19 +272,19 @@ An example of doing this with Thymeleaf is shown below:
|
|
|
== 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 WebFlux environments.
|
|
|
-Refer to xref:overview/features/exploits/csrf.adoc#csrf-considerations[CSRF Considerations] for a more general discussion.
|
|
|
+Refer to xref:features/exploits/csrf.adoc#csrf-considerations[CSRF Considerations] for a more general discussion.
|
|
|
|
|
|
|
|
|
[[webflux-considerations-csrf-login]]
|
|
|
=== Logging In
|
|
|
|
|
|
-It is important to xref:overview/features/exploits/csrf.adoc#csrf-considerations-login[require CSRF for log in] requests to protect against forging log in attempts.
|
|
|
+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.
|
|
|
Spring Security's WebFlux support does this out of the box.
|
|
|
|
|
|
[[webflux-considerations-csrf-logout]]
|
|
|
=== Logging Out
|
|
|
|
|
|
-It is important to xref:overview/features/exploits/csrf.adoc#csrf-considerations-logout[require CSRF for log out] requests to protect against forging log out attempts.
|
|
|
+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.
|
|
|
By default Spring Security's `LogoutWebFilter` only processes HTTP post requests.
|
|
|
This ensures that log out requires a CSRF token and that a malicious user cannot forcibly log out your users.
|
|
|
|
|
@@ -333,7 +333,7 @@ fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain
|
|
|
By default Spring Security stores the CSRF token in the `WebSession`.
|
|
|
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 xref:overview/features/exploits/csrf.adoc#csrf-considerations-login[general solutions] to session timeouts.
|
|
|
+We've already discussed xref:features/exploits/csrf.adoc#csrf-considerations-login[general solutions] to session timeouts.
|
|
|
This section discusses the specifics of CSRF timeouts as it pertains to the WebFlux support.
|
|
|
|
|
|
It is simple to change storage of the expected CSRF token to be in a cookie.
|
|
@@ -344,7 +344,7 @@ For details, refer to the <<webflux-csrf-configure-custom-repository>> section.
|
|
|
// FIXME: We need a WebFlux multipart body vs action story. WebFlux always has multipart enabled.
|
|
|
[[webflux-csrf-considerations-multipart]]
|
|
|
=== Multipart (file upload)
|
|
|
-We have xref:overview/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.
|
|
|
+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.
|
|
|
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.
|
|
|
|
|
|
[NOTE]
|
|
@@ -355,7 +355,7 @@ More information about using multipart forms with Spring can be found within the
|
|
|
[[webflux-csrf-considerations-multipart-body]]
|
|
|
==== Place CSRF Token in the Body
|
|
|
|
|
|
-We have xref:overview/features/exploits/csrf.adoc#csrf-considerations-multipart[already discussed] the trade-offs of placing the CSRF token in the body.
|
|
|
+We have xref:features/exploits/csrf.adoc#csrf-considerations-multipart[already discussed] the trade-offs of placing the CSRF token in the body.
|
|
|
|
|
|
In a WebFlux application, this can be configured with the following configuration:
|
|
|
|
|
@@ -391,7 +391,7 @@ fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain
|
|
|
[[webflux-csrf-considerations-multipart-url]]
|
|
|
==== Include CSRF Token in URL
|
|
|
|
|
|
-We have xref:overview/features/exploits/csrf.adoc#csrf-considerations-multipart[already discussed] the trade-offs of placing the CSRF token in the URL.
|
|
|
+We have xref:features/exploits/csrf.adoc#csrf-considerations-multipart[already discussed] the trade-offs of placing the CSRF token in the URL.
|
|
|
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.
|
|
|
An example with Thymeleaf is shown below:
|
|
|
|
|
@@ -407,6 +407,6 @@ An example with Thymeleaf is shown below:
|
|
|
|
|
|
[[webflux-csrf-considerations-override-method]]
|
|
|
=== HiddenHttpMethodFilter
|
|
|
-We have xref:overview/features/exploits/csrf.adoc#csrf-considerations-override-method[already discussed] overriding the HTTP method.
|
|
|
+We have xref:features/exploits/csrf.adoc#csrf-considerations-override-method[already discussed] overriding the HTTP method.
|
|
|
|
|
|
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].
|