Repository navigation
Block CSRF on [REDACTED SECRET] enable/disable + appointment confirm: POST-only, CSRF cookie token, SameSite=Lax (sfind-e783cad7) #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devin-ai-integration
wants to merge
1
commit into
master
Choose a base branch
from
devin/sfind-e783cad7-admin-enable-disable-post-csrf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
20 changes: 20 additions & 0 deletions
20
UserFront/src/main/java/com/userFront/config/SameSiteCookieConfig.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.userFront.config; | ||
|
|
||
| import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; | ||
| import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class SameSiteCookieConfig { | ||
|
|
||
| @Bean | ||
| public EmbeddedServletContainerCustomizer sameSiteCookieCustomizer() { | ||
| return container -> { | ||
| if (container instanceof TomcatEmbeddedServletContainerFactory) { | ||
| ((TomcatEmbeddedServletContainerFactory) container).addContextCustomizers( | ||
| context -> context.setCookieProcessor(new SameSiteCookieProcessor("Lax"))); | ||
| } | ||
| }; | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
UserFront/src/main/java/com/userFront/config/SameSiteCookieProcessor.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.userFront.config; | ||
|
|
||
| import java.nio.charset.Charset; | ||
| import java.util.Locale; | ||
|
|
||
| import javax.servlet.http.Cookie; | ||
|
|
||
| import org.apache.tomcat.util.http.CookieProcessor; | ||
| import org.apache.tomcat.util.http.MimeHeaders; | ||
| import org.apache.tomcat.util.http.Rfc6265CookieProcessor; | ||
| import org.apache.tomcat.util.http.ServerCookies; | ||
|
|
||
| /** | ||
| * Adds a SameSite attribute to every cookie Tomcat writes (JSESSIONID, remember-me, XSRF-TOKEN). | ||
| * The bundled Tomcat predates Rfc6265CookieProcessor#setSameSiteCookies, so the header is appended here. | ||
| */ | ||
| public class SameSiteCookieProcessor implements CookieProcessor { | ||
|
|
||
| private final CookieProcessor delegate; | ||
| private final String sameSite; | ||
|
|
||
| public SameSiteCookieProcessor(String sameSite) { | ||
| this(new Rfc6265CookieProcessor(), sameSite); | ||
| } | ||
|
|
||
| SameSiteCookieProcessor(CookieProcessor delegate, String sameSite) { | ||
| this.delegate = delegate; | ||
| this.sameSite = sameSite; | ||
| } | ||
|
|
||
| @Override | ||
| public void parseCookieHeader(MimeHeaders headers, ServerCookies serverCookies) { | ||
| delegate.parseCookieHeader(headers, serverCookies); | ||
| } | ||
|
|
||
| @Override | ||
| public String generateHeader(Cookie cookie) { | ||
| String header = delegate.generateHeader(cookie); | ||
| if (header.toLowerCase(Locale.ROOT).contains("; samesite=")) { | ||
| return header; | ||
| } | ||
| return header + "; SameSite=" + sameSite; | ||
| } | ||
|
|
||
| @Override | ||
| public Charset getCharset() { | ||
| return delegate.getCharset(); | ||
| } | ||
| } |
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
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
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
18 changes: 18 additions & 0 deletions
18
UserFront/src/main/java/com/userFront/resource/CsrfResource.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.userFront.resource; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestMethod; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| /** | ||
| * Lets the AdminPortal SPA obtain the XSRF-TOKEN cookie (issued by the CSRF filter) before its first POST. | ||
| */ | ||
| @RestController | ||
| public class CsrfResource { | ||
|
|
||
| @RequestMapping(value = "/api/csrf", method = RequestMethod.GET) | ||
| public ResponseEntity<Void> csrf() { | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
| } |
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
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
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
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟥 Localhost pages can forge administrator actions
When an administrator visits another localhost page,
XSRF-TOKENremains readable across ports. That page can send credentialed POSTs with the token and change customer accounts or appointments.Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed that this is possible, but I don't think it's a regression this PR introduces. Here's my analysis.
Why it happens
localhost:*can readdocument.cookie, including the non-HttpOnlyXSRF-TOKEN.localhost:3000andlocalhost:8080count as the same site, soSameSite=Laxstill sends the session cookie on that page's POSTs.X-XSRF-TOKEN) from that page fails CORS preflight, becauseRequestFilteronly allowshttp://localhost:4200. A plain form POST with_csrf=<token>doesn't need preflight, so it would get through.Why I'm treating it as residual risk
master, any website anywhere can do this with a single GET.CookieCsrfTokenRepository.withHttpOnlyFalse()specifically. This is also Spring's standard SPA pattern, where the token is readable by design.localhost:4200→localhost:8080URLs are the project's dev layout. A real deployment would put the portal and API on their own origins.Option if you want this closed too
Store the token server-side (
HttpSessionCsrfTokenRepository). Return it fromGET /api/csrfin a response header that only the allowed CORS origin can read. Have AdminPortal attachX-XSRF-TOKENto each POST itself, instead of relying on Angular's cookie strategy.That changes the approach the finding asked for, so I've left it for the requester to decide rather than changing it in this PR.
Written by Devin