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 5499c3aa..8ee5352b 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 + 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. - **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..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,6 +3913,12 @@ fn run_remote(op: RemoteOp) -> Result<()> { if url.starts_with("ssh://") { scl_repo::SshUrl::parse(&url)?; // fail fast on malformed URLs } + // 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://") { 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..86d8b310 --- /dev/null +++ b/crates/cli/tests/remote_add_validation.rs @@ -0,0 +1,96 @@ +//! `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", + "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"); + 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"); +} + +/// 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 28e95ccb..a57cf7d4 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(), @@ -72,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 @@ -345,6 +359,18 @@ 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_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); + 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", + ignored.join(", "), + url.authority() + ); + } let policy = if url.tls { Some(crate::tls_pins::TlsClientPolicy::from_env()?) } else { @@ -365,6 +391,31 @@ impl HttpTransport { url: &ScHttpUrl, token: Option<&str>, policy: Option<&crate::tls_pins::TlsClientPolicy>, + ) -> Result { + Self::connect_bounded( + url, + token, + policy, + CLIENT_OPENING_TIMEOUT, + CLIENT_HANDSHAKE_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']) { @@ -385,8 +436,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, _) => { @@ -422,7 +483,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)), @@ -474,7 +536,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) => { @@ -512,7 +574,13 @@ impl HttpTransport { } } - let client = WireClient::handshake(r, w)?; + // 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)) + .map_err(|e| Error::ConnectionLost(format!("sc+http clear timeouts: {e}")))?; Ok(HttpTransport { client }) } } @@ -570,6 +638,117 @@ 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); + +/// 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 { + 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; + let total = addrs.len(); + for (i, addr) in addrs.into_iter().enumerate() { + // One deadline for the whole connect, not one bound per address: a + // dual-stack host with unreachable addresses must not take n x timeout. + // Each attempt gets an even share of what is left, so a first address + // that silently drops packets cannot starve a reachable later one. + let remaining = deadline.saturating_duration_since(std::time::Instant::now()); + if remaining.is_zero() { + break; + } + let slice = attempt_share(remaining, total - i); + match TcpStream::connect_timeout(&addr, slice) { + Ok(s) => return Ok(s), + Err(e) => last = Some(e), + } + } + Err(last.unwrap_or_else(|| { + std::io::Error::new( + std::io::ErrorKind::TimedOut, + "no address connected within the bound", + ) + })) +} + +/// The share of `remaining` one connect attempt may use when `left` addresses +/// (itself included) are still to try: an even split, so every address gets a +/// turn inside the overall deadline. +fn attempt_share(remaining: Duration, left: usize) -> 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 +/// 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 +/// `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| 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() +} + /// 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 +935,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 +943,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)) } @@ -1310,6 +1505,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); @@ -1897,6 +2095,259 @@ 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, SHORT, SHORT).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(); + } + + /// `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)); + } + + #[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`). + #[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] + 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, 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 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), SHORT, SHORT).unwrap_err(); + assert!( + 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" + ); + tx.send(()).unwrap(); + holder.join().unwrap(); + } + + /// 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_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 { + host: "127.0.0.1".to_string(), + port, + path: "/repo".to_string(), + tls: false, + }; + 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); + + 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".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!( + ignored_https_knobs(&plain, env), + 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()); + + // 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`). ── /// `HttpTransport::connect_with_token` presents the given bearer and maps 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/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}" ))); diff --git a/docs/adr/0042-in-binary-tls-sc-https.md b/docs/adr/0042-in-binary-tls-sc-https.md index a1c7afa7..ad53e47a 100644 --- a/docs/adr/0042-in-binary-tls-sc-https.md +++ b/docs/adr/0042-in-binary-tls-sc-https.md @@ -172,6 +172,26 @@ 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 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 (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 + at add time, like `ssh://` and the bucket schemes. + ## Rejected alternatives - **aws-lc-rs as the primary provider.** Rejected for its heavier build