-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Fix url matcher edge cases with trailing slash #31240
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
Merged
Merged
Conversation
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
nicolas-grekas
requested changes
Apr 26, 2019
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks
be careful when addressing the comment, I rebased this on 4.2 so I push-forced on your fork.
src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherTrait.php
Outdated
Show resolved
Hide resolved
Status: Needs Review |
nicolas-grekas
approved these changes
Apr 27, 2019
Thank you @arjenm. |
nicolas-grekas
added a commit
that referenced
this pull request
Apr 27, 2019
This PR was squashed before being merged into the 4.2 branch (closes #31240). Discussion ---------- Fix url matcher edge cases with trailing slash | Q | A | ------------- | --- | Branch? | master / 4.2 (not sure whether this should've been against 4.2) | Bug fix? | yes | New feature? | no | BC breaks? | no (I think... if so, in obscure route configurations like the ones that broke in 4.2.7 ;) ) | Deprecations? | no | Tests pass? | yes | Fixed tickets | #30721 | License | MIT As stated in #30721 the "redirecting" (compiled) UrlMatcher ignored host-requirements when doing a 'matched url except for trailing slash difference'-redirect. With routes like this, you'd get 404's rather than 200's on the second routes. ```yaml host1.withTrail: path: /foo/ # host2/foo would become host2/foo/ due to this partial path-match host: host1 host2.withoutTrail: # A request for host2/foo should match this route path: /foo # host2/foo/ does not match this path host: host2 ``` This was caused by too eagerly testing whether a route could've worked with an additional trailing slash. If it could be, that would result in an attempt to redirect _before_ testing the host. The original url would get the additional slash and prior to actually redirecting, it'd be retested against the available routes. _That new url_ would actually match no routes, since now the host check for the first routes would actually be executed and fail the match for those routes. The adjusted path in the route does not match the second sets of routes... This PR moves the trailing-slash check to after the host checks. I've added several tests with variants on these edge cases. *Note:* This is a bug in 4.2 (and 4.3). I fixed it against master. It appears 4.2's PhpMatcherTrait was renamed to CompiledUrlMatcherTrait in 4.3. But that made it loose its history. So I'm not sure how to proceed from here. I can rebase it on 4.2 and do the change in PhpMatcherTrait, but that would probably fail to merge in master? I'm not nearly proficient enough in Symfony PR's to know how to solve all this ;) @nicolas-grekas also asked for these or similar tests to be added to 3.4 as 'proof' those routes worked in that version as well. I have not (yet) done so. The tests did work on the non-redirecting UrlMatcher without change (i.e. just running UrlMatcherTest), which implies - to me at least - the routes where properly defined and should not result in 404's simply because a RedirectableUrlMatcherInterface _can_ do redirects. Actually, since I needed to change UrlMatcher as well, I'm not convinced these bugs where totally absent in 3.4. They didn't occur with the compiled version, since the host check was the very first if-statement in that humongous if-tree it generated. PS, the branch name is a bit dramatic ;) Commits ------- 4fcfa9d Fix url matcher edge cases with trailing slash
Merged
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.
As stated in #30721 the "redirecting" (compiled) UrlMatcher ignored host-requirements when doing a 'matched url except for trailing slash difference'-redirect.
With routes like this, you'd get 404's rather than 200's on the second routes.
This was caused by too eagerly testing whether a route could've worked with an additional trailing slash.
If it could be, that would result in an attempt to redirect before testing the host.
The original url would get the additional slash and prior to actually redirecting, it'd be retested against the available routes. That new url would actually match no routes, since now the host check for the first routes would actually be executed and fail the match for those routes. The adjusted path in the route does not match the second sets of routes...
This PR moves the trailing-slash check to after the host checks. I've added several tests with variants on these edge cases.
Note: This is a bug in 4.2 (and 4.3).
I fixed it against master. It appears 4.2's PhpMatcherTrait was renamed to CompiledUrlMatcherTrait in 4.3. But that made it loose its history.
So I'm not sure how to proceed from here. I can rebase it on 4.2 and do the change in PhpMatcherTrait, but that would probably fail to merge in master? I'm not nearly proficient enough in Symfony PR's to know how to solve all this ;)
@nicolas-grekas also asked for these or similar tests to be added to 3.4 as 'proof' those routes worked in that version as well. I have not (yet) done so.
The tests did work on the non-redirecting UrlMatcher without change (i.e. just running UrlMatcherTest), which implies - to me at least - the routes where properly defined and should not result in 404's simply because a RedirectableUrlMatcherInterface can do redirects.
Actually, since I needed to change UrlMatcher as well, I'm not convinced these bugs where totally absent in 3.4. They didn't occur with the compiled version, since the host check was the very first if-statement in that humongous if-tree it generated.
PS, the branch name is a bit dramatic ;)