Skip to content

fix(curl): follow protocol-relative redirects - #144

Merged
ferdingler merged 5 commits into
strands-agents:mainfrom
ikenyal:fix/curl-protocol-relative-redirects
Nov 21, 2026
Merged

ferdingler merged 5 commits into
strands-agents:mainfrom
ikenyal:fix/curl-protocol-relative-redirects

Conversation

@ikenyal

@ikenyal ikenyal commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Description

curl -L treats a protocol-relative Location: //host/path as a path on the original server, so valid redirects reach the wrong destination. Resolve these references by preserving the current scheme and using the authority from Location, on both the initial request and subsequent redirect hops.

Absolute URLs and ordinary relative paths retain their existing handling. URL allowlisting, DNS/IP checks, protocol restrictions, and credential injection are unchanged.

Related Issues

Fixes #132

Documentation PR

Not applicable: this restores the expected redirect behavior without changing the command's public interface.

Type of Change

Bug fix

Testing

cargo xtask check --rust-only passed on macOS arm64 with Rust 1.92: formatting, clippy, 1,808 Rust tests, and documentation generation with warnings denied. Clippy reports existing warnings in untouched files; none concern the changed files.

The new redirect and target-allowlist regressions fail against the original implementation and pass with the fix. The local HTTP integration tests cover both first and later hops, target path/query preservation, injected credentials being stripped, blocked destinations, and --proto-redir enforcement.

Also exercised the built CLI against two local HTTP servers: direct and chained redirects returned the target's response when allowed and were denied when only the original server was allowed. No AWS or model calls were made.

Python, Node, and WASM binding suites and the cross-platform matrix were not run locally; their binding surfaces are unchanged.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test) — Rust core suite; other bindings not changed
  • 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 (not applicable: no documentation changes required)
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed — no new docs needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published (not applicable: no dependent PRs)

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

@ikenyal
ikenyal requested a review from a team as a code owner October 11, 2026 22:39
@ikenyal
ikenyal requested a review from opieter-aws October 11, 2026 22:39
@opieter-aws
opieter-aws requested review from mkmeral and removed request for opieter-aws October 16, 2026 14:43
@ferdingler
ferdingler requested a review from a team as a code owner November 9, 2026 23:49
@ferdingler

Copy link
Copy Markdown
Collaborator

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

Verdict: approve, nothing blocking. Posted as a comment, so a maintainer's approval is still needed. @ferdingler

The fix does what #132 asks. Location: //host/path now resolves against the current scheme on the first and later hops, the same as curl 7.88.1. The allowlist, the SSRF guard, --proto-redir and credential handling treat it exactly like the same redirect written as an absolute URL.

Two non-blocking test comments are inline:

  • 🟡 curl_protocol_relative_redirect_respects_proto_redir passes without the fix.
  • 🟡 No test covers inheriting the scheme from an https origin.
✅ What was verified

Head b3cd4b6, base 5f80040, rustc 1.100.0, Debian 12 aarch64.

  • Repo gates on head: cargo test --workspace --all-targets gave 2,573 passed, 0 failed, exit 0. cargo fmt --all -- --check and cargo doc with -D warnings are clean. cargo clippy --workspace --all-targets reports nothing in the two changed files.
  • Regression: with src/commands/curl.rs from base, …preserves_target_and_strips_credentials and …checks_target_allowlist fail; both pass on head. Removing either arm alone is caught too (redirect covers the first-request arm, chain the hop arm).
  • Against real curl 7.88.1, same local servers: 97 redirect cases across -w '%{url_effective}', -v, -I, -i, -o, -f, 301–308, IPv6, //HOST, //host, //host?x=1 and queries containing ://. Head matches curl on exit code, servers hit and final path in 65. Of the other 32, 16 are malformed Location values that curl rejects, 6 are cosmetic, and 10 are the older relative-path arms, unchanged by this PR.
  • Unchanged outside //: 330 Location values that don't start with // behave the same on base and head.
  • SSRF and allowlist: 29 payloads on first and later hops, each sent protocol-relative and absolute (loopback, localhost, IMDS, 10/8, [::1], v4-mapped, hex/decimal IPs, //allowed:port@blocked, backslash, tab). Protocol-relative on head equals absolute on head and base in 58/58, with no request to a server outside the allowlist. 1,550 fuzzed //… values: no request off the allowlisted source, no panic.
  • Credentials: injected bearer and query credentials, -u, -b and the -d body are not sent to the target on either hop.
  • Scheme inheritance: works for http and https against a local self-signed TLS server, and in one probe through the public https://httpbingo.org/redirect-to?url=//example.com/.
  • Stability: the three new tests ran 210 times with 0 failures.
  • Not run: Python, Node and WASM suites; macOS and Windows.
Appendix — non-blocking (3)
  • ⚪ https:/host falls back to http. A current URL spelled with a single slash has no ://, so map_or("http", …) sends the protocol-relative hop over plain http; base kept https. The default kernel only accepts that spelling for an allowlisted host, so this is contrived. split_once(':') instead of split_once("://") fixes it, and curl_integration stays 71/71 with that change.
  • Pre-existing, filed #342: a relative Location: name on a URL with no path resolves to http://name. After this PR a Location: //host with no path leaves the URL in that form for the next hop.
  • A few other pre-existing observations in this area fall under SECURITY.md, so they are not written up here. @ferdingler, ask me in a private channel for the details.

Comment thread tests/curl_integration.rs Resolved
Comment thread src/commands/curl.rs Resolved
@ferdingler
ferdingler removed request for a team and mkmeral November 21, 2026 04:11
The test passed with the fix reverted, because --proto-redir '=https'
refuses the unresolved http://source//target hop with the same message.
Assert the resolved hop from -v so it fails without the fix.
Every origin in the protocol-relative tests was http, so hard-coding
"http" for a //host Location still passed. A kernel that scripts HTTP
replies lets the test start from https://source.test without a TLS
server, and checks both the first request and a later hop.
@ferdingler

Copy link
Copy Markdown
Collaborator

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

Verdict: approve, nothing open. Both findings from my last review are fixed. Posted as a comment, so a maintainer's approval is still needed. @ferdingler

  • ✅ 🟡 …respects_proto_redir passes without the fix: fixed in 49e644c. The test now fails on the base curl.rs.
  • ✅ 🟡 Nothing tests that the scheme is inherited: fixed in f44d600. The new …keeps_https_scheme test catches a hard-coded "http" in either arm.

The delta since b3cd4b6 is test-only; src/commands/curl.rs is unchanged, so the earlier behavior checks still apply.

✅ What was verified

Head f44d600, base 5f80040, rustc 1.100.0, Debian 12 aarch64.

  • Repo gates on head: cargo test --workspace --all-targets gave 2,574 passed, 0 failed, exit 0 (one more than at b3cd4b6). cargo fmt --all -- --check and cargo doc with -D warnings are clean. cargo clippy --workspace --all-targets reports nothing in the two changed files. CI is green on f44d600.
  • Regression: with src/commands/curl.rs from base, all four curl_protocol_relative_* tests fail (4/4, exit 101). On head all four pass.
  • Scheme mutation (|(scheme, _)| scheme replaced by |_| "http"), cargo test --test curl_integration curl_protocol_relative:
    • both arms: …keeps_https_scheme fails with http://target.test/hello.
    • first-request arm only: fails on the redirect leg.
    • hop arm only: fails on the chain leg.
  • Hermetic: ScriptedHttpKernel answers http_request itself, so the .test hosts are never resolved or contacted.
  • Stability: the four tests ran 50 times with 0 failures.
  • Not run locally: Python, Node and WASM suites; macOS and Windows (CI covers them).
Appendix — non-blocking (1)
  • ⚪ Still open from the last review, contrived: a current URL spelled https:/host falls back to http for a // hop. split_once(':') fixes it.

@ferdingler
ferdingler merged commit d638dd6 into strands-agents:main Nov 21, 2026
32 of 34 checks passed
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.

[BUG] curl -L resolves a protocol-relative Location: //host/path as a path on the current host

3 participants