Repository navigation
Stop redirects that keep leading to more redirects - #211
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #208, with one limit for permanent redirects described below.
The problem
Two rules pointing into each other (
/a->/b/cand/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
RedirectLoopGuardcounts the redirects that really happen. A new url is one hop further than the url redirecting to it, and a url reached afterMaxRedirectHops(default 10) gets a normal 404 and a warning in the log. It stays that way for up to 30 seconds. Set the option to0to 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
/band back leaves/a->/band/b->/abehind. The page is at/a, so only/bis ever not found, and/b->/ais 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
MaxRedirectHopsif you have those./a->/a#top,//example.com/a, or an empty new url. Moving to another scheme on the same url still redirects.RequestHandlertakes aRedirectLoopGuard, which matters only if you construct or inherit from it.Testing
Geta.NotFoundHandler.Tests256/256 andGeta.NotFoundHandler.Optimizely.Tests79/79.fetchfrom 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
/a->/b,/b->/a) still end in the browser's error, as they did before. Chrome showed this./a%5Bb%5D->/a[b], aren't counted and end at the browser's limit, as before./a%2Fb->/cwith/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