Skip to content

fix(download): follow Telegram redirect chains - #259

Draft
dajiaohuang wants to merge 2 commits into
anmolkapil:mainfrom
dajiaohuang:fix/telegram-web-range-redirects
Draft

dajiaohuang wants to merge 2 commits into
anmolkapil:mainfrom
dajiaohuang:fix/telegram-web-range-redirects

Conversation

@dajiaohuang

@dajiaohuang dajiaohuang commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Refs #205.

A Telegram Web link (web.telegram.org/k/d/...) answers 302 with no Location header outside the browser, and current main already refuses that at the probe, so this PR does not fix #205. The redirect-chain premise is withdrawn: MAX_REDIRECTS stays at main's 5, and the six-hop E2E case that came with the increase is removed. No link needing more than five redirects on a range request was demonstrated, so the cap is not raised without evidence.

What is left is the diagnostic the reviewer suggested. When a redirect with no Location reaches a chunk request, it used to report "Too many redirects for range request", blaming a redirect budget the response never spent. It now reports:

The server sent a redirect with no address to follow

A chain that genuinely runs out of hops still reports the redirect limit.

Validation

  • New E2E case (e2e/download.spec.ts): a chunk request answered 302 with no Location reports the new message and not the redirect limit. Verified it fails against the pre-fix chunkDownloader.ts and passes after.
  • e2e/download.spec.ts + e2e/probeRedirects.spec.ts: 37 passed, 1 skipped.
  • tsc -p tsconfig.e2e.json and tsc --noEmit -p tsconfig.node.json: clean.
  • eslint on both changed files: 0 errors.
  • prettier --check: the two changed files pass on LF-normalized content; the Windows checkout reports line-ending warnings across the tree (core.autocrlf=true).

@Arpit-Khandelwal Arpit-Khandelwal left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for looking into #205! I tried to reproduce it, and I don't think a long redirect chain is the cause. A Telegram Web link (https://web.telegram.org/k/d/...) only works inside the browser, where Telegram's service worker answers it. Outside the browser, the server answers 302 with no Location header at all. In rc.15 the probe let that through. Then the chunk request failed on the missing Location, which gives the same "Too many redirects for range request" message as a real loop (chunkDownloader.ts:254 on main). I tested that answer: it fails the same way on main and on this PR. On current main, #177 already refuses it at the probe, and the browser keeps the download, which is what the issue asks for.

So please:

  1. Remove Fixes #205, so merging doesn't close the issue.
  2. If you have a real link that needs more than 5 redirects on a range request, please share a curl -L -v log or a HAR. Then this makes sense, and MAX_REDIRECTS in probe.ts (still 5) should go up too.
  3. Optional, but it would really help: give a missing Location its own message in chunkDownloader.ts, like "The server sent a redirect with no address to follow".

I also checked that a redirect loop still ends as an error (now after 20 hops), and that cookies and basic auth are still worked out again for each hop's host.

Comment thread src/main/download/chunkDownloader.ts Outdated
const MAX_REDIRECTS = 5
// Some download hosts use several redirects before serving a chunk. Keep a generous finite cap
// so those chains work while redirect loops still end as an error.
const MAX_REDIRECTS = 20

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The probe still allows only 5 (probe.ts:19), and chunks start from the probe's final URL. So 20 only helps a server that starts redirecting again on ranges after byte 0. Do you know a real host that does this? If this stays, please use one shared limit for both.

A 302 with no Location is what a Telegram Web link answers outside the
browser, and the probe already refuses it before a download starts. When one
reaches a chunk request anyway, the shared branch reported it as "Too many
redirects for range request", which blamed a redirect budget the response
never spent. It now says "The server sent a redirect with no address to
follow", and only a chain that actually runs out of hops still reports the
limit.

The MAX_REDIRECTS increase is dropped: no link needing more than five hops on
a range request was ever demonstrated, so the cap stays at main's 5 and the
six-hop E2E case goes with it. The new E2E case drives a chunk request at a
302 with no Location and checks the message, which fails on main.

Co-authored-by: Cursor <cursoragent@cursor.com>
@dajiaohuang

Copy link
Copy Markdown
Contributor Author

@Arpit-Khandelwal Thanks — you're right, and I accept the diagnosis.

Your reproduction matches what the source says. In probe.ts a 3xx with no Location breaks out of the redirect loop and is refused as Server responded with status 302, so on current main the Telegram Web link never reaches a chunk request at all. That means the redirect-chain framing in the original description was wrong, and the PR does not fix #205.

What I changed in fcf5cb3:

  1. Fixes #205 → Refs #205 in the description. Merging this no longer closes the issue.
  2. The MAX_REDIRECTS increase is dropped. It's back to main's 5, and the six-hop E2E case that went with it is removed. I don't have a real link needing more than five redirects on a range request, so I'm not raising the cap and not claiming one exists. If you ever get a curl -L -v or HAR showing that, your point 2 stands and probe.ts should move with it.
  3. The missing-Location case is now its own message in chunkDownloader.ts. The old branch collapsed "no Location" and "out of hops" into one check, so a response with nowhere to go reported "Too many redirects for range request" — blaming a redirect budget it never spent. It now says The server sent a redirect with no address to follow, and only a chain that actually exhausts its hops still reports the limit.

New E2E case in e2e/download.spec.ts drives a chunk request at a 302 with no Location and asserts the new message and the absence of the old one. I confirmed it fails against the pre-fix file and passes after. download.spec.ts and probeRedirects.spec.ts together: 37 passed, 1 skipped. Typecheck and lint are clean.

On whether to keep this open: I'd recommend closing it. The diagnostic is a genuine small improvement, but it was a side note on a disproved premise, it touches a download error path for a case the probe now stops earlier, and you can land the one-line message in main directly if you want it — so it doesn't need a PR of its own. Your call; I'll close it rather than force it if you'd prefer that.

This branch has not been deployed

No deployments
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.

Telegram Web downloads fail with "Too many redirects for range request"

2 participants