-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpaCsrfTokenRequestHandler.java
More file actions
60 lines (56 loc) · 2.6 KB
/
SpaCsrfTokenRequestHandler.java
File metadata and controls
60 lines (56 loc) · 2.6 KB
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
package contactapp.security;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.function.Supplier;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.security.web.csrf.CsrfTokenRequestAttributeHandler;
import org.springframework.security.web.csrf.CsrfTokenRequestHandler;
import org.springframework.security.web.csrf.XorCsrfTokenRequestAttributeHandler;
import org.springframework.util.StringUtils;
/**
* Custom CSRF token request handler for Single Page Applications.
*
* <p>Accepts raw CSRF tokens from cookies (as sent by SPAs that read XSRF-TOKEN cookie)
* while still providing BREACH protection via XOR masking for server-rendered pages.
*
* <p>Based on Spring Security 6 migration guide.
*
* @see <a href="https://docs.spring.io/spring-security/reference/5.8/migration/
* servlet/exploits.html#_i_am_using_a_single_page_application_with_cookiecsrftokenrepository">
* Spring Security 6 CSRF Migration</a>
*/
public final class SpaCsrfTokenRequestHandler extends CsrfTokenRequestAttributeHandler {
private final CsrfTokenRequestHandler delegate = new XorCsrfTokenRequestAttributeHandler();
@Override
public void handle(
final HttpServletRequest request,
final HttpServletResponse response,
final Supplier<CsrfToken> deferredCsrfToken) {
/*
* Always use XorCsrfTokenRequestAttributeHandler to provide BREACH protection of
* the CsrfToken when it is rendered in the response body.
*/
this.delegate.handle(request, response, deferredCsrfToken);
}
@Override
public String resolveCsrfTokenValue(
final HttpServletRequest request,
final CsrfToken csrfToken) {
/*
* If the request contains a request header, use CsrfTokenRequestAttributeHandler
* to resolve the CsrfToken. This applies when a single-page application includes
* the header value automatically, which was obtained via a cookie containing the
* raw CsrfToken.
*/
if (StringUtils.hasText(request.getHeader(csrfToken.getHeaderName()))) {
return super.resolveCsrfTokenValue(request, csrfToken);
}
/*
* In all other cases (e.g. if the request contains a request parameter), use
* XorCsrfTokenRequestAttributeHandler to resolve the CsrfToken. This applies
* when a server-side rendered form includes the _csrf request parameter as a
* hidden input.
*/
return this.delegate.resolveCsrfTokenValue(request, csrfToken);
}
}