From a169b0241bac467a3abaa80b9d36adc1ea973e4e Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 18:18:19 +0300 Subject: [PATCH 1/5] fix(repo): answer 401 before 404 on a mandatory-auth listener The .sc-missing 404 was written before the bearer-auth gate, so an unauthenticated client could tell a served repo (401) from none (404). The check now follows the gate; with auth off the 404 stays. Co-Authored-By: Claude Sonnet 5.5 --- ROADMAP.md | 7 ++--- crates/repo/src/http_transport.rs | 37 +++++++++++++++++++++---- docs/THREAT-MODEL.md | 8 +++--- docs/adr/0040-sc-http-access-control.md | 10 ++++--- 4 files changed, 45 insertions(+), 17 deletions(-) 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..c1b03780 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 @@ -1072,6 +1067,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 !root.join(".sc").is_dir() { + 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 +1833,28 @@ 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); + + std::fs::remove_dir_all(&bare).unwrap(); + assert!(!bare.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..c3b3cbce 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -277,10 +277,10 @@ 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 pre-auth (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. 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..98da8e24 100644 --- a/docs/adr/0040-sc-http-access-control.md +++ b/docs/adr/0040-sc-http-access-control.md @@ -85,7 +85,9 @@ 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. From c3b1ae1df68cd6b5dec0876bda01d9f5d0dcdab6 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 18:24:13 +0300 Subject: [PATCH 2/5] fix(repo): skip the token read when .sc is not a directory Codex: a .sc that is a regular file made serve_tokens::load fail with NotADirectory, dropping the connection without a status instead of the 404. Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/http_transport.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index c1b03780..e1087aa6 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -1038,7 +1038,15 @@ 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 { + crate::serve_tokens::load(&crate::layout::Layout::at(root))? + } else { + Vec::new() + }; let token_read_only = if tokens.is_empty() { if mandatory_auth { // The public bind's sole justification (tokens) was removed while @@ -1072,7 +1080,7 @@ fn handle_http_connection( // 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 !root.join(".sc").is_dir() { + if !has_repo { write_status(&mut writer, 404)?; return Ok(()); } @@ -1851,6 +1859,12 @@ mod tests { 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"); } From d9afe718c3920a20ba37732c5b503cee39121a0c Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 18:25:19 +0300 Subject: [PATCH 3/5] docs(repo): name the removed-repo cause in the 401 log; note the client-visible change Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/http_transport.rs | 9 +++++++-- docs/adr/0040-sc-http-access-control.md | 4 +++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index e1087aa6..8caa6527 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -1051,10 +1051,15 @@ fn handle_http_connection( 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(()); diff --git a/docs/adr/0040-sc-http-access-control.md b/docs/adr/0040-sc-http-access-control.md index 98da8e24..69fe07d2 100644 --- a/docs/adr/0040-sc-http-access-control.md +++ b/docs/adr/0040-sc-http-access-control.md @@ -90,4 +90,6 @@ the `Authorization` header. `wire::serve` stays a thin wrapper delegating to (`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. + remains. 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. From 53862f906df96476e5eb7afdec02fd2a4f22ac3f Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 18:37:35 +0300 Subject: [PATCH 4/5] docs: scope the repo-presence claim to status responses CodeRabbit: the status codes are indistinguishable, timing is not. Co-Authored-By: Claude Sonnet 5.5 --- docs/THREAT-MODEL.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index c3b3cbce..ba8d9ed0 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -277,9 +277,10 @@ 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. -- **Repo presence is not observable pre-auth (both `sc+http://` and `sc+https://`):** +- **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. With no tokens + 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). From 8d6fb6772c362f55ee3b03f9d987c20a4121d18a Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 18:48:05 +0300 Subject: [PATCH 5/5] fix(repo): 401, not a dropped connection, when the token store is unreadable under mandatory auth CodeRabbit: a malformed or unreadable serve-tokens.toml dropped the connection with no status, which a probe could tell from the missing-repo 401. Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/http_transport.rs | 25 ++++++++++++++++++++++++- docs/adr/0040-sc-http-access-control.md | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index 8caa6527..28e95ccb 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -1043,7 +1043,18 @@ fn handle_http_connection( // connection with no status instead of answering below. let has_repo = root.join(".sc").is_dir(); let tokens = if has_repo { - crate::serve_tokens::load(&crate::layout::Layout::at(root))? + 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() }; @@ -1872,6 +1883,18 @@ mod tests { 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`). ── diff --git a/docs/adr/0040-sc-http-access-control.md b/docs/adr/0040-sc-http-access-control.md index 69fe07d2..98be7667 100644 --- a/docs/adr/0040-sc-http-access-control.md +++ b/docs/adr/0040-sc-http-access-control.md @@ -90,6 +90,6 @@ the `Authorization` header. `wire::serve` stays a thin wrapper delegating to (`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. The two answers are status-indistinguishable, not timing-identical. A client + 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.