Problem/Motivation

Drupal\redirect\RedirectRepository::findMatchingRedirect() keeps a per-instance $foundRedirects array to detect redirect loops. Two early-return paths
in the method do not reset this array, so its state leaks into subsequent calls within the same request. The next call for the same source path then
encounters its own rid in $foundRedirects and throws a RedirectLoopException, even though no real loop exists.

The leaky paths are:

1. The prefixList short-circuit added in 1.12 (currently lines 107–109):

  if ($this->prefixList && !$this->prefixList->hasRedirectsWithPrefix($source_path)) {
    return NULL; // does not reset $foundRedirects                                                                                                       
  }   

2. The Trash/missing-entity guard (currently lines 142–144):

  if (!$redirect instanceof Redirect) {                                                                                                                  
    return NULL; // does not reset $foundRedirects                                                                                                       
  }       

The reset does happen on the no-match path (line 161) and on the chained-redirect path (line 153), so the missing resets are clearly oversights.

Steps to reproduce

1. Create a redirect (rid=1945) with source info/team and a destination that is an absolute URL on the same site (e.g.
https://example.com/info/team-new) rather than internal:/info/team-new. (Editors entering the destination as a full URL is common.)
2. Within a single request, call findMatchingRedirect('info/team') twice (e.g. resolved alias lookups during render, or any code that calls the service
more than once for the same path).

What happens internally on the first call:
- prefixList matches → continues.
- Hash matches rid=1945 → $foundRedirects = [1945].
- findByRedirect() reconstructs the destination path. Because the destination is absolute, $path = ltrim(substr($generated_url, strlen($base_url)),
'/') yields a string starting with https: rather than the trimmed path.
- The recursive findMatchingRedirect('https:/example.com/info/team-new') enters the prefix-list short-circuit and returns NULL without resetting
$foundRedirects.
- Outer call returns the redirect — $foundRedirects still contains [1945].

On the second call for the same source path:
- Hash matches rid=1945 again.
- in_array(1945, [1945]) is true → throws RedirectLoopException.

In our case the second call comes from a custom AliasManager::getPathByAlias() override that calls findMatchingRedirect() to detect whether a path has
a redirect before continuing alias logic. Because getPathByAlias() is called many times during page rendering, the spurious exception occurs frequently
and propagates as an Uncaught PHP Exception in production logs:

Uncaught PHP Exception Drupal\redirect\Exception\RedirectLoopException:
"Redirect loop identified at /info/team for redirect 1945"
at .../redirect/src/RedirectRepository.php line 137

Proposed resolution

Reset $this->foundRedirects = [] in both early-return branches. Patch attached. Two-line change.

--- a/src/RedirectRepository.php                                                                                                                       
  +++ b/src/RedirectRepository.php                                                                                                                       
  @@ -105,6 +105,7 @@
       // that don't have any redirects on common prefixes such as /node or                                                                              
       // /admin.                                                                                                                                        
       if ($this->prefixList && !$this->prefixList->hasRedirectsWithPrefix($source_path)) {                                                              
  +      $this->foundRedirects = [];                                                                                                                     
         return NULL;                                    
       }                                                                                                                                                 
                                                         
  @@ -140,6 +141,7 @@
         // The redirect could be found by the direct db query in ::findByHash(),
         // but other modules (e.g. Trash) might prevent it from being loaded.                                                                           
         if (!$redirect instanceof Redirect) {                                                                                                           
  +        $this->foundRedirects = [];                                                                                                                   
           return NULL;                                                                                                                                  
         }                                               
         $this->foundRedirects[] = $rid; 

Remaining tasks

- Review patch
- Add a kernel test that calls findMatchingRedirect() twice for a source path whose destination is an absolute external URL, asserts no exception is
thrown.

Issue fork redirect-3587786

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

hammerslammer created an issue. See original summary.

sourav_paul’s picture

checking...

sourav_paul’s picture

I tested this locally and was able to reproduce the issue.

To verify it, I created a redirect with a normal source path and used a full same-site URL as the destination.

Before applying the patch:
- the first lookup worked
- the second lookup threw a `RedirectLoopException`
- but there was no actual redirect loop

The proposed fix solves that by resetting `$foundRedirects` in those missing early return paths. After applying the patch locally, I tested the same scenario again and both lookups worked correctly without any exception.

I also added a kernel test for this case and it passes locally. The full `RedirectAPITest` class also still passes here.

So from local testing, the proposed fix looks correct and fixes the reported problem.

Thanks..

[Took help from AI, to create Test]

sourav_paul’s picture

Status: Active » Needs review
jagraj_singh_gill’s picture

Assigned: Unassigned » jagraj_singh_gill
jagraj_singh_gill’s picture

Assigned: jagraj_singh_gill » Unassigned
Status: Needs review » Reviewed & tested by the community

This issue came after the update to version 8.x-1.13. I was getting this error on screen : Service unavailable and in logs I was getting this error : Redirect loop identified at /abc for redirect 1234
After applying this patch, things started working correctly. So updating the status.

claudiu.cristea’s picture

We hit this in a test of a module that has a dev dependency on Redirect. The test started to fail after updated to 8.x-1.13, see https://git.drupalcode.org/issue/track_usage-3565462/-/jobs/10663428.

I've run the "test-only changes" and that tests are failing, proving the bug. Then applied the patch to our module and that fixed the test failure. The bug is proved and evident, the MR looks good, it has tests, RTBC+1

claudiu.cristea’s picture

Issue tags: +Regression
nicross-com’s picture

We encountered this recently and solved it by creating a custom service that simply extends the redirect repository so they don't share the same in-memory cache. Glad there's a working patch now!

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

  • berdir committed 134ee9fa on 8.x-1.x authored by sourav_paul
    fix: #3587786 RedirectRepository::findMatchingRedirect leaks $...
berdir’s picture

Status: Reviewed & tested by the community » Fixed

Merged, thanks.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.