-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Turn on CSRF protection. Refactor the integration test setup to handl…
…e CSRF tokens. Update tests for CSRF tokens.
- Loading branch information
1 parent
bede17c
commit aa073fd
Showing
16 changed files
with
348 additions
and
96 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
pass-core-main/src/main/java/org/eclipse/pass/main/security/CsrfCookieFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.eclipse.pass.main.security; | ||
|
||
import java.io.IOException; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.security.web.csrf.CsrfToken; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
public class CsrfCookieFilter extends OncePerRequestFilter { | ||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
CsrfToken csrfToken = (CsrfToken) request.getAttribute("_csrf"); | ||
// Render the token value to a cookie by causing the deferred token to be loaded | ||
csrfToken.getToken(); | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
pass-core-main/src/main/java/org/eclipse/pass/main/security/SpaCsrfTokenRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.eclipse.pass.main.security; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
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; | ||
|
||
public class SpaCsrfTokenRequestHandler extends CsrfTokenRequestAttributeHandler { | ||
private final CsrfTokenRequestHandler delegate = new XorCsrfTokenRequestAttributeHandler(); | ||
|
||
@Override | ||
public void handle(HttpServletRequest request, HttpServletResponse response, Supplier<CsrfToken> csrfToken) { | ||
/* | ||
* Always use XorCsrfTokenRequestAttributeHandler to provide BREACH protection of | ||
* the CsrfToken when it is rendered in the response body. | ||
*/ | ||
this.delegate.handle(request, response, csrfToken); | ||
} | ||
|
||
@Override | ||
public String resolveCsrfTokenValue(HttpServletRequest request, 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. | ||
*/ | ||
String token = request.getHeader(csrfToken.getHeaderName()); | ||
|
||
if (token != null && !token.isEmpty()) { | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.