Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AntPathMatcher matches path with trailing slash differently if '**' is present in the pattern #27506

Closed
aomader opened this issue Oct 1, 2021 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@aomader
Copy link

aomader commented Oct 1, 2021

I'm curious why AntPathMatcher matches a path with a trailing slash differently if the pattern contains a ** pattern. See the following example:

AntPathMatcher matcher = new AntPathMatcher();

matcher.match("/en", "/en/") == false // does not match
matcher.match("/*/en", "/en/foo/") == false // does not match
matcher.match("/**/foo", "/en/foo/") == true // does match

Could someone enlighten me why AntPathMatcher behaves this way?

@aomader aomader changed the title AntPathMatcher differently matches path with trailing slash if '**' is present in the pattern AntPathMatcher matches path with trailing slash differently if '**' is present in the pattern Oct 1, 2021
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 1, 2021
@bclozel
Copy link
Member

bclozel commented Oct 1, 2021

Is there a typo in your code snippet?

// does not match because the pattern doesn't have a trailing slash and the path has one
matcher.match("/en", "/en/")
// does not match because the pattern ends with "/en" and the path ends with "/foo/"
matcher.match("/*/en", "/en/foo/")
// this is problematic
matcher.match("/**/foo", "/en/foo/") == true // does match

I've tested the following combinations and it looks like there is an inconsistency between * and ** when it comes to matching trailing slashes.

assertThat(pathMatcher.match("/*/foo", "/en/foo")).isTrue();
assertThat(pathMatcher.match("/*/foo", "/en/foo/")).isFalse();
assertThat(pathMatcher.match("/**/foo", "/en/foo")).isTrue();
assertThat(pathMatcher.match("/**/foo", "/en/foo/")).isFalse(); // fails

I've found a fix for this issue that doesn't break any test in our test suite, but AntPathMatcher has a long history of subtle behavior and lots of people relying on it.

Using ** within patterns and trailing slashes are very likely in web applications using AntPathMatcher. There is a trailing slash matching option in Spring MVC but applications might still rely on this behavior, so I'm not tempted to fix this in the 5.3.x branch.

On the other hand, I think that using AntPathMatcher as a matcher for request patterns in Spring MVC might be retired in Spring Framework 6.0. In the 6.0.x timeline we can consider several options for AntPathMatcher:

  1. keeping it around as it is and fixing small issues like this one
  2. reworking it to only target file patterns use cases (and not URL path patterns which is now PathPatternParser's job)
  3. or retiring it completely

In any case, this needs to be discussed within the Spring Framework team.

Did you notice this issue in a Spring MVC application while debugging the problem, or AntPathMatcher for some other use case?

@bclozel bclozel self-assigned this Oct 1, 2021
@bclozel bclozel added the type: bug A general bug label Oct 1, 2021
@aomader
Copy link
Author

aomader commented Oct 5, 2021

Regarding your first question, no, I cannot see a typo. The last case is obviously the problematic one and the reason I created this issue.

Exactly, I came across this "subtle behavior" when working with Spring MVC and Spring Security. The latter I find somewhat problematic with regards to "sublte behavior"...

@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) in: core Issues in core modules (aop, beans, core, context, expression) labels Nov 10, 2021
@bclozel bclozel removed for: team-attention status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 23, 2021
@bclozel bclozel added this to the 6.0.x milestone Nov 23, 2021
@bclozel bclozel modified the milestones: 6.0.x, 6.0.0-M4 Apr 4, 2022
@bclozel bclozel closed this as completed in 705bf78 Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants