fix(security): route non-CSRF access denials to a generic 403 handler - #242
Open
detail-app[bot] wants to merge 1 commit into
Open
Conversation
SecurityConfig wired CsrfAccessDeniedHandler as the catch-all AccessDeniedHandler, so every authenticated non-CSRF AccessDeniedException (including controller-thrown denials) was serialized as a false "CSRF token missing or invalid" 403. Install a DelegatingAccessDeniedHandler that routes MissingCsrfTokenException/InvalidCsrfTokenException to the CSRF handler and all other denials to a new GenericAccessDeniedHandler emitting a generic "Access denied." JSON 403, so non-CSRF failures no longer claim a CSRF failure while CSRF messaging is preserved.
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.
Detail bug report: View on Detail
Closes #229
Bug
SecurityConfig.appSecurityFilterChainwiredCsrfAccessDeniedHandleras the single, catch-allAccessDeniedHandlerfor the whole app filter chain. Spring'sExceptionTranslationFiltertherefore routed every authenticatedAccessDeniedExceptionto that CSRF-specific handler, which always emits{"message":"CSRF token missing or invalid. Refresh the page and retry the request."}with HTTP 403 — including non-CSRF denials thrown from controllers (e.g.AuthenticatedUserController#revokeCurrentApiKeythrowsAccessDeniedException("API key identity is required for revocation")for any caller that isn't aClerkApiKeyAuthenticationToken). The real authorization reason was discarded and the caller was falsely told the CSRF token was invalid.Fix
Installed Spring's
DelegatingAccessDeniedHandlerinSecurityConfigas the app-wideAccessDeniedHandler:MissingCsrfTokenException/InvalidCsrfTokenException→CsrfAccessDeniedHandler(CSRF messaging preserved).AccessDeniedException→ newGenericAccessDeniedHandler, which emits a generic JSON 403{"status":"error","message":"Access denied.","details":null}and never claims a CSRF failure or leaks the internal exception text.CsrfAccessDeniedHandleris unchanged — only the wiring is corrected, so it no longer receives non-CSRF denials. The fix matches the bug report's recommended approach and uses Spring's built-inDelegatingAccessDeniedHandlerrather than a hand-rolled dispatcher.Testing
RevokeApiKeyAuthIntegrationTest(Spring Boot@SpringBootTestwith the dev-profile security chain) pins the three routing branches at the HTTP boundary: authenticated Clerk-JWT + valid CSRF → 403Access denied.(the bug fix, body contains noCSRF); anonymous + no CSRF → 403 CSRF message; anonymous + valid CSRF → 401 from the entry point.GenericAccessDeniedHandlerTestpins the handler's contract: generic 403 body, no CSRF-string leak, no internal exception-message leak, committed-response guard, null-mapper fast-fail.SecurityConfigTest(missing/mismatched/single-sided CSRF tokens, non-ak_Bearer still CSRF-gated) and the endpoint auth paths byAuthenticatedUserEndpointSecurityIntegrationTest(ak_...204, Clerk-outage 503, anonymous 401) — both unchanged and passing.make test(1,169 unit tests, 0 failures/errors),make build,make lint(Spotless/Palantir, SpotBugs, PMD, oxlint/eslint/svelte-check, ast-grep), andgit diff --check.JwtAuthenticationTokenvia the repo's standardjwt()post-processor. Theak_...204 happy path likewise could not run live (no realak_...key /CLERK_SECRET_KEY); a fake key returns 503 withClerkApiKeyVerifier requires CLERK_SECRET_KEY, and the success/outage paths are covered by the existing endpoint-security integration test.Automatic Fixes PRs can be configured here.