Fix double submit csrf cookie - #4010
Open
joemahady-comm wants to merge 1 commit into
Open
Conversation
…-prefix-samesite-none-session / uaa ai-assisted=yes Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates UAA’s CookieBasedCsrfTokenRepository to emit and read a __Host--prefixed CSRF cookie on secure requests, aligning cookie attributes (name/prefix, Secure, and Path) with modern browser expectations for stronger CSRF cookie handling.
Changes:
- Generate
__Host-<cookieName>on secure requests and usePath=/for that cookie. - Adjust token loading logic to look for the secure vs non-secure cookie name depending on request security.
- Update unit test helper logic to account for Spring Framework 7
MockHttpServletResponsecookie parsing changes.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
server/src/main/java/org/cloudfoundry/identity/uaa/security/web/CookieBasedCsrfTokenRepository.java |
Adds secure-aware cookie naming (__Host-), Secure flag, and Path selection; updates lookup logic accordingly. |
server/src/test/java/org/cloudfoundry/identity/uaa/web/CookieBasedCsrfTokenRepositoryTests.java |
Updates test cookie extraction to handle Set-Cookie header parsing changes and introduces expectations around secure cookie naming. |
Comments suppressed due to low confidence (2)
server/src/main/java/org/cloudfoundry/identity/uaa/security/web/CookieBasedCsrfTokenRepository.java:145
loadToken()uses the rawparameterNameto buildexpectedCookieName. IfparameterNameis ever configured with a leading__Host-, secure requests will look for a double-prefixed cookie (__Host-__Host-…) and non-secure requests will look for a__Host-…cookie that browsers may reject. Normalize to an unprefixed base name (as insaveToken()) before applying__Host-based on request security.
boolean isSecure = secure || "https".equals(request.getScheme());
String expectedCookieName = isSecure ? "__Host-" + getParameterName() : getParameterName();
server/src/test/java/org/cloudfoundry/identity/uaa/web/CookieBasedCsrfTokenRepositoryTests.java:172
- The changes introduce different cookie names and paths when the request is secure (
__Host-…withPath=/). The current tests only assert the Secure/HttpOnly flags and don’t verify that theSet-Cookieheader actually uses the expected__Host-name andPath=/(or that non-secure requests keep the unprefixed name and context-path-based Path). Adding explicit assertions for these new behaviors would prevent regressions.
boolean expectSecure = isSecure || "https".equals(protocol);
String expectedCookieName = expectSecure ? "__Host-X-Uaa-Csrf" : "X-Uaa-Csrf";
Comment on lines
+117
to
+127
| boolean isSecure = secure || "https".equals(request.getScheme()); | ||
| String cookieName = isSecure ? "__Host-" + token.getParameterName() : token.getParameterName(); | ||
|
|
||
| Cookie csrfCookie = new Cookie(cookieName, token.getToken()); | ||
| csrfCookie.setHttpOnly(true); | ||
| csrfCookie.setSecure(secure || "https".equals(request.getScheme())); | ||
| csrfCookie.setPath(ofNullable(request.getContextPath()).orElse("") + "/"); | ||
| csrfCookie.setSecure(isSecure); | ||
| if (cookieName.startsWith("__Host-")) { | ||
| csrfCookie.setPath("/"); | ||
| } else { | ||
| csrfCookie.setPath(ofNullable(request.getContextPath()).orElse("") + "/"); | ||
| } |
Comment on lines
+169
to
+180
| MockHttpServletResponse response = saveTokenAndReturnResponse(isSecure, protocol); | ||
| boolean expectSecure = isSecure || "https".equals(protocol); | ||
| String expectedCookieName = expectSecure ? "__Host-X-Uaa-Csrf" : "X-Uaa-Csrf"; | ||
|
|
||
| String setCookie = response.getHeader("Set-Cookie"); | ||
| if (setCookie != null && setCookie.contains(expectedCookieName + "=")) { | ||
| Cookie cookie = new Cookie(expectedCookieName, ""); | ||
| cookie.setSecure(setCookie.contains("Secure")); | ||
| cookie.setHttpOnly(setCookie.contains("HttpOnly")); | ||
| return cookie; | ||
| } | ||
| return response.getCookie(expectedCookieName); |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ticket: double-submit-csrf-cookie-without-host-prefix-samesite-none-session
Fix: Adjusted CookieBasedCsrfTokenRepository to inject the __Host- prefix on the cookie name, strict / path, and Secure attribute dynamically whenever the connection is secure.