Skip to content

Commit 4994e67

Browse files
author
Steve Riesenberg
committed
Add servlet opt out steps for CSRF BREACH
Issue gh-12107
1 parent 2fe2f91 commit 4994e67

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

docs/modules/ROOT/pages/migration/servlet/exploits.adoc

+77
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ In Spring Security 6, the default is that the lookup of the `CsrfToken` will be
1111

1212
To opt into the new Spring Security 6 default, the following configuration can be used.
1313

14+
[[servlet-opt-in-defer-loading-csrf-token]]
1415
.Defer Loading `CsrfToken`
1516
====
1617
.Java
@@ -166,3 +167,79 @@ open fun springSecurity(http: HttpSecurity): SecurityFilterChain {
166167
p:csrfRequestAttributeName="_csrf"/>
167168
----
168169
====
170+
171+
[[servlet-csrf-breach-opt-out]]
172+
=== Opt-out Steps
173+
174+
If configuring CSRF BREACH protection gives you trouble, take a look at these scenarios for optimal opt out behavior:
175+
176+
==== I am using AngularJS or another Javascript framework
177+
178+
If you are using AngularJS and the https://angular.io/api/common/http/HttpClientXsrfModule[HttpClientXsrfModule] (or a similar module in another framework) along with `CookieCsrfTokenRepository.withHttpOnlyFalse()`, you may find that automatic support no longer works.
179+
180+
In this case, you can configure Spring Security to validate the raw `CsrfToken` from the cookie while keeping CSRF BREACH protection of the response using a custom `CsrfTokenRequestHandler` with delegation, like so:
181+
182+
.Configure `CsrfToken` BREACH Protection to validate raw tokens
183+
====
184+
.Java
185+
[source,java,role="primary"]
186+
----
187+
@Bean
188+
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
189+
CookieCsrfTokenRepository tokenRepository = CookieCsrfTokenRepository.withHttpOnlyFalse();
190+
XorCsrfTokenRequestAttributeHandler delegate = new XorCsrfTokenRequestAttributeHandler();
191+
// set the name of the attribute the CsrfToken will be populated on
192+
delegate.setCsrfRequestAttributeName("_csrf");
193+
// Use only the handle() method of XorCsrfTokenRequestAttributeHandler and the
194+
// default implementation of resolveCsrfTokenValue() from CsrfTokenRequestHandler
195+
CsrfTokenRequestHandler requestHandler = delegate::handle;
196+
http
197+
// ...
198+
.csrf((csrf) -> csrf
199+
.csrfTokenRepository(tokenRepository)
200+
.csrfTokenRequestHandler(requestHandler)
201+
);
202+
203+
return http.build();
204+
}
205+
----
206+
207+
.Kotlin
208+
[source,kotlin,role="secondary"]
209+
----
210+
@Bean
211+
open fun springSecurity(http: HttpSecurity): SecurityFilterChain {
212+
val tokenRepository = CookieCsrfTokenRepository.withHttpOnlyFalse()
213+
val delegate = XorCsrfTokenRequestAttributeHandler()
214+
// set the name of the attribute the CsrfToken will be populated on
215+
delegate.setCsrfRequestAttributeName("_csrf")
216+
// Use only the handle() method of XorCsrfTokenRequestAttributeHandler and the
217+
// default implementation of resolveCsrfTokenValue() from CsrfTokenRequestHandler
218+
val requestHandler = CsrfTokenRequestHandler(delegate::handle)
219+
http {
220+
csrf {
221+
csrfTokenRepository = tokenRepository
222+
csrfTokenRequestHandler = requestHandler
223+
}
224+
}
225+
return http.build()
226+
}
227+
----
228+
229+
.XML
230+
[source,xml,role="secondary"]
231+
----
232+
<http>
233+
<!-- ... -->
234+
<csrf token-repository-ref="tokenRepository"
235+
request-handler-ref="requestHandler"/>
236+
</http>
237+
<b:bean id="tokenRepository"
238+
class="org.springframework.security.web.csrf.CookieCsrfTokenRepository"
239+
p:cookieHttpOnly="false"/>
240+
----
241+
====
242+
243+
==== I need to opt out of CSRF BREACH protection for another reason
244+
245+
If CSRF BREACH protection does not work for you for another reason, you can opt out using the configuration from the <<servlet-opt-in-defer-loading-csrf-token>> section.

0 commit comments

Comments
 (0)