From d0cd037be596b94cd3c7905d68ac56517582a852 Mon Sep 17 00:00:00 2001 From: Kento Ikeda Date: Mon, 12 Oct 2026 07:35:06 +0900 Subject: [PATCH 1/3] fix(curl): follow protocol-relative redirects --- src/commands/curl.rs | 10 ++++ tests/curl_integration.rs | 97 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/src/commands/curl.rs b/src/commands/curl.rs index 0937dcc5..44736e84 100644 --- a/src/commands/curl.rs +++ b/src/commands/curl.rs @@ -448,6 +448,11 @@ async fn cmd_curl(os: &dyn Kernel, args: &[String]) -> CommandResult { { let next = if is_absolute(&loc) { loc + } else if loc.starts_with("//") { + let scheme = current_url + .split_once("://") + .map_or("http", |(scheme, _)| scheme); + format!("{scheme}:{loc}") } else if loc.starts_with('/') { let scheme_end = current_url.find("://").map(|i| i + 3).unwrap_or(0); let host_end = current_url[scheme_end..] @@ -527,6 +532,11 @@ async fn cmd_curl(os: &dyn Kernel, args: &[String]) -> CommandResult { { let next = if is_absolute(&loc) { loc + } else if loc.starts_with("//") { + let scheme = current_url + .split_once("://") + .map_or("http", |(scheme, _)| scheme); + format!("{scheme}:{loc}") } else if loc.starts_with('/') { let scheme_end = current_url.find("://").map(|i| i + 3).unwrap_or(0); let host_end = current_url[scheme_end..] diff --git a/tests/curl_integration.rs b/tests/curl_integration.rs index 425ce4f9..c7fcca5d 100644 --- a/tests/curl_integration.rs +++ b/tests/curl_integration.rs @@ -79,6 +79,25 @@ async fn shell_with_server() -> (Shell, String) { (shell, base) } +async fn start_redirect_server(location: String) -> String { + let app = Router::new() + .route( + "/redirect", + get(move || { + let location = location.clone(); + async move { Redirect::temporary(&location) } + }), + ) + .route("/chain", get(|| async { Redirect::temporary("/redirect") })) + .route("/echo", any(echo_handler)); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + tokio::task::spawn_local(async move { + axum::serve(listener, app).await.unwrap(); + }); + format!("http://{addr}") +} + async fn echo_handler( method: Method, headers: HeaderMap, @@ -302,6 +321,84 @@ fn curl_follow_redirect() { })); } +#[test] +fn curl_protocol_relative_redirect_preserves_target_and_strips_credentials() { + let (rt, local) = rt(); + rt.block_on(local.run_until(async { + let target = start_server().await; + let location = format!( + "{}/echo?from=redirect", + target.strip_prefix("http:").unwrap() + ); + let source = start_redirect_server(location).await; + let mut shell = Shell::builder() + .allow_url(&source) + .allow_url(&target) + .credential( + format!("{source}/"), + strands_shell::CredKind::Bearer, + "redirect-test-token", + ) + .build() + .unwrap(); + let initial = shell.run(&format!("curl {source}/echo")).await; + assert!( + initial + .stdout + .contains("authorization=Bearer redirect-test-token") + ); + + for path in ["redirect", "chain"] { + let out = shell.run(&format!("curl -sSL {source}/{path}")).await; + assert_eq!(out.status, 0, "{path}: {}", out.stderr); + assert_eq!( + out.stdout, "method=GET\naccept=*/*\nquery_from=redirect", + "{path}" + ); + } + })); +} + +#[test] +fn curl_protocol_relative_redirect_checks_target_allowlist() { + let (rt, local) = rt(); + rt.block_on(local.run_until(async { + let target = start_server().await; + let location = format!("{}/hello", target.strip_prefix("http:").unwrap()); + let source = start_redirect_server(location).await; + let mut shell = Shell::builder().allow_url(&source).build().unwrap(); + + for path in ["redirect", "chain"] { + let out = shell.run(&format!("curl -sSL {source}/{path}")).await; + assert_eq!(out.status, 1, "{path}: {}", out.stderr); + assert!(out.stderr.contains("denied"), "{path}: {}", out.stderr); + } + })); +} + +#[test] +fn curl_protocol_relative_redirect_respects_proto_redir() { + let (rt, local) = rt(); + rt.block_on(local.run_until(async { + let target = start_server().await; + let location = format!("{}/hello", target.strip_prefix("http:").unwrap()); + let source = start_redirect_server(location).await; + let mut shell = Shell::builder() + .allow_url(&source) + .allow_url(&target) + .build() + .unwrap(); + + for path in ["redirect", "chain"] { + let out = shell + .run(&format!("curl -sSL --proto-redir '=https' {source}/{path}")) + .await; + assert_eq!(out.status, 1, "{path}: {}", out.stderr); + assert!(out.stderr.contains("Protocol \"http\" disabled")); + } + })); +} + #[test] fn curl_no_follow_redirect() { let (rt, local) = rt(); From 49e644c8d981c637e343ba5d14a4ffe31113433c Mon Sep 17 00:00:00 2001 From: Fernando Dingler Date: Sat, 21 Nov 2026 04:15:04 +0000 Subject: [PATCH 2/3] test(curl): check which hop --proto-redir refuses on a // redirect 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. --- tests/curl_integration.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/tests/curl_integration.rs b/tests/curl_integration.rs index 8567fcaf..6badea95 100644 --- a/tests/curl_integration.rs +++ b/tests/curl_integration.rs @@ -650,13 +650,21 @@ fn curl_protocol_relative_redirect_respects_proto_redir() { .build() .unwrap(); - for path in ["redirect", "chain"] { - let out = shell - .run(&format!("curl -sSL --proto-redir '=https' {source}/{path}")) - .await; - assert_eq!(out.status, 1, "{path}: {}", out.stderr); - assert!(out.stderr.contains("Protocol \"http\" disabled")); - } + // `-v` names the resolved hop, so this fails if `//host` is not resolved to the target. + // Only `/redirect`: `--proto-redir` refuses `/chain` at its first, same-host hop. + let out = shell + .run(&format!( + "curl -sSLv --proto-redir '=https' {source}/redirect" + )) + .await; + assert_eq!(out.status, 1, "{}", out.stderr); + let hop = format!("Redirecting to {target}/hello"); + assert!(out.stderr.contains(&hop), "{}", out.stderr); + assert!( + out.stderr.contains("Protocol \"http\" disabled"), + "{}", + out.stderr + ); })); } From f44d600d967abce900997e0bd6ddd003422b2a30 Mon Sep 17 00:00:00 2001 From: Fernando Dingler Date: Sat, 21 Nov 2026 04:18:22 +0000 Subject: [PATCH 3/3] test(curl): check that a // redirect keeps an https origin scheme 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. --- tests/curl_integration.rs | 141 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) diff --git a/tests/curl_integration.rs b/tests/curl_integration.rs index 6badea95..394b6fa6 100644 --- a/tests/curl_integration.rs +++ b/tests/curl_integration.rs @@ -6,7 +6,13 @@ use axum::{ routing::{any, get}, }; use std::collections::HashMap; +use std::io; +use std::path::PathBuf; +use std::time::SystemTime; use strands_shell::Shell; +use strands_shell::os::{ + DirEntry, Fd, FileStat, HttpRequest, HttpResponse, Kernel, OpenFlags, Process, +}; fn rt() -> (tokio::runtime::Runtime, tokio::task::LocalSet) { let rt = tokio::runtime::Builder::new_current_thread() @@ -668,6 +674,141 @@ fn curl_protocol_relative_redirect_respects_proto_redir() { })); } +/// Forwards everything to the default kernel except HTTP, which it answers +/// from a script and records, so a test can use an `https://` origin +/// without a TLS server. +struct ScriptedHttpKernel { + inner: std::sync::Arc, + routes: Vec<(&'static str, &'static str)>, + requested: std::sync::Mutex>, +} + +#[async_trait::async_trait] +impl Kernel for ScriptedHttpKernel { + async fn http_request(&self, req: HttpRequest) -> io::Result { + self.requested.lock().unwrap().push(req.url.clone()); + let location = self.routes.iter().find(|(url, _)| *url == req.url); + let (status, headers, body) = match location { + Some((_, loc)) => (302, vec![("location".into(), loc.to_string())], Vec::new()), + None => (200, Vec::new(), b"ok".to_vec()), + }; + Ok(HttpResponse { + status, + headers, + body, + version: "1.1".into(), + reason: String::new(), + }) + } + + fn new_process(&self) -> Process { + self.inner.new_process() + } + + async fn open(&self, proc: &mut Process, path: &str, flags: OpenFlags) -> io::Result { + self.inner.open(proc, path, flags).await + } + + async fn list_dir(&self, proc: &Process, path: &str) -> io::Result> { + self.inner.list_dir(proc, path).await + } + + async fn change_dir(&self, proc: &mut Process, path: &str) -> io::Result<()> { + self.inner.change_dir(proc, path).await + } + + async fn stat(&self, proc: &Process, path: &str) -> FileStat { + self.inner.stat(proc, path).await + } + + async fn lstat(&self, proc: &Process, path: &str) -> FileStat { + self.inner.lstat(proc, path).await + } + + async fn access(&self, proc: &Process, path: &str, mode: i32) -> bool { + self.inner.access(proc, path, mode).await + } + + async fn canonicalize(&self, proc: &Process, path: &str) -> io::Result { + self.inner.canonicalize(proc, path).await + } + + async fn is_executable(&self, proc: &Process, path: &str) -> bool { + self.inner.is_executable(proc, path).await + } + + async fn glob(&self, proc: &Process, pattern: &str) -> Vec { + self.inner.glob(proc, pattern).await + } + + fn isatty(&self, fd: i32) -> bool { + self.inner.isatty(fd) + } + + async fn remove_file(&self, proc: &Process, path: &str) -> io::Result<()> { + self.inner.remove_file(proc, path).await + } + + async fn remove_dir(&self, proc: &Process, path: &str) -> io::Result<()> { + self.inner.remove_dir(proc, path).await + } + + async fn create_dir(&self, proc: &Process, path: &str) -> io::Result<()> { + self.inner.create_dir(proc, path).await + } + + async fn rename(&self, proc: &Process, from: &str, to: &str) -> io::Result<()> { + self.inner.rename(proc, from, to).await + } + + async fn symlink(&self, proc: &Process, target: &str, link: &str) -> io::Result<()> { + self.inner.symlink(proc, target, link).await + } + + async fn read_link(&self, proc: &Process, path: &str) -> io::Result { + self.inner.read_link(proc, path).await + } + + async fn set_permissions(&self, proc: &Process, path: &str, mode: u32) -> io::Result<()> { + self.inner.set_permissions(proc, path, mode).await + } + + fn now(&self) -> SystemTime { + self.inner.now() + } +} + +#[test] +fn curl_protocol_relative_redirect_keeps_https_scheme() { + let (rt, local) = rt(); + rt.block_on(local.run_until(async { + let kernel = std::sync::Arc::new(ScriptedHttpKernel { + inner: Shell::builder().build().unwrap().kernel().clone(), + routes: vec![ + ("https://source.test/redirect", "//target.test/hello"), + ("https://source.test/chain", "/redirect"), + ], + requested: std::sync::Mutex::new(Vec::new()), + }); + let mut shell = Shell::with_kernel(kernel.clone()); + + for path in ["redirect", "chain"] { + kernel.requested.lock().unwrap().clear(); + let out = shell + .run(&format!("curl -sSL https://source.test/{path}")) + .await; + assert_eq!(out.status, 0, "{path}: {}", out.stderr); + assert_eq!(out.stdout, "ok", "{path}"); + let requested = kernel.requested.lock().unwrap(); + assert_eq!( + requested.last().map(String::as_str), + Some("https://target.test/hello"), + "{path}: {requested:?}" + ); + } + })); +} + #[test] fn curl_no_follow_redirect() { let (rt, local) = rt();