From 54d053bdee1e8b7731c1193bc0ad2fa61521d9d8 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 19:32:57 +0300 Subject: [PATCH 1/7] feat(repo,cli): close the four P32 TLS follow-ons - sc serve fingerprint and --tls auto-mint refuse a root with no .sc/. - Warn when an SC_HTTPS_* knob is set but the URL is plaintext sc+http://. - Bound the sc+http(s) client's connect, TLS handshake, opening and wire handshake (30 s) so a silent peer fails the connect; lift it afterwards. - sc remote add parse-validates sc+http(s):// URLs at add time. Co-Authored-By: Claude Sonnet 5.5 --- ROADMAP.md | 23 +- crates/cli/src/main.rs | 3 + crates/cli/tests/remote_add_validation.rs | 58 +++++ crates/repo/src/http_transport.rs | 253 +++++++++++++++++++++- docs/adr/0042-in-binary-tls-sc-https.md | 17 ++ 5 files changed, 337 insertions(+), 17 deletions(-) create mode 100644 crates/cli/tests/remote_add_validation.rs diff --git a/ROADMAP.md b/ROADMAP.md index 5499c3aa..61124780 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -906,19 +906,16 @@ scale-&-reach horizon): today; and a one-line fix to `SignatureObj.snapshot`'s now-stale doc comment, which still describes the field as always a snapshot id even though signing a transcript stores a transcript id there instead. -- **P32 follow-ons.** Four items named but not closed by the P32 TLS work, - deferred rather than silently dropped: (1) a `.sc`-existence check before - `sc serve fingerprint`/`--tls` auto-mint — today it happily mints - `~/.sc/serve-tls` even when run outside a repo; (2) a stderr warning when - an `SC_HTTPS_*` env knob (`SC_HTTPS_STRICT`/`SC_HTTPS_FINGERPRINT`/ - `SC_HTTPS_KNOWN_HOSTS`) is set but the target URL is plaintext - `sc+http://` — a scheme-downgrade footgun where the knob silently does - nothing; (3) a client-side TLS handshake/read timeout — the server bounds - its side at 30s (the opening read), but the `sc+https://` client is - unbounded, so this folds into the existing deferred hostile-peer pass - alongside `read_frame_inner`'s unbounded frame-length allocation; (4) - `sc remote add` parse-validating `sc+http(s)://` URLs at add time, the - way `ssh://` already does, instead of deferring the parse to first use. +- **P32 follow-ons — shipped.** The four items the TLS work named: (1) `sc serve + fingerprint` and `--tls` auto-mint refuse a root with no `.sc/` instead of + minting there; (2) a stderr warning when an `SC_HTTPS_*` knob is set but the + URL is plaintext `sc+http://`; (3) the `sc+http(s)://` client bounds the + connect, TLS handshake, opening and wire handshake (30 s) so a silent peer + fails the connect, and lifts the bound afterwards (a read timeout is + `ConnectionLost`); (4) `sc remote add` parse-validates `sc+http(s)://` URLs + at add time like `ssh://` and the bucket schemes. The client bound is not + configurable, and `read_frame_inner`'s frame-length allocation (covered by the + `MAX_OBJECT_SIZE` cap since P28) is unchanged. - **P34 follow-ons (private branches, ADR-0044).** Deferred rather than built: (1) `sc branch publish --squash` — collapse the sealed history into diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index ca3f6b92..bf45c06e 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -3912,6 +3912,9 @@ fn run_remote(op: RemoteOp) -> Result<()> { if url.starts_with("ssh://") { scl_repo::SshUrl::parse(&url)?; // fail fast on malformed URLs } + if url.starts_with("sc+http://") || url.starts_with("sc+https://") { + scl_repo::ScHttpUrl::parse(&url)?; // fail fast on malformed URLs + } if url.starts_with("sc+wal://") || url.starts_with("sc+s3://") { scl_repo::BucketUrl::parse(&url)?; // fail fast on malformed URLs } diff --git a/crates/cli/tests/remote_add_validation.rs b/crates/cli/tests/remote_add_validation.rs new file mode 100644 index 00000000..379a5be6 --- /dev/null +++ b/crates/cli/tests/remote_add_validation.rs @@ -0,0 +1,58 @@ +//! `sc remote add` parse-validates the native network URL schemes at add time, +//! so a typo fails here and not at the first fetch (P32 follow-on). + +use std::process::Command; + +fn sc(dir: &std::path::Path, args: &[&str]) -> std::process::Output { + Command::new(env!("CARGO_BIN_EXE_sc")) + .args(args) + .current_dir(dir) + .output() + .expect("sc runs") +} + +fn repo(tag: &str) -> std::path::PathBuf { + let root = + std::env::temp_dir().join(format!("scl-cli-remote-add-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + assert!(sc(&root, &["init"]).status.success()); + root +} + +#[test] +fn malformed_http_urls_are_refused_at_add_time_and_good_ones_recorded() { + let root = repo("http"); + for bad in [ + "sc+http://", + "sc+http:///repo", + "sc+https://host:notaport/repo", + "sc+http://host:99999/repo", + ] { + let out = sc(&root, &["remote", "add", "origin", bad]); + assert!(!out.status.success(), "{bad} should be refused"); + let list = sc(&root, &["remote", "list"]); + assert!( + String::from_utf8_lossy(&list.stdout).trim().is_empty(), + "{bad} must not be recorded" + ); + } + + for (name, good) in [ + ("plain", "sc+http://127.0.0.1:7777/repo"), + ("secure", "sc+https://example.com/repo"), + ] { + let out = sc(&root, &["remote", "add", name, good]); + assert!( + out.status.success(), + "{good}: {}", + String::from_utf8_lossy(&out.stderr) + ); + } + let list = String::from_utf8_lossy(&sc(&root, &["remote", "list"]).stdout).into_owned(); + assert!(list.contains("sc+http://127.0.0.1:7777/repo"), "{list}"); + assert!(list.contains("sc+https://example.com/repo"), "{list}"); + + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists(), "test must leave no residue"); +} diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index 28e95ccb..18fcee06 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -40,9 +40,15 @@ fn read_bounded_opening(r: &mut impl Read) -> Result> { "HTTP opening exceeded {MAX_OPENING_BYTES} bytes without a terminating blank line" ))); } - let n = r - .read(&mut byte) - .map_err(|e| Error::InvalidArgument(format!("HTTP opening read failed: {e}")))?; + let n = r.read(&mut byte).map_err(|e| match e.kind() { + // A socket read timeout surfaces as WouldBlock (unix) or TimedOut + // (windows): the peer went quiet, which is a lost connection, not + // a malformed opening. + std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut => { + Error::ConnectionLost("timed out waiting for the HTTP opening".to_string()) + } + _ => Error::InvalidArgument(format!("HTTP opening read failed: {e}")), + })?; if n == 0 { return Err(Error::InvalidArgument( "HTTP opening ended before a terminating blank line".to_string(), @@ -345,6 +351,15 @@ impl HttpTransport { /// and delegated to [`Self::connect_with_pins`], the testable, /// env-free core. pub fn connect_with_token(url: &ScHttpUrl, token: Option<&str>) -> Result { + let ignored = ignored_https_knobs(url, |k| std::env::var(k).ok()); + if !ignored.is_empty() { + eprintln!( + "warning: {} set but {} is plaintext sc+http:// — it has no effect; use an \ + sc+https:// URL for TLS", + ignored.join(", "), + url.authority() + ); + } let policy = if url.tls { Some(crate::tls_pins::TlsClientPolicy::from_env()?) } else { @@ -365,6 +380,21 @@ impl HttpTransport { url: &ScHttpUrl, token: Option<&str>, policy: Option<&crate::tls_pins::TlsClientPolicy>, + ) -> Result { + Self::connect_bounded(url, token, policy, CLIENT_OPENING_TIMEOUT) + } + + /// [`Self::connect_with_pins`] with the opening bound as a parameter, so a + /// test can use a short one. The connect, the TLS handshake, the opening + /// line, the status line and the wire handshake are all bounded by + /// `opening`: a peer that accepts and then goes silent fails the connect + /// instead of hanging it. Once the handshake is done the bound is lifted; + /// a long transfer is governed by the server's session timeout. + fn connect_bounded( + url: &ScHttpUrl, + token: Option<&str>, + policy: Option<&crate::tls_pins::TlsClientPolicy>, + opening: Duration, ) -> Result { if let Some(t) = token { if t.contains(['\r', '\n']) { @@ -385,8 +415,18 @@ impl HttpTransport { } } - let stream = TcpStream::connect(url.authority()) + let stream = connect_within(&url.authority(), opening) .map_err(|e| Error::ConnectionLost(format!("sc+http connect to {url:?}: {e}")))?; + // Socket options are shared by every clone of the stream, so these + // bound the TLS handshake and both halves below; `ctl` lifts them + // once the opening is done. + let ctl = stream + .try_clone() + .map_err(|e| Error::ConnectionLost(format!("sc+http socket clone: {e}")))?; + stream + .set_read_timeout(Some(opening)) + .and_then(|()| stream.set_write_timeout(Some(opening))) + .map_err(|e| Error::ConnectionLost(format!("sc+http set timeouts: {e}")))?; let (mut r, mut w) = match (url.tls, policy) { (false, _) => { @@ -513,6 +553,9 @@ impl HttpTransport { } let client = WireClient::handshake(r, w)?; + ctl.set_read_timeout(None) + .and_then(|()| ctl.set_write_timeout(None)) + .map_err(|e| Error::ConnectionLost(format!("sc+http clear timeouts: {e}")))?; Ok(HttpTransport { client }) } } @@ -570,6 +613,51 @@ impl Transport for HttpTransport { /// see [`handle_http_connection`] for the timeout sequencing. const OPENING_READ_TIMEOUT: Duration = Duration::from_secs(30); +/// How long the client allows the connect, TLS handshake, opening and wire +/// handshake to take before giving up on a silent or stalled peer. Matches the +/// server's opening bound; lifted once the handshake completes. +const CLIENT_OPENING_TIMEOUT: Duration = Duration::from_secs(30); + +/// `TcpStream::connect` with a bound on each resolved address, so an +/// unroutable or black-holed host fails in `timeout` rather than the OS default. +fn connect_within(authority: &str, timeout: Duration) -> std::io::Result { + use std::net::ToSocketAddrs; + let mut last = None; + for addr in authority.to_socket_addrs()? { + match TcpStream::connect_timeout(&addr, timeout) { + Ok(s) => return Ok(s), + Err(e) => last = Some(e), + } + } + Err(last.unwrap_or_else(|| { + std::io::Error::new( + std::io::ErrorKind::AddrNotAvailable, + "no addresses resolved", + ) + })) +} + +/// The `SC_HTTPS_*` environment knobs that are set but cannot apply to `url`: +/// all of them configure TLS, so on a plaintext `sc+http://` URL they silently +/// do nothing (a scheme-downgrade footgun worth a warning). Empty for an +/// `sc+https://` URL. `env` is injected so this is testable without mutating +/// the process environment. +fn ignored_https_knobs(url: &ScHttpUrl, env: impl Fn(&str) -> Option) -> Vec<&'static str> { + if url.tls { + return Vec::new(); + } + [ + "SC_HTTPS_STRICT", + "SC_HTTPS_FINGERPRINT", + "SC_HTTPS_KNOWN_HOSTS", + "SC_HTTPS_CA_FILE", + "SC_HTTPS_CA", + ] + .into_iter() + .filter(|k| env(k).is_some_and(|v| !v.is_empty())) + .collect() +} + /// Exponential accept-error backoff (P31): Go net/http's shape — 5ms /// doubling to a 1s cap, reset on the next successful accept. Turns fd /// exhaustion (EMFILE) from a busy-loop into a paced retry. Hardcoded: no @@ -756,6 +844,7 @@ fn resolve_tls( let identity = match tls { TlsMode::Off => return Ok(None), TlsMode::AutoMint => { + require_repo_for_tls_identity(root)?; scl_tlsio::load_or_mint(&crate::layout::Layout::at(root).serve_tls_dir())? } TlsMode::Pem { cert, key } => scl_tlsio::load_pem(cert, key)?, @@ -763,11 +852,26 @@ fn resolve_tls( Ok(Some(scl_tlsio::server_config(identity)?)) } +/// The serve-TLS identity lives in `/.sc/serve-tls/`, so minting one +/// under a root that is not a repo would create a stray `.sc/` there (or fail +/// obscurely). Refuse first, naming the fix. +fn require_repo_for_tls_identity(root: &std::path::Path) -> Result<()> { + if root.join(".sc").is_dir() { + return Ok(()); + } + Err(Error::InvalidArgument(format!( + "{} has no .sc directory, so there is nowhere to keep the serve TLS identity \ + (.sc/serve-tls/); run `sc init` there, or point at a repo", + root.display() + ))) +} + /// The repo's serve-TLS fingerprint (`sha256:`), minting the identity /// if it doesn't exist yet — so an operator can distribute the pin BEFORE /// first serve (`sc serve fingerprint`, P32). Same load-or-mint path the /// server uses; no drift possible. pub fn serve_tls_fingerprint(root: &std::path::Path) -> Result { + require_repo_for_tls_identity(root)?; let id = scl_tlsio::load_or_mint(&crate::layout::Layout::at(root).serve_tls_dir())?; Ok(scl_tlsio::fingerprint_hex(&id.spki_sha256)) } @@ -1897,6 +2001,147 @@ mod tests { assert!(!broken.exists(), "test must leave no residue"); } + /// `sc serve fingerprint` and `--tls` auto-mint keep the identity in + /// `.sc/serve-tls/`, so a root that is not a repo is refused up front + /// rather than gaining a stray `.sc/`. + #[test] + fn tls_identity_is_not_minted_outside_a_repo() { + let bare = std::env::temp_dir().join(format!("scl-http-mint-bare-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&bare); + std::fs::create_dir_all(&bare).unwrap(); + + let err = serve_tls_fingerprint(&bare).unwrap_err(); + assert!(err.to_string().contains("sc init"), "got {err}"); + let err = match resolve_tls(&bare, &TlsMode::AutoMint) { + Err(e) => e, + Ok(_) => panic!("auto-mint outside a repo must be refused"), + }; + assert!(err.to_string().contains("no .sc directory"), "got {err}"); + assert!(!bare.join(".sc").exists(), "nothing may be created"); + + // Inside a repo it still mints. + let repo = tmp_repo("mint-ok"); + assert!(serve_tls_fingerprint(&repo).unwrap().starts_with("sha256:")); + std::fs::remove_dir_all(&repo).unwrap(); + + std::fs::remove_dir_all(&bare).unwrap(); + assert!(!bare.exists(), "test must leave no residue"); + } + + /// A peer that accepts the TCP connection and then says nothing must fail + /// the client's connect within the opening bound, not hang it: neither the + /// status line nor the wire handshake ever arrives. + #[test] + fn a_silent_server_fails_the_connect_within_the_opening_bound() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + // Hold accepted sockets open and mute until the test ends. + let (tx, rx) = std::sync::mpsc::channel::<()>(); + let holder = std::thread::spawn(move || { + let _conn = listener.accept().unwrap(); + let _ = rx.recv(); + }); + let url = ScHttpUrl { + host: "127.0.0.1".to_string(), + port, + path: "/repo".to_string(), + tls: false, + }; + + let started = std::time::Instant::now(); + let err = HttpTransport::connect_bounded(&url, None, None, Duration::from_millis(300)) + .unwrap_err(); + assert!( + started.elapsed() < Duration::from_secs(10), + "the connect was not bounded: {err:?} after {:?}", + started.elapsed() + ); + assert!(matches!(err, Error::ConnectionLost(_)), "got {err:?}"); + tx.send(()).unwrap(); + holder.join().unwrap(); + } + + /// The same bound covers the TLS handshake: a peer that accepts and never + /// answers the ClientHello fails the connect instead of hanging it. + #[test] + fn a_silent_tls_peer_fails_the_connect_within_the_opening_bound() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + let (tx, rx) = std::sync::mpsc::channel::<()>(); + let holder = std::thread::spawn(move || { + let _conn = listener.accept().unwrap(); + let _ = rx.recv(); + }); + let url = ScHttpUrl { + host: "127.0.0.1".to_string(), + port, + path: "/repo".to_string(), + tls: true, + }; + let home = std::env::temp_dir().join(format!("scl-http-silent-tls-{}", std::process::id())); + let policy = crate::tls_pins::TlsClientPolicy { + known_hosts: home.join("known_hosts"), + pre_pin: None, + strict: false, + ca: None, + }; + + let started = std::time::Instant::now(); + let err = + HttpTransport::connect_bounded(&url, None, Some(&policy), Duration::from_millis(300)) + .unwrap_err(); + assert!( + started.elapsed() < Duration::from_secs(10), + "the TLS handshake was not bounded: {err:?}" + ); + assert!( + !home.join("known_hosts").exists(), + "a failed handshake must not write a pin" + ); + tx.send(()).unwrap(); + holder.join().unwrap(); + } + + /// The opening bound is lifted once the handshake is done: a connection + /// that then sits idle for longer than the bound still works. + #[test] + fn the_opening_bound_is_lifted_after_the_handshake() { + let root = tmp_repo("opening-bound-lifted"); + let port = spawn_real_http_server(root.clone()); + let url = ScHttpUrl { + host: "127.0.0.1".to_string(), + port, + path: "/repo".to_string(), + tls: false, + }; + let transport = + HttpTransport::connect_bounded(&url, None, None, Duration::from_millis(300)).unwrap(); + std::thread::sleep(Duration::from_millis(900)); + transport.list_refs().unwrap(); + drop(transport); + + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists(), "test must leave no residue"); + } + + #[test] + fn https_knobs_on_a_plaintext_url_are_reported_and_otherwise_silent() { + let plain = ScHttpUrl::parse("sc+http://127.0.0.1:1/x").unwrap(); + let secure = ScHttpUrl::parse("sc+https://127.0.0.1:1/x").unwrap(); + let env = |k: &str| match k { + "SC_HTTPS_STRICT" => Some("1".to_string()), + "SC_HTTPS_CA" => Some("system".to_string()), + "SC_HTTPS_FINGERPRINT" => Some(String::new()), // empty counts as unset + _ => None, + }; + assert_eq!( + ignored_https_knobs(&plain, env), + vec!["SC_HTTPS_STRICT", "SC_HTTPS_CA"] + ); + assert!(ignored_https_knobs(&secure, env).is_empty()); + assert!(ignored_https_knobs(&plain, |_| None).is_empty()); + } + // ── Task 5: client-side `SC_HTTP_TOKEN` support (`connect_with_token`). ── /// `HttpTransport::connect_with_token` presents the given bearer and maps diff --git a/docs/adr/0042-in-binary-tls-sc-https.md b/docs/adr/0042-in-binary-tls-sc-https.md index a1c7afa7..d07fa7f5 100644 --- a/docs/adr/0042-in-binary-tls-sc-https.md +++ b/docs/adr/0042-in-binary-tls-sc-https.md @@ -172,6 +172,23 @@ channel was always a weaker guarantee than the gate's wording implied. blob, the TOFU pin/mismatch/strict/pre-pin lifecycle, and the tightened plaintext gate refusing a token-only public bind — run twice, zero residue. +## Follow-ons closed after the first release + +- **A repo is required to mint.** `sc serve fingerprint` and `--tls` auto-mint + refuse a root with no `.sc/` directory; the identity lives in `.sc/serve-tls/` + and minting elsewhere left a stray `.sc/`. +- **A downgrade is reported.** When an `SC_HTTPS_*` knob (strict, fingerprint, + known-hosts, CA file or CA) is set and the URL is plaintext `sc+http://`, the + client prints a warning, since the knob silently does nothing there. +- **The client is bounded.** The connect, the TLS handshake, the opening line, + the status line and the wire handshake share a 30 s bound, so a peer that + accepts and goes silent fails the connect instead of hanging it (a read + timeout is `ConnectionLost`). The bound is lifted once the handshake is done; + a long transfer is governed by the server's session timeout. It is not + configurable. +- **`sc remote add` validates.** `sc+http://` and `sc+https://` URLs are parsed + at add time, like `ssh://` and the bucket schemes. + ## Rejected alternatives - **aws-lc-rs as the primary provider.** Rejected for its heavier build From 2e26653800be23edcb3ed81e25bc9ec80abf0141 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 19:42:38 +0300 Subject: [PATCH 2/7] fix(repo,cli,tlsio): type every client timeout, one connect deadline, discover the repo Codex: a timeout in the TLS handshake or wire handshake now surfaces as ConnectionLost like the opening read; the connect shares one deadline across resolved addresses; an empty CA knob counts as set for the downgrade warning; sc serve fingerprint finds the repo from a subdirectory; and the reserved sc+http:/sc+https: prefix, not just the exact form, is parse-validated at add. Co-Authored-By: Claude Sonnet 5.5 --- crates/cli/src/main.rs | 8 ++- crates/cli/tests/remote_add_validation.rs | 38 ++++++++++ crates/repo/src/http_transport.rs | 84 ++++++++++++++++++++--- crates/repo/src/wire.rs | 7 +- crates/tlsio/src/stream.rs | 8 +++ crates/tlsio/src/trust.rs | 6 ++ 6 files changed, 139 insertions(+), 12 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index bf45c06e..7efe3e88 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -1033,7 +1033,8 @@ fn main() -> Result<()> { Some(ServeSub::Fingerprint { path }) => { let root = match path { Some(p) => p, - None => std::env::current_dir()?, + // Like every repo command, find the repo from a subdirectory. + None => scl_repo::layout::Layout::discover(std::env::current_dir()?)?.root, }; println!( "{}", @@ -3912,7 +3913,10 @@ fn run_remote(op: RemoteOp) -> Result<()> { if url.starts_with("ssh://") { scl_repo::SshUrl::parse(&url)?; // fail fast on malformed URLs } - if url.starts_with("sc+http://") || url.starts_with("sc+https://") { + // The reserved scheme prefix, not just the exact `://` form: a + // near miss such as `sc+https:/host/repo` would otherwise be + // recorded and later read as a local path. + if url.starts_with("sc+http:") || url.starts_with("sc+https:") { scl_repo::ScHttpUrl::parse(&url)?; // fail fast on malformed URLs } if url.starts_with("sc+wal://") || url.starts_with("sc+s3://") { diff --git a/crates/cli/tests/remote_add_validation.rs b/crates/cli/tests/remote_add_validation.rs index 379a5be6..86d8b310 100644 --- a/crates/cli/tests/remote_add_validation.rs +++ b/crates/cli/tests/remote_add_validation.rs @@ -28,6 +28,8 @@ fn malformed_http_urls_are_refused_at_add_time_and_good_ones_recorded() { "sc+http:///repo", "sc+https://host:notaport/repo", "sc+http://host:99999/repo", + "sc+https:/host/repo", // near-miss scheme: one slash + "sc+http:host/repo", ] { let out = sc(&root, &["remote", "add", "origin", bad]); assert!(!out.status.success(), "{bad} should be refused"); @@ -56,3 +58,39 @@ fn malformed_http_urls_are_refused_at_add_time_and_good_ones_recorded() { std::fs::remove_dir_all(&root).unwrap(); assert!(!root.exists(), "test must leave no residue"); } + +/// With no path, `sc serve fingerprint` finds the repo from a subdirectory, +/// and outside any repo it refuses instead of minting. +#[test] +fn serve_fingerprint_discovers_the_repo_and_refuses_outside_one() { + let root = repo("fingerprint"); + let sub = root.join("a/b"); + std::fs::create_dir_all(&sub).unwrap(); + let out = sc(&sub, &["serve", "fingerprint"]); + assert!( + out.status.success(), + "{}", + String::from_utf8_lossy(&out.stderr) + ); + assert!(String::from_utf8_lossy(&out.stdout).starts_with("sha256:")); + assert!( + root.join(".sc/serve-tls").is_dir(), + "minted in the repo root" + ); + assert!(!sub.join(".sc").exists(), "never in the subdirectory"); + + let bare = + std::env::temp_dir().join(format!("scl-cli-fingerprint-bare-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&bare); + std::fs::create_dir_all(&bare).unwrap(); + let out = sc(&bare, &["serve", "fingerprint"]); + assert!(!out.status.success()); + assert!(!bare.join(".sc").exists(), "nothing may be created"); + + std::fs::remove_dir_all(&root).unwrap(); + std::fs::remove_dir_all(&bare).unwrap(); + assert!( + !root.exists() && !bare.exists(), + "test must leave no residue" + ); +} diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index 18fcee06..8014aaf2 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -462,7 +462,8 @@ impl HttpTransport { for w in trust.warnings() { eprintln!("warning: sc+https CA trust: {w}"); } - let tls = scl_tlsio::client_connect_ca(stream, &url.host, &trust)?; + let tls = scl_tlsio::client_connect_ca(stream, &url.host, &trust) + .map_err(|e| lost_if_timeout(e.into()))?; let (rh, wh) = tls.split(); ( BufReader::new(HttpReadHalf::Tls(rh)), @@ -514,7 +515,7 @@ impl HttpTransport { Err(scl_tlsio::Error::UnknownHostStrict) => { return Err(Error::TlsStrictUnknownHost(url.authority())) } - Err(e) => return Err(e.into()), + Err(e) => return Err(lost_if_timeout(e.into())), } } (true, None) => { @@ -552,7 +553,7 @@ impl HttpTransport { } } - let client = WireClient::handshake(r, w)?; + let client = WireClient::handshake(r, w).map_err(lost_if_timeout)?; ctl.set_read_timeout(None) .and_then(|()| ctl.set_write_timeout(None)) .map_err(|e| Error::ConnectionLost(format!("sc+http clear timeouts: {e}")))?; @@ -622,21 +623,47 @@ const CLIENT_OPENING_TIMEOUT: Duration = Duration::from_secs(30); /// unroutable or black-holed host fails in `timeout` rather than the OS default. fn connect_within(authority: &str, timeout: Duration) -> std::io::Result { use std::net::ToSocketAddrs; + let deadline = std::time::Instant::now() + timeout; let mut last = None; for addr in authority.to_socket_addrs()? { - match TcpStream::connect_timeout(&addr, timeout) { + // One deadline for the whole connect, not one bound per address: a + // dual-stack host with unreachable addresses must not take n x timeout. + let remaining = deadline.saturating_duration_since(std::time::Instant::now()); + if remaining.is_zero() { + break; + } + match TcpStream::connect_timeout(&addr, remaining) { Ok(s) => return Ok(s), Err(e) => last = Some(e), } } Err(last.unwrap_or_else(|| { std::io::Error::new( - std::io::ErrorKind::AddrNotAvailable, - "no addresses resolved", + std::io::ErrorKind::TimedOut, + "no address connected within the bound", ) })) } +/// A timeout from the client's opening bound, in whatever shape the layer that +/// hit it reported it (plain I/O from the wire framing, or wrapped by TLS), as +/// the one typed `ConnectionLost` a caller can match. Every other error passes +/// through unchanged. +fn lost_if_timeout(e: Error) -> Error { + let timed_out = |io: &std::io::Error| { + matches!( + io.kind(), + std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut + ) + }; + match &e { + Error::Io(io) | Error::Tls(scl_tlsio::Error::Io(io)) if timed_out(io) => { + Error::ConnectionLost("sc+http timed out waiting for the server".to_string()) + } + _ => e, + } +} + /// The `SC_HTTPS_*` environment knobs that are set but cannot apply to `url`: /// all of them configure TLS, so on a plaintext `sc+http://` URL they silently /// do nothing (a scheme-downgrade footgun worth a warning). Empty for an @@ -654,7 +681,13 @@ fn ignored_https_knobs(url: &ScHttpUrl, env: impl Fn(&str) -> Option) -> "SC_HTTPS_CA", ] .into_iter() - .filter(|k| env(k).is_some_and(|v| !v.is_empty())) + .filter(|k| match (*k, env(k)) { + // The CA knobs fail closed when present-but-empty (an unset shell + // variable), so presence alone is the signal; for the rest an empty + // value is the same as unset. + ("SC_HTTPS_CA_FILE" | "SC_HTTPS_CA", v) => v.is_some(), + (_, v) => v.is_some_and(|v| !v.is_empty()), + }) .collect() } @@ -2061,6 +2094,39 @@ mod tests { holder.join().unwrap(); } + /// The bound also covers the wire handshake: a server that sends the 200 + /// opening and then goes silent fails the connect as `ConnectionLost`. + #[test] + fn a_server_silent_after_the_opening_fails_the_wire_handshake_as_lost() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + let (tx, rx) = std::sync::mpsc::channel::<()>(); + let holder = std::thread::spawn(move || { + let (mut conn, _) = listener.accept().unwrap(); + // Read the client opening, answer 200, then say nothing. + let mut reader = BufReader::new(conn.try_clone().unwrap()); + let _ = read_client_opening(&mut reader); + write_status(&mut conn, 200).unwrap(); + let _ = rx.recv(); + }); + let url = ScHttpUrl { + host: "127.0.0.1".to_string(), + port, + path: "/repo".to_string(), + tls: false, + }; + let started = std::time::Instant::now(); + let err = HttpTransport::connect_bounded(&url, None, None, Duration::from_millis(300)) + .unwrap_err(); + assert!( + started.elapsed() < Duration::from_secs(10), + "unbounded: {err:?}" + ); + assert!(matches!(err, Error::ConnectionLost(_)), "got {err:?}"); + tx.send(()).unwrap(); + holder.join().unwrap(); + } + /// The same bound covers the TLS handshake: a peer that accepts and never /// answers the ClientHello fails the connect instead of hanging it. #[test] @@ -2094,6 +2160,7 @@ mod tests { started.elapsed() < Duration::from_secs(10), "the TLS handshake was not bounded: {err:?}" ); + assert!(matches!(err, Error::ConnectionLost(_)), "got {err:?}"); assert!( !home.join("known_hosts").exists(), "a failed handshake must not write a pin" @@ -2132,11 +2199,12 @@ mod tests { "SC_HTTPS_STRICT" => Some("1".to_string()), "SC_HTTPS_CA" => Some("system".to_string()), "SC_HTTPS_FINGERPRINT" => Some(String::new()), // empty counts as unset + "SC_HTTPS_CA_FILE" => Some(String::new()), // but not for a CA knob _ => None, }; assert_eq!( ignored_https_knobs(&plain, env), - vec!["SC_HTTPS_STRICT", "SC_HTTPS_CA"] + vec!["SC_HTTPS_STRICT", "SC_HTTPS_CA_FILE", "SC_HTTPS_CA"] ); assert!(ignored_https_knobs(&secure, env).is_empty()); assert!(ignored_https_knobs(&plain, |_| None).is_empty()); diff --git a/crates/repo/src/wire.rs b/crates/repo/src/wire.rs index a9a61001..bae08a5c 100644 --- a/crates/repo/src/wire.rs +++ b/crates/repo/src/wire.rs @@ -417,8 +417,11 @@ fn read_frame_inner(r: &mut impl Read) -> Result>> { ))); } let mut buf = vec![0u8; len]; - r.read_exact(&mut buf) - .map_err(|e| Error::Protocol(format!("read inside {len}-byte frame body failed: {e}")))?; + r.read_exact(&mut buf).map_err(|e| match e.kind() { + // A read timeout is the peer going quiet, not a malformed frame. + std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut => Error::Io(e), + _ => Error::Protocol(format!("read inside {len}-byte frame body failed: {e}")), + })?; Ok(Some(buf)) } diff --git a/crates/tlsio/src/stream.rs b/crates/tlsio/src/stream.rs index af1e6e09..f3c510b5 100644 --- a/crates/tlsio/src/stream.rs +++ b/crates/tlsio/src/stream.rs @@ -180,6 +180,14 @@ pub fn client_connect( .map_err(|e| Error::Handshake(e.to_string()))?; while conn.is_handshaking() { if let Err(e) = conn.complete_io(&mut tcp) { + // A socket timeout is the caller's bound firing, not a rejection: + // keep it typed so it can be told from a failed handshake. + if matches!( + e.kind(), + std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut + ) { + return Err(Error::Io(e)); + } // Translate our verifier's rejections into typed errors. let seen = *verifier.seen.lock().unwrap(); if let (Some(expected), Some(seen)) = (expected_pin, seen) { diff --git a/crates/tlsio/src/trust.rs b/crates/tlsio/src/trust.rs index 080e4478..489d8cc4 100644 --- a/crates/tlsio/src/trust.rs +++ b/crates/tlsio/src/trust.rs @@ -128,6 +128,12 @@ pub fn client_connect_ca( .map_err(|e| Error::Handshake(e.to_string()))?; while conn.is_handshaking() { if let Err(e) = conn.complete_io(&mut tcp) { + if matches!( + e.kind(), + std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut + ) { + return Err(Error::Io(e)); + } return Err(Error::Handshake(format!( "certificate validation for {host} failed: {e}" ))); From ec1399eb7db637e554e26d4d2660ec539b59adaf Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 19:44:43 +0300 Subject: [PATCH 3/7] fix(repo): give the wire handshake its own bound; test the lift for real Reviewer: the lift test passed with the lift removed, so it now inspects the live socket options (and fails without the lift). A bucket-backed server opens its bucket before answering HELLO, so the wire handshake gets 300 s while the connect, TLS and opening keep 30 s. Docs say these are inactivity bounds. Co-Authored-By: Claude Sonnet 5.5 --- .claude/skills/sc-cli-reference/SKILL.md | 3 +- ROADMAP.md | 10 ++-- crates/repo/src/http_transport.rs | 63 +++++++++++++++++------- crates/repo/src/stdio_transport.rs | 8 +++ docs/adr/0042-in-binary-tls-sc-https.md | 14 +++--- 5 files changed, 67 insertions(+), 31 deletions(-) diff --git a/.claude/skills/sc-cli-reference/SKILL.md b/.claude/skills/sc-cli-reference/SKILL.md index 5da2abfa..f0bc86d8 100644 --- a/.claude/skills/sc-cli-reference/SKILL.md +++ b/.claude/skills/sc-cli-reference/SKILL.md @@ -345,7 +345,8 @@ cargo run --bin sc -- serve --http --tls [--tls-cert --tls-k # --tls now — plaintext public needs # --allow-public (or --read-only) cargo run --bin sc -- serve fingerprint [] # print (minting if absent) the serve-TLS - # SPKI fingerprint (sha256:) + # SPKI fingerprint (sha256:); needs a repo + # (.sc/) — found from the cwd, or cargo run --bin sc -- clone sc+https://host[:port]/repo # TLS clone (P32); accept-new # TOFU: first connect pins into # ~/.config/sc/known_hosts (SC_HTTPS_KNOWN_HOSTS diff --git a/ROADMAP.md b/ROADMAP.md index 61124780..8ee5352b 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -909,11 +909,11 @@ scale-&-reach horizon): - **P32 follow-ons — shipped.** The four items the TLS work named: (1) `sc serve fingerprint` and `--tls` auto-mint refuse a root with no `.sc/` instead of minting there; (2) a stderr warning when an `SC_HTTPS_*` knob is set but the - URL is plaintext `sc+http://`; (3) the `sc+http(s)://` client bounds the - connect, TLS handshake, opening and wire handshake (30 s) so a silent peer - fails the connect, and lifts the bound afterwards (a read timeout is - `ConnectionLost`); (4) `sc remote add` parse-validates `sc+http(s)://` URLs - at add time like `ssh://` and the bucket schemes. The client bound is not + URL is plaintext `sc+http://`; (3) the `sc+http(s)://` client bounds the connect, TLS handshake, opening and + status line (30 s inactivity) and the wire handshake (300 s, for a bucket + server's slow open), so a silent peer fails the connect as `ConnectionLost`, + and lifts both afterwards; (4) `sc remote add` parse-validates `sc+http(s)://` URLs + at add time like `ssh://` and the bucket schemes. The client bounds are not configurable, and `read_frame_inner`'s frame-length allocation (covered by the `MAX_OBJECT_SIZE` cap since P28) is unchanged. diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index 8014aaf2..2fb9286a 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -381,20 +381,30 @@ impl HttpTransport { token: Option<&str>, policy: Option<&crate::tls_pins::TlsClientPolicy>, ) -> Result { - Self::connect_bounded(url, token, policy, CLIENT_OPENING_TIMEOUT) + Self::connect_bounded( + url, + token, + policy, + CLIENT_OPENING_TIMEOUT, + CLIENT_HANDSHAKE_TIMEOUT, + ) } - /// [`Self::connect_with_pins`] with the opening bound as a parameter, so a - /// test can use a short one. The connect, the TLS handshake, the opening - /// line, the status line and the wire handshake are all bounded by - /// `opening`: a peer that accepts and then goes silent fails the connect - /// instead of hanging it. Once the handshake is done the bound is lifted; - /// a long transfer is governed by the server's session timeout. + /// [`Self::connect_with_pins`] with the two bounds as parameters, so a test + /// can use short ones. `opening` covers the connect, the TLS handshake, the + /// opening line and the status line; `handshake` covers the wire handshake + /// that follows the 200, which can legitimately be slow (a bucket-backed + /// server opens its bucket before it answers HELLO). Both are inactivity + /// bounds on each read or write, not a total deadline: a peer that goes + /// silent fails the connect, instead of hanging it. Once the handshake is + /// done both are lifted; a long transfer is governed by the server's + /// session timeout. fn connect_bounded( url: &ScHttpUrl, token: Option<&str>, policy: Option<&crate::tls_pins::TlsClientPolicy>, opening: Duration, + handshake: Duration, ) -> Result { if let Some(t) = token { if t.contains(['\r', '\n']) { @@ -553,6 +563,9 @@ impl HttpTransport { } } + // The 200 is in: the wire handshake gets its own, longer, bound. + ctl.set_read_timeout(Some(handshake)) + .map_err(|e| Error::ConnectionLost(format!("sc+http set timeouts: {e}")))?; let client = WireClient::handshake(r, w).map_err(lost_if_timeout)?; ctl.set_read_timeout(None) .and_then(|()| ctl.set_write_timeout(None)) @@ -619,6 +632,11 @@ const OPENING_READ_TIMEOUT: Duration = Duration::from_secs(30); /// server's opening bound; lifted once the handshake completes. const CLIENT_OPENING_TIMEOUT: Duration = Duration::from_secs(30); +/// How long the client waits for each part of the wire handshake after the +/// server's 200. Longer than the opening bound because a bucket-backed server +/// opens its bucket (replaying a log tail) before it answers HELLO. +const CLIENT_HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(300); + /// `TcpStream::connect` with a bound on each resolved address, so an /// unroutable or black-holed host fails in `timeout` rather than the OS default. fn connect_within(authority: &str, timeout: Duration) -> std::io::Result { @@ -1447,6 +1465,9 @@ mod tests { (port, handle) } + /// A bound short enough for a silent-peer test to finish quickly. + const SHORT: Duration = Duration::from_millis(300); + fn tmp_repo(tag: &str) -> std::path::PathBuf { let root = std::env::temp_dir().join(format!("scl-http-{tag}-{}", std::process::id())); let _ = std::fs::remove_dir_all(&root); @@ -2082,8 +2103,7 @@ mod tests { }; let started = std::time::Instant::now(); - let err = HttpTransport::connect_bounded(&url, None, None, Duration::from_millis(300)) - .unwrap_err(); + let err = HttpTransport::connect_bounded(&url, None, None, SHORT, SHORT).unwrap_err(); assert!( started.elapsed() < Duration::from_secs(10), "the connect was not bounded: {err:?} after {:?}", @@ -2116,8 +2136,7 @@ mod tests { tls: false, }; let started = std::time::Instant::now(); - let err = HttpTransport::connect_bounded(&url, None, None, Duration::from_millis(300)) - .unwrap_err(); + let err = HttpTransport::connect_bounded(&url, None, None, SHORT, SHORT).unwrap_err(); assert!( started.elapsed() < Duration::from_secs(10), "unbounded: {err:?}" @@ -2154,8 +2173,7 @@ mod tests { let started = std::time::Instant::now(); let err = - HttpTransport::connect_bounded(&url, None, Some(&policy), Duration::from_millis(300)) - .unwrap_err(); + HttpTransport::connect_bounded(&url, None, Some(&policy), SHORT, SHORT).unwrap_err(); assert!( started.elapsed() < Duration::from_secs(10), "the TLS handshake was not bounded: {err:?}" @@ -2169,10 +2187,10 @@ mod tests { holder.join().unwrap(); } - /// The opening bound is lifted once the handshake is done: a connection - /// that then sits idle for longer than the bound still works. + /// Both client bounds are lifted once the handshake is done, so a long + /// transfer or a slow server reply is not cut off by them. #[test] - fn the_opening_bound_is_lifted_after_the_handshake() { + fn the_client_bounds_are_lifted_after_the_handshake() { let root = tmp_repo("opening-bound-lifted"); let port = spawn_real_http_server(root.clone()); let url = ScHttpUrl { @@ -2181,9 +2199,16 @@ mod tests { path: "/repo".to_string(), tls: false, }; - let transport = - HttpTransport::connect_bounded(&url, None, None, Duration::from_millis(300)).unwrap(); - std::thread::sleep(Duration::from_millis(900)); + let transport = HttpTransport::connect_bounded(&url, None, None, SHORT, SHORT).unwrap(); + // A timeout only fires while a read waits, so an idle sleep proves + // nothing: look at the socket options the live transport holds. + transport.client.with_halves(|r, w| match (r.get_ref(), w) { + (HttpReadHalf::Plain(rd), HttpWriteHalf::Plain(wr)) => { + assert_eq!(rd.read_timeout().unwrap(), None, "read bound not lifted"); + assert_eq!(wr.write_timeout().unwrap(), None, "write bound not lifted"); + } + _ => panic!("expected a plaintext transport"), + }); transport.list_refs().unwrap(); drop(transport); diff --git a/crates/repo/src/stdio_transport.rs b/crates/repo/src/stdio_transport.rs index 872bd529..06a4c11a 100644 --- a/crates/repo/src/stdio_transport.rs +++ b/crates/repo/src/stdio_transport.rs @@ -21,6 +21,14 @@ pub struct WireClient { } impl WireClient { + /// Test hook: look at the two halves (e.g. the socket options behind them) + /// without issuing a request. + #[cfg(test)] + pub(crate) fn with_halves(&self, f: impl FnOnce(&R, &W) -> T) -> T { + let rw = self.rw.borrow(); + f(&rw.0, &rw.1) + } + /// Exchange HELLOs and return a ready client. Fails typed: version skew is /// `Protocol`, a served non-repo path is `NotARepo`, a dead peer is /// `ConnectionLost`. diff --git a/docs/adr/0042-in-binary-tls-sc-https.md b/docs/adr/0042-in-binary-tls-sc-https.md index d07fa7f5..c571f0da 100644 --- a/docs/adr/0042-in-binary-tls-sc-https.md +++ b/docs/adr/0042-in-binary-tls-sc-https.md @@ -180,12 +180,14 @@ channel was always a weaker guarantee than the gate's wording implied. - **A downgrade is reported.** When an `SC_HTTPS_*` knob (strict, fingerprint, known-hosts, CA file or CA) is set and the URL is plaintext `sc+http://`, the client prints a warning, since the knob silently does nothing there. -- **The client is bounded.** The connect, the TLS handshake, the opening line, - the status line and the wire handshake share a 30 s bound, so a peer that - accepts and goes silent fails the connect instead of hanging it (a read - timeout is `ConnectionLost`). The bound is lifted once the handshake is done; - a long transfer is governed by the server's session timeout. It is not - configurable. +- **The client is bounded.** The connect, the TLS handshake, the opening line and + the status line each get a 30 s inactivity bound, and the wire handshake after + the 200 gets 300 s (a bucket-backed server opens its bucket before it answers + HELLO), so a peer that accepts and goes silent fails the connect as + `ConnectionLost` instead of hanging it. These bound each read or write, not + total time, and the connect shares one deadline across resolved addresses. Both + bounds are lifted once the handshake is done; a long transfer is governed by + the server's session timeout. They are not configurable. - **`sc remote add` validates.** `sc+http://` and `sc+https://` URLs are parsed at add time, like `ssh://` and the bucket schemes. From 987f6bab8fd81b4f837e0776bdcfd5c2373ccd9e Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 19:45:07 +0300 Subject: [PATCH 4/7] fix(repo): print the plaintext-URL knob warning once per process Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/http_transport.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index 2fb9286a..b586d0b1 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -352,7 +352,10 @@ impl HttpTransport { /// env-free core. pub fn connect_with_token(url: &ScHttpUrl, token: Option<&str>) -> Result { let ignored = ignored_https_knobs(url, |k| std::env::var(k).ok()); - if !ignored.is_empty() { + // Once per process: a command that connects repeatedly (batched lazy + // fetches) should not repeat the same warning. + static WARNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + if !ignored.is_empty() && !WARNED.swap(true, std::sync::atomic::Ordering::Relaxed) { eprintln!( "warning: {} set but {} is plaintext sc+http:// — it has no effect; use an \ sc+https:// URL for TLS", From 1b1fe9a1b7bec9451dff70cd1b9c41bd9d716a20 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 19:49:08 +0300 Subject: [PATCH 5/7] fix(repo): bound name resolution in the connect; see non-UTF-8 HTTPS knobs Codex: to_socket_addrs ran before the deadline could apply, so a stalled resolver defeated the connect bound; and var() hid a non-UTF-8 SC_HTTPS_* value from the downgrade warning. Resolve on a helper thread under the deadline and read the knobs with var_os. Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/http_transport.rs | 57 ++++++++++++++++++++++--- docs/adr/0042-in-binary-tls-sc-https.md | 3 +- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index b586d0b1..e0075c9b 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -351,7 +351,7 @@ impl HttpTransport { /// and delegated to [`Self::connect_with_pins`], the testable, /// env-free core. pub fn connect_with_token(url: &ScHttpUrl, token: Option<&str>) -> Result { - let ignored = ignored_https_knobs(url, |k| std::env::var(k).ok()); + let ignored = ignored_https_knobs(url, |k| std::env::var_os(k)); // Once per process: a command that connects repeatedly (batched lazy // fetches) should not repeat the same warning. static WARNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); @@ -645,8 +645,23 @@ const CLIENT_HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(300); fn connect_within(authority: &str, timeout: Duration) -> std::io::Result { use std::net::ToSocketAddrs; let deadline = std::time::Instant::now() + timeout; + // Name resolution is a blocking libc call with no timeout of its own, so it + // runs on a helper thread the connect only waits on up to the deadline. A + // resolver that stalls leaves that thread parked until it returns; the + // connect itself fails on time. + let (tx, rx) = std::sync::mpsc::channel(); + let target = authority.to_string(); + std::thread::spawn(move || { + let _ = tx.send(target.to_socket_addrs().map(|a| a.collect::>())); + }); + let addrs = rx.recv_timeout(timeout).map_err(|_| { + std::io::Error::new( + std::io::ErrorKind::TimedOut, + "name resolution did not finish within the bound", + ) + })??; let mut last = None; - for addr in authority.to_socket_addrs()? { + for addr in addrs { // One deadline for the whole connect, not one bound per address: a // dual-stack host with unreachable addresses must not take n x timeout. let remaining = deadline.saturating_duration_since(std::time::Instant::now()); @@ -690,7 +705,10 @@ fn lost_if_timeout(e: Error) -> Error { /// do nothing (a scheme-downgrade footgun worth a warning). Empty for an /// `sc+https://` URL. `env` is injected so this is testable without mutating /// the process environment. -fn ignored_https_knobs(url: &ScHttpUrl, env: impl Fn(&str) -> Option) -> Vec<&'static str> { +fn ignored_https_knobs( + url: &ScHttpUrl, + env: impl Fn(&str) -> Option, +) -> Vec<&'static str> { if url.tls { return Vec::new(); } @@ -2117,6 +2135,21 @@ mod tests { holder.join().unwrap(); } + /// `connect_within` resolves the name under the deadline and reports a + /// failure to connect as an error rather than hanging. + #[test] + fn connect_within_reaches_a_listener_and_fails_on_a_closed_port() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + assert!(connect_within(&format!("127.0.0.1:{port}"), SHORT * 10).is_ok()); + drop(listener); + assert!(connect_within(&format!("127.0.0.1:{port}"), SHORT * 10).is_err()); + // A name that cannot resolve is an error too, within the bound. + let started = std::time::Instant::now(); + assert!(connect_within("host.invalid:1", Duration::from_secs(5)).is_err()); + assert!(started.elapsed() < Duration::from_secs(10)); + } + /// The bound also covers the wire handshake: a server that sends the 200 /// opening and then goes silent fails the connect as `ConnectionLost`. #[test] @@ -2224,10 +2257,10 @@ mod tests { let plain = ScHttpUrl::parse("sc+http://127.0.0.1:1/x").unwrap(); let secure = ScHttpUrl::parse("sc+https://127.0.0.1:1/x").unwrap(); let env = |k: &str| match k { - "SC_HTTPS_STRICT" => Some("1".to_string()), - "SC_HTTPS_CA" => Some("system".to_string()), - "SC_HTTPS_FINGERPRINT" => Some(String::new()), // empty counts as unset - "SC_HTTPS_CA_FILE" => Some(String::new()), // but not for a CA knob + "SC_HTTPS_STRICT" => Some("1".into()), + "SC_HTTPS_CA" => Some("system".into()), + "SC_HTTPS_FINGERPRINT" => Some(std::ffi::OsString::new()), // empty counts as unset + "SC_HTTPS_CA_FILE" => Some(std::ffi::OsString::new()), // but not for a CA knob _ => None, }; assert_eq!( @@ -2236,6 +2269,16 @@ mod tests { ); assert!(ignored_https_knobs(&secure, env).is_empty()); assert!(ignored_https_knobs(&plain, |_| None).is_empty()); + + // A value that is not valid UTF-8 is still a value that was set. + #[cfg(unix)] + { + use std::os::unix::ffi::OsStringExt; + let odd = |k: &str| { + (k == "SC_HTTPS_CA_FILE").then(|| std::ffi::OsString::from_vec(vec![0xff, 0xfe])) + }; + assert_eq!(ignored_https_knobs(&plain, odd), vec!["SC_HTTPS_CA_FILE"]); + } } // ── Task 5: client-side `SC_HTTP_TOKEN` support (`connect_with_token`). ── diff --git a/docs/adr/0042-in-binary-tls-sc-https.md b/docs/adr/0042-in-binary-tls-sc-https.md index c571f0da..ad53e47a 100644 --- a/docs/adr/0042-in-binary-tls-sc-https.md +++ b/docs/adr/0042-in-binary-tls-sc-https.md @@ -185,7 +185,8 @@ channel was always a weaker guarantee than the gate's wording implied. the 200 gets 300 s (a bucket-backed server opens its bucket before it answers HELLO), so a peer that accepts and goes silent fails the connect as `ConnectionLost` instead of hanging it. These bound each read or write, not - total time, and the connect shares one deadline across resolved addresses. Both + total time, and the connect (name resolution included, on a helper thread) shares one + deadline across resolved addresses. Both bounds are lifted once the handshake is done; a long transfer is governed by the server's session timeout. They are not configurable. - **`sc remote add` validates.** `sc+http://` and `sc+https://` URLs are parsed From ab830f18cd41bf70fb679162aedc27367d2c8c59 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 19:55:00 +0300 Subject: [PATCH 6/7] fix(repo): report an opening-write timeout as ConnectionLost Codex: write_client_opening turned every write failure into InvalidArgument, so a peer that never reads (opening larger than the socket buffers) hit the new write bound but got the wrong typed error. Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/http_transport.rs | 48 ++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index e0075c9b..007749b6 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -78,16 +78,24 @@ pub(crate) fn write_client_opening( path: &str, bearer: Option<&str>, ) -> Result<()> { + // A write timeout is the peer not reading (the client's bound firing), a + // lost connection like the read-side timeout; any other failure is a bad + // opening. + let werr = |e: std::io::Error| match e.kind() { + std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut => { + Error::ConnectionLost("timed out writing the HTTP opening".to_string()) + } + _ => Error::InvalidArgument(format!("HTTP opening write failed: {e}")), + }; write!( w, "POST {path} HTTP/1.1\r\nHost: {host}\r\nUser-Agent: sc/2\r\n" ) - .map_err(|e| Error::InvalidArgument(format!("HTTP opening write failed: {e}")))?; + .map_err(werr)?; if let Some(tok) = bearer { - write!(w, "Authorization: Bearer {tok}\r\n") - .map_err(|e| Error::InvalidArgument(format!("HTTP opening write failed: {e}")))?; + write!(w, "Authorization: Bearer {tok}\r\n").map_err(werr)?; } - write!(w, "\r\n").map_err(|e| Error::InvalidArgument(format!("HTTP opening write failed: {e}"))) + write!(w, "\r\n").map_err(werr) } /// SERVER: read the request line + headers up to the blank line (bounded by @@ -2150,6 +2158,38 @@ mod tests { assert!(started.elapsed() < Duration::from_secs(10)); } + /// A peer that accepts but never reads must fail the connect when the + /// opening does not fit the socket buffers, as `ConnectionLost` like every + /// other timeout (not as a bad-opening `InvalidArgument`). + #[test] + fn a_peer_that_never_reads_fails_the_opening_write_as_lost() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + let (tx, rx) = std::sync::mpsc::channel::<()>(); + let holder = std::thread::spawn(move || { + let _conn = listener.accept().unwrap(); + let _ = rx.recv(); + }); + let url = ScHttpUrl { + host: "127.0.0.1".to_string(), + port, + path: "/repo".to_string(), + tls: false, + }; + // Far larger than any loopback send + receive buffer. + let huge = "t".repeat(64 * 1024 * 1024); + let started = std::time::Instant::now(); + let err = + HttpTransport::connect_bounded(&url, Some(&huge), None, SHORT, SHORT).unwrap_err(); + assert!( + started.elapsed() < Duration::from_secs(10), + "unbounded: {err:?}" + ); + assert!(matches!(err, Error::ConnectionLost(_)), "got {err:?}"); + tx.send(()).unwrap(); + holder.join().unwrap(); + } + /// The bound also covers the wire handshake: a server that sends the 200 /// opening and then goes silent fails the connect as `ConnectionLost`. #[test] From 681d5ea4e2a26cd584ee8cd7f30aa8e37aec55d0 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 20:01:22 +0300 Subject: [PATCH 7/7] fix(repo): give each connect attempt an even share of the deadline Codex: with one shared deadline, a first address that silently drops packets could use all of it and starve a reachable later address. Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/http_transport.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index 007749b6..a57cf7d4 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -669,14 +669,18 @@ fn connect_within(authority: &str, timeout: Duration) -> std::io::Result return Ok(s), Err(e) => last = Some(e), } @@ -689,6 +693,13 @@ fn connect_within(authority: &str, timeout: Duration) -> std::io::Result Duration { + remaining / u32::try_from(left.max(1)).unwrap_or(u32::MAX) +} + /// A timeout from the client's opening bound, in whatever shape the layer that /// hit it reported it (plain I/O from the wire framing, or wrapped by TLS), as /// the one typed `ConnectionLost` a caller can match. Every other error passes @@ -2158,6 +2169,22 @@ mod tests { assert!(started.elapsed() < Duration::from_secs(10)); } + #[test] + fn each_connect_attempt_gets_an_even_share_of_the_deadline() { + let t = Duration::from_secs(30); + assert_eq!(attempt_share(t, 1), t, "the only address gets everything"); + assert_eq!(attempt_share(t, 2), Duration::from_secs(15)); + assert_eq!(attempt_share(t, 3), Duration::from_secs(10)); + // Run three attempts back to back, each using its whole share: the + // deadline is used up exactly, never exceeded. + let mut left = t; + for remaining in (1..=3).rev() { + left -= attempt_share(left, remaining); + } + assert_eq!(left, Duration::ZERO); + assert_eq!(attempt_share(t, 0), t, "no divide by zero"); + } + /// A peer that accepts but never reads must fail the connect when the /// opening does not fit the socket buffers, as `ConnectionLost` like every /// other timeout (not as a bad-opening `InvalidArgument`).