Problem/Motivation

Setting the 403 or 404 page to a page that causes a redirect will result in an endless redirect for authenticated users.

The desired result is that the user is redirected to the 403 or 404 page.

Steps to reproduce

  1. Go to /admin/config/system/site-information
  2. Set the 403 page to "/user"
  3. Logout
  4. Go to /admin
    • Success, this redirects to /user -> /user/login
  5. Login as a user without access to /admin
  6. Go to /admin
    • Actual Result: Browser will stop responding after several redirect attempts back to /admin
    • Desired Result: User ends up at /user/:uid
  1. Go to /admin/config/system/site-information
  2. Set the 404 page to "/search"
  3. Go to /foobar
    • Actual Result: Browser will stop responding after several redirect attempts back to /search
    • Desired Result: User ends up at /search/node

Proposed resolution

Fix it so that pages with redirects will work as the 403/404 page, or else add documentation to the page saying they will not work.

Remaining tasks

Test and patch.

User interface changes

403/404 pages will work with redirects.

API changes

None.

Data model changes

None.

Issue fork drupal-927570

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mertskeli’s picture

Priority: Normal » Minor

[edit]
example.com/search/node works.

Josh The Geek’s picture

Also, Search 404.
Not closing because /search usually goes to /search/node

tedfordgif’s picture

Status: Active » Needs review
StatusFileSize
new784 bytes

This happens because the page not found handler (drupal_deliver_html_page > MENU_NOT_FOUND) adds the original path to $_GET['destination']. The search module needs a module specified (search/x), so it redirects to search/node by default (see the search_view function). That redirect is accomplished via drupal_goto, which of course redirects to $_GET['destination'], rather than the intended search/node.

Here's a patch that removes $_GET['destination'] before the redirect in search_view. I think this is a reasonable approach, since we shouldn't need to worry about form submissions or other uses of 'destination'.

arvinsingla’s picture

Component: system.module » search.module
StatusFileSize
new2.4 KB

At DrupalCon Chicago, this is my first patch that I am testing. The patch applies cleanly and works fine with the latest D7. I have re-rolled this patch adding a simpletest to check for this use case.

jhodgdon’s picture

Status: Needs review » Needs work

The patch says at the end:

\ No newline at end of file

Aside from this very very minor problem, I think this is excellent work and should be immediately committed to d7 and d8. Can you please reroll with a newline at the end of the search.test file? Thanks!

jhodgdon’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7

I just tested and this is still a problem in Drupal 8.

We need this patch to be rerolled so that it will work in the current Drupal 8, and the test will need to be put into a different file. Then we can consider backporting to Drupal 7.

jhodgdon’s picture

Issue summary: View changes

I just tested this again in D8 with the patch to #1366020: Overhaul SearchQuery; make search redirects use GET query params for keywords applied.

Steps:
a) Go to admin/config/system/site-information
b) In the Error Pages section, enter "search" for both 403 and 404 pages.
c) Go to example.com/foo (a 404 page). The site endlessly tries to redirect, but it fails as described here.

So this is still a problem.

The solution is not so obvious. The redirect from /search to /search/(page URL) is set up in \Drupal\search\Routing\SearchPageRoutes::routes(), and then handled in \Drupal\search\Controller\SearchController::redirectSearchPage() now, with the default search page entity being passed in as a parameter. The D7 patch here will not work as it is, and I personally do not know how to make it work in D8.

However, as suggested in #1, if you make the 403/404 handler page not be /search but instead something like /search/node, you will not have this problem.

sandipmkhairnar’s picture

As per comment #5 reroll patch.

jhodgdon’s picture

Thanks! Note that the patch in #8 is for Drupal 7. For Drupal 8, we should wait until #1366020: Overhaul SearchQuery; make search redirects use GET query params for keywords is done as it is overhauling all the search redirection stuff (but as noted in #7, even with that patch this is still a bug).

jhodgdon’s picture

Title: Search page as the default 403 and 404 page goes to infinity » Setting 403 or 404 handler to a page that redirects leads to endless loop
Component: search.module » routing system

Discussed this in IRC today... This is not really a problem with the search module per se. It is a problem with entering anything as your 403/404 page that is actually a redirect to another page.

The path "search" redirects to search/node (or whatever your default search page is), so it is a special case of this problem.

So I'm moving this into the Routing component to see if the maintainers of that component think it's a bug we should address or a "won't fix" issue, or maybe that we should tell people to be careful not to put redirecting pages in as their 403/404 page handlers?

mfb’s picture

Interestingly I am also seeing endless redirects when trying to issue a redirect on a URL that happens to be a 403 or 404 page. I get the redirect meta tag as the body, and 403 or 404 as the status code. I may be doing it wrong (TM) - if I figure it out I'll update this comment :) UPDATE: My working code for redirecting from a possibly 403/404 page is the secureRedirect() method here: http://cgit.drupalcode.org/securelogin/tree/src/SecureLoginManager.php?h...

dawehner’s picture

So I'm moving this into the Routing component to see if the maintainers of that component think it's a bug we should address or a "won't fix" issue, or maybe that we should tell people to be careful not to put redirecting pages in as their 403/404 page handlers?

So do I understand it correctly is that the destination itself points to the same path as the 404 page? In that case we could prevent it and unset it.
@jhodgdon Do you have a better insight in what is going on here?

jhodgdon’s picture

Not quite.

The original bug reported here is that if you configure the 403 or 404 handler page to be path "search", that doesn't work -- you end up in an endless redirect loop. So for instance you try URL "example.com/foobar", which doesn't exist. The 404 handler says to use path "search", but in the process of trying to redirect to "search", the route handler says "Oh, actually redirect to search/node". The redirect doesn't work due to some technicalities in how redirects are done. See comment #3 above for more explanation of this (although the patch is very outdated compared to the current code). It looks like #11 is another case of this same problem not related to configuration of the 403/404 handler, by the way.

So for this issue the question is whether:
a) We should fix the bug and make it possible to enter a path that itself redirects for the 403/404 handler.
or
b) We should tell people not to enter paths that themselves redirect to other paths in the 403/404 handler configuration.

And incidentally I just tested this and it is still a problem in a D8 build from last week at least; I would assume nothing has changed since then.

sandipmkhairnar’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 8: search-redirect-loop-with-simpletest-927570-8.patch, failed testing.

tim.plunkett’s picture

Component: routing system » system.module

This code is actually in system.module

jhodgdon’s picture

Issue summary: View changes
Issue tags: +Needs tests

I just tested this in 8.0.x (dev branch). It is still not working right, but the behavior is different now in 8 and 7. Updated summary. Still a bug.

leetom’s picture

There is a very common need that we should check if user is logged in when showing 403 page.
But if you redirect the anonymous user to user/login page, it will end up with infinite loop.

Strange behaviour.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dravenk’s picture

Test with 8.5.0.
I set the Default 404 (not found) page like the follow.
/search

Access a nonexistent page it is redirects the home page.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

mradcliffe made their first commit to this issue’s fork.

mradcliffe’s picture

Issue summary: View changes
Issue tags: -Needs tests

I ran into this issue today on a site that had the 403 page set as /user.

As an anonymous user, going to a route without access redirects to /user/login
As an authenticated user, going to a route without access results in an endless redirect.

The test I added will probably timeout the test bot due to the endless redirect.

I opted to create a separate test class because AccessDeniedTest seemed kind of big, but it could move the test into AccessDeniedTest.

mradcliffe’s picture

And there we are.

BrowserTestBase really doesn't handle infinite redirects very well.


Testing Drupal\Tests\system\Functional\System\AccessDeniedAsAuthenticatedUserTest
E 1 / 1 (100%)unserialize(): Error at offset 7661404121 of 7661404160 bytes

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mstrelan’s picture

I tried to follow the steps to reproduce against 11.x but getting slightly different results. For a start, going to /admin as an anonymous user just shows an Access Denied message. That's fine, but doing the same after logging in as an authenticated user results in a redirect loop. Also not seeing the same thing for /foobar, I'm redirected to the front page instead. Can anyone confirm the same?

Re #33:

BrowserTestBase really doesn't handle infinite redirects very well

I think instead we can use the guzzle http client to make requests to the site. There is an option not to follow redirects, so we can simply inspect the headers. There is also a max redirects option, so we could use that too.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.