Skip to content

SONARJAVA-6615 S4502 should not raise when the app is stateless with token-based auth - #6511

Merged
Luqmansonar merged 5 commits into
masterfrom
fix/luqmansonar/SONARJAVA-6615-statelessResourceServer
Nov 12, 2026
Merged

Luqmansonar merged 5 commits into
masterfrom
fix/luqmansonar/SONARJAVA-6615-statelessResourceServer

Conversation

@Luqmansonar

Copy link
Copy Markdown
Contributor

What

S4502 (SpringSecurityDisableCSRFCheck) no longer raises on a SecurityFilterChain bean that fully disables CSRF when the same HttpSecurity builder also:

  1. sets SessionCreationPolicy.STATELESS,
  2. authenticates with oauth2ResourceServer(...), and
  3. has no enabled formLogin(), httpBasic(), rememberMe() or oauth2Login().

Full-disable forms that can be suppressed: .disable() (member select or customizer lambda), AbstractHttpConfigurer::disable, and requireCsrfProtectionMatcher(request -> false).

Decisions to review

  • Disabled browser auth counts as "not configured". httpBasic(AbstractHttpConfigurer::disable), formLogin(f -> f.disable()) and rememberMe().disable() do not block the exemption. This is broader than the ticket's literal wording ("does not configure"), chosen because it is a very common shape in stateless JWT configs. Enabled forms (httpBasic(withDefaults()), httpBasic(b -> b.realmName(..)), no-arg httpBasic()) still raise.
  • Conservative chain visibility. The exemption only applies when the whole chain is visible. The method must be a @Bean returning SecurityFilterChain, with a single HttpSecurity parameter, a single return of that builder, and only expression or return statements at top level. http must only be used as a call receiver, and the session and resource-server customizers must be lambdas or withDefaults(). Anything else keeps raising, for example a builder passed to a helper, an aliased builder, conditional or deferred sessionCreationPolicy, a later policy override, a disabled resource server, or getSharedObject. One known consequence: a local variable declaration in the bean method keeps the issue raised.

Out of scope (still raises)

  • HTTP Basic, form login, OAuth2 login or remember-me enabled on the chain.
  • Partial exemptions: ignoringRequestMatchers(...), ignoringAntMatchers(...), requireCsrfProtectionMatcher(<anything other than r -> false>).
  • Custom authentication filters without oauth2ResourceServer (follow-up per ticket).
  • WebSecurityConfigurerAdapter.configure(HttpSecurity).

Open question for rule owners (from the ticket, not blocking)

Several reviewers resolved this STATELESS + JWT shape as WONTFIX to keep it visible rather than suppressed. Please confirm that dropping the finding entirely for this shape is the intended outcome.

Tests

SpringSecurityDisableCSRFCheckSample covers each Definition of Done case. Each must-still-raise case differs from a compliant base in exactly one condition, so a case cannot pass just because a base condition failed to resolve. test_without_semantic still expects no issues.

Locally (JDK 21):

  • SpringSecurityDisableCSRFCheckTest: 2/2 tests pass.
  • Reproducer commit against the original check: fails on exactly the 8 new compliant lines, with no expected issue missing.
  • The sample compiles against the java-checks-test-sources/default classpath (Spring Security config 5.8.11) with no new dependencies.

Not verified locally: a full build of java-checks-test-sources (it needs JDK 26), the ruling results (mall has S4502 expectations; any removed findings need a check that they are real stateless resource-server chains), and the autoscan IT.

🤖 Generated with Claude Code

Luqmansonar and others added 2 commits November 12, 2026 09:15
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

SONARJAVA-6615

Luqmansonar and others added 2 commits November 12, 2026 09:33
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Luqmansonar
Luqmansonar marked this pull request as ready for review November 12, 2026 09:48

@aurelien-coet-sonarsource aurelien-coet-sonarsource left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just a tiny nit, but feel free to ignore.

return http.build();
}

org.springframework.security.config.Customizer<org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer<HttpSecurity>> resourceServerCustomizer() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: is there a reason to use the fully qualified names here ?

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Luqmansonar
Luqmansonar enabled auto-merge (squash) November 12, 2026 14:23
@gitar-bot

gitar-bot Bot commented Nov 12, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 🟡 Medium risk 1 closed / 1 findings

Expanding S4502 suppression for stateless OAuth2 resource-server chains could hide CSRF findings when the analyzer misidentifies chain configuration.

Exempts stateless OAuth2 resource-server SecurityFilterChain beans from S4502 when CSRF is fully disabled and no browser auth (form login, HTTP Basic, OAuth2 login, remember-me) is enabled. The browser-auth denylist now also covers x509 and JEE, so those chains stay flagged. No open issues remain.

✅ 1 closed
✅ Edge Case: Browser-auth denylist misses x509/jee, so these chains are still exempted

📄 java-checks/src/main/java/org/sonar/java/checks/spring/SpringSecurityDisableCSRFCheck.java:70-71 📄 java-checks/src/main/java/org/sonar/java/checks/spring/SpringSecurityDisableCSRFCheck.java:162
BROWSER_AUTHENTICATION only lists httpBasic, formLogin, rememberMe and oauth2Login. The PR keeps raising on httpBasic because the browser sends those credentials automatically, which is exactly the case CSRF protection exists for. http.x509() (TLS client certificate, re-authenticated on every request by X509AuthenticationFilter) and http.jee() (container pre-authentication) are ambient credentials in the same way, and STATELESS does nothing to stop them.

Trigger: a bean that does csrf(AbstractHttpConfigurer::disable), sessionManagement(s -> s.sessionCreationPolicy(STATELESS)), oauth2ResourceServer(o -> o.jwt()) and x509(withDefaults()) gets past every check in isStatelessResourceServer, so S4502 stays silent. Yet a cross-site request from a browser that holds the client certificate is authenticated. Fix: add these mechanisms to the matcher, along with saml2Login, which also logs in through the browser. Then add a Noncompliant sample like statelessResourceServerBasic that uses x509().

Verification Rules 2/2 met Repos 5 inspected · PRs 2 inspected

Functional validation No results

Rules 2 passed 2 not applicable

✅ Internal artifact registries
✅ Repository license compliance

Broader impact Repos 5 inspected PRs 2 inspected
Cross-repo 5 repositories inspected 0 open impacts

SonarSource/sonar-java-symbolic-execution Inspected
SonarSource/sonarlint-eclipse Inspected
SonarSource/sonarlint-intellij Inspected
SonarSource/sonarqube Inspected
SonarSource/sonarlint-vscode Inspected

Cross-PR 2 PRs inspected 0 open impacts

PR 6485: SONARJAVA-7300 Decouple the Sonar Java frontend from Eclipse JDT core Inspected
PR 6503: Simplify the parser interface Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

Copy link
Copy Markdown
Contributor

@Luqmansonar
Luqmansonar merged commit b114138 into master Nov 12, 2026
18 checks passed
@Luqmansonar
Luqmansonar deleted the fix/luqmansonar/SONARJAVA-6615-statelessResourceServer branch November 12, 2026 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants