spring-projects / spring-projects/spring-security

PathPatternRequestMatcher caching leads to unexpected behavior when forwarding

Open
#17,203 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: feedback-provided type: bug
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Describe the bug

Spring Security version 6.5.0 release.

Since the AntPathMatcher is deprecated, I replaced it with PathPatternRequestMatcher expecting that it would behave the same. However I discovered that the PathPatternRequestMatcher caches the parsed RequestPath in a request attribute (see org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher#getPathContainer). This leads to unexpected behavior when forwarding.

For example if you make a request to /path-a then call request.getRequestDispatcher("/path-b").forward(request, response) then a PathPatternRequestMatcher will never match on /path-b (assuming that /path-a was cached in the request, in my case by a filter earlier in the chain which uses a PathPatternRequestMatcher).

To Reproduce

  • Make request to /path-a
  • Use any PathPatternRequestMatcher on the request e.g. PathPatternRequestMatcher.withDefaults().matcher("/path-a").matches(request)
  • Forward the request request.getRequestDispatcher("/path-b").forward(request, response)
  • After forwarding PathPatternRequestMatcher.withDefaults().matcher("/path-b").matches(request) will return false

Expected behavior

The PathPatternRequestMatcher should behave the same as the AntPathMatcher which it is replacing and always match on the jakarta.servlet.http.HttpServletRequest#getRequestURI.

Sample

Can supply if required.

Screenshot

Screenshot illustrating the issue

Image

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 org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher#getPathContainer and reproduce the issue by matching /path-a, forwarding to /path-b, and matching again. Trace how the cached RequestPath is used across the forward; done means the matcher evaluates the forwarded request correctly, with regression coverage for this sequence.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.