diff --git a/ROADMAP.md b/ROADMAP.md index 36b2bd80..5499c3aa 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -865,10 +865,9 @@ scale-&-reach horizon): skip majors on these (minor/patch security updates still flow). Migrating the whole stack to current majors is a deliberate, tested pass, not an automated bump; likewise `toml` 0.8 → 1.x and any pinned-toolchain bump. -- **`404`-before-auth repo-presence oracle (P29).** `handle_http_connection` - checks `.sc/` presence before the bearer-auth gate, so an unauthenticated - client can distinguish "repo here, need a token" (401) from "no repo here" - (404) — a minor information leak, not a read/write bypass. Deferred. +- **`404`-before-auth repo-presence oracle — closed.** `handle_http_connection` + now checks repo presence after the bearer-auth gate (ADR-0040), so behind + mandatory auth an unauthenticated probe sees `401` either way. - **`--transcript auto` probing (P30).** `sc ws harvest --transcript ` requires an explicit path today; auto-discovering a conventional transcript location (e.g. an agent-runner-written temp diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index 697d4b8b..28e95ccb 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -1031,11 +1031,6 @@ fn handle_http_connection( // The request-target isn't used to route (one repo per listener). let _ = &opening.target; - if !root.join(".sc").is_dir() { - write_status(&mut writer, 404)?; - return Ok(()); - } - // Auth gate (P29): if ≥1 token is configured, a valid bearer is // REQUIRED on every connection, loopback included — the bind gate only // decides whether the port may be opened at all, not who may use it @@ -1043,15 +1038,39 @@ fn handle_http_connection( // pre-P29 behavior). A matched token's scope sets this connection's // read-only flag; `--read-only` below is a floor an `rw` token cannot // elevate. - let tokens = crate::serve_tokens::load(&crate::layout::Layout::at(root))?; + // Tokens live under `.sc/`; without a repo directory there are none, and + // reading through a non-directory `.sc` would error and drop the + // connection with no status instead of answering below. + let has_repo = root.join(".sc").is_dir(); + let tokens = if has_repo { + match crate::serve_tokens::load(&crate::layout::Layout::at(root)) { + Ok(tokens) => tokens, + // A token store that cannot be read is a refusal, not a dropped + // connection: behind mandatory auth every unauthenticated client + // gets the same 401, so a broken store is not a presence signal. + Err(e) if mandatory_auth => { + eprintln!("sc serve --http: refusing connection — token store unavailable: {e}"); + write_status(&mut writer, 401)?; + return Ok(()); + } + Err(e) => return Err(e), + } + } else { + Vec::new() + }; let token_read_only = if tokens.is_empty() { if mandatory_auth { // The public bind's sole justification (tokens) was removed while // running — fail closed, do NOT serve an open unauthenticated server. + let why = if has_repo { + "none are configured (re-add a token, or restart with --read-only / \ + --allow-public)" + } else { + "the served root has no `.sc/` directory (the repo was removed while running)" + }; eprintln!( "sc serve --http: refusing connection — this non-loopback server was \ - justified only by tokens, but none are configured (re-add a token, or \ - restart with --read-only / --allow-public)" + justified only by tokens, but {why}" ); write_status(&mut writer, 401)?; return Ok(()); @@ -1072,6 +1091,16 @@ fn handle_http_connection( } }; + // Repo presence is checked only AFTER the auth gate, so a client that does + // not hold a valid token sees the same 401 whether or not a repo is served + // here. Tokens live under `.sc/`, so a bearer can only be required when the + // repo exists; with no tokens configured (open loopback, `--read-only` or + // `--allow-public`) there is no gate to leak around and the 404 stays. + if !has_repo { + write_status(&mut writer, 404)?; + return Ok(()); + } + write_status(&mut writer, 200)?; // P31: the opening timeout is REPLACED (not cleared) by the session @@ -1828,6 +1857,46 @@ mod tests { let _ = std::fs::remove_dir_all(&root); } + /// A mandatory-auth listener (tokens are the sole justification for its + /// public bind) answers 401 to everyone when its root holds no repo, not + /// 404: an unauthenticated probe must not learn whether a repo is served. + /// With auth off (the loopback default) the 404 stays, as there is no + /// gate to leak around. + #[test] + fn missing_repo_is_401_behind_mandatory_auth_and_404_without_it() { + let bare = std::env::temp_dir().join(format!("scl-http-oracle-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&bare); + std::fs::create_dir_all(&bare).unwrap(); + + let gated = spawn_real_http_server_policy_auth(bare.clone(), false, true); + assert_eq!(connect_raw_status(gated, None), 401); + assert_eq!(connect_raw_status(gated, Some("sct-not-a-real-token")), 401); + + let open = spawn_real_http_server_policy_auth(bare.clone(), false, false); + assert_eq!(connect_raw_status(open, None), 404); + + // A `.sc` that is a regular file is no repo either: still a status, + // never a dropped connection from the token read. + std::fs::write(bare.join(".sc"), b"not a directory").unwrap(); + assert_eq!(connect_raw_status(open, None), 404); + assert_eq!(connect_raw_status(gated, None), 401); + + std::fs::remove_dir_all(&bare).unwrap(); + assert!(!bare.exists(), "test must leave no residue"); + + // A malformed token store behind mandatory auth is a 401 too. + let broken = tmp_repo("oracle-broken-tokens"); + std::fs::write( + crate::layout::Layout::at(&broken).serve_tokens_path(), + b"this is = not [valid toml", + ) + .unwrap(); + let gated = spawn_real_http_server_policy_auth(broken.clone(), false, true); + assert_eq!(connect_raw_status(gated, None), 401); + std::fs::remove_dir_all(&broken).unwrap(); + assert!(!broken.exists(), "test must leave no residue"); + } + // ── 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/THREAT-MODEL.md b/docs/THREAT-MODEL.md index 4c063ebc..ba8d9ed0 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -277,10 +277,11 @@ too, not just the src-control-side metadata. - See `docs/research/tls-options-sc-http.md` §B for the full comparison (nginx/HAProxy/stunnel capabilities, Caddy's non-bundled layer-4 caveat) that this guidance is drawn from. -- **Minor pre-auth information leak (both `sc+http://` and `sc+https://`):** - a `404` (no repo) is written before the auth gate, so an unauthenticated - client can distinguish "a repo is served here" (`401`) from "no repo" - (`404`). No content is exposed. +- **Repo presence is not observable from pre-auth status responses (both `sc+http://` and `sc+https://`):** + the `404` (no repo) is written after the auth gate, so behind mandatory auth an + unauthenticated client gets `401` whether or not a repo is served (timing is not + equalized: a present repo costs a token-file read first). With no tokens + configured there is no auth to leak around and a missing repo still answers `404`. - **Not proxy/CDN-safe** (raw post-opening protocol, both plaintext and TLS variants) — this remains open, deferred in [`ROADMAP.md`](../ROADMAP.md). - **Listener resource bounds — closed by P31 (ADR-0041).** The three operational- diff --git a/docs/adr/0040-sc-http-access-control.md b/docs/adr/0040-sc-http-access-control.md index bc76aa7e..98be7667 100644 --- a/docs/adr/0040-sc-http-access-control.md +++ b/docs/adr/0040-sc-http-access-control.md @@ -85,7 +85,11 @@ the `Authorization` header. `wire::serve` stays a thin wrapper delegating to crosses the wire in plaintext, so a public deployment must front with a TLS reverse proxy. Nor does it defend against a leaked token (bearer, reusable until removed) or a malicious holder of a valid token. Not HTTP-proxy/CDN-safe (the raw post-opening protocol), as P26. -- **Minor pre-auth information leak:** the `.sc`-missing `404` is written before the auth - gate, so an unauthenticated client can distinguish "a repo is served here" (`401`) from - "no repo here" (`404`) — repo *presence* is observable pre-auth, though no content is. - Ordering the `404` after the auth check would close it; deferred (ROADMAP). +- **Pre-auth repo-presence leak — closed.** The `.sc`-missing `404` used to be written + before the auth gate, so an unauthenticated client could tell "a repo is served here" + (`401`) from "no repo here" (`404`). The `404` now follows the gate: behind mandatory auth + a client without a valid token gets `401` either way. With no tokens configured (open + loopback, `--read-only`, `--allow-public`) there is no gate to leak around and the `404` + remains. A token store that cannot be read or parsed is a `401` there too, not a dropped connection. The two answers are status-indistinguishable, not timing-identical. A client + holding a valid token sees the `401` authentication error rather than `NotARepo` if the + repo is removed while the server runs.