Skip to content

fix(curl): follow relative redirects from a URL with no path - #344

Merged
ferdingler merged 4 commits into
strands-agents:mainfrom
ferdingler:fix/curl-relative-redirect-no-path
Nov 26, 2026
Merged

ferdingler merged 4 commits into
strands-agents:mainfrom
ferdingler:fix/curl-relative-redirect-no-path

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

When curl -L got a redirect to a plain relative name like hello, it worked out the new URL by cutting the current one at its last slash. If the URL had no path (http://host:port), the last slash was the one after the scheme, so it tried to reach a host literally called hello and failed. A slash in the query or fragment threw it off the same way and landed on the wrong page.

Now those relative redirects are resolved the standard way against the current page's path, ignoring its query and fragment. Absolute, protocol-relative and root-relative redirects are handled as before, and the allowlist still checks every hop.

Related Issues

Fixes #342

Documentation PR

None needed.

Type of Change

Bug fix

Testing

I added a test with a local server covering a URL with no path, one with a trailing slash, a slash in the query, a slash in the fragment, and a protocol-relative hop to a bare host followed by a relative one. On main the no-path cases errored and the query and fragment cases fetched the wrong page; with the fix all of them pass, and the full Rust suite passes.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@github-actions github-actions Bot added the bug Something isn't working label Nov 21, 2026

@strandly-the-agent strandly-the-agent 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.

Changes requested — one regression, small fix. The #342 fix itself is correct at both call sites and I found no allowlist, credential or --proto bypass. But Url::join("") returns the current URL, so a 3xx with an empty or non-ASCII Location now replays the original request (body and injected credential included) 10 more times; on main it sent it once. Details and a tested 4-line fix are inline.

✅ What I verified (head 6fdfd45 vs base dbb6189)
  • cargo test --workspace --all-targets on head: exit 0, 2,734 passed, 0 failed, 0 ignored. cargo test --test curl_integration: 73 passed. cargo fmt --check exit 0; cargo clippy --workspace --all-targets exit 0, no warnings in the touched files.
  • The new test fails on base (http://hello/) and passes on head; reverting either call site fails it. 25 sequential + 40 parallel runs, no flakes.
  • Allowlist holds: 11 host-changing Location forms that now reach join (\\H/x, https:/H/x, http:/\H/x, ht<TAB>tp://H/x, …) × both call sites and a path-less URL all exit 1 access denied; the non-allowlisted server got 0 requests. 24 SSRF spellings: 0 reached the blocked server.
  • Injected Bearer, -u, -b and the body are not sent on a hop to another allowlisted host, same as base.
  • --proto / --proto-redir decide on the resolved scheme; 40,000 random Location values under https-only flags sent 0 http URLs to the kernel, no panics.
  • A 27,061-case base/head differential: same effective URL except for empty, leading ?, leading #, leading \, and scheme-like first segment.
  • Not run: wasm32, Python and Node bindings; real-curl controls are 7.88.1 only.
Appendix — non-blocking (4)
  • ⚪ Behaviour change worth knowing: a Location whose first segment looks like a scheme (item:42, localhost:8080/x, data:,x) used to be followed as a path and now exits 1 access denied: unsupported scheme. Real curl 7.88.1 also exits 1 on these, so this is closer to curl.
  • ⚪ src/commands/curl.rs:112: the Err(_) fallback has no test (changing it to loc.to_string() passes all 73 tests) and glues the strings, e.g. Location: https: requests …/dir/pagehttps:. Base produced different garbage; real curl exits 3. An error would be clearer than a request.
  • ⚪ The docstring names ../name and ?q; neither is asserted. Both work.
  • Pre-existing, not filed: src/vfs_kernel.rs:941 turns any non-ASCII header value into "", so Location: /café is never followed (real curl follows it). Say the word and I'll open an issue. One more pre-existing observation about request headers on cross-host redirects may fall under SECURITY.md, so I'm leaving it out of this thread; tell me where you'd like it.

Comment thread src/commands/curl.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent 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.

Approve at ae1a9f2. Previous finding: fixed. An empty or non-ASCII Location now sends the request once, and Location: ./new with --proto-redir '=https' exits 1 after one request, same as main and real curl. No new findings.

✅ What I verified (ae1a9f2, delta from 6fdfd45)
  • cargo test --workspace --all-targets: exit 0, 2,736 passed, 0 failed, 0 ignored. cargo fmt --check exit 0; cargo clippy --workspace --all-targets exit 0, nothing in the touched files.
  • My earlier repro (adv_repro.rs) now passes 4/4: empty and /café Location give 1 request each; ./new with --proto-redir '=https' gives 1 request and exit 1.
  • Both new tests fail with src/commands/curl.rs reverted to 6fdfd45 (left: 11 right: 1 and left: 0 right: 1) and pass on ae1a9f2.
  • Attacked the fix: an empty Location on the second hop (/a → b → "") stops after 2 requests, body sent once, stdout second302, exit 0. redirects_left only changes at the two continue sites, so hop count is a sound first-request test, including without -L (max_redirects = 0).
  • Not run: wasm32, Python and Node bindings.
Appendix — non-blocking (1)
  • ⚪ The .filter(|loc| !loc.is_empty()) at the redirect-hop site (src/commands/curl.rs:552) has no test: removing it passes all 75 curl_integration tests, and the second-hop case above then makes 11 requests (10 bodyless, no credential). A /a → b → empty case in curl_empty_redirect_location_is_not_followed would pin it.

@ferdingler
ferdingler merged commit 35fc65a into strands-agents:main Nov 26, 2026
33 of 34 checks passed
@ferdingler
ferdingler deleted the fix/curl-relative-redirect-no-path branch November 26, 2026 01:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] curl -L resolves a relative Location: name to the wrong host when the URL has no path

2 participants