Potential fix for code scanning alert no. 7: Disabled Spring CSRF protection#1487
Draft
lydiaralphgov wants to merge 1 commit into
Draft
Potential fix for code scanning alert no. 7: Disabled Spring CSRF protection#1487lydiaralphgov wants to merge 1 commit into
lydiaralphgov wants to merge 1 commit into
Conversation
See https://github.com/hmcts/pre-api/security/code-scanning/7 for details Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
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.
JIRA ticket(s)
Potential fix for https://github.com/hmcts/pre-api/security/code-scanning/7
In general, the fix is to stop globally disabling Spring Security’s CSRF protection and, if some endpoints truly need to be accessible without CSRF tokens (e.g. for non‑browser clients), explicitly configure CSRF to ignore only those specific paths while leaving protection enabled for everything else.
The best fix here, without changing existing functionality more than necessary, is:
.csrf(AbstractHttpConfigurer::disable)so that CSRF protection is not turned off globally./testing-support/**,/v3/api-docs/**, health/info/metrics URIs, batch processing, etc.) by usingcsrf.ignoringRequestMatchers(...).This preserves the current behavior for clearly non‑browser / operational endpoints while enabling CSRF protection for any remaining state‑changing browser‑accessible routes.
Concretely, in
SecurityConfig.filterChain(HttpSecurity http)insrc/main/java/uk/gov/hmcts/reform/preapi/config/SecurityConfig.java, replace.csrf(AbstractHttpConfigurer::disable)with a lambda that configuresignoringRequestMatchersusing patterns consistent with the already defined permit‑all URIs (and POST batch URIs). No new imports are required because we already useHttpMethodand path strings;ignoringRequestMatcherscan take those as ant‑style patterns directly.Suggested fixes powered by Copilot Autofix. Review carefully before merging.