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

[7.x] Fix double slashes matching in UriValidator (fix inconsistencies between cached and none cached routes) #32260

Merged
merged 2 commits into from
Apr 7, 2020
Merged

Conversation

BafS
Copy link
Contributor

@BafS BafS commented Apr 6, 2020

Sorry to open a new PR but the other one was closed, I think there were some misunderstanding.
The goal of this PR is to not match routes when we have double slashes and also fix the misleading 405 error with cached route collection when we have a double slash.

Before the PR

URL Not cached Cached
http://127.0.0.1/foo foo foo
http://127.0.0.1/foo/bar bar bar
http://127.0.0.1//foo ⚠️ foo 405
http://127.0.0.1//foo/bar ⚠️ bar 405
http://127.0.0.1/foo//bar 404 404
http://127.0.0.1//foo/bar 404 404

With this PR

URL Not cached Cached
http://127.0.0.1/foo foo foo
http://127.0.0.1/foo/bar bar bar
http://127.0.0.1//foo 404 404
http://127.0.0.1//foo/bar 404 404
http://127.0.0.1/foo//bar 404 404
http://127.0.0.1//foo/bar 404 404

The problem is that UriValidator uses $request->path() which use the trimmed path but CompiledRouteCollection uses:

    $parts = explode('?', $request->server->get('REQUEST_URI'), 2);

    $trimmedRequest->server->set(
        'REQUEST_URI', rtrim($parts[0], '/').(isset($parts[1]) ? '?'.$parts[1] : '')
    );

with rtrim, which creates the discrepancy.

What do you think ? Please let me know if you want me to fix it differently, I just want to fix this inconsistency.

@danilopinotti
Copy link
Contributor

Thoughts about sanitize route before matching?

@GrahamCampbell
Copy link
Member

Thoughts about sanitize route before matching?

Why? We want routing to be as fast as possible.

@GrahamCampbell
Copy link
Member

And what did you mean exactly be sanitizing?

@GrahamCampbell
Copy link
Member

@BafS could you add a couple of new tests please?

@danilopinotti
Copy link
Contributor

Why? We want routing to be as fast as possible.
And what did you mean exactly be sanitizing?

Maybe 'sanitize' is the wrong word.
What I meant was removing duplicate slashes in the route before matching.

I saw this behavior in some sites (github, by example)
image

@BafS
Copy link
Contributor Author

BafS commented Apr 7, 2020

@danilopinotti You can do that with your server, I don't think it's Laravel's job to do it

@GrahamCampbell I added a test.

I think it would be good to run the tests in CompiledRouteCollectionTest against the cached and none cached routes to be sure we stay consistent but I didn't want to add too much refactoring here.

@taylorotwell taylorotwell merged commit 876ac39 into laravel:7.x Apr 7, 2020
@no0by5
Copy link

no0by5 commented Jun 15, 2020

This PR is actually breaking my application. I provide a calendar through my application, and for some reason the frontend returned an URL with a double slash for the calendar. So this URL is now used by many users to import the calendar, but since this was merged, these imports are no longer working. Is there a way to work around this? Adding a route with '//' is filtered out.

@driesvints
Copy link
Member

Urls with // aren't valid. There's no workaround for this.

@BafS
Copy link
Contributor Author

BafS commented Jun 15, 2020

You can workaround that with your server, by using url rewrite, but it's not a good practice

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.

6 participants