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

[5.7] MethodNotAllowedHTTPException on Intended Redirect #25685

Closed
wants to merge 9 commits into from
Closed

[5.7] MethodNotAllowedHTTPException on Intended Redirect #25685

wants to merge 9 commits into from

Conversation

mcordingley
Copy link
Contributor

Environment:

  • Laravel Version: 5.5.40
  • PHP Version: 7.0
  • Database Driver & Version: N/A

Description:

redirect()->intended() can redirect the user to a bad route if the user interacts with a form after session timeout. This leads to errors being shown to a user from reasonable use of the application and is surprisingly difficult to avoid or work around.

Steps To Reproduce:

  • Have an expired session or no session.
  • POST to a route protected by the 'auth' middleware that does not have a corresponding GET route.
    • Easy way to do this is to login, navigate to a form, wait a while, then submit the form.
  • Authenticate and be redirected to the POST route.
  • Receive MethodNotAllowedHTTPException.

Proposed Solution:

Alter \Illuminate\Routing\Redirector::guest() to check if the current request is suitable for redirection and if the requested route isn't suitable for redirect, don't store it as url.intended and instead store the "previous" URL as the intended route to get the user back to being as close as possible to the failed request.

@ankurk91
Copy link
Contributor

Is there any way to exclude some URLs from intended behavior?

@mcordingley
Copy link
Contributor Author

@ankurk91 Not that I see. However, this issue affects all routes that do not accept GET requests so a blacklist wouldn't help even if we could specify one.

@mcordingley mcordingley changed the title [WIP] MethodNotAllowedHTTPException on Intended Redirect [5.7] MethodNotAllowedHTTPException on Intended Redirect Sep 18, 2018
$this->session->put('url.intended', $this->generator->full());
$request = $this->generator->getRequest();

$intended = $request->method() === 'GET' && $request->route() && ! $request->ajax() ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity - why is ajax specifically excluded?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we don't want to redirect the user to an AJAX route. We want to only redirect someplace that can be meaningfully loaded up in the browser.

See also Illuminate\Session\Middleware\StartSession::storeCurrentUrl().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unlikely --but possible-- scenario is that the user has an expired session, makes an AJAX request, then manually goes over to the login page to log back in. In that case, the AJAX request could have gotten stored as the intended route, since there'd be nothing to overwrite the value in session.

@taylorotwell
Copy link
Member

What if the previous route isn't valid either?

@mcordingley
Copy link
Contributor Author

@taylorotwell It has to be, else it wouldn't have been placed into the session for previous to fetch. Again, see Illuminate\Session\Middleware\StartSession::storeCurrentUrl() where that gets set. And assuming that there's nothing even in there, it'll return a null. That'll have the intended redirect then go to the default location.

@mcordingley
Copy link
Contributor Author

Note that I added one more commit to try to bullet-proof this change. With the PR closed, that commit does not appear to show up in here.

@X-Coder264
Copy link
Contributor

X-Coder264 commented Sep 22, 2018

@mcordingley Please create a new PR then since Taylor does not read PR comments after he already closed it.

@mcordingley
Copy link
Contributor Author

mcordingley commented Sep 22, 2018

@X-Coder264 I created a bug report over this. It got ignored for a month and then closed. I then created this PR. It immediately got closed. In both cases, it seemed that the people closing them didn't even bother to read what I had written. What's to say that opening another one won't simply get closed without being read?

@mcordingley
Copy link
Contributor Author

Reopened as #25739

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.

None yet

5 participants