Skip to content

Stop redirects that keep leading to more redirects - #211

Merged
frederikvig merged 4 commits into
masterfrom
fix/redirect-loops
Nov 7, 2026
Merged

frederikvig merged 4 commits into
masterfrom
fix/redirect-loops

Conversation

@frederikvig

Copy link
Copy Markdown
Member

Closes #208, with one limit for permanent redirects described below.

The problem

Two rules pointing into each other (/a -> /b/c and /b -> /a/d), or a regex rule matching its own result (^/(.*)$ -> /en/$1), kept redirecting until the browser gave up. Visitors got the browser's "too many redirects" error rather than a 404, and nothing was logged.

The fix

RedirectLoopGuard counts the redirects that really happen. A new url is one hop further than the url redirecting to it, and a url reached after MaxRedirectHops (default 10) gets a normal 404 and a warning in the log. It stays that way for up to 30 seconds. Set the option to 0 to turn this off.

Why it counts real hops

Looking at the rules can't work, because these rules are fine when the new url exists. A page moved to /b and back leaves /a -> /b and /b -> /a behind. The page is at /a, so only /b is ever not found, and /b -> /a is correct. Only a real request can show whether a page exists. Rules without a loop can't get past the length of their longest chain, however many visitors follow them, so a busy site doesn't build up to a false stop.

I also ruled out a hop count in a cookie or the query string: a cookie runs into consent rules and CDN caching of redirects, and a parameter changes the urls. Counts live in a bounded in-memory cache keyed by a hash, about 2 MB at most, so requests with long urls can't fill the memory. When it's full, the oldest counts go, and the browser's limit applies to those urls.

Check when upgrading

  • A chain of more than 10 redirects with no loop now ends in a 404, where a browser used to follow it up to around 20. Raise MaxRedirectHops if you have those.
  • A redirect that would only request the same url again now stays a 404 in more cases: /a -> /a#top, //example.com/a, or an empty new url. Moving to another scheme on the same url still redirects.
  • RequestHandler takes a RedirectLoopGuard, which matters only if you construct or inherit from it.

Testing

  • Geta.NotFoundHandler.Tests 256/256 and Geta.NotFoundHandler.Optimizely.Tests 79/79.
  • Over HTTP in the sandbox, following the redirects:
Case Limit off (control) Limit on
Two rules growing still redirecting at my cap of 40 10 redirects, then 404
Two rules back and forth still redirecting at 40 10 redirects, then 404
Regex rule matching its own result still redirecting at 40 10 redirects, then 404
Chain without a loop, 30 times 2 redirects 2 redirects
Page moved back: 30 requests to the old url all redirected, page 200 all redirected, page 200
  • With the limit on, the warning is in the log. A stopped url stays a 404, and a corrected rule worked 40 seconds later.
  • In Chrome on the final code, using fetch from the admin page, because Chrome shows its own error page for both an empty 404 and too many redirects: temporary loops and a first visit to a permanent growing loop ended in a 404, and the permanent-redirect limits below are what I saw there.

Known limits

  • Permanent redirects are remembered by the browser, which follows them without asking the server, so the server can't stop these:
    • Permanent redirects sending a url back and forth (/a -> /b, /b -> /a) still end in the browser's error, as they did before. Chrome showed this.
    • Permanent redirects that grow stop at the same 404 on repeat visits for up to 30 seconds. After that they get further (10 hops, then 20), and then end in the browser's error too.
    • Temporary redirects aren't affected, and they are the default. A warning when saving such rules would help here, but I left that out of this PR.
  • Each server counts on its own. With requests strictly alternating between two instances, the limit never stopped a loop, and my run reached its cap of 40. That's the old behaviour rather than a new problem, and it needs a load balancer without session affinity to happen. I didn't test on DXP.
  • Self-loops spelled differently, such as /a%5Bb%5D -> /a[b], aren't counted and end at the browser's limit, as before.
  • Under a flood of made-up urls through a wildcard rule, counts can be pushed out of the cache, and those urls fall back to the browser's limit.
  • Longer cycles that only exist once urls are decoded (/a%2Fb -> /c with /c -> /a/b) can add up over separate requests within 30 seconds. That needs an unusual set of rules.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GUDE6qHSSrhDDLNMCQ5suJ

frederikvig and others added 4 commits November 6, 2026 21:09
Two rules pointing into each other, or a regex rule matching its own result,
went on until the browser gave up. The rules alone can't show this, as they are
fine when a new url exists, so this counts the redirects that really happen and
gives a request a 404 and logs a warning after MaxRedirectHops in a row.

Closes #208.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GUDE6qHSSrhDDLNMCQ5suJ
From review. A stop only forgot the stopping url, so the rest of the loop still
held counts, and a request coming into a loop from outside could lower a count.
Keys also missed the next request when a new url was encoded differently.

A url redirecting to another spelling of itself, such as http to https or a rule
correcting the case, read and raised its own count, so separate requests added
up to a false stop. The key now keeps case, and these aren't counted.

Chains longer than the limit are listed as an upgrade note, as they used to work
up to the browser's limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GUDE6qHSSrhDDLNMCQ5suJ
From /simplify. "Same url" had three definitions, so /a -> /a got a silent 404
while /a -> /a#top took ten redirects. HandleRequest now compares the resolved
new url, and the guard only deals with chains.

One stop cleared every count, so with a few clients in a loop each stop reset
the others and most reached the browser's limit. A stopped url now stays stopped
until its count expires, in 30 seconds, which also makes repeat requests cheap.
Compacting a larger part of a full cache avoids repeatedly sorting all entries
under a flood of made-up urls, and the tests share one way to build a guard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GUDE6qHSSrhDDLNMCQ5suJ
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GUDE6qHSSrhDDLNMCQ5suJ
@frederikvig
frederikvig merged commit 87ab949 into master Nov 7, 2026
1 check passed
@frederikvig
frederikvig deleted the fix/redirect-loops branch November 7, 2026 05:54
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.

Redirects pointing into each other can grow on every hop

1 participant