spring-projects / spring-projects/spring-security

WebTestUtils mutates a final SecurityContextHolderFilter field

Open
#19,520 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Describe the bug

WebTestUtils.setSecurityContextRepository replaces SecurityContextHolderFilter.securityContextRepository through ReflectionTestUtils.setField. That field is final. On current JDKs this emits a restricted final-field mutation warning, and the JDK warning states that this mutation will be blocked in a future release.

WARNING: Final field securityContextRepository in class
org.springframework.security.web.context.SecurityContextHolderFilter has been mutated reflectively by
org.springframework.util.ReflectionUtils
WARNING: Mutating final fields will be blocked in a future release unless final field mutation is enabled

To reproduce

  1. Build a Spring Boot MVC application on JDK 26 with spring-security-test.
  2. Configure MockMvc with SecurityMockMvcConfigurers.springSecurity().
  3. Execute a request that uses a test security context.

The warning is emitted from WebTestUtils.setSecurityContextRepository. The existing WebTestUtilsTests.setSecurityContextRepositoryWhenSecurityContextHolderFilter also exercises this reflective write.

Expected behavior

Spring Security test support should replace the repository through a supported API rather than mutating a final field reflectively.

Proposed solution

Add a null-checked setSecurityContextRepository method to SecurityContextHolderFilter, make the backing field non-final, and have WebTestUtils call the setter. Keep reflective reads unchanged to avoid broadening the production API further. Add/adjust tests for the setter and utility path.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with SecurityContextHolderFilter and WebTestUtils.setSecurityContextRepository, then read WebTestUtilsTests.setSecurityContextRepositoryWhenSecurityContextHolderFilter. Check the existing repository access and test setup first. Done means the utility path no longer reflectively mutates the final field, the supported setter is covered, and the relevant tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
security, testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.