Skip to content

fix: send the upload to where its URL redirects - #164

Open
BKPepe wants to merge 3 commits into
librespeed:masterfrom
BKPepe:fix/upload-redirects
Open

BKPepe wants to merge 3 commits into
librespeed:masterfrom
BKPepe:fix/upload-redirects

Conversation

@BKPepe

@BKPepe BKPepe commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Stacked on #160 and needs its two commits.

With #160 an upload does not follow a redirect, so a server whose upload URL redirects gives no upload result. With --insecure, 13 of the 23 servers in the public list redirect the upload to HTTPS, and all 13 redirect the probe as well. Now a server whose probe is redirected is asked with an empty POST where its upload URL is, and the payload goes there. A single redirect costs two empty POSTs, one that gets the redirect and one where it ends. A server that does not redirect its probe sees no new request. If the redirects do not end at a place that takes the POST, the listed URL is used as before.

The payload stays with the listed server. A redirect may change the path and turn plain HTTP into HTTPS, but the host and port stay as the list writes them. A redirect elsewhere ends the upload test before anything is sent there. All 13 public servers redirect to the same host and path over HTTPS.

A server that redirects only POST, and not the GET of the probe, is not covered.

One of them, upload only:

HTTPS as listed:      159 to 231 Mbps (6 runs)
--insecure, master:   9 Mbps
--insecure, #160:     exit 1, HTTP 301
--insecure, now:      174 to 246 Mbps (6 runs)

A download or upload URL that answered with an error status, or an
upload URL that answered with a redirect, was timed as if it carried
data. Such answers are not counted now, and a server that only refuses
fails like one that is down.
A server could answer with a redirect to an http:// URL, and the probe,
the IP lookup, the transfers and the telemetry request followed it. A
run that asked for HTTPS was then measured, or its result sent, over
plain HTTP.

@maddie maddie 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.

Review of 2ebeb18

The commit itself is small: defs/server.go (+113), defs/upload_redirect_test.go (new), secure_redirect_test.go (±40). Build, vet, both test runs including -race, and the new redirect tests pass. Discovery and payload forwarding work for the cases the tests cover. Two points block approval.

❌ Security — the upload can leave the server's origin, including under --secure

uploadTarget (defs/server.go:928-995) follows 301/302/307/308 to any scheme, host and port, and defs/server.go:608-613 then sends the real payload there. The only policy applied is the client's redirect policy; under --secure that is refuseDowngrade (speedtest/speedtest.go:495-502), which rejects HTTPS -> HTTP but not a change of host.

Probe on this head: an HTTPS frontend redirecting its upload POST to a second HTTPS server on a different host and port; the second host received 143,015,936 payload bytes and the CLI exited 0. The same probe over plain HTTP delivered 569,376,768 bytes to the second host.

Why this matters: the server list is remote. A hostile or compromised listed server can use the client to POST a large random body to any reachable host and port, including an intranet service. Downloads and telemetry already follow redirects, but this adds an upload POST primitive, and it restores redirect following that #160 deliberately removed.

To resolve, pick one and add a test that pins it:

  • restrict upload discovery and the payload to the listed upload URL's origin (scheme + host + port), or
  • have the maintainer explicitly accept cross-origin upload POSTs and document it as intended behavior.

I cannot approve the cross-origin version without that decision, because it is a new SSRF primitive, not a refactor.

❌ Inherited from #160 — read-then-hang still reports a result

#164 is one commit whose parent is exactly 168a242, so it carries #160's commits and its open blocker at defs/server.go:869 (a body handed over whole after at least 250 ms and never answered is still counted as a successful upload). Merge order is #160 first, then rebase #164. The decision requested in the #160 turn-4 review also decides this PR.

⚠️ The empty POST proves reachability, not that the target consumes the payload

defs/server.go:989-995 accepts any 2xx answer to the empty POST and selects that URL. A target that answers 200 without reading is selected; the payload POST then also gets 200, and Upload reports a rate for bytes that were never read. Probe: rate=66.198 Mbps total=9109504 err=<nil>, target read 0 bytes. Any server can answer before reading, so this is a limit of the heuristic rather than a new bug, but the code comment should say that the empty POST only proves the URL answers, and a regression test for a 2xx-without-read target would document the behavior.

💡 Nitpicks

  • defs/upload_redirect_test.go:22-28 (uploadSeen.count) never records the request method, so TestUploadFollowsItsURL would pass if the payload went out as a non-POST with a body. Assert POST for the payload requests.
  • TestUploadAsksNothingOfAServerThatDoesNotRedirect (defs/upload_redirect_test.go:95) and TestUploadDoesNotDependOnTheEmptyPOST (:122) also pass on 168a242; only TestUploadFollowsItsURL, TestUploadStopsFollowingALoop and the new secure_redirect_test.go cases guard the feature.
  • defs/server.go:982 logs next.Redacted(), which keeps the query string; a signed redirect URL would appear in --debug output. Consider logging host and path only.
  • A server that redirects only the POST and not the probe is still refused. The PR body says so, and the probe confirms HTTP 302 with no payload at the target; worth a line in the code comment next to probeRedirected.
  • The PR description lists changes to defs/bytes_counter.go, speedtest/helper.go and speedtest/speedtest.go; those come from #160 and are not in this commit. Trim the description so the diff and the text agree.

Verification

  • go build ./..., go vet ./...: pass.
  • go test -count=1 ./...: pass (root 25.6s, defs 71.8s).
  • go test -count=1 -race ./...: pass, no race reports (root 43.2s, defs 73.9s).
  • go test -run TestUpload -v ./defs and go test -run TestSecureRefusesARedirectedUpload -v .: pass.
  • Probes: same-host redirect delivers the payload; cross-host delivers it (the security finding); a redirect loop stops after 10 requests with stopped after 10 redirects; 301/302/307/308 all deliver; 200-without-read gives the false success above; --secure HTTPS->HTTP is refused with zero plain-HTTP requests; POST-only redirect stays refused.
  • On the parent 168a242 with two stubs for probeRedirected/uploadProbeTimeout: TestUploadFollowsItsURL fails for all four statuses, TestUploadStopsFollowingALoop fails, and the new secure cases fail. The two fallback tests pass on the parent.
  • Not verified: real public/CDN servers, HTTP/3, cookies or Authorization across the redirect chain, 303 behavior, and public TLS validation (the cross-host probe used --skip-cert-verify).

An upload does not follow a redirect, so a server whose upload URL
redirects, as most public servers do under --insecure, gave no upload
result. A server whose probe is redirected is now asked with an empty
POST where its upload URL is. The test runs there if it is on the same
server, where plain HTTP may become HTTPS.
@BKPepe
BKPepe force-pushed the fix/upload-redirects branch from 2ebeb18 to 8487a4b Compare November 26, 2026 09:21
@BKPepe

BKPepe commented Nov 26, 2026

Copy link
Copy Markdown
Member Author

Thanks. The upload now stays with the listed server. A redirect may change the path and turn plain HTTP into HTTPS, which is what all 13 public servers do, but the host and port stay as the list writes them. A redirect elsewhere ends the upload test before anything goes there, not even the empty POST, with or without --secure. Strict origin would refuse the step to HTTPS that this PR is for, so it sits between your two options.

The comment on the empty POST, a test with a place that answers without reading, the POST check in the tests, the log without the query and the note at probeRedirected are in the commit. The two tests that pass on 168a242 cover the fallback. One checks that a server without a redirected probe gets no new request, the other that a failed lookup leaves the upload as it was. The PR body names no files. The rest of the diff is #160 underneath. The read-then-hang point waits for the decision on #160.

@maddie maddie 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.

Review of 8487a4b

✅ Cross-origin upload redirect fixed

defs/server.go:1016-1018 now requires the redirect target to keep the listed host and port and to keep the scheme or upgrade HTTP to HTTPS. uploadTarget checks each hop before sending, and TestUploadFollowsItsURL verifies that no payload reaches the listed URL and that the new place receives the empty probe and the payload. Both CI checks pass.

❌ Inherited blocker: a read-then-block server still reports a successful upload

defs/server.go:892 is unchanged from #160:

return s.whole.IsZero() || s.whole.Sub(s.first) >= sendingProof

A server that accepts the whole payload, reads 1 MiB, and then blocks is still reported as success (rate=2.621 Mbps total=1048576 err=<nil>, exit 0). This PR includes #160's commits, so it cannot be approved while that behavior remains. The choice between documenting this limitation and rejecting all whole bodies is still a product decision; please keep this PR stacked on #160 and rebase after that decision.

💡 Nits (non-blocking)

  • defs/upload_redirect_test.go:25-35: uploadSeen.count adds to payload for any method, and TestUploadFollowsItsURL checks only byte counts. A redirect that turned the payload into a GET could still pass the payload assertion; assert r.Method == http.MethodPost for payload requests.
  • Redirects that require the body to be replayed (307/308) are still unsupported; the PR body says so, but the user-visible limitation could be called out in the upload error path.
  • The two fallback tests still pass against the parent commit; they are not evidence for the new behavior.

The redirect-target security issue is resolved. The remaining blocker is inherited and already tracked in #160.

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.

2 participants