From 6c1fe8622eb8c518e477dd035980e0dd4b73d89e Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 10 Jul 2026 14:09:03 +0300 Subject: [PATCH 01/14] docs(specs): P32 sc+https design (#39) Claude-Session: https://claude.ai/code/session_01LhadyW9scQL95h3ag9yySB --- .../2026-07-10-p32-tls-sc-https-design.md | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 docs/superpowers/specs/2026-07-10-p32-tls-sc-https-design.md diff --git a/docs/superpowers/specs/2026-07-10-p32-tls-sc-https-design.md b/docs/superpowers/specs/2026-07-10-p32-tls-sc-https-design.md new file mode 100644 index 00000000..332686a8 --- /dev/null +++ b/docs/superpowers/specs/2026-07-10-p32-tls-sc-https-design.md @@ -0,0 +1,159 @@ +# P32 design: in-binary TLS — `sc+https://` via rustls (`crates/tlsio`) + +- **Issue:** [#39](https://github.com/git-agentic/src-control/issues/39) — closes + audit High #1 (plaintext bearer tokens + repo traffic on `sc+http://`), part of + the #24 audit-response map. +- **Locked upstream decisions:** #26 (transport = in-binary rustls, ring + provider), #35 (cert/key provisioning + accept-new TOFU pinning UX), research + in `docs/research/tls-options-sc-http.md`. +- **Decided during brainstorm (2026-07-10):** + 1. **TLS bind gate:** a non-loopback bind with `--tls` is justified iff ≥1 + serve token is configured (or `--read-only`/`--allow-public`, as today). + TLS alone without any auth still requires an explicit `--allow-public`. + 2. **Strict-mode knob:** `SC_HTTPS_STRICT=1` environment variable, checked at + the single client seam — matching the `SC_HTTP_TOKEN`/`SC_PACK_CHUNK`/ + `SC_SSH`/`SC_GIT` env-knob precedent, rather than threading a flag through + every network command. + +## 1. New crate: `crates/tlsio` (`scl-tlsio`) + +The only crate linking `rustls` and `rcgen` (the gix→gitio / RustCrypto→crypto +quarantine precedent). rustls is pinned with `default-features = false`, +features `ring, std, tls12, logging` (~14 new crates measured vs `Cargo.lock`; +no cmake — ring needs a C compiler only). aws-lc-rs is recorded in the ADR as +the swap-in fallback provider. + +`tlsio` is a workspace dependency **leaf**: it depends on no other workspace +crate (unlike its siblings vfs/gitio/crypto, which depend on core). The +dependency rule gains one edge — `repo → tlsio` — becoming +`cli → repo → {vfs, gitio, crypto, tlsio}`, with `{vfs, gitio, crypto} → core` +as before. The CLI reaches TLS functionality only through `repo` helpers +(single consumer). + +Public surface: + +- `TlsClientStream` / `TlsServerStream` — thin wrappers over + `rustls::StreamOwned<…, TcpStream>` implementing `io::Read + io::Write`, with + `get_ref()`/`get_mut()` passthrough to the inner `TcpStream` so + `http_transport` keeps setting the P31 socket timeouts below TLS. +- `client_connect(tcp: TcpStream, host: &str, expected_pin: Option<[u8; 32]>, + strict: bool) -> Result<(TlsClientStream, SeenPin)>` — installs the + hand-written (~100-line) `rustls::client::danger::ServerCertVerifier`: + - `expected_pin = Some(p)`: a leaf whose SPKI-SHA-256 ≠ `p` fails **during + the handshake**. + - `expected_pin = None`, `strict = true`: handshake refused + (`UnknownHostStrict`). + - `expected_pin = None`, accept-new: handshake completes; the observed SPKI + hash is returned for the caller to persist and announce. No application + byte (opening, bearer token) is written before pin disposition is settled. + - The verifier checks the key only — names and validity windows are + deliberately ignored (pin-only trust in v1). +- `server_stream(tcp: TcpStream, certs, key) -> Result`. +- `load_or_mint(dir: &Path) -> Result<(certs, key, spki)>` — loads + `cert.pem` + `key.pem` from the given dir (`.sc/serve-tls/`), or rcgen-mints + a long-validity self-signed pair only when missing (the key IS the identity; + key file written 0600). +- `spki_sha256(cert) -> [u8; 32]` + a `sha256:` display helper — the one + fingerprint format used by the banner, `sc serve fingerprint`, the pin file, + and `SC_HTTPS_FINGERPRINT`; openssl-verifiable + (`openssl x509 -pubkey | openssl pkey -pubin -outform der | sha256sum`). +- Its own `thiserror` enum (`Handshake`, `PinMismatch`, `UnknownHostStrict`, + `Mint`, `Io`), converted into `repo`'s error type via `#[from]`. + +## 2. Client side (`crates/repo/src/http_transport.rs`, `stdio_transport.rs`) + +- `ScHttpUrl` gains `tls: bool` from the scheme. `sc+https://` keeps default + port 8730 (one port speaks one protocol; the operator separates them). + `open_transport` routes `sc+https://` beside the existing `sc+http://` arm. + Existing `sc+http://` remotes keep working unchanged; `PROTOCOL_VERSION` + stays 3. +- `HttpTransport::connect_with_token`: after the TCP connect (existing opening + timeout logic untouched), the socket is wrapped in a small `MaybeTls` enum + (plain / TLS) implementing `Read + Write`, chosen by the scheme; the opening + codec, status read, and wire handshake are byte-identical over it. +- **Pin store:** `~/.config/sc/known_hosts` — `$XDG_CONFIG_HOME`, falling back + to `$HOME/.config` — one line per `host:port`, format + `host:port sha256:`. `SC_HTTPS_KNOWN_HOSTS` overrides the file path + (tests, CI). Semantics: + - **Unknown host (accept-new default):** pin silently (append), print the + fingerprint loudly to stderr, proceed. + - **Pin mismatch:** always hard-fail — never a prompt — naming the file, the + stored vs seen fingerprints, and the recovery action (remove that line if + the server key legitimately changed). + - **`SC_HTTPS_FINGERPRINT=`:** pre-pins for this process only — + verified against the presented cert, never persisted (CI-friendly). + - **`SC_HTTPS_STRICT=1`:** refuse unknown hosts; an existing pin or a + pre-pin satisfies it. + +## 3. Server side (`crates/cli` serve arm, `http_transport.rs`) + +- `sc serve --http --tls [--tls-cert --tls-key ]`. + `--tls-cert`/`--tls-key` require `--tls` and each other; `--tls` is refused + with `--stdio` (ssh already provides that channel's confidentiality). Without + PEM flags, `load_or_mint(.sc/serve-tls/)` runs; regeneration happens only + when the material is missing. The TLS startup banner prints the SPKI + fingerprint. +- `handle_http_connection` wraps the accepted socket in `TlsServerStream` + before the opening read; P31's whole-session read/write timeouts still apply + (set on the inner `TcpStream`). The `--max-connections` busy-shed changes + shape under TLS only: the handshake completes first, then the busy status + goes over TLS so the client still gets the clean message (bounded work — the + 30s opening timeout covers the handshake). +- New subcommand `sc serve fingerprint []`: prints the repo's + `.sc/serve-tls/` SPKI fingerprint, **minting if missing** (so an operator can + distribute the pin before first serve) — same `load_or_mint` path, no drift. + +## 4. P29 gate tightening + +`bind_is_allowed` gains a `tls` input. Non-loopback binds are allowed iff: + +- `--read-only`, or +- `--allow-public`, or +- **`--tls` AND ≥1 configured serve token.** + +A plaintext non-loopback bind justified only by tokens (the P29 rule) is now +**refused**, with the error naming `--tls` as the fix — the deliberate, narrow +pre-1.0 break from decision 5. Loopback binds are unchanged. +`auth_is_mandatory` keeps its fail-closed shape: on a TLS+tokens-justified +public bind, tokens are the sole justification, so removing the last token at +runtime yields `401` (the exact P29 precedent). + +## 5. Tests + +- **`tlsio` unit:** mint idempotence + key file perms (0600); SPKI hash + stability across a same-key cert re-mint; verifier accept/record, mismatch + hard-fail, strict refusal. +- **`http_transport` integration** (loopback, OS-assigned ports, existing + harness): full TOFU lifecycle — first connect writes the pin, second connect + is quiet, a swapped server key hard-fails, `SC_HTTPS_FINGERPRINT` pre-pin + works and does not persist, `SC_HTTPS_STRICT=1` refuses an unknown host; + user-PEM path; the bind-gate lattice matrix (plaintext+tokens refused, + TLS+tokens allowed, TLS alone refused, `--read-only`/`--allow-public` + unchanged); and the acceptance round trip — clone + push + fetch over + `sc+https://` with a signed ~1 MiB blob under forced `SC_PACK_CHUNK`, + byte-for-byte, zero `.sc/tmp` residue. +- **Demo:** `demo/run_tls_demo.sh` (the `run_http_auth_demo.sh` pattern, run + twice): TLS round trip, pin-on-first-connect, mismatch hard-fail after key + regeneration, strict + pre-pin behavior, and the tightened plaintext gate + (tokens-only public plaintext refused; `--tls` + token accepted). + +## 6. Docs + +- **ADR-0042** (required — first TLS dependency): why in-binary TLS is + load-bearing for sc's *client* (a CLI reaching servers its operator doesn't + control — the case Vaultwarden/Garage punt); ring vs aws-lc-rs with + aws-lc-rs as the recorded swap-in fallback; the tlsio quarantine; the + accept-new TOFU trust model including pin-only/no-name-validation v1; ACME + rejection (async stack). +- **CLAUDE.md:** new commands/flags, dependency rule + quarantine list updates. +- **THREAT-MODEL:** transport section rewritten for `sc+https://`. +- **Reverse-proxy guidance:** document **both legs** (server-side TCP-mode + termination — nginx `stream` / HAProxy `mode tcp` / stunnel — AND the + client-side tunnel, absent today); promote `ssh://` (ADR-0022) as the + already-shipped confidential transport. + +## Non-goals + +ACME in-binary; CA-path validation (later, additive for PEM deployments); +challenge-response / Ed25519 nonce-signing auth (rejected on #26); Noise +transport. From 9c89f729d83ae9f3b3d3b08fad6dd500b2603106 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 10 Jul 2026 14:22:41 +0300 Subject: [PATCH 02/14] docs(plans): P32 sc+https implementation plan (#39) Claude-Session: https://claude.ai/code/session_01LhadyW9scQL95h3ag9yySB --- .../plans/2026-07-10-p32-tls-sc-https.md | 2105 +++++++++++++++++ 1 file changed, 2105 insertions(+) create mode 100644 docs/superpowers/plans/2026-07-10-p32-tls-sc-https.md diff --git a/docs/superpowers/plans/2026-07-10-p32-tls-sc-https.md b/docs/superpowers/plans/2026-07-10-p32-tls-sc-https.md new file mode 100644 index 00000000..218e26a7 --- /dev/null +++ b/docs/superpowers/plans/2026-07-10-p32-tls-sc-https.md @@ -0,0 +1,2105 @@ +# P32: `sc+https://` via rustls (`crates/tlsio`) Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** In-binary TLS for the sc-native HTTP transport — `sc+https://` with accept-new TOFU SPKI pinning on the client and `sc serve --http … --tls` (auto-minted or PEM cert) on the server — closing audit High #1 (issue #39). + +**Architecture:** A new quarantine crate `crates/tlsio` is the only crate linking rustls (ring provider) + rcgen; it exposes blocking `Read+Write` TLS streams, a hand-written pin-only `ServerCertVerifier`, cert mint/load, and SPKI-SHA-256 fingerprints. `crates/repo` wraps the two existing seam functions (`HttpTransport::connect_*`, `handle_http_connection`) in those streams below the unchanged HTTP opening codec and wire protocol (`PROTOCOL_VERSION` stays 3), adds a known-hosts pin store, and tightens the P29 bind gate. The CLI adds `--tls/--tls-cert/--tls-key` and `sc serve fingerprint`. + +**Tech Stack:** Rust (stable, workspace edition 2021), rustls 0.23 (`default-features = false`, features `ring,std,tls12,logging`), rcgen 0.14, rustls-pki-types (PEM), ring (SHA-256 digest only, already in-tree via rustls), hex, thiserror. + +**Spec:** `docs/superpowers/specs/2026-07-10-p32-tls-sc-https-design.md`. Work happens on the existing `p32-tls` branch. + +## Global Constraints + +- `PROTOCOL_VERSION` stays **3**; the opening codec, `serve_tokens`, and all of `wire.rs` are byte-for-byte unchanged. +- rustls/rcgen/ring/rustls-pki-types are linked **only** by `crates/tlsio` (quarantine; the gix→gitio precedent). `tlsio` depends on **no** workspace crate. Only `repo` depends on `tlsio`. +- No RustCrypto crate in `tlsio` (RustCrypto stays quarantined in `crates/crypto`) — SHA-256 comes from `ring::digest`. +- Fingerprint = SHA-256 of the full server **SPKI DER TLV**, rendered `sha256:` everywhere (openssl-verifiable: `openssl x509 -in cert.pem -pubkey -noout | openssl pkey -pubin -outform der | shasum -a 256`). +- Pin mismatch always **hard-fails** — never a prompt. Accept-new is the default; `SC_HTTPS_STRICT=1` refuses unknown hosts; `SC_HTTPS_FINGERPRINT=` pre-pins without persisting; `SC_HTTPS_KNOWN_HOSTS=` overrides the pin-file path. +- No client application byte (opening line, bearer token) crosses the socket before pin disposition is settled. +- Deps: use `cargo add` (never hand-edit version pins); **stage `Cargo.lock` in the same commit as any dep change** (project memory). +- Every new behavior ships with a test; tests that touch disk clean up and assert the path is gone. Errors: `thiserror` per crate, `?`-converted; CLI uses `anyhow`. +- All commits go on the `p32-tls` branch. Run `cargo test --workspace` and `cargo clippy --all-targets -- -D warnings` before each commit (CI enforces clippy). + +--- + +### Task 1: `crates/tlsio` scaffold — SPKI extraction, fingerprints, cert mint/load + +**Files:** +- Create: `crates/tlsio/Cargo.toml` +- Create: `crates/tlsio/src/lib.rs` +- Create: `crates/tlsio/src/spki.rs` +- Create: `crates/tlsio/src/identity.rs` +- Modify: `Cargo.toml` (workspace members) + +**Interfaces:** +- Consumes: nothing (dependency leaf). +- Produces (used by Tasks 2, 4, 5, 7): + - `pub enum Error { BadCert, Handshake(String), PinMismatch { expected: [u8; 32], seen: [u8; 32] }, UnknownHostStrict, Mint(String), Io(std::io::Error) }` + `pub type Result` + - `pub fn spki_der(cert_der: &[u8]) -> Result<&[u8]>` + - `pub fn spki_sha256(cert_der: &[u8]) -> Result<[u8; 32]>` + - `pub fn fingerprint_hex(spki_hash: &[u8; 32]) -> String` → `"sha256:"` + - `pub struct ServerIdentity { pub certs: Vec>, pub key: PrivateKeyDer<'static>, pub spki_sha256: [u8; 32] }` + - `pub fn load_or_mint(dir: &Path) -> Result` (dir = `.sc/serve-tls/`; mints `cert.pem` + `key.pem` (0600) only when missing) + - `pub fn load_pem(cert: &Path, key: &Path) -> Result` + - Re-exports: `pub use rustls::pki_types::{CertificateDer, PrivateKeyDer};` (so `repo` never names rustls directly) + +- [ ] **Step 1: Scaffold the crate and add dependencies** + +```bash +mkdir -p crates/tlsio/src && cat > crates/tlsio/Cargo.toml <<'EOF' +[package] +name = "scl-tlsio" +version.workspace = true +edition.workspace = true +license.workspace = true +publish.workspace = true + +[dependencies] + +[lints] +workspace = true +EOF +printf '' > crates/tlsio/src/lib.rs +``` + +Add `"crates/tlsio"` to `members` in the root `Cargo.toml`: + +```toml +members = ["crates/core", "crates/vfs", "crates/gitio", "crates/crypto", "crates/repo", "crates/cli", "crates/tlsio"] +``` + +Then add deps (pins latest stable; do not hand-edit versions): + +```bash +cargo add -p scl-tlsio rustls --no-default-features -F ring,std,tls12,logging +cargo add -p scl-tlsio rustls-pki-types -F std,pem +cargo add -p scl-tlsio rcgen +cargo add -p scl-tlsio ring +cargo add -p scl-tlsio hex thiserror +``` + +Note: `ring` is already in the tree via rustls's ring feature — this adds no new crate, only a direct edge for `ring::digest::SHA256`. If `rustls-pki-types` has no `pem` feature at the resolved version, `cargo add` fails loudly — then use `-F std` only (PEM moved into default/std in newer releases; the `pem_file_iter`/`from_pem_file` calls in Step 5 are the real check). + +Run: `cargo check -p scl-tlsio` — Expected: PASS (empty lib). + +- [ ] **Step 2: Write failing tests for SPKI extraction + fingerprint** + +`crates/tlsio/src/spki.rs` (tests first; the correctness anchor is rcgen's own `public_key_der()`, which returns exactly the SPKI DER the cert embeds): + +```rust +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn spki_der_matches_rcgen_public_key_der() { + let key = rcgen::KeyPair::generate().unwrap(); + let params = rcgen::CertificateParams::new(vec!["sc-serve".to_string()]).unwrap(); + let cert = params.self_signed(&key).unwrap(); + let extracted = spki_der(cert.der()).unwrap(); + assert_eq!(extracted, key.public_key_der().as_slice()); + } + + #[test] + fn spki_sha256_is_stable_across_same_key_remint() { + let key = rcgen::KeyPair::generate().unwrap(); + let c1 = rcgen::CertificateParams::new(vec!["a".to_string()]) + .unwrap() + .self_signed(&key) + .unwrap(); + let c2 = rcgen::CertificateParams::new(vec!["b".to_string()]) + .unwrap() + .self_signed(&key) + .unwrap(); + // Different cert bytes, same key → same SPKI hash (the whole point + // of pinning the SPKI, not the cert). + assert_ne!(c1.der().as_ref(), c2.der().as_ref()); + assert_eq!(spki_sha256(c1.der()).unwrap(), spki_sha256(c2.der()).unwrap()); + } + + #[test] + fn fingerprint_hex_format() { + let h = [0xabu8; 32]; + let s = fingerprint_hex(&h); + assert!(s.starts_with("sha256:")); + assert_eq!(s.len(), "sha256:".len() + 64); + } + + #[test] + fn spki_der_rejects_garbage() { + assert!(spki_der(b"not a certificate").is_err()); + assert!(spki_der(&[]).is_err()); + assert!(spki_der(&[0x30, 0x82, 0xff]).is_err()); // truncated length + } +} +``` + +- [ ] **Step 3: Run tests to verify they fail** + +Run: `cargo test -p scl-tlsio` — Expected: COMPILE FAIL (`spki_der` not defined). + +- [ ] **Step 4: Implement `lib.rs` (error type) and `spki.rs`** + +`crates/tlsio/src/lib.rs`: + +```rust +//! TLS quarantine crate (P32, ADR-0042): the ONLY crate linking rustls and +//! rcgen (the gix→gitio / RustCrypto→crypto precedent). Exposes blocking +//! `Read + Write` TLS streams for the sc+https:// transport, a pin-only +//! TOFU certificate verifier, self-signed cert mint/load, and SPKI-SHA-256 +//! fingerprints. Depends on no other workspace crate. + +mod identity; +mod spki; +mod stream; // Task 2 + +pub use identity::{load_or_mint, load_pem, ServerIdentity}; +pub use rustls::pki_types::{CertificateDer, PrivateKeyDer}; +pub use spki::{fingerprint_hex, spki_der, spki_sha256}; + +/// Errors from the TLS layer. `PinMismatch` carries both fingerprints so the +/// caller (crates/repo) can render a recovery hint naming its pin source — +/// this crate knows nothing about known_hosts files or env vars. +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("could not parse the server certificate (DER)")] + BadCert, + #[error("TLS handshake failed: {0}")] + Handshake(String), + #[error("server key does not match the pinned fingerprint")] + PinMismatch { expected: [u8; 32], seen: [u8; 32] }, + #[error("unknown host refused (strict mode)")] + UnknownHostStrict, + #[error("certificate mint/load failed: {0}")] + Mint(String), + #[error("TLS I/O: {0}")] + Io(#[from] std::io::Error), +} + +pub type Result = std::result::Result; +``` + +(Leave `mod stream;` commented out until Task 2: `// mod stream; // Task 2`.) + +`crates/tlsio/src/spki.rs`: + +```rust +//! SubjectPublicKeyInfo extraction from a certificate DER, and the +//! `sha256:` fingerprint every P32 surface uses (pin file, banner, +//! `sc serve fingerprint`, `SC_HTTPS_FINGERPRINT`). +//! +//! The extraction is a minimal hand-written DER TLV walk rather than a full +//! x509 parser dependency: we need exactly one field, the walk is ~40 lines, +//! and a malformed certificate simply fails the connection (fail-closed). +//! Correctness is anchored by the test comparing against rcgen's own +//! `KeyPair::public_key_der()` for a cert we minted. + +use crate::{Error, Result}; + +/// One DER TLV: returns (tag, content_start..content_end, total_len). +fn der_tlv(buf: &[u8]) -> Option<(u8, core::ops::Range, usize)> { + let tag = *buf.first()?; + let first = *buf.get(1)?; + let (len, header) = if first & 0x80 == 0 { + (first as usize, 2) + } else { + let n = (first & 0x7f) as usize; + if n == 0 || n > 4 { + return None; + } + let mut len = 0usize; + for i in 0..n { + len = (len << 8) | *buf.get(2 + i)? as usize; + } + (len, 2 + n) + }; + let end = header.checked_add(len)?; + if end > buf.len() { + return None; + } + Some((tag, header..end, end)) +} + +/// Extract the full SubjectPublicKeyInfo TLV (header included) from a +/// certificate DER. Layout: `Certificate ::= SEQUENCE { tbsCertificate, +/// signatureAlgorithm, signature }`; `TBSCertificate ::= SEQUENCE { +/// [0] version OPTIONAL, serialNumber, signature, issuer, validity, +/// subject, subjectPublicKeyInfo, ... }` — so: descend two SEQUENCEs, skip +/// the optional context-0 version, skip five fields, take the sixth. +pub fn spki_der(cert_der: &[u8]) -> Result<&[u8]> { + let (tag, body, _) = der_tlv(cert_der).ok_or(Error::BadCert)?; + if tag != 0x30 { + return Err(Error::BadCert); + } + let tbs_buf = &cert_der[body]; + let (tag, body, _) = der_tlv(tbs_buf).ok_or(Error::BadCert)?; + if tag != 0x30 { + return Err(Error::BadCert); + } + let mut rest = &tbs_buf[body]; + if rest.first() == Some(&0xa0) { + let (_, _, used) = der_tlv(rest).ok_or(Error::BadCert)?; + rest = &rest[used..]; + } + for _ in 0..5 { + let (_, _, used) = der_tlv(rest).ok_or(Error::BadCert)?; + rest = &rest[used..]; + } + let (tag, _, used) = der_tlv(rest).ok_or(Error::BadCert)?; + if tag != 0x30 { + return Err(Error::BadCert); + } + Ok(&rest[..used]) +} + +/// SHA-256 over the SPKI TLV — the pin. ring's digest, not a RustCrypto +/// crate (RustCrypto stays quarantined in crates/crypto); ring is already +/// in-tree as rustls's crypto provider. +pub fn spki_sha256(cert_der: &[u8]) -> Result<[u8; 32]> { + let spki = spki_der(cert_der)?; + let d = ring::digest::digest(&ring::digest::SHA256, spki); + let mut out = [0u8; 32]; + out.copy_from_slice(d.as_ref()); + Ok(out) +} + +/// Render a pin as `sha256:` — the one fingerprint format. +pub fn fingerprint_hex(spki_hash: &[u8; 32]) -> String { + format!("sha256:{}", hex::encode(spki_hash)) +} +``` + +- [ ] **Step 5: Write failing tests for `identity.rs`, then implement** + +`crates/tlsio/src/identity.rs`: + +```rust +//! Server TLS identity: load `cert.pem`/`key.pem` from a directory, minting +//! a self-signed pair (rcgen) when absent. The KEY is the identity — +//! regenerate only when missing; the cert carries a far-future not_after so +//! renewal never bites a pinned deployment (pins are on the SPKI anyway). + +use std::path::Path; + +use rustls::pki_types::{pem::PemObject, CertificateDer, PrivateKeyDer}; + +use crate::{spki_sha256, Error, Result}; + +/// A loaded/minted server identity, ready for `server_config` (Task 2). +pub struct ServerIdentity { + pub certs: Vec>, + pub key: PrivateKeyDer<'static>, + pub spki_sha256: [u8; 32], +} + +/// Load `dir/cert.pem` + `dir/key.pem`, minting both (key mode 0600) when +/// the CERT is absent. A dir with a cert but no key (or vice versa) is an +/// error, not a silent re-mint — regenerating over half an identity would +/// invalidate every client pin without the operator asking for it. +pub fn load_or_mint(dir: &Path) -> Result { + let cert_path = dir.join("cert.pem"); + let key_path = dir.join("key.pem"); + match (cert_path.exists(), key_path.exists()) { + (true, true) => load_pem(&cert_path, &key_path), + (false, false) => { + std::fs::create_dir_all(dir)?; + let key = rcgen::KeyPair::generate().map_err(|e| Error::Mint(e.to_string()))?; + let mut params = rcgen::CertificateParams::new(vec!["sc-serve".to_string()]) + .map_err(|e| Error::Mint(e.to_string()))?; + params.not_after = rcgen::date_time_ymd(2126, 1, 1); + let cert = params + .self_signed(&key) + .map_err(|e| Error::Mint(e.to_string()))?; + std::fs::write(&cert_path, cert.pem())?; + write_key_0600(&key_path, key.serialize_pem().as_bytes())?; + load_pem(&cert_path, &key_path) + } + _ => Err(Error::Mint(format!( + "{} holds half a TLS identity (one of cert.pem/key.pem is missing); \ + restore the missing file or remove the directory to re-mint", + dir.display() + ))), + } +} + +/// Load a user-supplied PEM pair (certbot etc.). The fingerprint is the +/// leaf's (first cert's) SPKI hash. +pub fn load_pem(cert: &Path, key: &Path) -> Result { + let certs: Vec> = CertificateDer::pem_file_iter(cert) + .map_err(|e| Error::Mint(format!("read {}: {e}", cert.display())))? + .collect::>() + .map_err(|e| Error::Mint(format!("parse {}: {e}", cert.display())))?; + let leaf = certs.first().ok_or(Error::BadCert)?; + let spki = spki_sha256(leaf)?; + let key = PrivateKeyDer::from_pem_file(key) + .map_err(|e| Error::Mint(format!("read {}: {e}", key.display())))?; + Ok(ServerIdentity { + spki_sha256: spki, + certs, + key, + }) +} + +#[cfg(unix)] +fn write_key_0600(path: &Path, bytes: &[u8]) -> Result<()> { + use std::io::Write; + use std::os::unix::fs::OpenOptionsExt; + let mut f = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .mode(0o600) + .open(path)?; + f.write_all(bytes)?; + Ok(()) +} + +#[cfg(not(unix))] +fn write_key_0600(path: &Path, bytes: &[u8]) -> Result<()> { + std::fs::write(path, bytes)?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn tmp(tag: &str) -> std::path::PathBuf { + let d = std::env::temp_dir().join(format!("scl-tlsio-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + d + } + + #[test] + fn mint_then_load_is_idempotent_and_key_is_0600() { + let dir = tmp("mint"); + let a = load_or_mint(&dir).unwrap(); + let b = load_or_mint(&dir).unwrap(); + // Second call LOADS (same identity), never re-mints. + assert_eq!(a.spki_sha256, b.spki_sha256); + assert_eq!(a.certs, b.certs); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = std::fs::metadata(dir.join("key.pem")).unwrap().permissions().mode(); + assert_eq!(mode & 0o777, 0o600, "key.pem must be 0600"); + } + std::fs::remove_dir_all(&dir).unwrap(); + assert!(!dir.exists()); + } + + #[test] + fn half_an_identity_errors_instead_of_silently_reminting() { + let dir = tmp("half"); + load_or_mint(&dir).unwrap(); + std::fs::remove_file(dir.join("key.pem")).unwrap(); + let err = load_or_mint(&dir).unwrap_err(); + assert!(err.to_string().contains("half a TLS identity"), "got: {err}"); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn load_pem_reads_a_minted_pair() { + let dir = tmp("pem"); + let minted = load_or_mint(&dir).unwrap(); + let loaded = load_pem(&dir.join("cert.pem"), &dir.join("key.pem")).unwrap(); + assert_eq!(minted.spki_sha256, loaded.spki_sha256); + std::fs::remove_dir_all(&dir).unwrap(); + } +} +``` + +If the resolved rcgen version's API differs (e.g. `CertificateParams::new` not returning `Result`, or `date_time_ymd` renamed), check `cargo doc -p rcgen --no-deps` and adapt the mint block only — the public `ServerIdentity`/`load_or_mint`/`load_pem` signatures are fixed. + +- [ ] **Step 6: Run tests to verify they pass** + +Run: `cargo test -p scl-tlsio` — Expected: all PASS. + +- [ ] **Step 7: Commit (Cargo.lock staged with the dep change)** + +```bash +cargo clippy -p scl-tlsio --all-targets -- -D warnings +git add Cargo.toml Cargo.lock crates/tlsio +git commit -m "feat(tlsio): P32 scaffold — SPKI fingerprints + cert mint/load (rustls/rcgen quarantine)" +``` + +--- + +### Task 2: `tlsio` streams — pin verifier, `client_connect`, `server_stream`, split halves + +**Files:** +- Create: `crates/tlsio/src/stream.rs` +- Modify: `crates/tlsio/src/lib.rs` (enable `mod stream;`, re-export) + +**Interfaces:** +- Consumes: Task 1's `ServerIdentity`, `spki_sha256`, `Error`. +- Produces (used by Tasks 4–5): + - `pub struct TlsServerConfig { /* Arc */ pub spki_sha256: [u8; 32] }` — `Clone` + - `pub fn server_config(id: ServerIdentity) -> Result` + - `pub fn server_stream(cfg: &TlsServerConfig, tcp: TcpStream) -> Result` — handshake driven to completion before returning + - `pub fn client_connect(tcp: TcpStream, host: &str, expected_pin: Option<[u8; 32]>, strict: bool) -> Result<(TlsClientStream, [u8; 32])>` — returns the observed SPKI hash; handshake completed before returning + - `TlsClientStream::split(self) -> (TlsClientReadHalf, TlsClientWriteHalf)`; same for `TlsServerStream` → `TlsServerReadHalf`/`TlsServerWriteHalf` + - Read halves have `pub fn set_socket_timeouts(&self, read: Option, write: Option) -> std::io::Result<()>` + - All four halves implement `io::Read`/`io::Write` respectively and are `Send`. + +- [ ] **Step 1: Write the failing loopback tests** + +Append to `crates/tlsio/src/stream.rs` (module skeleton + tests; implementation lands in Step 3): + +```rust +#[cfg(test)] +mod tests { + use super::*; + use std::io::{Read, Write}; + use std::net::{TcpListener, TcpStream}; + + fn tmp(tag: &str) -> std::path::PathBuf { + let d = std::env::temp_dir().join(format!("scl-tlsio-st-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + d + } + + /// Loopback server: accept one connection, TLS-wrap it, echo one + /// length-prefixed message back. Returns (addr, join handle, spki). + fn echo_server(dir: &std::path::Path) -> (String, std::thread::JoinHandle<()>, [u8; 32]) { + let id = crate::load_or_mint(dir).unwrap(); + let spki = id.spki_sha256; + let cfg = server_config(id).unwrap(); + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap().to_string(); + let h = std::thread::spawn(move || { + let (tcp, _) = listener.accept().unwrap(); + // A client that aborts its handshake (mismatch/strict tests) + // surfaces as Err here — that's fine, just return. + let Ok(stream) = server_stream(&cfg, tcp) else { return }; + let (mut r, mut w) = stream.split(); + let mut buf = [0u8; 5]; + if r.read_exact(&mut buf).is_err() { + return; + } + w.write_all(&buf).unwrap(); + w.flush().unwrap(); + }); + (addr, h, spki) + } + + #[test] + fn accept_new_returns_seen_pin_and_data_flows_through_halves() { + let dir = tmp("tofu"); + let (addr, h, spki) = echo_server(&dir); + let tcp = TcpStream::connect(&addr).unwrap(); + let (stream, seen) = client_connect(tcp, "127.0.0.1", None, false).unwrap(); + assert_eq!(seen, spki, "observed pin must be the server's SPKI hash"); + let (mut r, mut w) = stream.split(); + w.write_all(b"hello").unwrap(); + w.flush().unwrap(); + let mut back = [0u8; 5]; + r.read_exact(&mut back).unwrap(); + assert_eq!(&back, b"hello"); + h.join().unwrap(); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn matching_pin_connects() { + let dir = tmp("pinok"); + let (addr, h, spki) = echo_server(&dir); + let tcp = TcpStream::connect(&addr).unwrap(); + assert!(client_connect(tcp, "127.0.0.1", Some(spki), false).is_ok()); + h.join().ok(); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn mismatched_pin_hard_fails_with_both_fingerprints() { + let dir = tmp("pinbad"); + let (addr, h, spki) = echo_server(&dir); + let wrong = [0x42u8; 32]; + assert_ne!(wrong, spki); + let tcp = TcpStream::connect(&addr).unwrap(); + match client_connect(tcp, "127.0.0.1", Some(wrong), false) { + Err(crate::Error::PinMismatch { expected, seen }) => { + assert_eq!(expected, wrong); + assert_eq!(seen, spki); + } + other => panic!("expected PinMismatch, got {other:?}"), + } + h.join().ok(); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn strict_refuses_unknown_host() { + let dir = tmp("strict"); + let (addr, h, _) = echo_server(&dir); + let tcp = TcpStream::connect(&addr).unwrap(); + match client_connect(tcp, "127.0.0.1", None, true) { + Err(crate::Error::UnknownHostStrict) => {} + other => panic!("expected UnknownHostStrict, got {other:?}"), + } + h.join().ok(); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn plain_tcp_client_against_tls_server_fails_cleanly() { + let dir = tmp("plain"); + let (addr, h, _) = echo_server(&dir); + let mut tcp = TcpStream::connect(&addr).unwrap(); + // A plaintext opening against a TLS listener must error server-side + // (covered by echo_server's `else return`), and the client just sees + // a dead/garbled connection — no hang. + tcp.write_all(b"POST / HTTP/1.1\r\n\r\n").ok(); + tcp.set_read_timeout(Some(std::time::Duration::from_secs(5))).unwrap(); + let mut buf = [0u8; 16]; + // Read either 0 (close) or a TLS alert — anything but a hang. + let _ = tcp.read(&mut buf); + h.join().unwrap(); + std::fs::remove_dir_all(&dir).unwrap(); + } +} +``` + +- [ ] **Step 2: Run tests to verify they fail** + +Enable `mod stream;` in `lib.rs` with re-exports: + +```rust +pub use stream::{ + client_connect, server_config, server_stream, TlsClientReadHalf, TlsClientStream, + TlsClientWriteHalf, TlsServerConfig, TlsServerReadHalf, TlsServerStream, TlsServerWriteHalf, +}; +``` + +Run: `cargo test -p scl-tlsio` — Expected: COMPILE FAIL (types not defined). + +- [ ] **Step 3: Implement `stream.rs`** + +```rust +//! Blocking TLS streams over `TcpStream`, split into Read/Write halves. +//! +//! The wire protocol needs SEPARATE reader/writer values (`WireClient` +//! and `wire::serve(r, w)`), but a `rustls::StreamOwned` is one object — so +//! `split` shares it behind `Arc>` with a half per side. That is +//! safe (not a deadlock risk) because the sc wire protocol is strictly +//! sequential request-reply on both ends: a read and a write never block +//! concurrently on one connection. +//! +//! Both `client_connect` and `server_stream` drive the handshake to +//! completion before returning: the client's pin disposition must be settled +//! BEFORE any application byte (the HTTP opening, the bearer token) is +//! written, and a garbage/non-TLS peer must fail at the seam, not +//! mid-protocol. + +use std::io::{Read, Write}; +use std::net::TcpStream; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use rustls::pki_types::ServerName; + +use crate::{spki_sha256, Error, Result, ServerIdentity}; + +/// Pin-only server-cert verifier (v1 trust model, ADR-0042): the SPKI hash +/// is the entire trust decision — names and validity windows are +/// deliberately ignored. Handshake signatures ARE still verified against +/// the presented key (otherwise a MITM could replay the pinned cert without +/// holding its private key). +#[derive(Debug)] +struct PinVerifier { + expected: Option<[u8; 32]>, + strict: bool, + seen: Mutex>, + algs: rustls::crypto::WebPkiSupportedAlgorithms, +} + +impl rustls::client::danger::ServerCertVerifier for PinVerifier { + fn verify_server_cert( + &self, + end_entity: &rustls::pki_types::CertificateDer<'_>, + _intermediates: &[rustls::pki_types::CertificateDer<'_>], + _server_name: &ServerName<'_>, + _ocsp_response: &[u8], + _now: rustls::pki_types::UnixTime, + ) -> std::result::Result { + let hash = spki_sha256(end_entity).map_err(|_| { + rustls::Error::InvalidCertificate(rustls::CertificateError::BadEncoding) + })?; + *self.seen.lock().unwrap() = Some(hash); + match self.expected { + Some(p) if p == hash => Ok(rustls::client::danger::ServerCertVerified::assertion()), + Some(_) => Err(rustls::Error::General("sc: pinned fingerprint mismatch".into())), + None if self.strict => Err(rustls::Error::General("sc: unknown host (strict)".into())), + None => Ok(rustls::client::danger::ServerCertVerified::assertion()), + } + } + + fn verify_tls12_signature( + &self, + message: &[u8], + cert: &rustls::pki_types::CertificateDer<'_>, + dss: &rustls::DigitallySignedStruct, + ) -> std::result::Result { + rustls::crypto::verify_tls12_signature(message, cert, dss, &self.algs) + } + + fn verify_tls13_signature( + &self, + message: &[u8], + cert: &rustls::pki_types::CertificateDer<'_>, + dss: &rustls::DigitallySignedStruct, + ) -> std::result::Result { + rustls::crypto::verify_tls13_signature(message, cert, dss, &self.algs) + } + + fn supported_verify_schemes(&self) -> Vec { + self.algs.supported_schemes() + } +} + +/// A built server config (one per listener), cheap to clone per connection. +#[derive(Clone)] +pub struct TlsServerConfig { + config: Arc, + /// The identity's SPKI hash, for the startup banner / `sc serve + /// fingerprint` without re-reading the PEM. + pub spki_sha256: [u8; 32], +} + +impl std::fmt::Debug for TlsServerConfig { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TlsServerConfig").finish_non_exhaustive() + } +} + +pub fn server_config(id: ServerIdentity) -> Result { + let provider = Arc::new(rustls::crypto::ring::default_provider()); + let config = rustls::ServerConfig::builder_with_provider(provider) + .with_safe_default_protocol_versions() + .map_err(|e| Error::Handshake(e.to_string()))? + .with_no_client_auth() + .with_single_cert(id.certs, id.key) + .map_err(|e| Error::Handshake(e.to_string()))?; + Ok(TlsServerConfig { + config: Arc::new(config), + spki_sha256: id.spki_sha256, + }) +} + +pub struct TlsServerStream { + inner: rustls::StreamOwned, +} + +pub struct TlsClientStream { + inner: rustls::StreamOwned, +} + +/// Accept-side wrap: complete the handshake (bounded by whatever socket +/// timeout the caller set on `tcp`) and return a ready stream. +pub fn server_stream(cfg: &TlsServerConfig, mut tcp: TcpStream) -> Result { + let mut conn = rustls::ServerConnection::new(cfg.config.clone()) + .map_err(|e| Error::Handshake(e.to_string()))?; + while conn.is_handshaking() { + conn.complete_io(&mut tcp) + .map_err(|e| Error::Handshake(format!("server handshake: {e}")))?; + } + Ok(TlsServerStream { + inner: rustls::StreamOwned::new(conn, tcp), + }) +} + +/// Connect-side wrap. Returns the ready stream AND the observed SPKI hash +/// (for TOFU recording). Error mapping: a handshake failure caused by our +/// own verifier is translated back into the typed pin errors using the +/// verifier's recorded state. +pub fn client_connect( + mut tcp: TcpStream, + host: &str, + expected_pin: Option<[u8; 32]>, + strict: bool, +) -> Result<(TlsClientStream, [u8; 32])> { + let provider = Arc::new(rustls::crypto::ring::default_provider()); + let algs = provider.signature_verification_algorithms; + let verifier = Arc::new(PinVerifier { + expected: expected_pin, + strict, + seen: Mutex::new(None), + algs, + }); + let config = rustls::ClientConfig::builder_with_provider(provider) + .with_safe_default_protocol_versions() + .map_err(|e| Error::Handshake(e.to_string()))? + .dangerous() + .with_custom_certificate_verifier(verifier.clone()) + .with_no_client_auth(); + let name = ServerName::try_from(host.to_string()) + .map_err(|e| Error::Handshake(format!("bad server name {host}: {e}")))?; + let mut conn = rustls::ClientConnection::new(Arc::new(config), name) + .map_err(|e| Error::Handshake(e.to_string()))?; + while conn.is_handshaking() { + if let Err(e) = conn.complete_io(&mut tcp) { + // Translate our verifier's rejections into typed errors. + let seen = *verifier.seen.lock().unwrap(); + if let (Some(expected), Some(seen)) = (expected_pin, seen) { + if expected != seen { + return Err(Error::PinMismatch { expected, seen }); + } + } + if expected_pin.is_none() && strict && seen.is_some() { + return Err(Error::UnknownHostStrict); + } + return Err(Error::Handshake(format!("client handshake with {host}: {e}"))); + } + } + let seen = verifier + .seen + .lock() + .unwrap() + .ok_or_else(|| Error::Handshake("handshake completed without a certificate".into()))?; + Ok(( + TlsClientStream { + inner: rustls::StreamOwned::new(conn, tcp), + }, + seen, + )) +} + +// ── split halves ─────────────────────────────────────────────────────────── + +macro_rules! halves { + ($stream:ident, $read:ident, $write:ident) => { + pub struct $read(Arc>); + pub struct $write(Arc>); + + impl $stream { + pub fn split(self) -> ($read, $write) { + let shared = Arc::new(Mutex::new(self)); + ($read(shared.clone()), $write(shared)) + } + } + + impl Read for $read { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + self.0.lock().unwrap().inner.read(buf) + } + } + + impl Write for $write { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.0.lock().unwrap().inner.write(buf) + } + fn flush(&mut self) -> std::io::Result<()> { + self.0.lock().unwrap().inner.flush() + } + } + + impl $read { + /// Set read/write timeouts on the UNDERLYING socket (SO_RCVTIMEO + /// / SO_SNDTIMEO apply to the socket, so they govern the TLS + /// stream's blocking behavior below the record layer) — the P31 + /// session-timeout hook. + pub fn set_socket_timeouts( + &self, + read: Option, + write: Option, + ) -> std::io::Result<()> { + let g = self.0.lock().unwrap(); + g.inner.get_ref().set_read_timeout(read)?; + g.inner.get_ref().set_write_timeout(write) + } + } + }; +} + +halves!(TlsClientStream, TlsClientReadHalf, TlsClientWriteHalf); +halves!(TlsServerStream, TlsServerReadHalf, TlsServerWriteHalf); +``` + +If the resolved rustls 0.23.x differs on builder method names (`builder_with_provider` / `with_safe_default_protocol_versions` / `.dangerous()`), consult `cargo doc -p rustls --no-deps` — the pattern is rustls's own documented custom-verifier example; keep the public tlsio signatures fixed. + +- [ ] **Step 4: Run tests to verify they pass** + +Run: `cargo test -p scl-tlsio` — Expected: all PASS (including Task 1's). + +- [ ] **Step 5: Commit** + +```bash +cargo clippy -p scl-tlsio --all-targets -- -D warnings +git add crates/tlsio +git commit -m "feat(tlsio): TOFU pin verifier + blocking client/server TLS streams with split halves" +``` + +--- + +### Task 3: `repo` pin store (`tls_pins.rs`) + error variants + +**Files:** +- Create: `crates/repo/src/tls_pins.rs` +- Modify: `crates/repo/src/lib.rs` (add `pub mod tls_pins;`) +- Modify: `crates/repo/src/error.rs` +- Modify: `crates/repo/Cargo.toml` (dep on `scl-tlsio`) + +**Interfaces:** +- Consumes: `scl_tlsio::{Error as TlsError, fingerprint_hex}`. +- Produces (used by Task 5): + - `pub struct TlsClientPolicy { pub known_hosts: PathBuf, pub pre_pin: Option<[u8; 32]>, pub strict: bool }` + - `impl TlsClientPolicy { pub fn from_env() -> Result }` + - `pub fn default_known_hosts_path() -> Result` + - `pub fn parse_fingerprint(s: &str) -> Result<[u8; 32]>` (accepts `sha256:` or bare 64-char hex) + - `pub fn lookup(file: &Path, host: &str, port: u16) -> Result>` + - `pub fn record(file: &Path, host: &str, port: u16, pin: &[u8; 32]) -> Result<()>` + - `repo::Error` gains: `Tls(#[from] scl_tlsio::Error)`, `TlsPinMismatch { host, file, pinned, seen }` (all `String`), `TlsStrictUnknownHost(String)` + +- [ ] **Step 1: Add the dependency** + +```bash +cargo add -p scl-repo --path crates/tlsio scl-tlsio +``` + +(If the invocation form differs, `cargo add scl-tlsio --package scl-repo --path crates/tlsio`.) + +- [ ] **Step 2: Add the error variants** + +In `crates/repo/src/error.rs`, after the existing `Remote(String)` variant: + +```rust +#[error("TLS: {0}")] +Tls(#[from] scl_tlsio::Error), +#[error( + "sc+https server key for {host} does not match the pinned fingerprint ({file})\n \ + pinned: {pinned}\n server: {seen}\n\ + If the server key legitimately changed, remove that host's line from the pin file \ + and reconnect (the next connect re-pins); verify with `sc serve fingerprint` on the server." +)] +TlsPinMismatch { + host: String, + file: String, + pinned: String, + seen: String, +}, +#[error( + "sc+https host {0} is not pinned and SC_HTTPS_STRICT=1 refuses unknown hosts; \ + pre-pin with SC_HTTPS_FINGERPRINT=sha256: or connect once without SC_HTTPS_STRICT" +)] +TlsStrictUnknownHost(String), +``` + +- [ ] **Step 3: Write failing tests for the pin store** + +`crates/repo/src/tls_pins.rs` test module: + +```rust +#[cfg(test)] +mod tests { + use super::*; + + fn tmp(tag: &str) -> std::path::PathBuf { + let d = std::env::temp_dir().join(format!("scl-pins-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + d + } + + #[test] + fn record_then_lookup_round_trips() { + let dir = tmp("rt"); + let file = dir.join("known_hosts"); + let pin = [0x11u8; 32]; + assert_eq!(lookup(&file, "example.com", 8730).unwrap(), None); + record(&file, "example.com", 8730, &pin).unwrap(); + assert_eq!(lookup(&file, "example.com", 8730).unwrap(), Some(pin)); + // Same host, different port = a different pin slot. + assert_eq!(lookup(&file, "example.com", 9000).unwrap(), None); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn pin_file_line_format_is_host_port_sha256_hex() { + let dir = tmp("fmt"); + let file = dir.join("known_hosts"); + record(&file, "h", 8730, &[0xaa; 32]).unwrap(); + let text = std::fs::read_to_string(&file).unwrap(); + assert_eq!(text, format!("h:8730 sha256:{}\n", "aa".repeat(32))); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn lookup_ignores_malformed_lines_and_comments() { + let dir = tmp("junk"); + let file = dir.join("known_hosts"); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(&file, "# comment\n\nnot a pin line\nh:1 sha256:zz\n").unwrap(); + assert_eq!(lookup(&file, "h", 1).unwrap(), None); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn parse_fingerprint_accepts_both_forms_and_rejects_junk() { + let hexs = "ab".repeat(32); + let expected = [0xabu8; 32]; + assert_eq!(parse_fingerprint(&format!("sha256:{hexs}")).unwrap(), expected); + assert_eq!(parse_fingerprint(&hexs).unwrap(), expected); + assert!(parse_fingerprint("sha256:short").is_err()); + assert!(parse_fingerprint("md5:abcd").is_err()); + assert!(parse_fingerprint("").is_err()); + } +} +``` + +- [ ] **Step 4: Run tests to verify they fail** + +Run: `cargo test -p scl-repo tls_pins` — Expected: COMPILE FAIL. + +- [ ] **Step 5: Implement `tls_pins.rs`** + +```rust +//! Client-side TOFU pin store for sc+https:// (P32): a user-level +//! known_hosts file, one line per `host:port`, format +//! `host:port sha256:` — plus the env knobs that shape a connection's +//! pin policy. The repo layer owns pin PERSISTENCE and POLICY; the actual +//! in-handshake check lives in scl-tlsio's verifier. + +use std::path::{Path, PathBuf}; + +use crate::error::{Error, Result}; + +/// The pin policy for one client connection, resolved from env by +/// [`TlsClientPolicy::from_env`] or built directly by tests (env mutation is +/// process-global and racy under parallel tests — the +/// `connect_with_token`/`connect_with_pins` split exists for this). +#[derive(Debug, Clone)] +pub struct TlsClientPolicy { + pub known_hosts: PathBuf, + /// `SC_HTTPS_FINGERPRINT`: verified for this process only, never + /// persisted (CI-friendly). + pub pre_pin: Option<[u8; 32]>, + /// `SC_HTTPS_STRICT=1`: refuse unknown hosts instead of accept-new. + pub strict: bool, +} + +impl TlsClientPolicy { + pub fn from_env() -> Result { + let known_hosts = match std::env::var_os("SC_HTTPS_KNOWN_HOSTS") { + Some(p) if !p.is_empty() => PathBuf::from(p), + _ => default_known_hosts_path()?, + }; + let pre_pin = match std::env::var("SC_HTTPS_FINGERPRINT") { + Ok(s) if !s.is_empty() => Some(parse_fingerprint(&s)?), + _ => None, + }; + let strict = std::env::var("SC_HTTPS_STRICT").map(|v| v == "1").unwrap_or(false); + Ok(TlsClientPolicy { + known_hosts, + pre_pin, + strict, + }) + } +} + +/// `$XDG_CONFIG_HOME/sc/known_hosts`, falling back to +/// `$HOME/.config/sc/known_hosts`. +pub fn default_known_hosts_path() -> Result { + if let Some(xdg) = std::env::var_os("XDG_CONFIG_HOME").filter(|v| !v.is_empty()) { + return Ok(PathBuf::from(xdg).join("sc").join("known_hosts")); + } + match std::env::var_os("HOME").filter(|v| !v.is_empty()) { + Some(home) => Ok(PathBuf::from(home).join(".config").join("sc").join("known_hosts")), + None => Err(Error::InvalidArgument( + "cannot resolve the sc+https pin file: set HOME, XDG_CONFIG_HOME, \ + or SC_HTTPS_KNOWN_HOSTS" + .to_string(), + )), + } +} + +/// Parse `sha256:<64 hex>` (or bare hex) into a pin. +pub fn parse_fingerprint(s: &str) -> Result<[u8; 32]> { + let hex_part = s.strip_prefix("sha256:").unwrap_or(s); + let bytes = hex::decode(hex_part) + .map_err(|_| Error::InvalidArgument(format!("bad fingerprint (want sha256:): {s}")))?; + let arr: [u8; 32] = bytes + .try_into() + .map_err(|_| Error::InvalidArgument(format!("fingerprint must be 32 bytes: {s}")))?; + Ok(arr) +} + +/// Find the pin for `host:port`, if any. A missing file means no pins; a +/// malformed or comment line is skipped, never an error (the file is +/// user-editable — the documented mismatch recovery is deleting a line). +pub fn lookup(file: &Path, host: &str, port: u16) -> Result> { + let text = match std::fs::read_to_string(file) { + Ok(t) => t, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(e) => { + return Err(Error::InvalidArgument(format!( + "read pin file {}: {e}", + file.display() + ))) + } + }; + let key = format!("{host}:{port}"); + for line in text.lines() { + let line = line.trim(); + if line.is_empty() || line.starts_with('#') { + continue; + } + if let Some((k, v)) = line.split_once(' ') { + if k == key { + if let Ok(pin) = parse_fingerprint(v.trim()) { + return Ok(Some(pin)); + } + } + } + } + Ok(None) +} + +/// Append a pin line (creating parent dirs and the file as needed). +pub fn record(file: &Path, host: &str, port: u16, pin: &[u8; 32]) -> Result<()> { + if let Some(parent) = file.parent() { + std::fs::create_dir_all(parent) + .map_err(|e| Error::InvalidArgument(format!("create {}: {e}", parent.display())))?; + } + use std::io::Write; + let mut f = std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(file) + .map_err(|e| Error::InvalidArgument(format!("open pin file {}: {e}", file.display())))?; + writeln!(f, "{host}:{port} {}", scl_tlsio::fingerprint_hex(pin)) + .map_err(|e| Error::InvalidArgument(format!("write pin file {}: {e}", file.display())))?; + Ok(()) +} +``` + +Add `pub mod tls_pins;` to `crates/repo/src/lib.rs` next to the other module declarations. `hex` may need adding to `crates/repo`: `cargo add -p scl-repo hex` (check first — `grep hex crates/repo/Cargo.toml`). + +- [ ] **Step 6: Run tests to verify they pass** + +Run: `cargo test -p scl-repo tls_pins` — Expected: PASS. Also `cargo test -p scl-repo` — Expected: no regressions. + +- [ ] **Step 7: Commit** + +```bash +cargo clippy -p scl-repo --all-targets -- -D warnings +git add Cargo.lock crates/repo +git commit -m "feat(repo): sc+https pin store (known_hosts), env pin policy, TLS error variants" +``` + +--- + +### Task 4: server seam — `TlsMode`, TLS-wrapped `handle_http_connection`, fingerprint helper + +**Files:** +- Modify: `crates/repo/src/http_transport.rs` (`serve_http`, `serve_http_listener`, `handle_http_connection`) +- Modify: `crates/repo/src/layout.rs` (add `serve_tls_dir()`) +- Modify: `crates/cli/src/main.rs:3191` call site (temporary: pass `TlsMode::Off` so the workspace compiles; real CLI wiring is Task 7) + +**Interfaces:** +- Consumes: Task 2's `TlsServerConfig`, `server_config`, `server_stream`, `load_or_mint`, `load_pem`, `fingerprint_hex`; Task 1's `ServerIdentity`. +- Produces (used by Tasks 5–7): + - `pub enum TlsMode { Off, AutoMint, Pem { cert: PathBuf, key: PathBuf } }` + - `pub fn serve_http(addr: &str, root: &Path, read_only: bool, allow_public: bool, limits: ServeLimits, tls: TlsMode) -> Result<()>` + - `pub fn serve_http_listener(listener: TcpListener, root: &Path, read_only: bool, mandatory_auth: bool, limits: ServeLimits, tls: Option) -> Result<()>` + - `pub fn serve_tls_fingerprint(root: &Path) -> Result` (mints if missing; returns `sha256:`) + - `Layout::serve_tls_dir(&self) -> PathBuf` → `.sc/serve-tls` + - Startup output: line 1 `listening on ` (unchanged — tests parse it), line 2 (TLS only) `tls fingerprint: sha256:` + +**Design note (deliberate spec deviation, record in the ADR):** the spec said the `--max-connections` busy-shed completes the TLS handshake so the 503 arrives readable. That would run a handshake **on the accept-loop thread**, letting one slow client stall all accepts — exactly the property P31 exists to protect. Instead, under TLS the cap-shed **closes the connection without a status** (git-daemon behavior); the plaintext 503 nicety is unchanged. Update the spec's §3 note when doing Task 9. + +- [ ] **Step 1: Write the failing tests** + +Add to `http_transport.rs`'s test module (the existing harness binds `127.0.0.1:0` and hands the listener to `serve_http_listener` on a thread — extend the existing `spawn_server`-style helper with a `tls: Option` parameter, defaulting existing call sites to `None`): + +```rust +/// Bind a TLS listener over a fresh repo; returns (addr, spki, join guard). +fn spawn_tls_server(root: &std::path::Path) -> (String, [u8; 32]) { + let id = scl_tlsio::load_or_mint(&root.join(".sc").join("serve-tls")).unwrap(); + let spki = id.spki_sha256; + let cfg = scl_tlsio::server_config(id).unwrap(); + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap().to_string(); + let root = root.to_path_buf(); + std::thread::spawn(move || { + let _ = serve_http_listener( + listener, + &root, + false, + false, + ServeLimits::default(), + Some(cfg), + ); + }); + (addr, spki) +} + +#[test] +fn tls_server_answers_opening_over_tls_and_rejects_plaintext() { + let root = tmp_repo("tls-opening"); // reuse the file's existing repo-fixture helper + let (addr, spki) = spawn_tls_server(&root); + + // A TLS client (raw tlsio, pinned) gets a 200 through the TLS channel. + let tcp = std::net::TcpStream::connect(&addr).unwrap(); + let (stream, seen) = scl_tlsio::client_connect(tcp, "127.0.0.1", Some(spki), false).unwrap(); + assert_eq!(seen, spki); + let (r, mut w) = stream.split(); + write_client_opening(&mut w, "127.0.0.1", "/", None).unwrap(); + use std::io::Write as _; + w.flush().unwrap(); + let mut br = std::io::BufReader::new(r); + assert_eq!(read_status(&mut br).unwrap(), 200); + + // A PLAINTEXT client against the TLS listener fails cleanly (the + // server-side handshake errors; the client sees close/garbage, not 200). + let mut plain = std::net::TcpStream::connect(&addr).unwrap(); + plain + .set_read_timeout(Some(std::time::Duration::from_secs(5))) + .unwrap(); + write_client_opening(&mut plain, "127.0.0.1", "/", None).unwrap(); + let mut resp = Vec::new(); + use std::io::Read as _; + let _ = plain.read_to_end(&mut resp); + assert!( + !String::from_utf8_lossy(&resp).starts_with("HTTP/1.1 200"), + "plaintext client must not reach the opening handler on a TLS listener" + ); + + std::fs::remove_dir_all(&root).unwrap(); +} + +#[test] +fn serve_tls_fingerprint_mints_and_is_stable() { + let root = tmp_repo("tls-fpr"); + let f1 = serve_tls_fingerprint(&root).unwrap(); + assert!(f1.starts_with("sha256:"), "got: {f1}"); + assert!(root.join(".sc").join("serve-tls").join("key.pem").exists()); + let f2 = serve_tls_fingerprint(&root).unwrap(); + assert_eq!(f1, f2, "fingerprint must load, not re-mint"); + std::fs::remove_dir_all(&root).unwrap(); +} +``` + +- [ ] **Step 2: Run tests to verify they fail** + +Run: `cargo test -p scl-repo http_transport` — Expected: COMPILE FAIL (`serve_http_listener` arity, `serve_tls_fingerprint` missing). + +- [ ] **Step 3: Implement the server seam** + +1. `crates/repo/src/layout.rs` — next to `tmp_dir()`: + +```rust +/// `.sc/serve-tls/` — the server's TLS identity (`cert.pem` + `key.pem`), +/// auto-minted on first `sc serve --http … --tls` (P32). The key IS the +/// identity: it is regenerated only when missing. +pub fn serve_tls_dir(&self) -> PathBuf { + self.dot_sc.join("serve-tls") +} +``` + +2. `http_transport.rs` — the mode enum, config resolution, fingerprint helper: + +```rust +/// How `sc serve --http` provides its TLS identity (P32). `Off` keeps the +/// P26 plaintext listener; `AutoMint` loads-or-mints `.sc/serve-tls/`; +/// `Pem` loads an operator-supplied pair (certbot etc.). +#[derive(Debug, Clone)] +pub enum TlsMode { + Off, + AutoMint, + Pem { + cert: std::path::PathBuf, + key: std::path::PathBuf, + }, +} + +fn resolve_tls(root: &std::path::Path, tls: &TlsMode) -> Result> { + let identity = match tls { + TlsMode::Off => return Ok(None), + TlsMode::AutoMint => { + scl_tlsio::load_or_mint(&crate::layout::Layout::at(root).serve_tls_dir())? + } + TlsMode::Pem { cert, key } => scl_tlsio::load_pem(cert, key)?, + }; + Ok(Some(scl_tlsio::server_config(identity)?)) +} + +/// 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 { + let id = scl_tlsio::load_or_mint(&crate::layout::Layout::at(root).serve_tls_dir())?; + Ok(scl_tlsio::fingerprint_hex(&id.spki_sha256)) +} +``` + +3. `serve_http`: new `tls: TlsMode` parameter. Resolve the config **before** the bind gate (Task 6 needs `tls_config.is_some()`); after the `listening on {bound}` println, add: + +```rust +if let Some(cfg) = &tls_config { + println!("tls fingerprint: {}", scl_tlsio::fingerprint_hex(&cfg.spki_sha256)); + std::io::Write::flush(&mut std::io::stdout()).ok(); +} +``` + +Pass `bind_is_allowed(addr, root, read_only, allow_public)` unchanged in THIS task (Task 6 tightens it); pass `tls_config` down: `serve_http_listener(listener, root, read_only, mandatory_auth, limits, tls_config)`. + +4. `serve_http_listener`: new `tls: Option` parameter. In the cap-shed arm, plaintext keeps its 503; TLS closes silently: + +```rust +if limits.max_connections != 0 && prev >= limits.max_connections as usize { + if tls.is_none() { + let _ = write_status(&mut stream, 503); + } + // Under TLS: close without a status. Sending a readable 503 would + // require a TLS handshake ON THE ACCEPT THREAD, letting one slow + // client stall all accepts — the property P31 exists to protect. + // (git-daemon sheds the same way: plain close.) + continue; // g drops here → count restored +} +``` + +Clone `tls` into each connection thread (`let tls = tls.clone();` before `spawn`) and pass `tls.as_ref()` to `handle_http_connection`. + +5. `handle_http_connection`: new `tls: Option<&scl_tlsio::TlsServerConfig>` parameter. Introduce the half enums (file-private) and rework the reader/writer construction; every subsequent `write_status(&mut stream, …)` becomes `write_status(&mut writer, …)` and the final `serve_with_policy(root, &mut reader, &mut writer, …)`: + +```rust +/// Server-side maybe-TLS halves: the opening codec, status writes, and +/// `wire::serve` are all generic over Read/Write, so one enum layer at the +/// seam keeps plaintext and TLS on the identical code path after wrap. +enum SrvRead { + Plain(TcpStream), + Tls(scl_tlsio::TlsServerReadHalf), +} +enum SrvWrite { + Plain(TcpStream), + Tls(scl_tlsio::TlsServerWriteHalf), +} +impl Read for SrvRead { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + match self { + SrvRead::Plain(s) => s.read(buf), + SrvRead::Tls(r) => r.read(buf), + } + } +} +impl Write for SrvWrite { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + match self { + SrvWrite::Plain(s) => s.write(buf), + SrvWrite::Tls(w) => w.write(buf), + } + } + fn flush(&mut self) -> std::io::Result<()> { + match self { + SrvWrite::Plain(s) => s.flush(), + SrvWrite::Tls(w) => w.flush(), + } + } +} +impl SrvRead { + /// P31 session timeouts, below TLS when wrapped. SO_RCVTIMEO/SO_SNDTIMEO + /// live on the socket (shared across `try_clone` fds), so setting them + /// through either half governs the whole connection — the same fact the + /// plaintext path has relied on since P31. + fn set_session_timeouts(&self, d: Option) -> std::io::Result<()> { + match self { + SrvRead::Plain(s) => { + s.set_read_timeout(d)?; + s.set_write_timeout(d) + } + SrvRead::Tls(r) => r.set_socket_timeouts(d, d), + } + } +} +``` + +Construction, replacing the current `try_clone` block (the opening timeout is set on the raw `TcpStream` FIRST, so it also bounds the TLS handshake — closing the handshake-slow-loris case for free): + +```rust +stream + .set_read_timeout(Some(OPENING_READ_TIMEOUT)) + .map_err(|e| Error::ConnectionLost(format!("sc+http set_read_timeout: {e}")))?; + +let (mut reader, mut writer) = match tls { + None => { + let r = stream + .try_clone() + .map_err(|e| Error::ConnectionLost(format!("sc+http socket clone: {e}")))?; + (BufReader::new(SrvRead::Plain(r)), SrvWrite::Plain(stream)) + } + Some(cfg) => { + let t = scl_tlsio::server_stream(cfg, stream)?; + let (r, w) = t.split(); + (BufReader::new(SrvRead::Tls(r)), SrvWrite::Tls(w)) + } +}; +``` + +The session-timeout block after the 200 becomes `reader.get_ref().set_session_timeouts(session).map_err(…)?;` (note: `stream` no longer exists at that point — the halves own it). + +6. Update in-crate call sites: every existing test calling `serve_http_listener(…)` gains a trailing `None`; the CLI call site at `crates/cli/src/main.rs:3191` gains a trailing `scl_repo::http_transport::TlsMode::Off` (real wiring in Task 7). + +- [ ] **Step 4: Run the workspace tests** + +Run: `cargo test --workspace` — Expected: new tests PASS; all existing http tests still PASS (plaintext path is byte-identical). + +- [ ] **Step 5: Commit** + +```bash +cargo clippy --all-targets -- -D warnings +git add crates/repo crates/cli +git commit -m "feat(repo): TLS-wrapped sc serve --http (TlsMode, serve-tls identity, fingerprint helper)" +``` + +--- + +### Task 5: client seam — `sc+https://` URLs, TOFU connect flow, `open_transport` routing + +**Files:** +- Modify: `crates/repo/src/http_transport.rs` (`ScHttpUrl`, `HttpTransport`) +- Modify: `crates/repo/src/stdio_transport.rs` (`open_transport`) + +**Interfaces:** +- Consumes: Task 2's `client_connect` + client halves; Task 3's `TlsClientPolicy`/`lookup`/`record`; Task 4's `spawn_tls_server` test helper. +- Produces (used by Task 7 and every existing caller of `open_transport`): + - `ScHttpUrl` gains `pub tls: bool`; `ScHttpUrl::parse` accepts `sc+http://` and `sc+https://` (same port default 8730) + - `pub fn connect_with_pins(url: &ScHttpUrl, token: Option<&str>, policy: Option<&crate::tls_pins::TlsClientPolicy>) -> Result` — the testable core; `connect_with_token` resolves the policy from env for TLS URLs and delegates; `connect` is unchanged in signature + - `HttpTransport` field becomes `WireClient, HttpWriteHalf>` (private enums, `Plain`/`Tls`) + +- [ ] **Step 1: Write the failing tests** + +In `http_transport.rs` tests: + +```rust +#[test] +fn parse_sc_https_sets_tls() { + let u = ScHttpUrl::parse("sc+https://example.com:9443/srv/repo").unwrap(); + assert!(u.tls); + assert_eq!((u.host.as_str(), u.port, u.path.as_str()), ("example.com", 9443, "/srv/repo")); + let u = ScHttpUrl::parse("sc+https://host/repo").unwrap(); + assert_eq!(u.port, DEFAULT_PORT); + let u = ScHttpUrl::parse("sc+http://host/repo").unwrap(); + assert!(!u.tls); + // Plain https:// is a GIT url (P18), never sc-native. + assert!(ScHttpUrl::parse("https://host/repo").is_err()); +} + +/// The full client TOFU lifecycle against a real TLS listener: +/// first connect pins, second is quiet, a swapped key hard-fails, +/// pre-pin never persists, strict refuses unknown. +#[test] +fn tofu_lifecycle_end_to_end() { + let root = tmp_repo("tofu-e2e"); + let (addr, spki) = spawn_tls_server(&root); + let (host, port) = addr.rsplit_once(':').map(|(h, p)| (h.to_string(), p.parse::().unwrap())).unwrap(); + let url = ScHttpUrl { host: host.clone(), port, path: "/".into(), tls: true }; + + let pins_dir = std::env::temp_dir().join(format!("scl-tofu-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&pins_dir); + let kh = pins_dir.join("known_hosts"); + let policy = crate::tls_pins::TlsClientPolicy { known_hosts: kh.clone(), pre_pin: None, strict: false }; + + // 1. Unknown host, accept-new: connects AND records the pin. + HttpTransport::connect_with_pins(&url, None, Some(&policy)).unwrap(); + assert_eq!(crate::tls_pins::lookup(&kh, &host, port).unwrap(), Some(spki)); + + // 2. Second connect: pin already known, still connects. + HttpTransport::connect_with_pins(&url, None, Some(&policy)).unwrap(); + + // 3. Strict mode with a pin present: fine. Strict against an UNKNOWN + // host (fresh pin file): refused with the typed error. + let strict_known = crate::tls_pins::TlsClientPolicy { known_hosts: kh.clone(), pre_pin: None, strict: true }; + HttpTransport::connect_with_pins(&url, None, Some(&strict_known)).unwrap(); + let fresh = pins_dir.join("fresh_kh"); + let strict_unknown = crate::tls_pins::TlsClientPolicy { known_hosts: fresh.clone(), pre_pin: None, strict: true }; + match HttpTransport::connect_with_pins(&url, None, Some(&strict_unknown)) { + Err(Error::TlsStrictUnknownHost(_)) => {} + other => panic!("expected TlsStrictUnknownHost, got {other:?}"), + } + assert!(!fresh.exists(), "strict refusal must not write a pin"); + + // 4. Pre-pin (SC_HTTPS_FINGERPRINT semantics): connects, never persists. + let prepin_file = pins_dir.join("prepin_kh"); + let prepin = crate::tls_pins::TlsClientPolicy { known_hosts: prepin_file.clone(), pre_pin: Some(spki), strict: true }; + HttpTransport::connect_with_pins(&url, None, Some(&prepin)).unwrap(); + assert!(!prepin_file.exists(), "pre-pin must not persist"); + + // 5. A WRONG stored pin hard-fails with recovery context. + let bad = pins_dir.join("bad_kh"); + crate::tls_pins::record(&bad, &host, port, &[0x24u8; 32]).unwrap(); + let badpol = crate::tls_pins::TlsClientPolicy { known_hosts: bad.clone(), pre_pin: None, strict: false }; + match HttpTransport::connect_with_pins(&url, None, Some(&badpol)) { + Err(Error::TlsPinMismatch { host: h, file, pinned, seen }) => { + assert!(h.contains(&host)); + assert!(file.contains("bad_kh")); + assert!(pinned.starts_with("sha256:") && seen.starts_with("sha256:")); + assert_eq!(seen, scl_tlsio::fingerprint_hex(&spki)); + } + other => panic!("expected TlsPinMismatch, got {other:?}"), + } + + std::fs::remove_dir_all(&pins_dir).unwrap(); + std::fs::remove_dir_all(&root).unwrap(); +} + +/// Transport verbs work over TLS end to end (list_refs against the real +/// server — the full clone/push/fetch acceptance rides the CLI test in +/// Task 7). +#[test] +fn transport_verbs_over_tls() { + let root = tmp_repo("tls-verbs"); + let (addr, spki) = spawn_tls_server(&root); + let (host, port) = addr.rsplit_once(':').map(|(h, p)| (h.to_string(), p.parse::().unwrap())).unwrap(); + let url = ScHttpUrl { host, port, path: "/".into(), tls: true }; + let policy = crate::tls_pins::TlsClientPolicy { + known_hosts: std::env::temp_dir().join(format!("scl-verbs-kh-{}", std::process::id())), + pre_pin: Some(spki), + strict: false, + }; + let t = HttpTransport::connect_with_pins(&url, None, Some(&policy)).unwrap(); + let refs = t.list_refs().unwrap(); + assert!(!refs.is_empty(), "fresh repo has at least its default branch tip or HEAD"); + let _ = std::fs::remove_file(&policy.known_hosts); + std::fs::remove_dir_all(&root).unwrap(); +} +``` + +(If a fresh `tmp_repo` has no commits and `list_refs` is legitimately empty, assert on `head_branch()` instead — mirror whatever the existing plaintext `real_server_*` test asserts right after connect.) + +- [ ] **Step 2: Run tests to verify they fail** + +Run: `cargo test -p scl-repo http_transport` — Expected: COMPILE FAIL (`tls` field, `connect_with_pins` missing). + +- [ ] **Step 3: Implement the client seam** + +1. `ScHttpUrl`: add `pub tls: bool`; parse both schemes: + +```rust +let (rest, tls) = if let Some(r) = url.strip_prefix("sc+http://") { + (r, false) +} else if let Some(r) = url.strip_prefix("sc+https://") { + (r, true) +} else { + return Err(Error::InvalidArgument(format!( + "not an sc+http(s):// url: {url}" + ))); +}; +``` + +(and `tls` in the constructed struct; update the existing struct-literal test fixtures.) + +2. Client half enums (file-private, beside `SrvRead`/`SrvWrite`): + +```rust +enum HttpReadHalf { + Plain(TcpStream), + Tls(scl_tlsio::TlsClientReadHalf), +} +enum HttpWriteHalf { + Plain(TcpStream), + Tls(scl_tlsio::TlsClientWriteHalf), +} +// impl Read for HttpReadHalf / impl Write for HttpWriteHalf: identical +// match-forwarding shape as SrvRead/SrvWrite above. +``` + +`HttpTransport` becomes `client: WireClient, HttpWriteHalf>`. + +3. `connect_with_token` / `connect_with_pins`: + +```rust +pub fn connect_with_token(url: &ScHttpUrl, token: Option<&str>) -> Result { + let policy = if url.tls { + Some(crate::tls_pins::TlsClientPolicy::from_env()?) + } else { + None + }; + Self::connect_with_pins(url, token, policy.as_ref()) +} + +/// The testable core: env-free. `policy` must be `Some` iff `url.tls` — +/// the pin policy IS the TLS client decision. +pub fn connect_with_pins( + url: &ScHttpUrl, + token: Option<&str>, + policy: Option<&crate::tls_pins::TlsClientPolicy>, +) -> Result { + if let Some(t) = token { + if t.contains(['\r', '\n']) { + return Err(Error::InvalidArgument( + "SC_HTTP_TOKEN must not contain CR or LF".to_string(), + )); + } + } + let stream = TcpStream::connect(url.authority()) + .map_err(|e| Error::ConnectionLost(format!("sc+http connect to {url:?}: {e}")))?; + + let (mut r, mut w) = match (url.tls, policy) { + (false, _) => { + let rd = stream + .try_clone() + .map_err(|e| Error::ConnectionLost(format!("sc+http socket clone: {e}")))?; + ( + BufReader::new(HttpReadHalf::Plain(rd)), + HttpWriteHalf::Plain(stream), + ) + } + (true, Some(p)) => { + let expected = match p.pre_pin { + Some(pin) => Some(pin), + None => crate::tls_pins::lookup(&p.known_hosts, &url.host, url.port)?, + }; + let known_before = expected.is_some(); + let pin_source = if p.pre_pin.is_some() { + "SC_HTTPS_FINGERPRINT".to_string() + } else { + p.known_hosts.display().to_string() + }; + match scl_tlsio::client_connect(stream, &url.host, expected, p.strict) { + Ok((tls_stream, seen)) => { + if !known_before { + // Accept-new TOFU: pin silently, ANNOUNCE loudly. + crate::tls_pins::record(&p.known_hosts, &url.host, url.port, &seen)?; + eprintln!( + "sc+https: first connection to {}:{} — pinned {}", + url.host, + url.port, + scl_tlsio::fingerprint_hex(&seen) + ); + eprintln!( + "sc+https: verify against `sc serve fingerprint` on the server; \ + stored in {}", + p.known_hosts.display() + ); + } + let (rh, wh) = tls_stream.split(); + ( + BufReader::new(HttpReadHalf::Tls(rh)), + HttpWriteHalf::Tls(wh), + ) + } + Err(scl_tlsio::Error::PinMismatch { expected, seen }) => { + return Err(Error::TlsPinMismatch { + host: url.authority(), + file: pin_source, + pinned: scl_tlsio::fingerprint_hex(&expected), + seen: scl_tlsio::fingerprint_hex(&seen), + }) + } + Err(scl_tlsio::Error::UnknownHostStrict) => { + return Err(Error::TlsStrictUnknownHost(url.authority())) + } + Err(e) => return Err(e.into()), + } + } + (true, None) => { + return Err(Error::InvalidArgument( + "internal: sc+https connect without a pin policy".to_string(), + )) + } + }; + + write_client_opening(&mut w, &url.host, &url.path, token)?; + use std::io::Write as _; + w.flush() + .map_err(|e| Error::ConnectionLost(format!("sc+http opening flush: {e}")))?; + + let status = read_status(&mut r)?; + // …status match unchanged from today… + let client = WireClient::handshake(r, w)?; + Ok(HttpTransport { client }) +} +``` + +(The status-mapping match moves verbatim; keep the doc comments. Note the opening is written AFTER pin disposition — the global constraint.) + +4. `open_transport` in `stdio_transport.rs`: + +```rust +} else if url.starts_with("sc+http://") || url.starts_with("sc+https://") { + let parsed = crate::http_transport::ScHttpUrl::parse(url)?; + Ok(Box::new(crate::http_transport::HttpTransport::connect(&parsed)?)) +} +``` + +- [ ] **Step 4: Run tests to verify they pass** + +Run: `cargo test -p scl-repo` — Expected: all PASS (new + existing plaintext http tests). + +- [ ] **Step 5: Commit** + +```bash +cargo clippy --all-targets -- -D warnings +git add crates/repo +git commit -m "feat(repo): sc+https:// client — TOFU pin flow at the connect seam, open_transport routing" +``` + +--- + +### Task 6: P29 gate tightening — plaintext public binds need `--allow-public` + +**Files:** +- Modify: `crates/repo/src/http_transport.rs` (`bind_is_allowed`, `serve_http`'s refusal message, existing gate tests) + +**Interfaces:** +- Consumes: Task 4's `TlsMode`/`resolve_tls` (already resolved before the gate). +- Produces: `fn bind_is_allowed(addr, root, read_only, allow_public, tls: bool) -> Result` — the decided lattice: loopback always; else `--read-only` or `--allow-public`; else tokens justify **only with TLS**. + +- [ ] **Step 1: Write/flip the failing tests** + +In the existing `bind_refuses_public_without_justification` test (http_transport.rs:1204) and around it, update to the new lattice (adding the `tls` argument to every existing `bind_is_allowed` call — existing plaintext cases pass `false`): + +```rust +#[test] +fn gate_lattice_p32() { + let root = tmp_repo("gate32"); + let no_tls = false; + let tls = true; + + // Loopback: always, TLS or not, tokens or not. + assert!(bind_is_allowed("127.0.0.1:1", &root, false, false, no_tls).unwrap()); + assert!(bind_is_allowed("127.0.0.1:1", &root, false, false, tls).unwrap()); + + // Non-loopback, nothing configured: refused either way. + assert!(!bind_is_allowed("0.0.0.0:1", &root, false, false, no_tls).unwrap()); + assert!(!bind_is_allowed("0.0.0.0:1", &root, false, false, tls).unwrap()); + + // --read-only / --allow-public: justify on their own, unchanged. + assert!(bind_is_allowed("0.0.0.0:1", &root, true, false, no_tls).unwrap()); + assert!(bind_is_allowed("0.0.0.0:1", &root, false, true, no_tls).unwrap()); + + // Configure a token… + add_test_token(&root); // reuse/extract the helper the P29 tests use to write .sc/serve-tokens.toml + + // …tokens + TLS: the blessed public posture. + assert!(bind_is_allowed("0.0.0.0:1", &root, false, false, tls).unwrap()); + // …tokens WITHOUT TLS: no longer justifies (the P32 break, decision 5). + assert!(!bind_is_allowed("0.0.0.0:1", &root, false, false, no_tls).unwrap()); + + std::fs::remove_dir_all(&root).unwrap(); +} +``` + +(Look at how the existing P29 gate tests configure a token — `serve_tokens::add`/direct file write — and reuse that exact mechanism as `add_test_token`.) + +- [ ] **Step 2: Run tests to verify they fail** + +Run: `cargo test -p scl-repo gate` — Expected: COMPILE FAIL (arity), then after mechanical arity fixes the P32 assertions FAIL against the old logic. + +- [ ] **Step 3: Implement the tightened gate** + +```rust +/// The fail-closed bind decision (P29, tightened by P32/ADR-0042): a +/// non-loopback `addr` is allowed only if justified by `--read-only`, +/// `--allow-public`, or — once TLS is available — `--tls` plus ≥1 configured +/// serve token. A PLAINTEXT public bind justified only by tokens (the P29 +/// rule) is refused: the token would cross the wire in the clear, which is +/// the exact exposure this phase closes. Deliberate, narrow pre-1.0 break. +fn bind_is_allowed( + addr: &str, + root: &std::path::Path, + read_only: bool, + allow_public: bool, + tls: bool, +) -> Result { + let host = addr.rsplit_once(':').map(|(h, _)| h).unwrap_or(addr); + let host = host.trim_start_matches('[').trim_end_matches(']'); + if is_loopback_host(host) { + return Ok(true); + } + if read_only || allow_public { + return Ok(true); + } + if !tls { + return Ok(false); + } + let tokens_configured = + !crate::serve_tokens::load(&crate::layout::Layout::at(root))?.is_empty(); + Ok(tokens_configured) +} +``` + +In `serve_http`, call it with `tls_config.is_some()` and update the refusal message: + +```rust +return Err(Error::InvalidArgument(format!( + "refusing to bind non-loopback address {addr}: use --tls with a configured serve \ + token (sc serve token add), or --read-only, or --allow-public; a plaintext public \ + bind would send bearer tokens and repo traffic in the clear (use 127.0.0.1 for \ + local-only serving)" +))); +``` + +`auth_is_mandatory` is UNCHANGED: on a TLS+tokens-justified public bind it already returns true (non-loopback, not ro, not allow-public), preserving the P29 fail-closed 401 when the last token is removed at runtime. Add one line to its doc comment noting the P32 case rides the same rule. + +- [ ] **Step 4: Run tests to verify they pass** + +Run: `cargo test --workspace` — Expected: PASS. Check specifically that P29's `auth_is_mandatory` tests and the existing bind tests (now arity-updated) are green. + +- [ ] **Step 5: Commit** + +```bash +cargo clippy --all-targets -- -D warnings +git add crates/repo +git commit -m "feat(repo)!: plaintext non-loopback binds now require --allow-public; tokens justify only with --tls (P32 gate)" +``` + +--- + +### Task 7: CLI — `--tls/--tls-cert/--tls-key`, `sc serve fingerprint`, acceptance test + +**Files:** +- Modify: `crates/cli/src/main.rs` (Serve command ~line 275–320, `ServeSub` ~line 639, `run_serve` ~line 3140) +- Create: `crates/cli/tests/https_remote.rs` + +**Interfaces:** +- Consumes: Task 4's `TlsMode`, `serve_tls_fingerprint`; Task 6's gate; Task 5's client (via `sc clone/push/fetch` on `sc+https://` URLs through `open_transport`). +- Produces: user surface — `sc serve --http --tls [--tls-cert --tls-key ] `, `sc serve fingerprint []`. + +- [ ] **Step 1: Write the failing CLI tests** + +`crates/cli/tests/https_remote.rs` (copy the `sc`/`tmp`/`spawn_http_server` helpers from `crates/cli/tests/http_remote.rs` verbatim — subprocess tests can't share them across files; extend `spawn_http_server` to also return the fingerprint line when `--tls` is among `extra`, reading stdout line 2 `tls fingerprint: sha256:`): + +```rust +//! CLI acceptance for sc+https:// (P32): flag validation, `sc serve +//! fingerprint`, and the spec's round-trip criterion — clone + push + fetch +//! over TLS with a signed ~1 MiB blob under forced SC_PACK_CHUNK, +//! byte-for-byte, zero .sc/tmp residue. Env knobs (SC_HTTPS_*) are safe here +//! because every command is a SUBPROCESS with its own env (Command::env), +//! not a racy in-process set_var. + +// …helpers as described… + +#[test] +fn tls_flags_validated() { + let root = tmp("flags"); + assert!(sc(&root, &["init"]).status.success()); + // --tls-cert without --tls + let out = sc(&root, &["serve", "--http", "127.0.0.1:0", "--tls-cert", "x.pem", root.to_str().unwrap()]); + assert!(!out.status.success()); + assert!(String::from_utf8_lossy(&out.stderr).contains("--tls")); + // --tls with --stdio + let out = sc(&root, &["serve", "--stdio", "--tls", root.to_str().unwrap()]); + assert!(!out.status.success()); + // --tls-cert without --tls-key + let out = sc(&root, &["serve", "--http", "127.0.0.1:0", "--tls", "--tls-cert", "x.pem", root.to_str().unwrap()]); + assert!(!out.status.success()); +} + +#[test] +fn serve_fingerprint_mints_and_matches_banner() { + let root = tmp("fpr"); + assert!(sc(&root, &["init"]).status.success()); + let out = sc(&root, &["serve", "fingerprint", root.to_str().unwrap()]); + assert!(out.status.success()); + let fpr = String::from_utf8_lossy(&out.stdout).trim().to_string(); + assert!(fpr.starts_with("sha256:")); + // The identity persisted; a TLS serve now banners the SAME fingerprint. + let (mut child, _addr, banner_fpr) = spawn_tls_http_server(&root, &[]); + assert_eq!(banner_fpr, fpr); + child.kill().ok(); +} + +#[test] +fn https_clone_push_fetch_round_trip_with_signed_chunked_blob() { + let w = tmp("rt"); + let origin = w.join("origin"); + std::fs::create_dir_all(&origin).unwrap(); + assert!(sc(&origin, &["init"]).status.success()); + + // Identity OUTSIDE the working tree (P5 scanner flags scl-id files). + let keyout = sc(&w, &["keygen"]); // writes into cwd = w, not the repo + assert!(keyout.status.success()); + let identity = /* parse the emitted path/name from keygen stdout, as provenance.rs does */; + + // ~1 MiB deterministic blob + a signed commit. + let blob: Vec = (0..1_048_576u32).map(|i| (i % 251) as u8).collect(); + std::fs::write(origin.join("big.bin"), &blob).unwrap(); + assert!(sc(&origin, &["commit", "-m", "big", "--sign", "--identity", &identity]).status.success()); + + // rw token; capture the raw value from stdout (auth demo pattern). + let tok_out = sc(&origin, &["serve", "token", "add", "--label", "ci", "--scope", "rw"]); + assert!(tok_out.status.success()); + let token = String::from_utf8_lossy(&tok_out.stdout).trim().to_string(); + + let (mut child, addr, _fpr) = spawn_tls_http_server(&origin, &[]); + let url = format!("sc+https://{addr}/"); + let kh = w.join("known_hosts"); + + // Clone with forced tiny chunks; env is per-subprocess, race-free. + let clone_dir = w.join("clone"); + let out = sc_env(&w, &["clone", &url, clone_dir.to_str().unwrap()], &[ + ("SC_HTTP_TOKEN", &token), + ("SC_HTTPS_KNOWN_HOSTS", kh.to_str().unwrap()), + ("SC_PACK_CHUNK", "4096"), + ]); + assert!(out.status.success(), "clone failed: {}", String::from_utf8_lossy(&out.stderr)); + // First-connect TOFU announced + pinned. + assert!(String::from_utf8_lossy(&out.stderr).contains("pinned")); + assert!(kh.exists()); + // Byte-for-byte. + assert_eq!(std::fs::read(clone_dir.join("big.bin")).unwrap(), blob); + // Signature rode the chunked TLS stream. + let log = sc(&clone_dir, &["log"]); + assert!(String::from_utf8_lossy(&log.stdout).contains("signed:")); + + // Push an edit back over TLS (second connect: pin known → NO "pinned"). + std::fs::write(clone_dir.join("new.txt"), "from clone").unwrap(); + assert!(sc(&clone_dir, &["commit", "-m", "edit"]).status.success()); + let out = sc_env(&clone_dir, &["push", "origin"], &[ + ("SC_HTTP_TOKEN", &token), + ("SC_HTTPS_KNOWN_HOSTS", kh.to_str().unwrap()), + ]); + assert!(out.status.success(), "push failed: {}", String::from_utf8_lossy(&out.stderr)); + assert!(!String::from_utf8_lossy(&out.stderr).contains("pinned"), "second connect must be quiet"); + + // Fetch from a second clone sees the edit. + let clone2 = w.join("clone2"); + assert!(sc_env(&w, &["clone", &url, clone2.to_str().unwrap()], &[ + ("SC_HTTP_TOKEN", &token), + ("SC_HTTPS_KNOWN_HOSTS", kh.to_str().unwrap()), + ]).status.success()); + assert_eq!(std::fs::read_to_string(clone2.join("new.txt")).unwrap(), "from clone"); + + // Zero .sc/tmp residue on every end. + for repo in [&origin, &clone_dir, &clone2] { + let tmp_dir = repo.join(".sc").join("tmp"); + let empty = !tmp_dir.exists() + || std::fs::read_dir(&tmp_dir).unwrap().next().is_none(); + assert!(empty, ".sc/tmp residue in {}", repo.display()); + } + + // Key swap → pin mismatch hard-fails. + child.kill().ok(); + child.wait().ok(); + std::fs::remove_dir_all(origin.join(".sc").join("serve-tls")).unwrap(); + let (mut child2, addr2, _f) = spawn_tls_http_server(&origin, &[]); + let url2 = format!("sc+https://{addr2}/"); + let clone3 = w.join("clone3"); + // Re-pin the OLD server's fingerprint under the NEW address first, so + // the lookup hits: copy the kh line for addr → addr2. Simplest: write a + // kh containing addr2 mapped to the OLD fingerprint captured earlier. + let out = sc_env(&w, &["clone", &url2, clone3.to_str().unwrap()], &[ + ("SC_HTTP_TOKEN", &token), + ("SC_HTTPS_KNOWN_HOSTS", mismatch_kh(&w, &addr2, /*old fpr*/).to_str().unwrap()), + ]); + assert!(!out.status.success()); + let err = String::from_utf8_lossy(&out.stderr); + assert!(err.contains("does not match the pinned fingerprint"), "got: {err}"); + child2.kill().ok(); +} +``` + +Fill in `sc_env` (a `sc()` variant taking `envs: &[(&str, &str)]`), `spawn_tls_http_server` (spawn with `--tls`, parse stdout lines 1–2), `mismatch_kh` (write a kh file mapping `addr2`'s host:port to the first server's fingerprint), and the keygen-output parsing by copying whatever `crates/cli/tests/provenance.rs` does for identities — mirror, don't invent. The tightened-gate CLI behavior (public plaintext bind refused) is already unit-tested in Task 6 and demo-proven in Task 8; a CLI duplicate would need a real non-loopback bind, which CI can't do reliably. + +- [ ] **Step 2: Run tests to verify they fail** + +Run: `cargo test -p scl-cli --test https_remote` — Expected: FAIL (`--tls` unknown flag; `fingerprint` unknown subcommand). + +- [ ] **Step 3: Implement the CLI wiring** + +1. `Serve` command variant — add after `allow_public`: + +```rust +/// Serve TLS (`sc+https://`): auto-mints a self-signed identity into +/// `.sc/serve-tls/` unless --tls-cert/--tls-key supply a PEM pair. +/// `--http` only. (P32) +#[arg(long)] +tls: bool, +/// PEM certificate chain for --tls (requires --tls-key). +#[arg(long, requires = "tls_key", requires = "tls")] +tls_cert: Option, +/// PEM private key for --tls (requires --tls-cert). +#[arg(long, requires = "tls_cert", requires = "tls")] +tls_key: Option, +``` + +(Check the exact clap `requires` key names against the generated field names; if `requires = "tls"` misbehaves with `bool`, do the validation in `run_serve` instead — the tests assert behavior, not mechanism.) + +2. `ServeSub` — add: + +```rust +/// Print this repo's serve-TLS fingerprint (sha256: of the SPKI), +/// minting `.sc/serve-tls/` if absent — distribute this to clients as +/// SC_HTTPS_FINGERPRINT or to verify a first-connect pin. (P32) +Fingerprint { path: Option }, +``` + +with a match arm where `ServeSub::Token` is handled: + +```rust +ServeSub::Fingerprint { path } => { + let root = match path { + Some(p) => p, + None => std::env::current_dir()?, + }; + println!("{}", scl_repo::http_transport::serve_tls_fingerprint(&root)?); + Ok(()) +} +``` + +3. `run_serve` — thread the three new params; in the `(true, None)` stdio arm add: + +```rust +if tls || tls_cert.is_some() || tls_key.is_some() { + anyhow::bail!("--tls applies only to --http (ssh already provides --stdio's confidential channel)"); +} +``` + +in the `(false, Some(addr))` arm build the mode: + +```rust +let tls_mode = match (tls, tls_cert, tls_key) { + (false, None, None) => scl_repo::http_transport::TlsMode::Off, + (false, _, _) => anyhow::bail!("--tls-cert/--tls-key require --tls"), + (true, None, None) => scl_repo::http_transport::TlsMode::AutoMint, + (true, Some(cert), Some(key)) => scl_repo::http_transport::TlsMode::Pem { cert, key }, + (true, _, _) => anyhow::bail!("--tls-cert and --tls-key must be given together"), +}; +scl_repo::http_transport::serve_http(&addr, &path, read_only, allow_public, limits, tls_mode)?; +``` + +(remove Task 4's temporary `TlsMode::Off` literal.) + +4. Clone routing sanity: `sc+https://` must NOT be caught by the P18 hosted-git URL detection (`https://` prefix check). Verify with `grep -n '"https://"' crates/cli/src/main.rs` that the detection uses `starts_with("https://")` (which `sc+https://…` does not match). If any check is looser, tighten it and pin with a small routing unit test. + +- [ ] **Step 4: Run tests to verify they pass** + +Run: `cargo test -p scl-cli` then `cargo test --workspace` — Expected: PASS. + +- [ ] **Step 5: Commit** + +```bash +cargo clippy --all-targets -- -D warnings +git add crates/cli +git commit -m "feat(cli): sc serve --tls / --tls-cert / --tls-key + sc serve fingerprint; sc+https acceptance test" +``` + +--- + +### Task 8: demo — `demo/run_tls_demo.sh` + +**Files:** +- Create: `demo/run_tls_demo.sh` (executable) + +**Interfaces:** +- Consumes: the full CLI surface from Task 7. +- Produces: the phase's demoable proof, `run_http_auth_demo.sh` pattern (self-checking assertions, `fail()`, trap cleanup, port picking with `nc -z`, run-twice at the end). + +- [ ] **Step 1: Write the script** + +Follow `demo/run_http_auth_demo.sh`'s skeleton exactly (shebang, `set -euo pipefail`, `cd "$(dirname "$0")/.."`, `cargo build --quiet --bin sc`, `fail()`, `mktemp -d`, `cleanup` trap killing server PIDs, `pick_port`, `wait_for_port`). Content, in order — every claim an assertion: + +1. **Setup:** init `$W/origin`; keygen into `$W` (identity outside the tree); write a ~1 MiB blob (`head -c 1048576 /dev/zero | tr '\0' 'x'` is fine — determinism matters, entropy doesn't); `sc commit --sign --identity`; `sc serve token add --label demo --scope rw` capturing the raw token. +2. **Fingerprint before first serve:** `FPR=$("$SC" serve fingerprint "$W/origin")`; assert `sha256:` prefix; assert `.sc/serve-tls/key.pem` exists with `stat` mode 600. +3. **TLS round trip:** start `sc serve --http 127.0.0.1:$PORT --tls "$W/origin"` in the background; assert its stdout banner contains `tls fingerprint: $FPR`. Clone with `SC_HTTP_TOKEN=$TOKEN SC_HTTPS_KNOWN_HOSTS=$W/kh SC_PACK_CHUNK=4096 sc clone "sc+https://127.0.0.1:$PORT/" "$W/clone"` — assert stderr contains `pinned` AND the fingerprint equals `$FPR` (grep); `cmp` the blob byte-for-byte; `sc log` in the clone contains `signed:`. +4. **Quiet second connect + push/fetch:** commit an edit in the clone, `sc push origin` (same env, no `SC_PACK_CHUNK` needed) — assert stderr does NOT contain `pinned`; second clone sees the edit. +5. **Strict + pre-pin:** `SC_HTTPS_STRICT=1` with a FRESH known_hosts file → clone fails, stderr mentions `SC_HTTPS_STRICT`; adding `SC_HTTPS_FINGERPRINT=$FPR` (still fresh kh) → succeeds AND the fresh kh file was never created. +6. **Mismatch hard-fail:** stop the server; `rm -rf "$W/origin/.sc/serve-tls"`; restart (new key, new fingerprint — assert banner fingerprint differs from `$FPR`); clone with the OLD `$W/kh` → fails, stderr contains `does not match the pinned fingerprint`; assert it names the kh path. +7. **Tightened plaintext gate:** `sc serve --http 0.0.0.0:$PORT2 "$W/origin"` (tokens configured, NO `--tls`) → must exit non-zero, stderr names `--tls`; `sc serve --http 0.0.0.0:$PORT2 --tls "$W/origin"` → binds (assert `listening on`, then kill). (`run_http_auth_demo.sh` already binds `0.0.0.0` for its gate proof — reuse that pattern.) +8. **Zero residue:** assert `.sc/tmp` empty/absent in origin and both clones; assert `$W` cleanup via trap. +9. **Run twice:** the auth demo's pattern — the whole body in a function invoked twice with fresh temp dirs, `echo "RESULT: ok"` at the end. + +- [ ] **Step 2: Run it** + +Run: `bash demo/run_tls_demo.sh` — Expected: `RESULT: ok` (twice), exit 0. Fix any assertion drift (exact stderr phrasing) by adjusting the demo's greps to the real messages — the messages are the source of truth, they were pinned by tests in Tasks 5–7. + +- [ ] **Step 3: Commit** + +```bash +chmod +x demo/run_tls_demo.sh +git add demo/run_tls_demo.sh +git commit -m "demo: sc+https TLS round trip, TOFU lifecycle, tightened plaintext gate (run twice)" +``` + +--- + +### Task 9: docs — ADR-0042, CLAUDE.md, THREAT-MODEL, ROADMAP, spec amendment + +**Files:** +- Create: `docs/adr/0042-in-binary-tls-sc-https.md` +- Modify: `CLAUDE.md` (commands block, dependency rule, quarantine list, P32 section) +- Modify: `docs/THREAT-MODEL.md` (transport section) +- Modify: `ROADMAP.md` (move the TLS deferral to shipped; record follow-ons) +- Modify: `docs/superpowers/specs/2026-07-10-p32-tls-sc-https-design.md` (busy-shed deviation) + +- [ ] **Step 1: Write ADR-0042** + +Follow the house ADR format (look at `docs/adr/0041-listener-resource-limits.md` for the exact header/section shape). Required content, all decided — write it as prose in the house style: + +- **Context:** audit High #1; ADR-0036/0040 boundaries ("no TLS", reverse proxy guidance incomplete — server leg only); research doc `docs/research/tls-options-sc-http.md`; decide tickets #26/#35. +- **Decision — why in-binary TLS is load-bearing here:** Vaultwarden/Garage punt to reverse proxies because they are server-only; sc's CLIENT must reach servers its operator doesn't control, so a proxy can never cover the client leg — the case where in-binary TLS is genuinely load-bearing. +- **Provider:** rustls 0.23, ring provider (`default-features = false`; ~14 new crates measured; C compiler only, no cmake); **aws-lc-rs is the recorded swap-in fallback** (18 crates + cmake); pure-Rust providers rejected as immature (graviola, rustls-rustcrypto). +- **Quarantine:** `crates/tlsio`, the only crate linking rustls/rcgen/ring/pki-types; dependency leaf; `repo → tlsio`; SHA-256 via `ring::digest` (NOT RustCrypto — that quarantine holds). +- **Trust model:** accept-new TOFU; pin = SPKI-SHA-256 (survives same-key renewal, openssl-verifiable); pin-only in v1 — names/validity deliberately ignored; handshake signatures still verified; mismatch always hard-fails, never prompts; `SC_HTTPS_FINGERPRINT` pre-pin; `SC_HTTPS_STRICT`; `~/.config/sc/known_hosts` (`SC_HTTPS_KNOWN_HOSTS` override). First connection remains vulnerable by construction (the SSH known_hosts trade, stated plainly). +- **Server lifecycle:** explicit `--tls`; rcgen auto-mint into `.sc/serve-tls/` (key 0600, key-is-identity, long validity); PEM path; `sc serve fingerprint` mints-if-missing; ACME rejected (async stack — certbot/proxies instead). +- **Gate change (breaking):** the P32 lattice; tokens alone no longer justify plaintext public binds. +- **Accepted consequences:** (1) under TLS the `--max-connections` shed closes without a 503 — a readable busy status would require a handshake on the accept thread (deviation from the phase spec's first draft, deliberate); (2) first TLS dep means tracking the rustls 0.23→0.24 API break; (3) CA-path validation deferred (additive later for PEM deployments); (4) the plaintext-gate break is pre-1.0 and narrow. + +- [ ] **Step 2: Update CLAUDE.md** + +1. Workspace layout block: add `crates/tlsio → TLS for sc+https (depends on nothing; ONLY crate linking rustls/rcgen)` and extend the dependency-rule sentence: `repo` also depends on `tlsio`; **rustls/rcgen must stay quarantined in `tlsio`**. +2. Commands block, after the P31 serve entries: + +``` +cargo run --bin sc -- serve --http --tls [--tls-cert --tls-key ] + # TLS listener (P32): sc+https://; auto-mints a + # self-signed identity into .sc/serve-tls/ (key + # 0600, key-is-identity) unless PEM given; banner + # prints the SPKI fingerprint; NB gate change: + # tokens justify a non-loopback bind ONLY with + # --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:) +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 + # overrides), mismatch always hard-fails; + # SC_HTTPS_FINGERPRINT= pre-pins (CI), + # SC_HTTPS_STRICT=1 refuses unknown hosts; + # remote add/fetch/push accept the same URL form +bash demo/run_tls_demo.sh # sc+https proof (P32): TLS round trip w/ signed + # chunked blob, TOFU pin/mismatch/strict/pre-pin, + # tightened plaintext gate — run twice +``` + +3. Add a `**Phase 32 is built.**` section in the phase history (match the established voice/density: seams touched, the gate break, the busy-shed accepted consequence, boundaries — no CA validation, first-connect trust, plaintext token on loopback-no-token setups unchanged) and update the P26/P29 sections' forward references ("no TLS … deferred" → "closed by P32 (ADR-0042)"). +4. Remaining follow-ons list: add CA-path validation, `sc+https` SNI/name validation as an opt-in, TLS session resumption knobs (if rejected, say rejected), pin management UX (`sc tls` list/remove pins). + +- [ ] **Step 3: Update docs/THREAT-MODEL.md, ROADMAP.md, and the spec** + +- THREAT-MODEL transport section: rewrite around `sc+https://` — bearer tokens and traffic are confidential on TLS transports (`sc+https://`, `ssh://`); the plaintext `sc+http://` boundary statement now applies only to loopback/`--allow-public` deployments; TOFU first-connect exposure stated plainly; reverse-proxy guidance must document **both legs** (server-side TCP-mode termination — nginx `stream` / HAProxy `mode tcp` / stunnel — AND the client-side tunnel: client-mode stunnel or `ssh -L` — OR just use `sc+https://`/`ssh://` now that they exist); promote `ssh://` (ADR-0022) as the long-shipped confidential transport. Pull the both-legs specifics from `docs/research/tls-options-sc-http.md` §B (already written and cited there). +- ROADMAP.md: the "first-party TLS dependency deferred" line moves to shipped-in-P32; add the follow-ons from Step 2.4. +- Spec amendment (`docs/superpowers/specs/2026-07-10-p32-tls-sc-https-design.md` §3): replace the busy-shed sentence with the Task 4 decision (TLS shed = silent close; rationale: no handshake on the accept thread). + +- [ ] **Step 4: Full workspace check + all demos** + +```bash +cargo test --workspace +cargo clippy --all-targets -- -D warnings +bash demo/run_http_auth_demo.sh # P29 demo must still pass (loopback binds unchanged; + # check its 0.0.0.0 token-only case — if it asserted the + # OLD gate (tokens justify plaintext public), update that + # demo to expect the P32 refusal and add --tls) +bash demo/run_http_remote_demo.sh +bash demo/run_tls_demo.sh +``` + +Expected: all green. The `run_http_auth_demo.sh` note is real work: P29's demo proves a token-justified public bind — under the P32 gate that exact case now refuses. Update that demo's assertion to the new behavior (refusal + `--tls` hint) as part of this task. + +- [ ] **Step 5: Commit** + +```bash +git add docs CLAUDE.md ROADMAP.md demo +git commit -m "docs: ADR-0042 (in-binary TLS), CLAUDE.md P32, THREAT-MODEL transport rewrite, roadmap" +``` + +--- + +## Self-Review (performed while writing) + +- **Spec coverage:** crate+quarantine (T1–2), TOFU/pins/env knobs (T2–3, 5), server lifecycle+fingerprint (T4, 7), gate (T6), CLI (T7), acceptance tests incl. signed chunked blob + residue (T5, 7), demo (T8), ADR/CLAUDE/THREAT-MODEL/both-legs docs (T9). One deliberate deviation (TLS busy-shed closes silently) is flagged in T4 and folded back into the spec in T9. +- **Type consistency:** `TlsServerConfig`/`server_config(ServerIdentity)`/`client_connect(tcp, host, Option<[u8;32]>, bool) -> (TlsClientStream, [u8;32])`/`connect_with_pins(url, token, Option<&TlsClientPolicy>)`/`serve_http(..., TlsMode)`/`serve_http_listener(..., Option)`/`bind_is_allowed(..., tls: bool)` are used identically across tasks. +- **Known API risk:** exact rustls-0.23 builder / rcgen-0.14 method names may drift at the resolved patch version; Tasks 1–2 carry explicit "check `cargo doc`, keep public tlsio signatures fixed" notes at the two spots where that can bite. From 68d55e73f7a92eb6cdb5cb401cd256985bda89ae Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 10 Jul 2026 14:42:59 +0300 Subject: [PATCH 03/14] =?UTF-8?q?feat(tlsio):=20P32=20scaffold=20=E2=80=94?= =?UTF-8?q?=20SPKI=20fingerprints=20+=20cert=20mint/load=20(rustls/rcgen?= =?UTF-8?q?=20quarantine)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 424 ++++++++++++++++++++++++++++++++++- Cargo.toml | 2 +- crates/tlsio/Cargo.toml | 17 ++ crates/tlsio/src/identity.rs | 133 +++++++++++ crates/tlsio/src/lib.rs | 34 +++ crates/tlsio/src/spki.rs | 129 +++++++++++ 6 files changed, 731 insertions(+), 8 deletions(-) create mode 100644 crates/tlsio/Cargo.toml create mode 100644 crates/tlsio/src/identity.rs create mode 100644 crates/tlsio/src/lib.rs create mode 100644 crates/tlsio/src/spki.rs diff --git a/Cargo.lock b/Cargo.lock index decb69f2..e1c6e660 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -63,7 +63,7 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -74,7 +74,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -104,6 +104,66 @@ version = "0.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f02882884d3e1bc524fb12c79f107f6ad0e1cfd498c536ffb494301740995dfe" +[[package]] +name = "asn1-rs" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7f43a50ac4fdca5df8e885c21b835997f0a1cdee65494a6847694a98652d9d8" +dependencies = [ + "asn1-rs-derive", + "asn1-rs-impl", + "displaydoc", + "nom", + "num-traits", + "rusticata-macros", + "thiserror", + "time", +] + +[[package]] +name = "asn1-rs-derive" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3109e49b1e4909e9db6515a30c633684d68cdeaa252f215214cb4fa1a5bfee2c" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "asn1-rs-impl" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b18050c2cd6fe86c3a76584ef5e0baf286d038cda203eb6223df2cc413565f7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bit-vec" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b71798fca2c1fe1086445a7258a4bc81e6e49dcd24c8d0dd9a1e57395b603f51" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -409,6 +469,12 @@ dependencies = [ "parking_lot_core", ] +[[package]] +name = "data-encoding" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4ae5f15dda3c708c0ade84bfee31ccab44a3da4f88015ed22f63732abe300c8" + [[package]] name = "defmt" version = "1.1.0" @@ -441,6 +507,26 @@ dependencies = [ "thiserror", ] +[[package]] +name = "der-parser" +version = "10.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07da5016415d5a3c4dd39b11ed26f915f52fc4e0dc197d87908bc916e51bc1a6" +dependencies = [ + "asn1-rs", + "displaydoc", + "nom", + "num-bigint", + "num-traits", + "rusticata-macros", +] + +[[package]] +name = "deranged" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c" + [[package]] name = "digest" version = "0.10.7" @@ -462,6 +548,17 @@ dependencies = [ "crypto-common 0.2.2", ] +[[package]] +name = "displaydoc" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ac70aa55017e108007fbaf5aa0f54b021c98f92ff8af59d42eda9da96e3dd4f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "dunce" version = "1.0.5" @@ -512,7 +609,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -1314,7 +1411,7 @@ dependencies = [ "bitflags 2.13.0", "gix-path", "libc", - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -1702,6 +1799,12 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.186" @@ -1755,12 +1858,71 @@ dependencies = [ "libc", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "nonempty" version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9737e026353e5cd0736f98eddae28665118eb6f6600902a7f50db585621fecb6" +[[package]] +name = "num-bigint" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-conv" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "oid-registry" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12f40cff3dde1b6087cc5d5f5d4d65712f34016a03ed60e9c08dcc392736b5b7" +dependencies = [ + "asn1-rs", +] + [[package]] name = "once_cell" version = "1.21.4" @@ -1802,6 +1964,16 @@ dependencies = [ "windows-link", ] +[[package]] +name = "pem" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d30c53c26bc5b31a98cd02d20f25a7c8567146caf63ed593a9d87b2775291be" +dependencies = [ + "base64", + "serde_core", +] + [[package]] name = "percent-encoding" version = "2.3.2" @@ -1840,6 +2012,12 @@ dependencies = [ "portable-atomic", ] +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "ppv-lite86" version = "0.2.21" @@ -1931,6 +2109,20 @@ dependencies = [ "getrandom 0.2.17", ] +[[package]] +name = "rcgen" +version = "0.14.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57f6d249aad744e274e682777a50283a225a32705394ee6d5fcc01efa25e4055" +dependencies = [ + "pem", + "ring", + "rustls-pki-types", + "time", + "x509-parser", + "yasna", +] + [[package]] name = "redox_syscall" version = "0.5.18" @@ -1969,6 +2161,20 @@ version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + [[package]] name = "rustc_version" version = "0.4.1" @@ -1978,6 +2184,15 @@ dependencies = [ "semver", ] +[[package]] +name = "rusticata-macros" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "faf0c4a6ece9950b9abdb62b1cfcf2a68b3b67a10ba445b3bb85be2a293d0632" +dependencies = [ + "nom", +] + [[package]] name = "rustix" version = "1.1.4" @@ -1988,7 +2203,42 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustls" +version = "0.23.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b92b125634d9b795e7beca796cc790df15a7fb38323bf3196fda83292d06b1f" +dependencies = [ + "log", + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "764899a24af3980067ee14bc143654f297b22eaebfe3c7b6b211920a5a59b046" +dependencies = [ + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c429a8649f110dddef65e2a5ad240f747e85f7758a6bccc7e5777bd33f756e" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", ] [[package]] @@ -2075,6 +2325,18 @@ dependencies = [ "toml", ] +[[package]] +name = "scl-tlsio" +version = "0.1.0" +dependencies = [ + "hex", + "rcgen", + "ring", + "rustls", + "rustls-pki-types", + "thiserror", +] + [[package]] name = "scl-vfs" version = "0.1.0" @@ -2269,6 +2531,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tempfile" version = "3.27.0" @@ -2279,7 +2552,7 @@ dependencies = [ "getrandom 0.4.3", "once_cell", "rustix", - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -2302,6 +2575,36 @@ dependencies = [ "syn", ] +[[package]] +name = "time" +version = "0.3.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18dfaaeddcb932337b5e7866ee7d0ce9b76d2fd092997146f187ec09b4558a50" +dependencies = [ + "deranged", + "num-conv", + "powerfmt", + "serde_core", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109" + +[[package]] +name = "time-macros" +version = "0.2.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c431b87111666e491a90baa837f914fb45cd5dc3c268591b0220ff5057f2085f" +dependencies = [ + "num-conv", + "time-core", +] + [[package]] name = "tinyvec" version = "1.11.0" @@ -2402,6 +2705,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + [[package]] name = "utf8parse" version = "0.2.2" @@ -2461,7 +2770,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -2476,6 +2785,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.61.2" @@ -2485,6 +2803,70 @@ dependencies = [ "windows-link", ] +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + [[package]] name = "winnow" version = "1.0.3" @@ -2509,6 +2891,34 @@ dependencies = [ "zeroize", ] +[[package]] +name = "x509-parser" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d43b0f71ce057da06bc0851b23ee24f3f86190b07203dd8f567d0b706a185202" +dependencies = [ + "asn1-rs", + "data-encoding", + "der-parser", + "lazy_static", + "nom", + "oid-registry", + "ring", + "rusticata-macros", + "thiserror", + "time", +] + +[[package]] +name = "yasna" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5f6765e852b9b4dc8e2a76843e4d64d1cea8e79bcde0b6901aea8e7c7f08282" +dependencies = [ + "bit-vec", + "time", +] + [[package]] name = "zerocopy" version = "0.8.52" diff --git a/Cargo.toml b/Cargo.toml index 5e8999e3..80fbf79f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] resolver = "2" -members = ["crates/core", "crates/vfs", "crates/gitio", "crates/crypto", "crates/repo", "crates/cli"] +members = ["crates/core", "crates/vfs", "crates/gitio", "crates/crypto", "crates/repo", "crates/cli", "crates/tlsio"] [workspace.package] version = "0.1.0" diff --git a/crates/tlsio/Cargo.toml b/crates/tlsio/Cargo.toml new file mode 100644 index 00000000..20fc6289 --- /dev/null +++ b/crates/tlsio/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "scl-tlsio" +version.workspace = true +edition.workspace = true +license.workspace = true +publish.workspace = true + +[dependencies] +hex = "0.4.3" +rcgen = "0.14.8" +ring = "0.17.14" +rustls = { version = "0.23.41", default-features = false, features = ["ring", "std", "tls12", "logging"] } +rustls-pki-types = { version = "1.15.0", features = ["std"] } +thiserror = "2.0.18" + +[lints] +workspace = true diff --git a/crates/tlsio/src/identity.rs b/crates/tlsio/src/identity.rs new file mode 100644 index 00000000..38fe0ebb --- /dev/null +++ b/crates/tlsio/src/identity.rs @@ -0,0 +1,133 @@ +//! Server TLS identity: load `cert.pem`/`key.pem` from a directory, minting +//! a self-signed pair (rcgen) when absent. The KEY is the identity — +//! regenerate only when missing; the cert carries a far-future not_after so +//! renewal never bites a pinned deployment (pins are on the SPKI anyway). + +use std::path::Path; + +use rustls::pki_types::{pem::PemObject, CertificateDer, PrivateKeyDer}; + +use crate::{spki_sha256, Error, Result}; + +/// A loaded/minted server identity, ready for `server_config` (Task 2). +#[derive(Debug)] +pub struct ServerIdentity { + pub certs: Vec>, + pub key: PrivateKeyDer<'static>, + pub spki_sha256: [u8; 32], +} + +/// Load `dir/cert.pem` + `dir/key.pem`, minting both (key mode 0600) when +/// the CERT is absent. A dir with a cert but no key (or vice versa) is an +/// error, not a silent re-mint — regenerating over half an identity would +/// invalidate every client pin without the operator asking for it. +pub fn load_or_mint(dir: &Path) -> Result { + let cert_path = dir.join("cert.pem"); + let key_path = dir.join("key.pem"); + match (cert_path.exists(), key_path.exists()) { + (true, true) => load_pem(&cert_path, &key_path), + (false, false) => { + std::fs::create_dir_all(dir)?; + let key = rcgen::KeyPair::generate().map_err(|e| Error::Mint(e.to_string()))?; + let mut params = rcgen::CertificateParams::new(vec!["sc-serve".to_string()]) + .map_err(|e| Error::Mint(e.to_string()))?; + params.not_after = rcgen::date_time_ymd(2126, 1, 1); + let cert = params + .self_signed(&key) + .map_err(|e| Error::Mint(e.to_string()))?; + std::fs::write(&cert_path, cert.pem())?; + write_key_0600(&key_path, key.serialize_pem().as_bytes())?; + load_pem(&cert_path, &key_path) + } + _ => Err(Error::Mint(format!( + "{} holds half a TLS identity (one of cert.pem/key.pem is missing); \ + restore the missing file or remove the directory to re-mint", + dir.display() + ))), + } +} + +/// Load a user-supplied PEM pair (certbot etc.). The fingerprint is the +/// leaf's (first cert's) SPKI hash. +pub fn load_pem(cert: &Path, key: &Path) -> Result { + let certs: Vec> = CertificateDer::pem_file_iter(cert) + .map_err(|e| Error::Mint(format!("read {}: {e}", cert.display())))? + .collect::>() + .map_err(|e| Error::Mint(format!("parse {}: {e}", cert.display())))?; + let leaf = certs.first().ok_or(Error::BadCert)?; + let spki = spki_sha256(leaf)?; + let key = PrivateKeyDer::from_pem_file(key) + .map_err(|e| Error::Mint(format!("read {}: {e}", key.display())))?; + Ok(ServerIdentity { + spki_sha256: spki, + certs, + key, + }) +} + +#[cfg(unix)] +fn write_key_0600(path: &Path, bytes: &[u8]) -> Result<()> { + use std::io::Write; + use std::os::unix::fs::OpenOptionsExt; + let mut f = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .mode(0o600) + .open(path)?; + f.write_all(bytes)?; + Ok(()) +} + +#[cfg(not(unix))] +fn write_key_0600(path: &Path, bytes: &[u8]) -> Result<()> { + std::fs::write(path, bytes)?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn tmp(tag: &str) -> std::path::PathBuf { + let d = std::env::temp_dir().join(format!("scl-tlsio-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + d + } + + #[test] + fn mint_then_load_is_idempotent_and_key_is_0600() { + let dir = tmp("mint"); + let a = load_or_mint(&dir).unwrap(); + let b = load_or_mint(&dir).unwrap(); + // Second call LOADS (same identity), never re-mints. + assert_eq!(a.spki_sha256, b.spki_sha256); + assert_eq!(a.certs, b.certs); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = std::fs::metadata(dir.join("key.pem")).unwrap().permissions().mode(); + assert_eq!(mode & 0o777, 0o600, "key.pem must be 0600"); + } + std::fs::remove_dir_all(&dir).unwrap(); + assert!(!dir.exists()); + } + + #[test] + fn half_an_identity_errors_instead_of_silently_reminting() { + let dir = tmp("half"); + load_or_mint(&dir).unwrap(); + std::fs::remove_file(dir.join("key.pem")).unwrap(); + let err = load_or_mint(&dir).unwrap_err(); + assert!(err.to_string().contains("half a TLS identity"), "got: {err}"); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn load_pem_reads_a_minted_pair() { + let dir = tmp("pem"); + let minted = load_or_mint(&dir).unwrap(); + let loaded = load_pem(&dir.join("cert.pem"), &dir.join("key.pem")).unwrap(); + assert_eq!(minted.spki_sha256, loaded.spki_sha256); + std::fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/crates/tlsio/src/lib.rs b/crates/tlsio/src/lib.rs new file mode 100644 index 00000000..fef6b4c2 --- /dev/null +++ b/crates/tlsio/src/lib.rs @@ -0,0 +1,34 @@ +//! TLS quarantine crate (P32, ADR-0042): the ONLY crate linking rustls and +//! rcgen (the gix→gitio / RustCrypto→crypto precedent). Exposes blocking +//! `Read + Write` TLS streams for the sc+https:// transport, a pin-only +//! TOFU certificate verifier, self-signed cert mint/load, and SPKI-SHA-256 +//! fingerprints. Depends on no other workspace crate. + +mod identity; +mod spki; +// mod stream; // Task 2 + +pub use identity::{load_or_mint, load_pem, ServerIdentity}; +pub use rustls::pki_types::{CertificateDer, PrivateKeyDer}; +pub use spki::{fingerprint_hex, spki_der, spki_sha256}; + +/// Errors from the TLS layer. `PinMismatch` carries both fingerprints so the +/// caller (crates/repo) can render a recovery hint naming its pin source — +/// this crate knows nothing about known_hosts files or env vars. +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("could not parse the server certificate (DER)")] + BadCert, + #[error("TLS handshake failed: {0}")] + Handshake(String), + #[error("server key does not match the pinned fingerprint")] + PinMismatch { expected: [u8; 32], seen: [u8; 32] }, + #[error("unknown host refused (strict mode)")] + UnknownHostStrict, + #[error("certificate mint/load failed: {0}")] + Mint(String), + #[error("TLS I/O: {0}")] + Io(#[from] std::io::Error), +} + +pub type Result = std::result::Result; diff --git a/crates/tlsio/src/spki.rs b/crates/tlsio/src/spki.rs new file mode 100644 index 00000000..0f63ff63 --- /dev/null +++ b/crates/tlsio/src/spki.rs @@ -0,0 +1,129 @@ +//! SubjectPublicKeyInfo extraction from a certificate DER, and the +//! `sha256:` fingerprint every P32 surface uses (pin file, banner, +//! `sc serve fingerprint`, `SC_HTTPS_FINGERPRINT`). +//! +//! The extraction is a minimal hand-written DER TLV walk rather than a full +//! x509 parser dependency: we need exactly one field, the walk is ~40 lines, +//! and a malformed certificate simply fails the connection (fail-closed). +//! Correctness is anchored by the test comparing against rcgen's own +//! `PublicKeyData::subject_public_key_info()` for a cert we minted. + +use crate::{Error, Result}; + +/// One DER TLV: returns (tag, content_start..content_end, total_len). +fn der_tlv(buf: &[u8]) -> Option<(u8, core::ops::Range, usize)> { + let tag = *buf.first()?; + let first = *buf.get(1)?; + let (len, header) = if first & 0x80 == 0 { + (first as usize, 2) + } else { + let n = (first & 0x7f) as usize; + if n == 0 || n > 4 { + return None; + } + let mut len = 0usize; + for i in 0..n { + len = (len << 8) | *buf.get(2 + i)? as usize; + } + (len, 2 + n) + }; + let end = header.checked_add(len)?; + if end > buf.len() { + return None; + } + Some((tag, header..end, end)) +} + +/// Extract the full SubjectPublicKeyInfo TLV (header included) from a +/// certificate DER. Layout: `Certificate ::= SEQUENCE { tbsCertificate, +/// signatureAlgorithm, signature }`; `TBSCertificate ::= SEQUENCE { +/// [0] version OPTIONAL, serialNumber, signature, issuer, validity, +/// subject, subjectPublicKeyInfo, ... }` — so: descend two SEQUENCEs, skip +/// the optional context-0 version, skip five fields, take the sixth. +pub fn spki_der(cert_der: &[u8]) -> Result<&[u8]> { + let (tag, body, _) = der_tlv(cert_der).ok_or(Error::BadCert)?; + if tag != 0x30 { + return Err(Error::BadCert); + } + let tbs_buf = &cert_der[body]; + let (tag, body, _) = der_tlv(tbs_buf).ok_or(Error::BadCert)?; + if tag != 0x30 { + return Err(Error::BadCert); + } + let mut rest = &tbs_buf[body]; + if rest.first() == Some(&0xa0) { + let (_, _, used) = der_tlv(rest).ok_or(Error::BadCert)?; + rest = &rest[used..]; + } + for _ in 0..5 { + let (_, _, used) = der_tlv(rest).ok_or(Error::BadCert)?; + rest = &rest[used..]; + } + let (tag, _, used) = der_tlv(rest).ok_or(Error::BadCert)?; + if tag != 0x30 { + return Err(Error::BadCert); + } + Ok(&rest[..used]) +} + +/// SHA-256 over the SPKI TLV — the pin. ring's digest, not a RustCrypto +/// crate (RustCrypto stays quarantined in crates/crypto); ring is already +/// in-tree as rustls's crypto provider. +pub fn spki_sha256(cert_der: &[u8]) -> Result<[u8; 32]> { + let spki = spki_der(cert_der)?; + let d = ring::digest::digest(&ring::digest::SHA256, spki); + let mut out = [0u8; 32]; + out.copy_from_slice(d.as_ref()); + Ok(out) +} + +/// Render a pin as `sha256:` — the one fingerprint format. +pub fn fingerprint_hex(spki_hash: &[u8; 32]) -> String { + format!("sha256:{}", hex::encode(spki_hash)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn spki_der_matches_rcgen_public_key_der() { + let key = rcgen::KeyPair::generate().unwrap(); + let params = rcgen::CertificateParams::new(vec!["sc-serve".to_string()]).unwrap(); + let cert = params.self_signed(&key).unwrap(); + let extracted = spki_der(cert.der()).unwrap(); + assert_eq!(extracted, rcgen::PublicKeyData::subject_public_key_info(&key).as_slice()); + } + + #[test] + fn spki_sha256_is_stable_across_same_key_remint() { + let key = rcgen::KeyPair::generate().unwrap(); + let c1 = rcgen::CertificateParams::new(vec!["a".to_string()]) + .unwrap() + .self_signed(&key) + .unwrap(); + let c2 = rcgen::CertificateParams::new(vec!["b".to_string()]) + .unwrap() + .self_signed(&key) + .unwrap(); + // Different cert bytes, same key → same SPKI hash (the whole point + // of pinning the SPKI, not the cert). + assert_ne!(c1.der().as_ref(), c2.der().as_ref()); + assert_eq!(spki_sha256(c1.der()).unwrap(), spki_sha256(c2.der()).unwrap()); + } + + #[test] + fn fingerprint_hex_format() { + let h = [0xabu8; 32]; + let s = fingerprint_hex(&h); + assert!(s.starts_with("sha256:")); + assert_eq!(s.len(), "sha256:".len() + 64); + } + + #[test] + fn spki_der_rejects_garbage() { + assert!(spki_der(b"not a certificate").is_err()); + assert!(spki_der(&[]).is_err()); + assert!(spki_der(&[0x30, 0x82, 0xff]).is_err()); // truncated length + } +} From db7b35bb427366d797e1387768de04b02905a58b Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 10 Jul 2026 14:52:20 +0300 Subject: [PATCH 04/14] feat(tlsio): TOFU pin verifier + blocking client/server TLS streams with split halves --- crates/tlsio/src/lib.rs | 6 +- crates/tlsio/src/stream.rs | 381 +++++++++++++++++++++++++++++++++++++ 2 files changed, 386 insertions(+), 1 deletion(-) create mode 100644 crates/tlsio/src/stream.rs diff --git a/crates/tlsio/src/lib.rs b/crates/tlsio/src/lib.rs index fef6b4c2..83c55872 100644 --- a/crates/tlsio/src/lib.rs +++ b/crates/tlsio/src/lib.rs @@ -6,11 +6,15 @@ mod identity; mod spki; -// mod stream; // Task 2 +mod stream; pub use identity::{load_or_mint, load_pem, ServerIdentity}; pub use rustls::pki_types::{CertificateDer, PrivateKeyDer}; pub use spki::{fingerprint_hex, spki_der, spki_sha256}; +pub use stream::{ + client_connect, server_config, server_stream, TlsClientReadHalf, TlsClientStream, + TlsClientWriteHalf, TlsServerConfig, TlsServerReadHalf, TlsServerStream, TlsServerWriteHalf, +}; /// Errors from the TLS layer. `PinMismatch` carries both fingerprints so the /// caller (crates/repo) can render a recovery hint naming its pin source — diff --git a/crates/tlsio/src/stream.rs b/crates/tlsio/src/stream.rs new file mode 100644 index 00000000..c23c4631 --- /dev/null +++ b/crates/tlsio/src/stream.rs @@ -0,0 +1,381 @@ +//! Blocking TLS streams over `TcpStream`, split into Read/Write halves. +//! +//! The wire protocol needs SEPARATE reader/writer values (`WireClient` +//! and `wire::serve(r, w)`), but a `rustls::StreamOwned` is one object — so +//! `split` shares it behind `Arc>` with a half per side. That is +//! safe (not a deadlock risk) because the sc wire protocol is strictly +//! sequential request-reply on both ends: a read and a write never block +//! concurrently on one connection. +//! +//! Both `client_connect` and `server_stream` drive the handshake to +//! completion before returning: the client's pin disposition must be settled +//! BEFORE any application byte (the HTTP opening, the bearer token) is +//! written, and a garbage/non-TLS peer must fail at the seam, not +//! mid-protocol. + +use std::io::{Read, Write}; +use std::net::TcpStream; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use rustls::pki_types::ServerName; + +use crate::{spki_sha256, Error, Result, ServerIdentity}; + +/// Pin-only server-cert verifier (v1 trust model, ADR-0042): the SPKI hash +/// is the entire trust decision — names and validity windows are +/// deliberately ignored. Handshake signatures ARE still verified against +/// the presented key (otherwise a MITM could replay the pinned cert without +/// holding its private key). +#[derive(Debug)] +struct PinVerifier { + expected: Option<[u8; 32]>, + strict: bool, + seen: Mutex>, + algs: rustls::crypto::WebPkiSupportedAlgorithms, +} + +impl rustls::client::danger::ServerCertVerifier for PinVerifier { + fn verify_server_cert( + &self, + end_entity: &rustls::pki_types::CertificateDer<'_>, + _intermediates: &[rustls::pki_types::CertificateDer<'_>], + _server_name: &ServerName<'_>, + _ocsp_response: &[u8], + _now: rustls::pki_types::UnixTime, + ) -> std::result::Result { + let hash = spki_sha256(end_entity).map_err(|_| { + rustls::Error::InvalidCertificate(rustls::CertificateError::BadEncoding) + })?; + *self.seen.lock().unwrap() = Some(hash); + match self.expected { + Some(p) if p == hash => Ok(rustls::client::danger::ServerCertVerified::assertion()), + Some(_) => Err(rustls::Error::General("sc: pinned fingerprint mismatch".into())), + None if self.strict => Err(rustls::Error::General("sc: unknown host (strict)".into())), + None => Ok(rustls::client::danger::ServerCertVerified::assertion()), + } + } + + fn verify_tls12_signature( + &self, + message: &[u8], + cert: &rustls::pki_types::CertificateDer<'_>, + dss: &rustls::DigitallySignedStruct, + ) -> std::result::Result { + rustls::crypto::verify_tls12_signature(message, cert, dss, &self.algs) + } + + fn verify_tls13_signature( + &self, + message: &[u8], + cert: &rustls::pki_types::CertificateDer<'_>, + dss: &rustls::DigitallySignedStruct, + ) -> std::result::Result { + rustls::crypto::verify_tls13_signature(message, cert, dss, &self.algs) + } + + fn supported_verify_schemes(&self) -> Vec { + self.algs.supported_schemes() + } +} + +/// A built server config (one per listener), cheap to clone per connection. +#[derive(Clone)] +pub struct TlsServerConfig { + config: Arc, + /// The identity's SPKI hash, for the startup banner / `sc serve + /// fingerprint` without re-reading the PEM. + pub spki_sha256: [u8; 32], +} + +impl std::fmt::Debug for TlsServerConfig { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TlsServerConfig").finish_non_exhaustive() + } +} + +/// Build a reusable server TLS config from a minted/loaded identity. +pub fn server_config(id: ServerIdentity) -> Result { + let provider = Arc::new(rustls::crypto::ring::default_provider()); + let config = rustls::ServerConfig::builder_with_provider(provider) + .with_safe_default_protocol_versions() + .map_err(|e| Error::Handshake(e.to_string()))? + .with_no_client_auth() + .with_single_cert(id.certs, id.key) + .map_err(|e| Error::Handshake(e.to_string()))?; + Ok(TlsServerConfig { + config: Arc::new(config), + spki_sha256: id.spki_sha256, + }) +} + +/// A completed server-side TLS connection wrapping a `TcpStream`. +pub struct TlsServerStream { + inner: rustls::StreamOwned, +} + +/// A completed client-side TLS connection wrapping a `TcpStream`. +pub struct TlsClientStream { + inner: rustls::StreamOwned, +} + +impl std::fmt::Debug for TlsClientStream { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TlsClientStream").finish_non_exhaustive() + } +} + +/// Accept-side wrap: complete the handshake (bounded by whatever socket +/// timeout the caller set on `tcp`) and return a ready stream. +pub fn server_stream(cfg: &TlsServerConfig, mut tcp: TcpStream) -> Result { + let mut conn = rustls::ServerConnection::new(cfg.config.clone()) + .map_err(|e| Error::Handshake(e.to_string()))?; + while conn.is_handshaking() { + conn.complete_io(&mut tcp) + .map_err(|e| Error::Handshake(format!("server handshake: {e}")))?; + } + Ok(TlsServerStream { + inner: rustls::StreamOwned::new(conn, tcp), + }) +} + +/// Connect-side wrap. Returns the ready stream AND the observed SPKI hash +/// (for TOFU recording). Error mapping: a handshake failure caused by our +/// own verifier is translated back into the typed pin errors using the +/// verifier's recorded state. +pub fn client_connect( + mut tcp: TcpStream, + host: &str, + expected_pin: Option<[u8; 32]>, + strict: bool, +) -> Result<(TlsClientStream, [u8; 32])> { + let provider = Arc::new(rustls::crypto::ring::default_provider()); + let algs = provider.signature_verification_algorithms; + let verifier = Arc::new(PinVerifier { + expected: expected_pin, + strict, + seen: Mutex::new(None), + algs, + }); + let config = rustls::ClientConfig::builder_with_provider(provider) + .with_safe_default_protocol_versions() + .map_err(|e| Error::Handshake(e.to_string()))? + .dangerous() + .with_custom_certificate_verifier(verifier.clone()) + .with_no_client_auth(); + let name = ServerName::try_from(host.to_string()) + .map_err(|e| Error::Handshake(format!("bad server name {host}: {e}")))?; + let mut conn = rustls::ClientConnection::new(Arc::new(config), name) + .map_err(|e| Error::Handshake(e.to_string()))?; + while conn.is_handshaking() { + if let Err(e) = conn.complete_io(&mut tcp) { + // Translate our verifier's rejections into typed errors. + let seen = *verifier.seen.lock().unwrap(); + if let (Some(expected), Some(seen)) = (expected_pin, seen) { + if expected != seen { + return Err(Error::PinMismatch { expected, seen }); + } + } + if expected_pin.is_none() && strict && seen.is_some() { + return Err(Error::UnknownHostStrict); + } + return Err(Error::Handshake(format!("client handshake with {host}: {e}"))); + } + } + let seen = verifier + .seen + .lock() + .unwrap() + .ok_or_else(|| Error::Handshake("handshake completed without a certificate".into()))?; + Ok(( + TlsClientStream { + inner: rustls::StreamOwned::new(conn, tcp), + }, + seen, + )) +} + +// ── split halves ─────────────────────────────────────────────────────────── + +macro_rules! halves { + ($stream:ident, $read:ident, $write:ident) => { + /// TLS read half, sharing the underlying connection with its write + /// half via `Arc>` (safe under sc's strict + /// request-reply-sequential wire discipline — see the module docs). + pub struct $read(Arc>); + /// TLS write half; see the read half's doc comment for the sharing + /// model. + pub struct $write(Arc>); + + impl $stream { + /// Split into independent read/write halves that can move to + /// separate threads (both are `Send`). + pub fn split(self) -> ($read, $write) { + let shared = Arc::new(Mutex::new(self)); + ($read(shared.clone()), $write(shared)) + } + } + + impl Read for $read { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + self.0.lock().unwrap().inner.read(buf) + } + } + + impl Write for $write { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.0.lock().unwrap().inner.write(buf) + } + fn flush(&mut self) -> std::io::Result<()> { + self.0.lock().unwrap().inner.flush() + } + } + + impl $read { + /// Set read/write timeouts on the UNDERLYING socket (SO_RCVTIMEO + /// / SO_SNDTIMEO apply to the socket, so they govern the TLS + /// stream's blocking behavior below the record layer) — the P31 + /// session-timeout hook. + pub fn set_socket_timeouts( + &self, + read: Option, + write: Option, + ) -> std::io::Result<()> { + let g = self.0.lock().unwrap(); + g.inner.get_ref().set_read_timeout(read)?; + g.inner.get_ref().set_write_timeout(write) + } + } + }; +} + +halves!(TlsClientStream, TlsClientReadHalf, TlsClientWriteHalf); +halves!(TlsServerStream, TlsServerReadHalf, TlsServerWriteHalf); + +// Compile-time proof that every half can cross a thread boundary — the sc +// wire server (Task 4/5) spawns a thread per connection and hands one half +// to each side's I/O loop. +const _: fn() = || { + fn assert_send() {} + assert_send::(); + assert_send::(); + assert_send::(); + assert_send::(); +}; + +#[cfg(test)] +mod tests { + use super::*; + use std::io::{Read, Write}; + use std::net::{TcpListener, TcpStream}; + + fn tmp(tag: &str) -> std::path::PathBuf { + let d = std::env::temp_dir().join(format!("scl-tlsio-st-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + d + } + + /// Loopback server: accept one connection, TLS-wrap it, echo one + /// length-prefixed message back. Returns (addr, join handle, spki). + fn echo_server(dir: &std::path::Path) -> (String, std::thread::JoinHandle<()>, [u8; 32]) { + let id = crate::load_or_mint(dir).unwrap(); + let spki = id.spki_sha256; + let cfg = server_config(id).unwrap(); + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap().to_string(); + let h = std::thread::spawn(move || { + let (tcp, _) = listener.accept().unwrap(); + // A client that aborts its handshake (mismatch/strict tests) + // surfaces as Err here — that's fine, just return. + let Ok(stream) = server_stream(&cfg, tcp) else { return }; + let (mut r, mut w) = stream.split(); + let mut buf = [0u8; 5]; + if r.read_exact(&mut buf).is_err() { + return; + } + w.write_all(&buf).unwrap(); + w.flush().unwrap(); + }); + (addr, h, spki) + } + + #[test] + fn accept_new_returns_seen_pin_and_data_flows_through_halves() { + let dir = tmp("tofu"); + let (addr, h, spki) = echo_server(&dir); + let tcp = TcpStream::connect(&addr).unwrap(); + let (stream, seen) = client_connect(tcp, "127.0.0.1", None, false).unwrap(); + assert_eq!(seen, spki, "observed pin must be the server's SPKI hash"); + let (mut r, mut w) = stream.split(); + w.write_all(b"hello").unwrap(); + w.flush().unwrap(); + let mut back = [0u8; 5]; + r.read_exact(&mut back).unwrap(); + assert_eq!(&back, b"hello"); + h.join().unwrap(); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(!dir.exists()); + } + + #[test] + fn matching_pin_connects() { + let dir = tmp("pinok"); + let (addr, h, spki) = echo_server(&dir); + let tcp = TcpStream::connect(&addr).unwrap(); + assert!(client_connect(tcp, "127.0.0.1", Some(spki), false).is_ok()); + h.join().ok(); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(!dir.exists()); + } + + #[test] + fn mismatched_pin_hard_fails_with_both_fingerprints() { + let dir = tmp("pinbad"); + let (addr, h, spki) = echo_server(&dir); + let wrong = [0x42u8; 32]; + assert_ne!(wrong, spki); + let tcp = TcpStream::connect(&addr).unwrap(); + match client_connect(tcp, "127.0.0.1", Some(wrong), false) { + Err(crate::Error::PinMismatch { expected, seen }) => { + assert_eq!(expected, wrong); + assert_eq!(seen, spki); + } + other => panic!("expected PinMismatch, got {other:?}"), + } + h.join().ok(); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(!dir.exists()); + } + + #[test] + fn strict_refuses_unknown_host() { + let dir = tmp("strict"); + let (addr, h, _) = echo_server(&dir); + let tcp = TcpStream::connect(&addr).unwrap(); + match client_connect(tcp, "127.0.0.1", None, true) { + Err(crate::Error::UnknownHostStrict) => {} + other => panic!("expected UnknownHostStrict, got {other:?}"), + } + h.join().ok(); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(!dir.exists()); + } + + #[test] + fn plain_tcp_client_against_tls_server_fails_cleanly() { + let dir = tmp("plain"); + let (addr, h, _) = echo_server(&dir); + let mut tcp = TcpStream::connect(&addr).unwrap(); + // A plaintext opening against a TLS listener must error server-side + // (covered by echo_server's `else return`), and the client just sees + // a dead/garbled connection — no hang. + tcp.write_all(b"POST / HTTP/1.1\r\n\r\n").ok(); + tcp.set_read_timeout(Some(std::time::Duration::from_secs(5))).unwrap(); + let mut buf = [0u8; 16]; + // Read either 0 (close) or a TLS alert — anything but a hang. + let _ = tcp.read(&mut buf); + h.join().unwrap(); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(!dir.exists()); + } +} From 76c761c0e06ca1d2ebe8fbf9657ef8e298744fb1 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 10 Jul 2026 15:02:09 +0300 Subject: [PATCH 05/14] feat(repo): sc+https pin store (known_hosts), env pin policy, TLS error variants --- Cargo.lock | 2 + crates/repo/Cargo.toml | 2 + crates/repo/src/error.rs | 19 ++++ crates/repo/src/lib.rs | 1 + crates/repo/src/tls_pins.rs | 175 ++++++++++++++++++++++++++++++++++++ 5 files changed, 199 insertions(+) create mode 100644 crates/repo/src/tls_pins.rs diff --git a/Cargo.lock b/Cargo.lock index e1c6e660..5f2424dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2315,10 +2315,12 @@ dependencies = [ name = "scl-repo" version = "0.1.0" dependencies = [ + "hex", "libc", "regex", "scl-core", "scl-crypto", + "scl-tlsio", "scl-vfs", "serde", "thiserror", diff --git a/crates/repo/Cargo.toml b/crates/repo/Cargo.toml index 3d7fdad4..471aa181 100644 --- a/crates/repo/Cargo.toml +++ b/crates/repo/Cargo.toml @@ -14,6 +14,8 @@ regex = "1" toml = "1" serde = { version = "1", features = ["derive"] } libc = "0.2.186" +scl-tlsio = { version = "0.1.0", path = "../tlsio" } +hex = "0.4.3" [lints] workspace = true diff --git a/crates/repo/src/error.rs b/crates/repo/src/error.rs index f64e8ddf..1c1fc2d3 100644 --- a/crates/repo/src/error.rs +++ b/crates/repo/src/error.rs @@ -76,6 +76,25 @@ pub enum Error { ConnectionLost(String), #[error("remote error: {0}")] Remote(String), + #[error("TLS: {0}")] + Tls(#[from] scl_tlsio::Error), + #[error( + "sc+https server key for {host} does not match the pinned fingerprint ({file})\n \ + pinned: {pinned}\n server: {seen}\n\ + If the server key legitimately changed, remove that host's line from the pin file \ + and reconnect (the next connect re-pins); verify with `sc serve fingerprint` on the server." + )] + TlsPinMismatch { + host: String, + file: String, + pinned: String, + seen: String, + }, + #[error( + "sc+https host {0} is not pinned and SC_HTTPS_STRICT=1 refuses unknown hosts; \ + pre-pin with SC_HTTPS_FINGERPRINT=sha256: or connect once without SC_HTTPS_STRICT" + )] + TlsStrictUnknownHost(String), #[error( "secret {0} changed differently on both branches; resolve with `sc secret` then retry" )] diff --git a/crates/repo/src/lib.rs b/crates/repo/src/lib.rs index fdd2804c..5095a5df 100644 --- a/crates/repo/src/lib.rs +++ b/crates/repo/src/lib.rs @@ -33,6 +33,7 @@ pub mod sparse; pub mod stdio_transport; pub mod sync; pub mod textdiff; +pub mod tls_pins; pub mod transcripts; pub mod transport; pub mod wire; diff --git a/crates/repo/src/tls_pins.rs b/crates/repo/src/tls_pins.rs new file mode 100644 index 00000000..628d1623 --- /dev/null +++ b/crates/repo/src/tls_pins.rs @@ -0,0 +1,175 @@ +//! Client-side TOFU pin store for sc+https:// (P32): a user-level +//! known_hosts file, one line per `host:port`, format +//! `host:port sha256:` — plus the env knobs that shape a connection's +//! pin policy. The repo layer owns pin PERSISTENCE and POLICY; the actual +//! in-handshake check lives in scl-tlsio's verifier. + +use std::path::{Path, PathBuf}; + +use crate::error::{Error, Result}; + +/// The pin policy for one client connection, resolved from env by +/// [`TlsClientPolicy::from_env`] or built directly by tests (env mutation is +/// process-global and racy under parallel tests — the +/// `connect_with_token`/`connect_with_pins` split exists for this). +#[derive(Debug, Clone)] +pub struct TlsClientPolicy { + pub known_hosts: PathBuf, + /// `SC_HTTPS_FINGERPRINT`: verified for this process only, never + /// persisted (CI-friendly). + pub pre_pin: Option<[u8; 32]>, + /// `SC_HTTPS_STRICT=1`: refuse unknown hosts instead of accept-new. + pub strict: bool, +} + +impl TlsClientPolicy { + pub fn from_env() -> Result { + let known_hosts = match std::env::var_os("SC_HTTPS_KNOWN_HOSTS") { + Some(p) if !p.is_empty() => PathBuf::from(p), + _ => default_known_hosts_path()?, + }; + let pre_pin = match std::env::var("SC_HTTPS_FINGERPRINT") { + Ok(s) if !s.is_empty() => Some(parse_fingerprint(&s)?), + _ => None, + }; + let strict = std::env::var("SC_HTTPS_STRICT").map(|v| v == "1").unwrap_or(false); + Ok(TlsClientPolicy { + known_hosts, + pre_pin, + strict, + }) + } +} + +/// `$XDG_CONFIG_HOME/sc/known_hosts`, falling back to +/// `$HOME/.config/sc/known_hosts`. +pub fn default_known_hosts_path() -> Result { + if let Some(xdg) = std::env::var_os("XDG_CONFIG_HOME").filter(|v| !v.is_empty()) { + return Ok(PathBuf::from(xdg).join("sc").join("known_hosts")); + } + match std::env::var_os("HOME").filter(|v| !v.is_empty()) { + Some(home) => Ok(PathBuf::from(home).join(".config").join("sc").join("known_hosts")), + None => Err(Error::InvalidArgument( + "cannot resolve the sc+https pin file: set HOME, XDG_CONFIG_HOME, \ + or SC_HTTPS_KNOWN_HOSTS" + .to_string(), + )), + } +} + +/// Parse `sha256:<64 hex>` (or bare hex) into a pin. +pub fn parse_fingerprint(s: &str) -> Result<[u8; 32]> { + let hex_part = s.strip_prefix("sha256:").unwrap_or(s); + let bytes = hex::decode(hex_part) + .map_err(|_| Error::InvalidArgument(format!("bad fingerprint (want sha256:): {s}")))?; + let arr: [u8; 32] = bytes + .try_into() + .map_err(|_| Error::InvalidArgument(format!("fingerprint must be 32 bytes: {s}")))?; + Ok(arr) +} + +/// Find the pin for `host:port`, if any. A missing file means no pins; a +/// malformed or comment line is skipped, never an error (the file is +/// user-editable — the documented mismatch recovery is deleting a line). +pub fn lookup(file: &Path, host: &str, port: u16) -> Result> { + let text = match std::fs::read_to_string(file) { + Ok(t) => t, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(e) => { + return Err(Error::InvalidArgument(format!( + "read pin file {}: {e}", + file.display() + ))) + } + }; + let key = format!("{host}:{port}"); + for line in text.lines() { + let line = line.trim(); + if line.is_empty() || line.starts_with('#') { + continue; + } + if let Some((k, v)) = line.split_once(' ') { + if k == key { + if let Ok(pin) = parse_fingerprint(v.trim()) { + return Ok(Some(pin)); + } + } + } + } + Ok(None) +} + +/// Append a pin line (creating parent dirs and the file as needed). +pub fn record(file: &Path, host: &str, port: u16, pin: &[u8; 32]) -> Result<()> { + if let Some(parent) = file.parent() { + std::fs::create_dir_all(parent) + .map_err(|e| Error::InvalidArgument(format!("create {}: {e}", parent.display())))?; + } + use std::io::Write; + let mut f = std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(file) + .map_err(|e| Error::InvalidArgument(format!("open pin file {}: {e}", file.display())))?; + writeln!(f, "{host}:{port} {}", scl_tlsio::fingerprint_hex(pin)) + .map_err(|e| Error::InvalidArgument(format!("write pin file {}: {e}", file.display())))?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn tmp(tag: &str) -> std::path::PathBuf { + let d = std::env::temp_dir().join(format!("scl-pins-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + d + } + + #[test] + fn record_then_lookup_round_trips() { + let dir = tmp("rt"); + let file = dir.join("known_hosts"); + let pin = [0x11u8; 32]; + assert_eq!(lookup(&file, "example.com", 8730).unwrap(), None); + record(&file, "example.com", 8730, &pin).unwrap(); + assert_eq!(lookup(&file, "example.com", 8730).unwrap(), Some(pin)); + // Same host, different port = a different pin slot. + assert_eq!(lookup(&file, "example.com", 9000).unwrap(), None); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(!dir.exists()); + } + + #[test] + fn pin_file_line_format_is_host_port_sha256_hex() { + let dir = tmp("fmt"); + let file = dir.join("known_hosts"); + record(&file, "h", 8730, &[0xaa; 32]).unwrap(); + let text = std::fs::read_to_string(&file).unwrap(); + assert_eq!(text, format!("h:8730 sha256:{}\n", "aa".repeat(32))); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(!dir.exists()); + } + + #[test] + fn lookup_ignores_malformed_lines_and_comments() { + let dir = tmp("junk"); + let file = dir.join("known_hosts"); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(&file, "# comment\n\nnot a pin line\nh:1 sha256:zz\n").unwrap(); + assert_eq!(lookup(&file, "h", 1).unwrap(), None); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(!dir.exists()); + } + + #[test] + fn parse_fingerprint_accepts_both_forms_and_rejects_junk() { + let hexs = "ab".repeat(32); + let expected = [0xabu8; 32]; + assert_eq!(parse_fingerprint(&format!("sha256:{hexs}")).unwrap(), expected); + assert_eq!(parse_fingerprint(&hexs).unwrap(), expected); + assert!(parse_fingerprint("sha256:short").is_err()); + assert!(parse_fingerprint("md5:abcd").is_err()); + assert!(parse_fingerprint("").is_err()); + } +} From a953abdbfe0201bdcbbacf37940c5b5bb09fef45 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 10 Jul 2026 15:11:48 +0300 Subject: [PATCH 06/14] feat(repo): TLS-wrapped sc serve --http (TlsMode, serve-tls identity, fingerprint helper) --- crates/cli/src/main.rs | 9 +- crates/repo/src/http_transport.rs | 244 +++++++++++++++++++++++++++--- crates/repo/src/layout.rs | 6 + 3 files changed, 234 insertions(+), 25 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 159f97f6..41856043 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -3188,7 +3188,14 @@ fn run_serve( if let Some(m) = max_pack_size { limits.max_pack_size = m; } - scl_repo::http_transport::serve_http(&addr, &path, read_only, allow_public, limits)?; + scl_repo::http_transport::serve_http( + &addr, + &path, + read_only, + allow_public, + limits, + scl_repo::http_transport::TlsMode::Off, + )?; Ok(()) } (true, Some(_)) => anyhow::bail!("sc serve accepts only one of --stdio or --http"), diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index 781779e2..f9ee3829 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -515,6 +515,46 @@ fn auth_is_mandatory(addr: &str, read_only: bool, allow_public: bool) -> bool { !is_loopback_host(host) && !read_only && !allow_public } +/// How `sc serve --http` provides its TLS identity (P32). `Off` keeps the +/// P26 plaintext listener; `AutoMint` loads-or-mints `.sc/serve-tls/`; +/// `Pem` loads an operator-supplied pair (certbot etc.). +#[derive(Debug, Clone)] +pub enum TlsMode { + Off, + AutoMint, + Pem { + cert: std::path::PathBuf, + key: std::path::PathBuf, + }, +} + +/// Resolve a [`TlsMode`] into a ready-to-use server config, or `None` for +/// [`TlsMode::Off`]. Factored out of [`serve_http`] so the config can be +/// built BEFORE the bind gate (Task 6 needs `tls_config.is_some()` to relax +/// the non-loopback refusal for a TLS-protected listener). +fn resolve_tls( + root: &std::path::Path, + tls: &TlsMode, +) -> Result> { + let identity = match tls { + TlsMode::Off => return Ok(None), + TlsMode::AutoMint => { + scl_tlsio::load_or_mint(&crate::layout::Layout::at(root).serve_tls_dir())? + } + TlsMode::Pem { cert, key } => scl_tlsio::load_pem(cert, key)?, + }; + Ok(Some(scl_tlsio::server_config(identity)?)) +} + +/// 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 { + let id = scl_tlsio::load_or_mint(&crate::layout::Layout::at(root).serve_tls_dir())?; + Ok(scl_tlsio::fingerprint_hex(&id.spki_sha256)) +} + /// Bind `addr`, serve the single repo at `root` to `sc+http://` clients /// until the listener is dropped or the process exits. Thin wrapper around /// [`serve_http_listener`] — see that function for the accept-loop @@ -530,8 +570,10 @@ pub fn serve_http( read_only: bool, allow_public: bool, limits: ServeLimits, + tls: TlsMode, ) -> Result<()> { crate::wire::validate_max_pack_size(limits.max_pack_size)?; + let tls_config = resolve_tls(root, &tls)?; if !bind_is_allowed(addr, root, read_only, allow_public)? { return Err(Error::InvalidArgument(format!( "refusing to bind non-loopback address {addr} without --read-only, \ @@ -552,8 +594,14 @@ pub fn serve_http( .local_addr() .map_err(|e| Error::ConnectionLost(format!("sc+http local_addr: {e}")))?; println!("listening on {bound}"); + if let Some(cfg) = &tls_config { + println!( + "tls fingerprint: {}", + scl_tlsio::fingerprint_hex(&cfg.spki_sha256) + ); + } std::io::Write::flush(&mut std::io::stdout()).ok(); - serve_http_listener(listener, root, read_only, mandatory_auth, limits) + serve_http_listener(listener, root, read_only, mandatory_auth, limits, tls_config) } /// Accept-loop core, factored out of [`serve_http`] so tests can bind @@ -581,6 +629,7 @@ pub fn serve_http_listener( read_only: bool, mandatory_auth: bool, limits: ServeLimits, + tls: Option, ) -> Result<()> { let live = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0)); let mut backoff = AcceptBackoff::new(); @@ -601,16 +650,30 @@ pub fn serve_http_listener( let prev = live.fetch_add(1, std::sync::atomic::Ordering::SeqCst); let g = SlotGuard(live.clone()); if limits.max_connections != 0 && prev >= limits.max_connections as usize { - let _ = write_status(&mut stream, 503); + if tls.is_none() { + let _ = write_status(&mut stream, 503); + } + // Under TLS: close without a status. Sending a readable 503 + // would require a TLS handshake ON THE ACCEPT THREAD, letting + // one slow client stall all accepts — the property P31 + // exists to protect. (git-daemon sheds the same way: plain + // close.) continue; // g drops here → count restored; no thread, no handshake } g }; let root = root.to_path_buf(); + let tls = tls.clone(); let spawn_result = std::thread::Builder::new().spawn(move || { let _guard = guard; // slot held for the connection's lifetime - if let Err(e) = handle_http_connection(stream, &root, read_only, mandatory_auth, limits) - { + if let Err(e) = handle_http_connection( + stream, + &root, + read_only, + mandatory_auth, + limits, + tls.as_ref(), + ) { eprintln!("sc serve --http: connection error: {e}"); } }); @@ -644,28 +707,86 @@ pub fn serve_http_listener( /// but a session that goes silent (or, on the write side, stalls reading a /// streamed response) must still be reaped rather than parking the thread /// forever. +/// Server-side maybe-TLS halves: the opening codec, status writes, and +/// `wire::serve` are all generic over Read/Write, so one enum layer at the +/// seam keeps plaintext and TLS on the identical code path after wrap. +enum SrvRead { + Plain(TcpStream), + Tls(scl_tlsio::TlsServerReadHalf), +} +enum SrvWrite { + Plain(TcpStream), + Tls(scl_tlsio::TlsServerWriteHalf), +} +impl Read for SrvRead { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + match self { + SrvRead::Plain(s) => s.read(buf), + SrvRead::Tls(r) => r.read(buf), + } + } +} +impl Write for SrvWrite { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + match self { + SrvWrite::Plain(s) => s.write(buf), + SrvWrite::Tls(w) => w.write(buf), + } + } + fn flush(&mut self) -> std::io::Result<()> { + match self { + SrvWrite::Plain(s) => s.flush(), + SrvWrite::Tls(w) => w.flush(), + } + } +} +impl SrvRead { + /// P31 session timeouts, below TLS when wrapped. SO_RCVTIMEO/SO_SNDTIMEO + /// live on the socket (shared across `try_clone` fds), so setting them + /// through either half governs the whole connection — the same fact the + /// plaintext path has relied on since P31. + fn set_session_timeouts(&self, d: Option) -> std::io::Result<()> { + match self { + SrvRead::Plain(s) => { + s.set_read_timeout(d)?; + s.set_write_timeout(d) + } + SrvRead::Tls(r) => r.set_socket_timeouts(d, d), + } + } +} + fn handle_http_connection( - mut stream: TcpStream, + stream: TcpStream, root: &std::path::Path, server_read_only: bool, mandatory_auth: bool, limits: ServeLimits, + tls: Option<&scl_tlsio::TlsServerConfig>, ) -> Result<()> { stream .set_read_timeout(Some(OPENING_READ_TIMEOUT)) .map_err(|e| Error::ConnectionLost(format!("sc+http set_read_timeout: {e}")))?; - let mut reader = BufReader::new( - stream - .try_clone() - .map_err(|e| Error::ConnectionLost(format!("sc+http socket clone: {e}")))?, - ); + let (mut reader, mut writer) = match tls { + None => { + let r = stream + .try_clone() + .map_err(|e| Error::ConnectionLost(format!("sc+http socket clone: {e}")))?; + (BufReader::new(SrvRead::Plain(r)), SrvWrite::Plain(stream)) + } + Some(cfg) => { + let t = scl_tlsio::server_stream(cfg, stream)?; + let (r, w) = t.split(); + (BufReader::new(SrvRead::Tls(r)), SrvWrite::Tls(w)) + } + }; let opening = match read_client_opening(&mut reader) { Ok(o) => o, Err(_) => { // Malformed/slow-loris opening: best-effort 400, then close. - let _ = write_status(&mut stream, 400); + let _ = write_status(&mut writer, 400); return Ok(()); } }; @@ -673,7 +794,7 @@ fn handle_http_connection( let _ = &opening.target; if !root.join(".sc").is_dir() { - write_status(&mut stream, 404)?; + write_status(&mut writer, 404)?; return Ok(()); } @@ -694,7 +815,7 @@ fn handle_http_connection( justified only by tokens, but none are configured (re-add a token, or \ restart with --read-only / --allow-public)" ); - write_status(&mut stream, 401)?; + write_status(&mut writer, 401)?; return Ok(()); } false // loopback, or --read-only / --allow-public public bind: proceed unauthenticated @@ -707,13 +828,13 @@ fn handle_http_connection( Some(crate::serve_tokens::Scope::Ro) => true, Some(crate::serve_tokens::Scope::Rw) => false, None => { - write_status(&mut stream, 401)?; + write_status(&mut writer, 401)?; return Ok(()); } } }; - write_status(&mut stream, 200)?; + write_status(&mut writer, 200)?; // P31: the opening timeout is REPLACED (not cleared) by the session // timeout, on BOTH read and write sides — a write timeout covers a client @@ -724,18 +845,16 @@ fn handle_http_connection( } else { Some(Duration::from_secs(limits.timeout_secs)) }; - stream - .set_read_timeout(session) - .map_err(|e| Error::ConnectionLost(format!("sc+http set session read_timeout: {e}")))?; - stream - .set_write_timeout(session) - .map_err(|e| Error::ConnectionLost(format!("sc+http set session write_timeout: {e}")))?; + reader + .get_ref() + .set_session_timeouts(session) + .map_err(|e| Error::ConnectionLost(format!("sc+http set session timeouts: {e}")))?; let read_only = server_read_only || token_read_only; crate::wire::serve_with_policy( root, &mut reader, - &mut stream, + &mut writer, crate::wire::WirePolicy { read_only, max_pack_size: limits.max_pack_size, @@ -1047,6 +1166,7 @@ mod tests { read_only, mandatory_auth, ServeLimits::default(), + None, ) .unwrap(); }); @@ -1207,7 +1327,15 @@ mod tests { // Non-loopback, no --read-only / --allow-public / tokens → refused, // and refused *before* any bind is attempted (port 0 would always // succeed to bind if we got that far). - let err = serve_http("0.0.0.0:0", &root, false, false, ServeLimits::default()).unwrap_err(); + let err = serve_http( + "0.0.0.0:0", + &root, + false, + false, + ServeLimits::default(), + TlsMode::Off, + ) + .unwrap_err(); assert!( matches!(err, Error::InvalidArgument(_)), "public bind refused: {err:?}" @@ -1464,7 +1592,7 @@ mod tests { let addr = listener.local_addr().unwrap(); let root = root.to_path_buf(); std::thread::spawn(move || { - let _ = serve_http_listener(listener, &root, false, false, limits); + let _ = serve_http_listener(listener, &root, false, false, limits, None); }); addr } @@ -1654,4 +1782,72 @@ mod tests { assert!(!tmp.exists() || std::fs::read_dir(&tmp).unwrap().next().is_none()); let _ = std::fs::remove_dir_all(&root); } + + // ── Task 4: TLS-wrapped server seam. ── + + /// Bind a TLS listener over a fresh repo; returns (addr, spki, join guard). + fn spawn_tls_server(root: &std::path::Path) -> (String, [u8; 32]) { + let id = scl_tlsio::load_or_mint(&root.join(".sc").join("serve-tls")).unwrap(); + let spki = id.spki_sha256; + let cfg = scl_tlsio::server_config(id).unwrap(); + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap().to_string(); + let root = root.to_path_buf(); + std::thread::spawn(move || { + let _ = serve_http_listener( + listener, + &root, + false, + false, + ServeLimits::default(), + Some(cfg), + ); + }); + (addr, spki) + } + + #[test] + fn tls_server_answers_opening_over_tls_and_rejects_plaintext() { + let root = tmp_repo("tls-opening"); // reuse the file's existing repo-fixture helper + let (addr, spki) = spawn_tls_server(&root); + + // A TLS client (raw tlsio, pinned) gets a 200 through the TLS channel. + let tcp = std::net::TcpStream::connect(&addr).unwrap(); + let (stream, seen) = scl_tlsio::client_connect(tcp, "127.0.0.1", Some(spki), false).unwrap(); + assert_eq!(seen, spki); + let (r, mut w) = stream.split(); + write_client_opening(&mut w, "127.0.0.1", "/", None).unwrap(); + use std::io::Write as _; + w.flush().unwrap(); + let mut br = std::io::BufReader::new(r); + assert_eq!(read_status(&mut br).unwrap(), 200); + + // A PLAINTEXT client against the TLS listener fails cleanly (the + // server-side handshake errors; the client sees close/garbage, not 200). + let mut plain = std::net::TcpStream::connect(&addr).unwrap(); + plain + .set_read_timeout(Some(std::time::Duration::from_secs(5))) + .unwrap(); + write_client_opening(&mut plain, "127.0.0.1", "/", None).unwrap(); + let mut resp = Vec::new(); + use std::io::Read as _; + let _ = plain.read_to_end(&mut resp); + assert!( + !String::from_utf8_lossy(&resp).starts_with("HTTP/1.1 200"), + "plaintext client must not reach the opening handler on a TLS listener" + ); + + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn serve_tls_fingerprint_mints_and_is_stable() { + let root = tmp_repo("tls-fpr"); + let f1 = serve_tls_fingerprint(&root).unwrap(); + assert!(f1.starts_with("sha256:"), "got: {f1}"); + assert!(root.join(".sc").join("serve-tls").join("key.pem").exists()); + let f2 = serve_tls_fingerprint(&root).unwrap(); + assert_eq!(f1, f2, "fingerprint must load, not re-mint"); + std::fs::remove_dir_all(&root).unwrap(); + } } diff --git a/crates/repo/src/layout.rs b/crates/repo/src/layout.rs index d886eb7c..25acf17d 100644 --- a/crates/repo/src/layout.rs +++ b/crates/repo/src/layout.rs @@ -91,6 +91,12 @@ impl Layout { pub fn tmp_dir(&self) -> PathBuf { self.dot_sc.join("tmp") } + /// `.sc/serve-tls/` — the server's TLS identity (`cert.pem` + `key.pem`), + /// auto-minted on first `sc serve --http … --tls` (P32). The key IS the + /// identity: it is regenerated only when missing. + pub fn serve_tls_dir(&self) -> PathBuf { + self.dot_sc.join("serve-tls") + } /// `.sc/promisor` — the partial-clone marker (P27): line 1 `origin /// `, then one fetch-filter prefix per line. Absent means a full /// clone (every object was fetched, nothing is a promised gap). From 5ff4af795c4249120e31f14e3b276709e42ebf3c Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 10 Jul 2026 15:21:32 +0300 Subject: [PATCH 07/14] =?UTF-8?q?feat(repo):=20sc+https://=20client=20?= =?UTF-8?q?=E2=80=94=20TOFU=20pin=20flow=20at=20the=20connect=20seam,=20op?= =?UTF-8?q?en=5Ftransport=20routing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/repo/src/http_transport.rs | 369 ++++++++++++++++++++++++++--- crates/repo/src/stdio_transport.rs | 7 +- 2 files changed, 338 insertions(+), 38 deletions(-) diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index f9ee3829..051db23f 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -181,26 +181,38 @@ pub(crate) fn read_status(r: &mut impl Read) -> Result { Ok(code) } -/// A parsed sc-native HTTP URL: `sc+http://host[:port]/repo/path`. +/// A parsed sc-native HTTP URL: `sc+http://host[:port]/repo/path` or +/// `sc+https://host[:port]/repo/path` (P32). /// /// Port defaults to [`DEFAULT_PORT`] when omitted. `path` is everything /// after the authority, leading `/` kept; an empty remainder becomes `/`. +/// `tls` distinguishes the two schemes — it IS the TLS client decision (see +/// [`HttpTransport::connect_with_pins`]). #[derive(Debug, Clone, PartialEq, Eq)] pub struct ScHttpUrl { pub host: String, pub port: u16, pub path: String, + pub tls: bool, } impl ScHttpUrl { - /// Parse an `sc+http://` URL; anything malformed is `InvalidArgument` - /// with a message naming the URL (mirrors `SshUrl::parse`'s style — - /// URL parsing fails before any connection exists, so `Protocol`'s - /// wire-protocol-error semantics don't apply here). + /// Parse an `sc+http://` or `sc+https://` URL; anything malformed is + /// `InvalidArgument` with a message naming the URL (mirrors + /// `SshUrl::parse`'s style — URL parsing fails before any connection + /// exists, so `Protocol`'s wire-protocol-error semantics don't apply + /// here). Plain `https://`/`http://` are GIT urls (P18), never + /// sc-native, and are rejected here. pub fn parse(url: &str) -> Result { - let rest = url - .strip_prefix("sc+http://") - .ok_or_else(|| Error::InvalidArgument(format!("not an sc+http:// url: {url}")))?; + let (rest, tls) = if let Some(r) = url.strip_prefix("sc+http://") { + (r, false) + } else if let Some(r) = url.strip_prefix("sc+https://") { + (r, true) + } else { + return Err(Error::InvalidArgument(format!( + "not an sc+http(s):// url: {url}" + ))); + }; let slash = rest.find('/').unwrap_or(rest.len()); let (authority, path) = rest.split_at(slash); let (host, port) = match authority.split_once(':') { @@ -238,6 +250,7 @@ impl ScHttpUrl { host: host.to_string(), port, path, + tls, }) } @@ -247,14 +260,66 @@ impl ScHttpUrl { } } +/// Client-side maybe-TLS halves: the opening codec, status reads, and +/// `WireClient` are all generic over Read/Write, so one enum layer at the +/// seam keeps plaintext and TLS on the identical code path after connect — +/// the same shape as the server's `SrvRead`/`SrvWrite` above. +enum HttpReadHalf { + Plain(TcpStream), + Tls(scl_tlsio::TlsClientReadHalf), +} +enum HttpWriteHalf { + Plain(TcpStream), + Tls(scl_tlsio::TlsClientWriteHalf), +} +impl std::fmt::Debug for HttpReadHalf { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + HttpReadHalf::Plain(_) => f.write_str("HttpReadHalf::Plain"), + HttpReadHalf::Tls(_) => f.write_str("HttpReadHalf::Tls"), + } + } +} +impl std::fmt::Debug for HttpWriteHalf { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + HttpWriteHalf::Plain(_) => f.write_str("HttpWriteHalf::Plain"), + HttpWriteHalf::Tls(_) => f.write_str("HttpWriteHalf::Tls"), + } + } +} +impl Read for HttpReadHalf { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + match self { + HttpReadHalf::Plain(s) => s.read(buf), + HttpReadHalf::Tls(r) => r.read(buf), + } + } +} +impl Write for HttpWriteHalf { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + match self { + HttpWriteHalf::Plain(s) => s.write(buf), + HttpWriteHalf::Tls(w) => w.write(buf), + } + } + fn flush(&mut self) -> std::io::Result<()> { + match self { + HttpWriteHalf::Plain(s) => s.flush(), + HttpWriteHalf::Tls(w) => w.flush(), + } + } +} + /// A [`Transport`] that speaks the wire protocol over a `TcpStream` -/// established via the HTTP opening: connect, write the client opening line, -/// read+map the status BEFORE any wire-protocol byte crosses the socket, -/// then hand the (split) stream to the same [`WireClient`] `StdioTransport` -/// uses over a child process's stdio. +/// established via the HTTP opening: connect (through a TLS handshake first +/// when `url.tls`), write the client opening line, read+map the status +/// BEFORE any wire-protocol byte crosses the socket, then hand the (split) +/// stream to the same [`WireClient`] `StdioTransport` uses over a child +/// process's stdio. #[derive(Debug)] pub struct HttpTransport { - client: WireClient, TcpStream>, + client: WireClient, HttpWriteHalf>, } impl HttpTransport { @@ -275,8 +340,32 @@ impl HttpTransport { /// reads the token from `SC_HTTP_TOKEN`; this split keeps the env read /// out of the socket logic and testable without mutating process env /// (`std::env::set_var` is process-global and racy under parallel - /// tests). + /// tests). For a `sc+https://` url, the TLS pin policy is resolved from + /// env (`SC_HTTPS_KNOWN_HOSTS`/`SC_HTTPS_FINGERPRINT`/`SC_HTTPS_STRICT`) + /// and delegated to [`Self::connect_with_pins`], the testable, + /// env-free core. pub fn connect_with_token(url: &ScHttpUrl, token: Option<&str>) -> Result { + let policy = if url.tls { + Some(crate::tls_pins::TlsClientPolicy::from_env()?) + } else { + None + }; + Self::connect_with_pins(url, token, policy.as_ref()) + } + + /// The testable core: env-free. `policy` must be `Some` iff `url.tls` — + /// the pin policy IS the TLS client decision. + /// + /// CRITICAL ordering property: for a TLS connect, no application byte — + /// the opening line, the bearer token — may be written before the pin + /// disposition is settled. `scl_tlsio::client_connect` completes the + /// full handshake (and pin check) before returning, so the opening write + /// below always happens strictly after that decision, never before. + pub fn connect_with_pins( + url: &ScHttpUrl, + token: Option<&str>, + policy: Option<&crate::tls_pins::TlsClientPolicy>, + ) -> Result { if let Some(t) = token { if t.contains(['\r', '\n']) { return Err(Error::InvalidArgument( @@ -285,28 +374,90 @@ impl HttpTransport { } } - let mut stream = TcpStream::connect(url.authority()) + let stream = TcpStream::connect(url.authority()) .map_err(|e| Error::ConnectionLost(format!("sc+http connect to {url:?}: {e}")))?; - write_client_opening(&mut stream, &url.host, &url.path, token)?; - - // Split the stream into independent read/write halves up front — - // `try_clone` duplicates the socket handle (both `r` and `w` refer - // to the same TCP connection, matching StdioTransport's separate - // ChildStdout/ChildStdin), mirroring the read-half construction the - // brief specifies. The status line is read through this SAME - // `BufReader` that goes on to become the WireClient's `r`, not a - // throwaway clone: `BufReader::read` can pull more than one byte - // from the kernel into its internal buffer per call, so a separate, - // later-dropped reader could silently swallow the first - // wire-protocol frame byte(s) if the server ever raced ahead of the - // status line. Reusing one buffer means anything it over-reads stays - // available for the handshake that follows. - let mut r = BufReader::new( - stream - .try_clone() - .map_err(|e| Error::ConnectionLost(format!("sc+http socket clone: {e}")))?, - ); - let w = stream; + + let (mut r, mut w) = match (url.tls, policy) { + (false, _) => { + // Split the stream into independent read/write halves up + // front — `try_clone` duplicates the socket handle (both `r` + // and `w` refer to the same TCP connection, matching + // StdioTransport's separate ChildStdout/ChildStdin). The + // status line is read through this SAME `BufReader` that + // goes on to become the WireClient's `r`, not a throwaway + // clone: `BufReader::read` can pull more than one byte from + // the kernel into its internal buffer per call, so a + // separate, later-dropped reader could silently swallow the + // first wire-protocol frame byte(s) if the server ever raced + // ahead of the status line. Reusing one buffer means + // anything it over-reads stays available for the handshake + // that follows. + let rd = stream + .try_clone() + .map_err(|e| Error::ConnectionLost(format!("sc+http socket clone: {e}")))?; + ( + BufReader::new(HttpReadHalf::Plain(rd)), + HttpWriteHalf::Plain(stream), + ) + } + (true, Some(p)) => { + let expected = match p.pre_pin { + Some(pin) => Some(pin), + None => crate::tls_pins::lookup(&p.known_hosts, &url.host, url.port)?, + }; + let known_before = expected.is_some(); + let pin_source = if p.pre_pin.is_some() { + "SC_HTTPS_FINGERPRINT".to_string() + } else { + p.known_hosts.display().to_string() + }; + match scl_tlsio::client_connect(stream, &url.host, expected, p.strict) { + Ok((tls_stream, seen)) => { + if !known_before { + // Accept-new TOFU: pin silently, ANNOUNCE loudly. + crate::tls_pins::record(&p.known_hosts, &url.host, url.port, &seen)?; + eprintln!( + "sc+https: first connection to {}:{} — pinned {}", + url.host, + url.port, + scl_tlsio::fingerprint_hex(&seen) + ); + eprintln!( + "sc+https: verify against `sc serve fingerprint` on the \ + server; stored in {}", + p.known_hosts.display() + ); + } + let (rh, wh) = tls_stream.split(); + ( + BufReader::new(HttpReadHalf::Tls(rh)), + HttpWriteHalf::Tls(wh), + ) + } + Err(scl_tlsio::Error::PinMismatch { expected, seen }) => { + return Err(Error::TlsPinMismatch { + host: url.authority(), + file: pin_source, + pinned: scl_tlsio::fingerprint_hex(&expected), + seen: scl_tlsio::fingerprint_hex(&seen), + }) + } + Err(scl_tlsio::Error::UnknownHostStrict) => { + return Err(Error::TlsStrictUnknownHost(url.authority())) + } + Err(e) => return Err(e.into()), + } + } + (true, None) => { + return Err(Error::InvalidArgument( + "internal: sc+https connect without a pin policy".to_string(), + )) + } + }; + + write_client_opening(&mut w, &url.host, &url.path, token)?; + w.flush() + .map_err(|e| Error::ConnectionLost(format!("sc+http opening flush: {e}")))?; // Status mapping happens BEFORE the wire-protocol handshake: 200 // proceeds, 401 means the server rejected the bearer (or required @@ -1536,6 +1687,7 @@ mod tests { host: "127.0.0.1".to_string(), port, path: "/repo".to_string(), + tls: false, }; // A valid rw token is accepted and the wire protocol proceeds. @@ -1850,4 +2002,151 @@ mod tests { assert_eq!(f1, f2, "fingerprint must load, not re-mint"); std::fs::remove_dir_all(&root).unwrap(); } + + // ── Task 5: client seam — sc+https:// URLs, TOFU connect flow. ── + + #[test] + fn parse_sc_https_sets_tls() { + let u = ScHttpUrl::parse("sc+https://example.com:9443/srv/repo").unwrap(); + assert!(u.tls); + assert_eq!( + (u.host.as_str(), u.port, u.path.as_str()), + ("example.com", 9443, "/srv/repo") + ); + let u = ScHttpUrl::parse("sc+https://host/repo").unwrap(); + assert_eq!(u.port, DEFAULT_PORT); + let u = ScHttpUrl::parse("sc+http://host/repo").unwrap(); + assert!(!u.tls); + // Plain https:// is a GIT url (P18), never sc-native. + assert!(ScHttpUrl::parse("https://host/repo").is_err()); + } + + /// The full client TOFU lifecycle against a real TLS listener: + /// first connect pins, second is quiet, a swapped key hard-fails, + /// pre-pin never persists, strict refuses unknown. + #[test] + fn tofu_lifecycle_end_to_end() { + let root = tmp_repo("tofu-e2e"); + let (addr, spki) = spawn_tls_server(&root); + let (host, port) = addr + .rsplit_once(':') + .map(|(h, p)| (h.to_string(), p.parse::().unwrap())) + .unwrap(); + let url = ScHttpUrl { + host: host.clone(), + port, + path: "/".into(), + tls: true, + }; + + let pins_dir = std::env::temp_dir().join(format!("scl-tofu-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&pins_dir); + let kh = pins_dir.join("known_hosts"); + let policy = crate::tls_pins::TlsClientPolicy { + known_hosts: kh.clone(), + pre_pin: None, + strict: false, + }; + + // 1. Unknown host, accept-new: connects AND records the pin. + HttpTransport::connect_with_pins(&url, None, Some(&policy)).unwrap(); + assert_eq!( + crate::tls_pins::lookup(&kh, &host, port).unwrap(), + Some(spki) + ); + + // 2. Second connect: pin already known, still connects. + HttpTransport::connect_with_pins(&url, None, Some(&policy)).unwrap(); + + // 3. Strict mode with a pin present: fine. Strict against an UNKNOWN + // host (fresh pin file): refused with the typed error. + let strict_known = crate::tls_pins::TlsClientPolicy { + known_hosts: kh.clone(), + pre_pin: None, + strict: true, + }; + HttpTransport::connect_with_pins(&url, None, Some(&strict_known)).unwrap(); + let fresh = pins_dir.join("fresh_kh"); + let strict_unknown = crate::tls_pins::TlsClientPolicy { + known_hosts: fresh.clone(), + pre_pin: None, + strict: true, + }; + match HttpTransport::connect_with_pins(&url, None, Some(&strict_unknown)) { + Err(Error::TlsStrictUnknownHost(_)) => {} + other => panic!("expected TlsStrictUnknownHost, got {other:?}"), + } + assert!(!fresh.exists(), "strict refusal must not write a pin"); + + // 4. Pre-pin (SC_HTTPS_FINGERPRINT semantics): connects, never persists. + let prepin_file = pins_dir.join("prepin_kh"); + let prepin = crate::tls_pins::TlsClientPolicy { + known_hosts: prepin_file.clone(), + pre_pin: Some(spki), + strict: true, + }; + HttpTransport::connect_with_pins(&url, None, Some(&prepin)).unwrap(); + assert!(!prepin_file.exists(), "pre-pin must not persist"); + + // 5. A WRONG stored pin hard-fails with recovery context. + let bad = pins_dir.join("bad_kh"); + crate::tls_pins::record(&bad, &host, port, &[0x24u8; 32]).unwrap(); + let badpol = crate::tls_pins::TlsClientPolicy { + known_hosts: bad.clone(), + pre_pin: None, + strict: false, + }; + match HttpTransport::connect_with_pins(&url, None, Some(&badpol)) { + Err(Error::TlsPinMismatch { + host: h, + file, + pinned, + seen, + }) => { + assert!(h.contains(&host)); + assert!(file.contains("bad_kh")); + assert!(pinned.starts_with("sha256:") && seen.starts_with("sha256:")); + assert_eq!(seen, scl_tlsio::fingerprint_hex(&spki)); + } + other => panic!("expected TlsPinMismatch, got {other:?}"), + } + + std::fs::remove_dir_all(&pins_dir).unwrap(); + std::fs::remove_dir_all(&root).unwrap(); + } + + /// Transport verbs work over TLS end to end (list_refs against the real + /// server — the full clone/push/fetch acceptance rides the CLI test in + /// Task 7). + #[test] + fn transport_verbs_over_tls() { + let root = tmp_repo("tls-verbs"); + let (addr, spki) = spawn_tls_server(&root); + let (host, port) = addr + .rsplit_once(':') + .map(|(h, p)| (h.to_string(), p.parse::().unwrap())) + .unwrap(); + let url = ScHttpUrl { + host, + port, + path: "/".into(), + tls: true, + }; + let policy = crate::tls_pins::TlsClientPolicy { + known_hosts: std::env::temp_dir() + .join(format!("scl-verbs-kh-{}", std::process::id())), + pre_pin: Some(spki), + strict: false, + }; + let t = HttpTransport::connect_with_pins(&url, None, Some(&policy)).unwrap(); + // A fresh `tmp_repo` has no commits, so `list_refs()` is legitimately + // empty (no branch file has been written yet) — assert on + // `head_branch()` instead, which mirrors what the plaintext + // `no_tokens_configured_no_auth_required` test above exercises right + // after connect (a successful call, unborn branch and all). + t.list_refs().unwrap(); + assert_eq!(t.head_branch().unwrap(), "main"); + let _ = std::fs::remove_file(&policy.known_hosts); + std::fs::remove_dir_all(&root).unwrap(); + } } diff --git a/crates/repo/src/stdio_transport.rs b/crates/repo/src/stdio_transport.rs index a705e4f9..1a457888 100644 --- a/crates/repo/src/stdio_transport.rs +++ b/crates/repo/src/stdio_transport.rs @@ -360,13 +360,14 @@ pub(crate) fn ssh_command(url: &SshUrl) -> Command { } /// Open the right [`Transport`] for a remote URL: `ssh://` spawns the wire -/// client over a child process's stdio, `sc+http://` dials it directly over -/// TCP; anything else is a local `.sc/` path. +/// client over a child process's stdio, `sc+http://`/`sc+https://` dial it +/// directly over TCP (TLS-wrapped for the latter, P32); anything else is a +/// local `.sc/` path. pub fn open_transport(url: &str) -> Result> { if url.starts_with("ssh://") { let parsed = SshUrl::parse(url)?; Ok(Box::new(StdioTransport::spawn(ssh_command(&parsed))?)) - } else if url.starts_with("sc+http://") { + } else if url.starts_with("sc+http://") || url.starts_with("sc+https://") { let parsed = crate::http_transport::ScHttpUrl::parse(url)?; Ok(Box::new(crate::http_transport::HttpTransport::connect( &parsed, From 7471e5466d0e6fb8fdd7a8ad369c67a25ee3e4e9 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 10 Jul 2026 15:30:04 +0300 Subject: [PATCH 08/14] feat(repo)!: plaintext non-loopback binds now require --allow-public; tokens justify only with --tls (P32 gate) bind_is_allowed gains a `tls: bool` parameter and tightens the lattice: non-loopback is allowed iff --read-only, --allow-public, or (tls && >=1 configured serve token). A configured token no longer justifies a PLAINTEXT public bind (the P29 rule) since the token itself would then cross the wire in the clear -- deliberate, narrow pre-1.0 break (decision 5 of issue #39 / ADR-0042). auth_is_mandatory is unchanged; its doc comment now notes the TLS+tokens case rides the same fail-closed rule. serve_http's refusal message names --tls as the third justification. --- crates/repo/src/http_transport.rs | 92 +++++++++++++++++++++++++------ 1 file changed, 74 insertions(+), 18 deletions(-) diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index 051db23f..1ac37887 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -628,15 +628,20 @@ fn is_loopback_host(host: &str) -> bool { } } -/// The fail-closed bind decision (P29): a non-loopback `addr` is allowed -/// only if justified by `--read-only`, `--allow-public`, or ≥1 configured -/// serve token; loopback always binds. Factored out of [`serve_http`] so -/// tests can exercise the decision without binding a public port. +/// The fail-closed bind decision (P29, tightened by P32/ADR-0042): a +/// non-loopback `addr` is allowed only if justified by `--read-only`, +/// `--allow-public`, or — once TLS is available — `--tls` plus ≥1 configured +/// serve token. A PLAINTEXT public bind justified only by tokens (the P29 +/// rule) is refused: the token would cross the wire in the clear, which is +/// the exact exposure this phase closes. Deliberate, narrow pre-1.0 break. +/// Factored out of [`serve_http`] so tests can exercise the decision without +/// binding a public port. fn bind_is_allowed( addr: &str, root: &std::path::Path, read_only: bool, allow_public: bool, + tls: bool, ) -> Result { let host = addr.rsplit_once(':').map(|(h, _)| h).unwrap_or(addr); // Strip optional [..] brackets around an IPv6 literal. @@ -647,6 +652,9 @@ fn bind_is_allowed( if read_only || allow_public { return Ok(true); } + if !tls { + return Ok(false); + } let tokens_configured = !crate::serve_tokens::load(&crate::layout::Layout::at(root))?.is_empty(); Ok(tokens_configured) @@ -660,6 +668,9 @@ fn bind_is_allowed( /// the handler must then fail closed (reject) rather than serve open. Loopback, /// `--read-only` (public read-mirror), and `--allow-public` (deliberately open) /// all keep their standing posture when tokens vanish, so this is false for them. +/// (P32: the TLS+tokens public posture rides this same rule unchanged — it is +/// still non-loopback && !ro && !allow_public, so tokens are still the sole +/// justification and auth stays mandatory, fail-closed 401 if they vanish.) fn auth_is_mandatory(addr: &str, read_only: bool, allow_public: bool) -> bool { let host = addr.rsplit_once(':').map(|(h, _)| h).unwrap_or(addr); let host = host.trim_start_matches('[').trim_end_matches(']'); @@ -725,11 +736,12 @@ pub fn serve_http( ) -> Result<()> { crate::wire::validate_max_pack_size(limits.max_pack_size)?; let tls_config = resolve_tls(root, &tls)?; - if !bind_is_allowed(addr, root, read_only, allow_public)? { + if !bind_is_allowed(addr, root, read_only, allow_public, tls_config.is_some())? { return Err(Error::InvalidArgument(format!( - "refusing to bind non-loopback address {addr} without --read-only, \ - --allow-public, or a configured serve token (sc serve token add); \ - use 127.0.0.1 for local-only serving" + "refusing to bind non-loopback address {addr}: use --tls with a configured serve \ + token (sc serve token add), or --read-only, or --allow-public; a plaintext public \ + bind would send bearer tokens and repo traffic in the clear (use 127.0.0.1 for \ + local-only serving)" ))); } let mandatory_auth = auth_is_mandatory(addr, read_only, allow_public); @@ -1493,26 +1505,70 @@ mod tests { ); // Justified by --read-only. - assert!(bind_is_allowed("0.0.0.0:0", &root, true, false).unwrap()); + assert!(bind_is_allowed("0.0.0.0:0", &root, true, false, false).unwrap()); // Justified by --allow-public. - assert!(bind_is_allowed("0.0.0.0:0", &root, false, true).unwrap()); + assert!(bind_is_allowed("0.0.0.0:0", &root, false, true, false).unwrap()); // Still refused with neither justification and no tokens yet. - assert!(!bind_is_allowed("0.0.0.0:0", &root, false, false).unwrap()); + assert!(!bind_is_allowed("0.0.0.0:0", &root, false, false, false).unwrap()); + + // A configured token WITHOUT TLS no longer justifies a public bind + // (P32/ADR-0042, decision 5 — the P29 rule broke here on purpose: + // a plaintext token crosses the wire in the clear). + add_test_token(&root); + assert!(!bind_is_allowed("0.0.0.0:0", &root, false, false, false).unwrap()); + // With TLS, the same configured token justifies the bind. + assert!(bind_is_allowed("0.0.0.0:0", &root, false, false, true).unwrap()); + + // Loopback always allowed regardless of flags/tokens/tls. + assert!(bind_is_allowed("127.0.0.1:0", &root, false, false, false).unwrap()); + assert!(bind_is_allowed("[::1]:0", &root, false, false, false).unwrap()); + + let _ = std::fs::remove_dir_all(&root); + } - // Justified by a configured token. + /// Configure one `rw` serve token on `root`'s `.sc/serve-tokens.toml`, + /// via the same mechanism the P29 gate tests use — shared here so the + /// P32 gate-lattice test doesn't duplicate the inline setup. + fn add_test_token(root: &std::path::Path) { crate::serve_tokens::add( - &crate::layout::Layout::at(&root), + &crate::layout::Layout::at(root), "t", crate::serve_tokens::Scope::Rw, ) .unwrap(); - assert!(bind_is_allowed("0.0.0.0:0", &root, false, false).unwrap()); + } - // Loopback always allowed regardless of flags/tokens. - assert!(bind_is_allowed("127.0.0.1:0", &root, false, false).unwrap()); - assert!(bind_is_allowed("[::1]:0", &root, false, false).unwrap()); + /// The full P32 bind-decision lattice: loopback always; non-loopback + /// needs `--read-only`/`--allow-public` on their own, or TLS plus a + /// configured token; a configured token WITHOUT TLS no longer justifies + /// a public bind (the P32 break from the P29 rule, ADR-0042 decision 5). + #[test] + fn gate_lattice_p32() { + let root = tmp_repo("gate32"); + let no_tls = false; + let tls = true; - let _ = std::fs::remove_dir_all(&root); + // Loopback: always, TLS or not, tokens or not. + assert!(bind_is_allowed("127.0.0.1:1", &root, false, false, no_tls).unwrap()); + assert!(bind_is_allowed("127.0.0.1:1", &root, false, false, tls).unwrap()); + + // Non-loopback, nothing configured: refused either way. + assert!(!bind_is_allowed("0.0.0.0:1", &root, false, false, no_tls).unwrap()); + assert!(!bind_is_allowed("0.0.0.0:1", &root, false, false, tls).unwrap()); + + // --read-only / --allow-public: justify on their own, unchanged. + assert!(bind_is_allowed("0.0.0.0:1", &root, true, false, no_tls).unwrap()); + assert!(bind_is_allowed("0.0.0.0:1", &root, false, true, no_tls).unwrap()); + + // Configure a token… + add_test_token(&root); + + // …tokens + TLS: the blessed public posture. + assert!(bind_is_allowed("0.0.0.0:1", &root, false, false, tls).unwrap()); + // …tokens WITHOUT TLS: no longer justifies (the P32 break, decision 5). + assert!(!bind_is_allowed("0.0.0.0:1", &root, false, false, no_tls).unwrap()); + + std::fs::remove_dir_all(&root).unwrap(); } /// Read the client opening + status line only, without proceeding to the From 8ccfd74975ceaa463508697c230781711410d9bf Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 10 Jul 2026 15:40:09 +0300 Subject: [PATCH 09/14] feat(cli): sc serve --tls / --tls-cert / --tls-key + sc serve fingerprint; sc+https acceptance test --- crates/cli/src/main.rs | 50 ++++- crates/cli/tests/https_remote.rs | 328 +++++++++++++++++++++++++++++++ 2 files changed, 376 insertions(+), 2 deletions(-) create mode 100644 crates/cli/tests/https_remote.rs diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 41856043..084f568e 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -313,8 +313,19 @@ enum Cmd { /// when non-zero. (P31) #[arg(long)] max_pack_size: Option, + /// Serve TLS (`sc+https://`): auto-mints a self-signed identity into + /// `.sc/serve-tls/` unless --tls-cert/--tls-key supply a PEM pair. + /// `--http` only. (P32) + #[arg(long)] + tls: bool, + /// PEM certificate chain for --tls (requires --tls-key). + #[arg(long)] + tls_cert: Option, + /// PEM private key for --tls (requires --tls-cert). + #[arg(long)] + tls_key: Option, /// Repo root to serve (the directory containing `.sc/`). Required - /// unless a `token` subcommand is used. + /// unless a `token`/`fingerprint` subcommand is used. path: Option, }, /// Manage remotes. @@ -642,6 +653,10 @@ enum ServeSub { #[command(subcommand)] op: ServeTokenOp, }, + /// Print this repo's serve-TLS fingerprint (sha256: of the SPKI), + /// minting `.sc/serve-tls/` if absent — distribute this to clients as + /// SC_HTTPS_FINGERPRINT or to verify a first-connect pin. (P32) + Fingerprint { path: Option }, } #[derive(Subcommand)] @@ -834,9 +849,20 @@ fn main() -> Result<()> { max_connections, timeout, max_pack_size, + tls, + tls_cert, + tls_key, path, } => match sub { Some(ServeSub::Token { op }) => run_serve_token(op), + Some(ServeSub::Fingerprint { path }) => { + let root = match path { + Some(p) => p, + None => std::env::current_dir()?, + }; + println!("{}", scl_repo::http_transport::serve_tls_fingerprint(&root)?); + Ok(()) + } None => { let path = path.ok_or_else(|| anyhow::anyhow!("sc serve requires a "))?; run_serve( @@ -847,6 +873,9 @@ fn main() -> Result<()> { max_connections, timeout, max_pack_size, + tls, + tls_cert, + tls_key, path, ) } @@ -3155,6 +3184,9 @@ fn run_serve( max_connections: Option, timeout: Option, max_pack_size: Option, + tls: bool, + tls_cert: Option, + tls_key: Option, path: PathBuf, ) -> Result<()> { match (stdio, http) { @@ -3165,6 +3197,11 @@ fn run_serve( if max_connections.is_some() || timeout.is_some() { anyhow::bail!("--max-connections/--timeout apply only to --http, not --stdio"); } + if tls || tls_cert.is_some() || tls_key.is_some() { + anyhow::bail!( + "--tls applies only to --http (ssh already provides --stdio's confidential channel)" + ); + } let max_pack = max_pack_size.unwrap_or(scl_repo::wire::DEFAULT_MAX_PACK_SIZE); scl_repo::wire::validate_max_pack_size(max_pack)?; let policy = scl_repo::wire::WirePolicy { @@ -3188,13 +3225,22 @@ fn run_serve( if let Some(m) = max_pack_size { limits.max_pack_size = m; } + let tls_mode = match (tls, tls_cert, tls_key) { + (false, None, None) => scl_repo::http_transport::TlsMode::Off, + (false, _, _) => anyhow::bail!("--tls-cert/--tls-key require --tls"), + (true, None, None) => scl_repo::http_transport::TlsMode::AutoMint, + (true, Some(cert), Some(key)) => { + scl_repo::http_transport::TlsMode::Pem { cert, key } + } + (true, _, _) => anyhow::bail!("--tls-cert and --tls-key must be given together"), + }; scl_repo::http_transport::serve_http( &addr, &path, read_only, allow_public, limits, - scl_repo::http_transport::TlsMode::Off, + tls_mode, )?; Ok(()) } diff --git a/crates/cli/tests/https_remote.rs b/crates/cli/tests/https_remote.rs new file mode 100644 index 00000000..6702939c --- /dev/null +++ b/crates/cli/tests/https_remote.rs @@ -0,0 +1,328 @@ +//! CLI acceptance for sc+https:// (P32): flag validation, `sc serve +//! fingerprint`, and the spec's round-trip criterion — clone + push + fetch +//! over TLS with a signed ~1 MiB blob under forced SC_PACK_CHUNK, +//! byte-for-byte, zero .sc/tmp residue. Env knobs (SC_HTTPS_*) are safe here +//! because every command is a SUBPROCESS with its own env (Command::env), +//! not a racy in-process set_var. + +use std::io::{BufRead, Write}; +use std::path::{Path, PathBuf}; +use std::process::{Child, Command, Output, Stdio}; + +fn sc(dir: &Path, args: &[&str]) -> Output { + Command::new(env!("CARGO_BIN_EXE_sc")) + .args(args) + .current_dir(dir) + .output() + .expect("sc runs") +} + +/// `sc()` variant that sets extra env vars on the subprocess — safe because +/// each `Command` gets its own environment (unlike `std::env::set_var`, +/// which is process-global and races under parallel tests). +fn sc_env(dir: &Path, args: &[&str], envs: &[(&str, &str)]) -> Output { + let mut c = Command::new(env!("CARGO_BIN_EXE_sc")); + c.args(args).current_dir(dir); + for (k, v) in envs { + c.env(k, v); + } + c.output().expect("sc runs") +} + +fn tmp(tag: &str) -> PathBuf { + let d = std::env::temp_dir().join(format!("scl-cli-https-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d +} + +/// Spawn `sc serve --http 127.0.0.1:0 --tls ` and return the +/// child, the OS-assigned `host:port` (line 1: `listening on `), and +/// the TLS fingerprint the server banners on startup (line 2: `tls +/// fingerprint: sha256:`). Mirrors `http_remote.rs`'s +/// `spawn_http_server`, extended to also read the second announce line. +fn spawn_tls_http_server(root: &Path, extra: &[&str]) -> (Child, String, String) { + let mut args = vec!["serve", "--http", "127.0.0.1:0", "--tls"]; + args.extend_from_slice(extra); + args.push(root.to_str().unwrap()); + let mut child = Command::new(env!("CARGO_BIN_EXE_sc")) + .args(&args) + .stdout(Stdio::piped()) + .spawn() + .expect("spawn sc serve --http --tls"); + let stdout = child.stdout.take().expect("child stdout is piped"); + let mut reader = std::io::BufReader::new(stdout); + + let mut line1 = String::new(); + let n1 = reader + .read_line(&mut line1) + .expect("read serve startup line 1"); + if n1 == 0 { + let status = child.wait().ok(); + panic!("sc serve --http --tls exited before announcing a bound address: {status:?}"); + } + let addr = line1 + .trim() + .strip_prefix("listening on ") + .unwrap_or_else(|| panic!("unexpected serve startup line 1: {line1:?}")) + .to_string(); + + let mut line2 = String::new(); + let n2 = reader + .read_line(&mut line2) + .expect("read serve startup line 2"); + if n2 == 0 { + let status = child.wait().ok(); + panic!("sc serve --http --tls exited before announcing a fingerprint: {status:?}"); + } + let fpr = line2 + .trim() + .strip_prefix("tls fingerprint: ") + .unwrap_or_else(|| panic!("unexpected serve startup line 2: {line2:?}")) + .to_string(); + + (child, addr, fpr) +} + +/// keygen a v2 identity OUTSIDE the working tree (the P5 scanner flags +/// `scl-id-` files found inside one), mirroring `provenance.rs`'s `keygen` +/// helper, returning the identity file path. +fn keygen(keys_dir: &Path, name: &str) -> PathBuf { + let idfile = keys_dir.join(format!("{name}.id")); + let out = sc(keys_dir, &["keygen", "--out", idfile.to_str().unwrap()]); + assert!( + out.status.success(), + "keygen: {}", + String::from_utf8_lossy(&out.stderr) + ); + idfile +} + +/// Deterministic ~1 MiB of hex-text "random" bytes (splitmix64-seeded), +/// mirroring `demo/run_http_remote_demo.sh`'s `head -c 1048576 /dev/urandom +/// | od -An -tx1` — hex text keeps per-character entropy under the P5 +/// scanner's threshold (16 symbols, ≤4 bits/char) while raw high-entropy +/// binary of the same size trips it as a false-positive secret. +fn hex_blob(byte_len: usize) -> Vec { + let mut state: u64 = 0x9E3779B97F4A7C15; + let mut next = || { + state = state.wrapping_add(0x9E3779B97F4A7C15); + let mut z = state; + z = (z ^ (z >> 30)).wrapping_mul(0xBF58476D1CE4E5B9); + z = (z ^ (z >> 27)).wrapping_mul(0x94D049BB133111EB); + z ^ (z >> 31) + }; + let mut out = Vec::with_capacity(byte_len); + while out.len() < byte_len { + let v = next(); + out.extend_from_slice(format!("{v:016x}").as_bytes()); + } + out.truncate(byte_len); + out +} + +/// Write a known_hosts file pinning `addr` (`host:port`) to `fpr` +/// (`sha256:`), in the same `host:port sha256:` line format +/// `tls_pins::record` writes — used to simulate "this address used to be +/// the old server" for the key-swap mismatch scenario. +fn mismatch_kh(dir: &Path, addr: &str, fpr: &str) -> PathBuf { + let kh = dir.join("mismatch_known_hosts"); + let mut f = std::fs::File::create(&kh).unwrap(); + writeln!(f, "{addr} {fpr}").unwrap(); + kh +} + +#[test] +fn tls_flags_validated() { + let root = tmp("flags"); + assert!(sc(&root, &["init"]).status.success()); + // --tls-cert without --tls + let out = sc( + &root, + &[ + "serve", + "--http", + "127.0.0.1:0", + "--tls-cert", + "x.pem", + root.to_str().unwrap(), + ], + ); + assert!(!out.status.success()); + assert!(String::from_utf8_lossy(&out.stderr).contains("--tls")); + // --tls with --stdio + let out = sc( + &root, + &["serve", "--stdio", "--tls", root.to_str().unwrap()], + ); + assert!(!out.status.success()); + // --tls-cert without --tls-key + let out = sc( + &root, + &[ + "serve", + "--http", + "127.0.0.1:0", + "--tls", + "--tls-cert", + "x.pem", + root.to_str().unwrap(), + ], + ); + assert!(!out.status.success()); +} + +#[test] +fn serve_fingerprint_mints_and_matches_banner() { + let root = tmp("fpr"); + assert!(sc(&root, &["init"]).status.success()); + let out = sc(&root, &["serve", "fingerprint", root.to_str().unwrap()]); + assert!(out.status.success()); + let fpr = String::from_utf8_lossy(&out.stdout).trim().to_string(); + assert!(fpr.starts_with("sha256:")); + // The identity persisted; a TLS serve now banners the SAME fingerprint. + let (mut child, _addr, banner_fpr) = spawn_tls_http_server(&root, &[]); + assert_eq!(banner_fpr, fpr); + child.kill().ok(); +} + +#[test] +fn https_clone_push_fetch_round_trip_with_signed_chunked_blob() { + let w = tmp("rt"); + let origin = w.join("origin"); + std::fs::create_dir_all(&origin).unwrap(); + assert!(sc(&origin, &["init"]).status.success()); + + // Identity OUTSIDE the working tree (P5 scanner flags scl-id files). + let identity = keygen(&w, "alice"); + + // ~1 MiB deterministic blob + a signed commit. Hex text, not raw random + // bytes: see `hex_blob`'s doc comment for why (P5 scanner false positive). + let blob = hex_blob(1_048_576); + std::fs::write(origin.join("big.bin"), &blob).unwrap(); + assert!(sc( + &origin, + &[ + "commit", + "-m", + "big", + "--sign", + "--identity", + identity.to_str().unwrap(), + ], + ) + .status + .success()); + + // rw token; capture the raw value from stdout (auth demo pattern). + let tok_out = sc( + &origin, + &["serve", "token", "add", "--label", "ci", "--scope", "rw"], + ); + assert!(tok_out.status.success()); + let token = String::from_utf8_lossy(&tok_out.stdout).trim().to_string(); + + let (mut child, addr, fpr) = spawn_tls_http_server(&origin, &[]); + let url = format!("sc+https://{addr}/"); + let kh = w.join("known_hosts"); + + // Clone with forced tiny chunks; env is per-subprocess, race-free. + let clone_dir = w.join("clone"); + let out = sc_env( + &w, + &["clone", &url, clone_dir.to_str().unwrap()], + &[ + ("SC_HTTP_TOKEN", token.as_str()), + ("SC_HTTPS_KNOWN_HOSTS", kh.to_str().unwrap()), + ("SC_PACK_CHUNK", "4096"), + ], + ); + assert!( + out.status.success(), + "clone failed: {}", + String::from_utf8_lossy(&out.stderr) + ); + // First-connect TOFU announced + pinned. + assert!(String::from_utf8_lossy(&out.stderr).contains("pinned")); + assert!(kh.exists()); + // Byte-for-byte. + assert_eq!(std::fs::read(clone_dir.join("big.bin")).unwrap(), blob); + // Signature rode the chunked TLS stream. + let log = sc(&clone_dir, &["log"]); + assert!(String::from_utf8_lossy(&log.stdout).contains("signed:")); + + // Push an edit back over TLS (second connect: pin known → NO "pinned"). + std::fs::write(clone_dir.join("new.txt"), "from clone").unwrap(); + assert!(sc(&clone_dir, &["commit", "-m", "edit"]).status.success()); + let out = sc_env( + &clone_dir, + &["push", "origin"], + &[ + ("SC_HTTP_TOKEN", token.as_str()), + ("SC_HTTPS_KNOWN_HOSTS", kh.to_str().unwrap()), + ], + ); + assert!( + out.status.success(), + "push failed: {}", + String::from_utf8_lossy(&out.stderr) + ); + assert!( + !String::from_utf8_lossy(&out.stderr).contains("pinned"), + "second connect must be quiet" + ); + + // Fetch from a second clone sees the edit. + let clone2 = w.join("clone2"); + assert!(sc_env( + &w, + &["clone", &url, clone2.to_str().unwrap()], + &[ + ("SC_HTTP_TOKEN", token.as_str()), + ("SC_HTTPS_KNOWN_HOSTS", kh.to_str().unwrap()), + ], + ) + .status + .success()); + assert_eq!( + std::fs::read_to_string(clone2.join("new.txt")).unwrap(), + "from clone" + ); + + // Zero .sc/tmp residue on every end. + for repo in [&origin, &clone_dir, &clone2] { + let tmp_dir = repo.join(".sc").join("tmp"); + let empty = + !tmp_dir.exists() || std::fs::read_dir(&tmp_dir).unwrap().next().is_none(); + assert!(empty, ".sc/tmp residue in {}", repo.display()); + } + + // Key swap → pin mismatch hard-fails. + child.kill().ok(); + child.wait().ok(); + std::fs::remove_dir_all(origin.join(".sc").join("serve-tls")).unwrap(); + let (mut child2, addr2, _f) = spawn_tls_http_server(&origin, &[]); + let url2 = format!("sc+https://{addr2}/"); + let clone3 = w.join("clone3"); + // Re-pin the OLD server's fingerprint under the NEW address first, so + // the lookup hits: write a kh containing addr2 mapped to the OLD + // fingerprint captured earlier. + let out = sc_env( + &w, + &["clone", &url2, clone3.to_str().unwrap()], + &[ + ("SC_HTTP_TOKEN", token.as_str()), + ( + "SC_HTTPS_KNOWN_HOSTS", + mismatch_kh(&w, &addr2, &fpr).to_str().unwrap(), + ), + ], + ); + assert!(!out.status.success()); + let err = String::from_utf8_lossy(&out.stderr); + assert!( + err.contains("does not match the pinned fingerprint"), + "got: {err}" + ); + child2.kill().ok(); +} From 36d2383fd75c35d4964113a954267b770eb7cabe Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 10 Jul 2026 15:49:00 +0300 Subject: [PATCH 10/14] demo: sc+https TLS round trip, TOFU lifecycle, tightened plaintext gate (run twice) Claude-Session: https://claude.ai/code/session_01LhadyW9scQL95h3ag9yySB --- demo/run_tls_demo.sh | 257 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 257 insertions(+) create mode 100755 demo/run_tls_demo.sh diff --git a/demo/run_tls_demo.sh b/demo/run_tls_demo.sh new file mode 100755 index 00000000..6e3de6ec --- /dev/null +++ b/demo/run_tls_demo.sh @@ -0,0 +1,257 @@ +#!/usr/bin/env bash +# P32 sc+https demo: TLS-wrapped sc-native HTTP transport. `sc serve --http +# --tls ` auto-mints a self-signed identity into +# `.sc/serve-tls/` (key.pem mode 0600) and banners its SHA-256 SPKI +# fingerprint on startup; `sc serve fingerprint ` prints the same +# fingerprint up front, before first serve, so an operator can distribute it +# out of band. `sc clone/push/fetch` on `sc+https://host:port/` URLs verify +# the server's key against a local TOFU pin store (`SC_HTTPS_KNOWN_HOSTS`, +# defaulting to a user-level file): first connect pins and announces loudly, +# later connects are quiet, a swapped server key hard-fails, `SC_HTTPS_STRICT` +# refuses an unknown host outright, and `SC_HTTPS_FINGERPRINT` pre-pins +# without ever persisting. Finally, P32 tightens the P29 non-loopback bind +# gate: a plaintext public bind justified only by configured tokens (the old +# P29 rule) is now refused — a bearer token must not cross the wire in the +# clear — while `--tls` plus a configured token still justifies it. +# +# Self-checking: every claim is an assertion; any failure exits non-zero +# before the RESULT line. +set -euo pipefail + +cd "$(dirname "$0")/.." +cargo build --quiet --bin sc +SC="$PWD/target/debug/sc" + +fail() { echo "FAIL: $1"; exit 1; } + +W="" +SERVER_PID="" +SERVER2_PID="" + +cleanup() { + for pid in "$SERVER_PID" "$SERVER2_PID"; do + if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then + kill "$pid" 2>/dev/null || true + wait "$pid" 2>/dev/null || true + fi + done + if [ -n "$W" ]; then rm -rf "$W"; fi + return 0 +} +trap cleanup EXIT + +# --- Pick a free port, excluding any already handed out this run. --- +USED_PORTS="" +pick_port() { + local candidate + for candidate in 8761 8762 8763 8764 8765 18761 18762 18763; do + case " $USED_PORTS " in *" $candidate "*) continue ;; esac + if ! nc -z 127.0.0.1 "$candidate" 2>/dev/null; then + USED_PORTS="$USED_PORTS $candidate" + echo "$candidate" + return 0 + fi + done + fail "no free port found among the candidates" +} + +wait_for_port() { + local port="$1" tries=0 + while ! nc -z 127.0.0.1 "$port" 2>/dev/null; do + tries=$((tries + 1)) + [ "$tries" -lt 100 ] || fail "server on port $port never became ready" + sleep 0.05 + done +} + +# --- Wait for a background server's stdout file to contain a line (the +# port opening the socket and the process flushing its startup banner +# are not perfectly synchronized). --- +wait_for_line() { + local file="$1" pattern="$2" tries=0 + while ! grep -q "$pattern" "$file" 2>/dev/null; do + tries=$((tries + 1)) + [ "$tries" -lt 100 ] || fail "'$pattern' never appeared in $file: $(cat "$file" 2>/dev/null)" + sleep 0.05 + done +} + +# --- File mode, portable across BSD (macOS) and GNU stat. --- +file_mode() { + stat -f "%Lp" "$1" 2>/dev/null || stat -c "%a" "$1" +} + +check_no_tmp_residue() { + local dir="$1" label="$2" + [ -d "$dir/.sc/tmp" ] && [ -n "$(ls -A "$dir/.sc/tmp" 2>/dev/null)" ] \ + && fail "$label: .sc/tmp is not empty" + return 0 +} + +run_demo() { + local run_label="$1" + W="$(mktemp -d)" + SERVER_PID="" + SERVER2_PID="" + local ORIGIN="$W/origin" + + echo "=== [$run_label] 1: init origin, keygen (outside the tree), sign a ~1 MiB blob, mint a token ===" + mkdir -p "$ORIGIN"; cd "$ORIGIN" + "$SC" init >/dev/null + "$SC" keygen --out "$W/alice.id" >/dev/null + # Deterministic ~1 MiB blob. Repeating text, not high-entropy binary — the + # P5 commit scanner flags entropy-dense content as a possible secret. + head -c 1048576 /dev/zero | tr '\0' 'x' > big.bin + "$SC" commit -m "big" --sign --identity "$W/alice.id" >/dev/null + # Raw token prints once on stdout; the confirmation goes to stderr, so + # command substitution captures exactly the raw value. + TOKEN="$("$SC" serve token add --label demo --scope rw 2>/dev/null)" + [ -n "$TOKEN" ] || fail "token add produced no stdout value" + echo "origin: signed commit with a ~1 MiB blob; rw token '${TOKEN:0:8}…' ✔" + + echo + echo "=== [$run_label] 2: sc serve fingerprint mints the identity before any serve ===" + FPR="$("$SC" serve fingerprint "$ORIGIN")" + case "$FPR" in + sha256:*) ;; + *) fail "fingerprint must start with sha256:, got: $FPR" ;; + esac + [ -f "$ORIGIN/.sc/serve-tls/key.pem" ] || fail "sc serve fingerprint must mint .sc/serve-tls/key.pem" + KEY_MODE="$(file_mode "$ORIGIN/.sc/serve-tls/key.pem")" + [ "$KEY_MODE" = "600" ] || fail "key.pem must be mode 600, got: $KEY_MODE" + echo "fingerprint: $FPR, key.pem minted at mode 600 ✔" + + echo + echo "=== [$run_label] 3: sc serve --http --tls (background); TLS clone pins on first connect ===" + cd "$W" + PORT="$(pick_port)" + "$SC" serve --http "127.0.0.1:$PORT" --tls "$ORIGIN" \ + >"$W/server1.out" 2>"$W/server1.err" & + SERVER_PID=$! + wait_for_port "$PORT" + wait_for_line "$W/server1.out" "^listening on 127.0.0.1:$PORT\$" + wait_for_line "$W/server1.out" "tls fingerprint: $FPR" + echo "server: listening on 127.0.0.1:$PORT (pid $SERVER_PID), banner matches $FPR ✔" + + URL="sc+https://127.0.0.1:$PORT/" + KH="$W/known_hosts" + + SC_HTTP_TOKEN="$TOKEN" SC_HTTPS_KNOWN_HOSTS="$KH" SC_PACK_CHUNK=4096 \ + "$SC" clone "$URL" "$W/clone" 2>"$W/clone1.err" + grep -q "pinned" "$W/clone1.err" || fail "first connect must announce a pin: $(cat "$W/clone1.err")" + grep -qF "$FPR" "$W/clone1.err" || fail "pin announcement must include the fingerprint: $(cat "$W/clone1.err")" + [ -f "$KH" ] || fail "TOFU pin must have been recorded to SC_HTTPS_KNOWN_HOSTS" + cmp -s "$ORIGIN/big.bin" "$W/clone/big.bin" || fail "cloned big.bin is not byte-identical to origin" + (cd "$W/clone" && "$SC" log) | grep -q "signed:" \ + || fail "clone's sc log must show the signed: marker" + echo "TLS clone: first-connect pin announced with the fingerprint, blob byte-identical, signature marker present ✔" + + echo + echo "=== [$run_label] 4: second connect is quiet; push + fetch round-trip over TLS ===" + cd "$W/clone" + printf 'from clone over tls\n' > new.txt + "$SC" commit -m "edit" >/dev/null + SC_HTTP_TOKEN="$TOKEN" SC_HTTPS_KNOWN_HOSTS="$KH" "$SC" push origin 2>"$W/push.err" + grep -q "pinned" "$W/push.err" && fail "second connect must be quiet, got: $(cat "$W/push.err")" + echo "push: landed, second connect made no TOFU announcement ✔" + + cd "$W" + SC_HTTP_TOKEN="$TOKEN" SC_HTTPS_KNOWN_HOSTS="$KH" "$SC" clone "$URL" "$W/clone2" >/dev/null + [ "$(cat "$W/clone2/new.txt")" = "from clone over tls" ] \ + || fail "clone2 did not see the pushed edit" + echo "clone2: sees the pushed edit ✔" + + echo + echo "=== [$run_label] 5: SC_HTTPS_STRICT refuses an unknown host; SC_HTTPS_FINGERPRINT pre-pins without persisting ===" + KH_FRESH="$W/known_hosts_fresh" + if SC_HTTPS_STRICT=1 SC_HTTP_TOKEN="$TOKEN" SC_HTTPS_KNOWN_HOSTS="$KH_FRESH" \ + "$SC" clone "$URL" "$W/clone_strict_fail" 2>"$W/strict.err"; then + fail "strict mode against an unpinned host should have refused the clone" + fi + grep -q "SC_HTTPS_STRICT" "$W/strict.err" \ + || fail "strict refusal must mention SC_HTTPS_STRICT: $(cat "$W/strict.err")" + [ ! -e "$KH_FRESH" ] || fail "a strict refusal must not write a pin file" + echo "SC_HTTPS_STRICT=1, unpinned host: refused, no pin file written ✔" + + SC_HTTPS_STRICT=1 SC_HTTPS_FINGERPRINT="$FPR" SC_HTTP_TOKEN="$TOKEN" SC_HTTPS_KNOWN_HOSTS="$KH_FRESH" \ + "$SC" clone "$URL" "$W/clone_prepin" >/dev/null + [ ! -e "$KH_FRESH" ] || fail "SC_HTTPS_FINGERPRINT pre-pin must never persist to the known_hosts file" + echo "SC_HTTPS_FINGERPRINT pre-pin: strict clone succeeded, still no pin file written ✔" + + echo + echo "=== [$run_label] 6: server key swap -> pin mismatch hard-fails ===" + kill "$SERVER_PID" + wait "$SERVER_PID" 2>/dev/null || true + SERVER_PID="" + ! nc -z 127.0.0.1 "$PORT" 2>/dev/null || fail "server still accepting connections after kill" + rm -rf "$ORIGIN/.sc/serve-tls" + + PORT2="$(pick_port)" + "$SC" serve --http "127.0.0.1:$PORT2" --tls "$ORIGIN" \ + >"$W/server2.out" 2>"$W/server2.err" & + SERVER_PID=$! + wait_for_port "$PORT2" + wait_for_line "$W/server2.out" "^listening on 127.0.0.1:$PORT2\$" + wait_for_line "$W/server2.out" "tls fingerprint: sha256:" + FPR2="$(grep "tls fingerprint:" "$W/server2.out" | awk '{print $3}')" + [ "$FPR2" != "$FPR" ] || fail "restarted server minted the same fingerprint — key swap didn't happen" + echo "server restarted on a fresh port with a NEW identity: $FPR2 (differs from $FPR) ✔" + + # Pin the OLD fingerprint against the NEW server's address, simulating a + # client that connected to this host:port before the key changed. + KH_MISMATCH="$W/known_hosts_mismatch" + printf '127.0.0.1:%s %s\n' "$PORT2" "$FPR" > "$KH_MISMATCH" + URL2="sc+https://127.0.0.1:$PORT2/" + if SC_HTTP_TOKEN="$TOKEN" SC_HTTPS_KNOWN_HOSTS="$KH_MISMATCH" \ + "$SC" clone "$URL2" "$W/clone_mismatch" 2>"$W/mismatch.err"; then + fail "a pin mismatch must hard-fail the clone" + fi + grep -q "does not match the pinned fingerprint" "$W/mismatch.err" \ + || fail "mismatch error must say 'does not match the pinned fingerprint': $(cat "$W/mismatch.err")" + grep -qF "$KH_MISMATCH" "$W/mismatch.err" \ + || fail "mismatch error must name the pin file: $(cat "$W/mismatch.err")" + echo "pin mismatch: clone refused, error names the mismatch and the pin file ✔" + + kill "$SERVER_PID" + wait "$SERVER_PID" 2>/dev/null || true + SERVER_PID="" + + echo + echo "=== [$run_label] 7: tightened plaintext public-bind gate — tokens alone no longer justify it ===" + PORT3="$(pick_port)" + if "$SC" serve --http "0.0.0.0:$PORT3" "$ORIGIN" 2>"$W/gate.err"; then + fail "a plaintext non-loopback bind justified only by tokens should now be refused" + fi + grep -q -- "--tls" "$W/gate.err" \ + || fail "the refusal must name --tls as the fix: $(cat "$W/gate.err")" + echo "0.0.0.0 bind, tokens configured, no --tls: refused, error names --tls ✔" + + "$SC" serve --http "0.0.0.0:$PORT3" --tls "$ORIGIN" \ + >"$W/gate_ok.out" 2>"$W/gate_ok.err" & + SERVER2_PID=$! + wait_for_port "$PORT3" + wait_for_line "$W/gate_ok.out" "^listening on " + echo "0.0.0.0 bind, tokens configured, --tls: accepted ✔" + kill "$SERVER2_PID" + wait "$SERVER2_PID" 2>/dev/null || true + SERVER2_PID="" + ! nc -z 127.0.0.1 "$PORT3" 2>/dev/null || fail "gate server still accepting connections after kill" + + echo + echo "=== [$run_label] 8: zero .sc/tmp residue anywhere ===" + check_no_tmp_residue "$ORIGIN" "origin" + check_no_tmp_residue "$W/clone" "clone" + check_no_tmp_residue "$W/clone2" "clone2" + check_no_tmp_residue "$W/clone_prepin" "clone_prepin" + echo "origin + every clone: zero .sc/tmp residue ✔" + + cd "$W" + rm -rf "$W" + W="" + + echo + echo "RESULT: ok" +} + +run_demo "run 1" +run_demo "run 2 (repeatable, not a one-shot fluke)" From 68a4f27bb2e7e4bf5c18a7d383054d2fb66ec42e Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 10 Jul 2026 16:05:18 +0300 Subject: [PATCH 11/14] docs: ADR-0042 (in-binary TLS), CLAUDE.md P32, THREAT-MODEL transport rewrite, roadmap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Documents the P32 in-binary TLS phase (sc+https://, crates/tlsio, accept-new TOFU pinning, the tightened non-loopback bind gate) closing ADR-0036's "no TLS" boundary and the audit's High #1: ADR-0042, CLAUDE.md's workspace layout/dependency rule/commands/Phase 32 section/forward-reference updates, a rewritten THREAT-MODEL transport section covering both confidential transports and both reverse-proxy legs, ROADMAP's shipped-in-P32 entry and follow-ons, and the P32 spec's busy-shed amendment. Extends demo/run_http_auth_demo.sh with a step proving the P29→P32 gate narrowing (tokens alone no longer justify a plaintext public bind; --tls + a token does). --- CLAUDE.md | 116 +++++++++-- ROADMAP.md | 52 ++++- demo/run_http_auth_demo.sh | 56 ++++- docs/THREAT-MODEL.md | 89 ++++++-- docs/adr/0042-in-binary-tls-sc-https.md | 194 ++++++++++++++++++ docs/adr/README.md | 1 + .../2026-07-10-p32-tls-sc-https-design.md | 14 +- 7 files changed, 479 insertions(+), 43 deletions(-) create mode 100644 docs/adr/0042-in-binary-tls-sc-https.md diff --git a/CLAUDE.md b/CLAUDE.md index 57009658..7ad295c2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -37,17 +37,21 @@ crates/core → content-addressed store, object model, budget + eviction crates/vfs → in-memory worktree engine (depends on core) crates/gitio → Git import via gix (depends on core; ONLY crate that links gix) crates/crypto → envelope encryption (depends on core; ONLY crate linking RustCrypto) -crates/repo → persistent .sc/ repo: objects, refs, branches, working tree (depends on core/vfs/crypto) +crates/tlsio → TLS for sc+https (depends on nothing; ONLY crate linking rustls/rcgen) +crates/repo → persistent .sc/ repo: objects, refs, branches, working tree (depends on core/vfs/crypto/tlsio) crates/cli → `sc` binary (depends on repo + vfs + gitio + crypto + core) ``` -Strict dependency direction: `cli → repo → {vfs, gitio, crypto} → core`. +Strict dependency direction: `cli → repo → {vfs, gitio, crypto, tlsio} → core` +(`tlsio` is a leaf — it depends on no other workspace crate, not even `core`). **`core` must never depend on Git, worktrees, or crypto.** **`gix` must stay quarantined in `gitio`** — if you find yourself reaching for `gix` elsewhere, add a function to `gitio` instead. **RustCrypto must stay quarantined in `crypto`** — if you find yourself reaching for it elsewhere, add a function to -`crypto` instead. **`repo` must not depend on `gitio`** — `cli` links both and -passes imported snapshots down; `repo` stays Git-agnostic. +`crypto` instead. **rustls/rcgen must stay quarantined in `tlsio`** — if you +find yourself reaching for TLS elsewhere, add a function to `tlsio` instead. +**`repo` must not depend on `gitio`** — `cli` links both and passes imported +snapshots down; `repo` stays Git-agnostic. ## Core invariants (do not break) @@ -346,6 +350,26 @@ bash demo/run_http_auth_demo.sh # sc+http access-control proof (P2 # ro-token clone sees it, an unjustified 0.0.0.0 # bind is refused while --allow-public opens it # deliberately, zero .sc/tmp residue anywhere +cargo run --bin sc -- serve --http --tls [--tls-cert --tls-key ] + # TLS listener (P32): sc+https://; auto-mints a + # self-signed identity into .sc/serve-tls/ (key + # 0600, key-is-identity) unless PEM given; banner + # prints the SPKI fingerprint; NB gate change: + # tokens justify a non-loopback bind ONLY with + # --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:) +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 + # overrides), mismatch always hard-fails; + # SC_HTTPS_FINGERPRINT= pre-pins (CI), + # SC_HTTPS_STRICT=1 refuses unknown hosts; + # remote add/fetch/push accept the same URL form +bash demo/run_tls_demo.sh # sc+https proof (P32): TLS round trip w/ signed + # chunked blob, TOFU pin/mismatch/strict/pre-pin, + # tightened plaintext gate — run twice ``` Set `CARGO_TARGET_DIR` to a path outside this folder to keep `target/` out of @@ -1003,8 +1027,9 @@ tree/`sc log` byte-for-byte identical to the origin, `sc verify --require` clean in the clone, a push from a second clone lands and a third clone's fetch sees it, zero `.sc/tmp` residue on either end — run twice. **Standing boundaries, stated plainly (same class as the ssh transport, -plus one new one):** plaintext only, **no TLS** (`sc+https://` deferred to -a TLS-dep phase or a fronting reverse proxy); **no authentication** (`sc +plus one new one):** plaintext only, **no TLS at the time — closed by P32 +(ADR-0042)**, which adds `sc+https://` in-binary rather than requiring a +fronting reverse proxy; **no authentication** (`sc serve --http` is unauthenticated, as `--stdio` delegates auth to ssh — production auth + TLS means fronting with a reverse proxy; this is the reach primitive, not a hosted-git competitor); **not HTTP-proxy/CDN safe** @@ -1193,13 +1218,16 @@ connection into `wire::serve_with_policy`, which rejects `PutObject`/`PutPack`/`UpdateRef` before any store write via a new `EC_READONLY`. A review fix closed a fail-open: a non-loopback bind justified only by tokens now fails closed (`401`, not an open server) if its -last token is removed while running. **Accepted boundaries:** no TLS — a -bearer token crosses the wire in plaintext, so a public deployment must -front with a TLS reverse proxy (`sc+https://` deferred); loopback with no -tokens configured stays unauthenticated by design, for local-dev ergonomics. -Wire-unchanged but for `EC_READONLY`; `PROTOCOL_VERSION` stays 3; ssh -`--stdio` is untouched. Proven by `demo/run_http_auth_demo.sh`. See -ADR-0040. +last token is removed while running. **Accepted boundaries at the time — +closed by P32 (ADR-0042):** no TLS — a bearer token crossed the wire in +plaintext, so a public deployment had to front with a TLS reverse proxy +(`sc+https://` deferred); loopback with no tokens configured stays +unauthenticated by design, for local-dev ergonomics, unchanged by P32. **Note +also:** P32 narrows this phase's non-loopback bind gate — tokens alone no +longer justify a *plaintext* public bind; a non-loopback bind now needs +`--read-only`, `--allow-public`, or (`--tls` AND ≥1 token). Wire-unchanged but +for `EC_READONLY`; `PROTOCOL_VERSION` stays 3; ssh `--stdio` is untouched. +Proven by `demo/run_http_auth_demo.sh`. See ADR-0040. **Phase 30 is built.** Agent session transcripts: a sealed provenance record for an agent's session can now attach to a commit. `Transcript { @@ -1291,6 +1319,59 @@ and accept-backoff pacing are unit-test-proven rather than demo-proven, since forcing a real stalled socket or fd exhaustion reliably in a demo script is impractical. See ADR-0041. +**Phase 32 is built.** In-binary TLS: `sc+https://` closes ADR-0036's "no +TLS" boundary and the audit's High #1 (plaintext bearer tokens/traffic). A +new leaf crate, `crates/tlsio` (`scl-tlsio`), is the only crate linking +rustls (0.23, `default-features = false`, features `ring, std, tls12, +logging` — ~14 new crates, C compiler only, no cmake), rcgen, and `ring` +(digest only, for the SPKI fingerprint — the RustCrypto quarantine in +`crates/crypto` is untouched); `repo` is its sole consumer, extending the +dependency rule to `cli → repo → {vfs, gitio, crypto, tlsio} → core`. Two +seam functions grow a TLS wrap and nothing else changes: client +`HttpTransport::connect` wraps the `TcpStream` before `write_client_opening` +when the URL is `sc+https://`; server `handle_http_connection` wraps before +`read_client_opening` when `--tls` is set — the opening codec, `wire.rs`, and +`serve_tokens.rs` are byte-for-byte unchanged, `PROTOCOL_VERSION` stays 3. +Trust model is accept-new TOFU, the SSH `known_hosts` shape: the pin is +`SHA-256(SPKI DER)`, stored one `host:port sha256:` line per host in +`~/.config/sc/known_hosts` (`SC_HTTPS_KNOWN_HOSTS` overrides the path); +`SC_HTTPS_FINGERPRINT` pre-pins without persisting (CI); `SC_HTTPS_STRICT=1` +refuses an unknown host outright. Pinning is deliberately pin-only in v1 — +certificate names/validity are ignored — but the handshake signature is +still verified, so a captured cert replayed without its private key is +rejected even against a matching pin; a pin mismatch always hard-fails, never +prompts. Server: `--tls` (refused together with `--stdio`) auto-mints a +self-signed identity into `.sc/serve-tls/` (`cert.pem`/`key.pem`, key file +`0600` — the key IS the server's identity — regenerated only if missing, +`not_after` 2126) unless `--tls-cert`/`--tls-key` PEM is supplied; the +startup banner's second line and `sc serve fingerprint []` (mints if +missing) print the SPKI fingerprint an operator distributes out-of-band +before a client's first connect. **Gate change (breaking, decision 5 of +#39):** a non-loopback bind is now allowed iff `--read-only` OR +`--allow-public` OR (`--tls` AND ≥1 configured token) — P29's "tokens alone +justify a public bind" is narrowed, since a token protecting only a +plaintext channel was never the guarantee its wording implied; the refusal +message names `--tls` as the fix. **Deliberate spec deviation (plan-approved, +recorded in ADR-0042):** under TLS, the `--max-connections` busy-shed at the +connection cap closes the socket silently instead of writing a readable +`503` — a readable status would need a TLS handshake on the accept thread, +which would violate ADR-0041's accepts-never-block property; plaintext +connections keep the readable `503` unchanged. Provider choice: ring over +the rustls-default aws-lc-rs (18 crates + cmake vs. 14 crates + C-compiler- +only), aws-lc-rs recorded as the swap-in fallback; pure-Rust providers +(`graviola`, `rustls-rustcrypto`) rejected as immature; ACME rejected +outright (`rustls-acme`'s async stack is the wrong shape for this project's +blocking design). Zero wire-protocol changes; the four `WirePolicy`/ +`ServeLimits` bounds from P31 apply unchanged under TLS (set on the inner +`TcpStream`, below the TLS wrap). Proven by `demo/run_tls_demo.sh`: a TLS +round trip carrying a signed chunked blob byte-for-byte, the TOFU +pin/mismatch/strict/pre-pin lifecycle, and the tightened plaintext gate — run +twice, zero residue. **Accepted boundaries:** pin-only trust means no +CA-path validation option yet (deferred, additive for PEM deployments); the +first connection to any host is unverified by construction (the TOFU trade, +same as SSH's first connect); a loopback bind with no tokens configured +stays unauthenticated by design, unchanged from P29. See ADR-0042. + Remaining follow-ons: operation objects in the CAS, oplog entries for remote-tracking refs, extending the sparse gate to `materialize_conflict_state`'s `to_encrypt`/sidecar write loops (see the @@ -1322,7 +1403,14 @@ DAG rather than only the first-parent mainline; a `transcripts_ride_ssh_transport` wire-harness test twin; and the now-stale `SignatureObj.snapshot` doc comment, which still describes the field as always a snapshot id though signing a transcript stores a -transcript id there instead). +transcript id there instead), and the P32 items — CA-path validation as an +additive trust option alongside pin-only for PEM-provisioned deployments, +opt-in SNI/certificate-name validation (v1 is pin-only and ignores names), +pin-management UX (`sc tls` list/remove entries in +`~/.config/sc/known_hosts` — today only `sc serve fingerprint` prints and +first-connect/`SC_HTTPS_FINGERPRINT` write), and TLS session-resumption +knobs (not evaluated in this phase; a later throughput pass if repeated +short-lived connections to the same host prove handshake cost material). ## Agent skills diff --git a/ROADMAP.md b/ROADMAP.md index 452f5b85..8eab7ce3 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -475,10 +475,43 @@ across every phase. transcript while the still-reachable one survives — run twice, zero residue. No new dependency. (ADR-0038.) +- **Phase 32 — In-binary TLS (`sc+https://`).** Closes the audit's High #1 + (plaintext bearer tokens/traffic on `sc+http://`) and ADR-0036's "no TLS" + boundary. A new leaf crate, `crates/tlsio` (`scl-tlsio`), is the only crate + linking rustls (0.23, ring provider, `default-features = false` — + ~14 new crates, C compiler only, no cmake) and rcgen; `repo` is its sole + consumer, extending the dependency rule to + `cli → repo → {vfs, gitio, crypto, tlsio} → core`. Two seam functions grow + a TLS wrap and nothing else changes — the opening codec, `wire.rs`, and + `serve_tokens.rs` are byte-for-byte unchanged, `PROTOCOL_VERSION` stays 3. + Trust is accept-new TOFU (the SSH `known_hosts` shape): the client pins + `SHA-256(SPKI)` into `~/.config/sc/known_hosts` on first connect + (`SC_HTTPS_KNOWN_HOSTS` override, `SC_HTTPS_FINGERPRINT` pre-pin, + `SC_HTTPS_STRICT=1` to refuse unknown hosts); pinning is pin-only in v1 + (names/validity ignored) but the handshake signature is still verified, and + a pin mismatch always hard-fails. `sc serve --http --tls + [--tls-cert --tls-key ]` auto-mints a self-signed identity into + `.sc/serve-tls/` (key `0600`, key-is-identity) unless PEM is supplied; + `sc serve fingerprint []` mints-if-missing and prints the SPKI + fingerprint for out-of-band distribution. **Gate change (breaking):** a + non-loopback bind now needs `--read-only`, `--allow-public`, or (`--tls` + AND ≥1 token) — P29's "tokens alone justify a public bind" is narrowed, + since a token protecting only a plaintext channel was never the guarantee + its wording implied. **Deliberate spec deviation:** under TLS, the + `--max-connections` busy-shed at the connection cap closes the socket + silently instead of writing a readable `503`, because a readable status + would require a TLS handshake on the accept thread, violating ADR-0041's + accepts-never-block property; plaintext connections keep the readable + `503` unchanged. Proven by `demo/run_tls_demo.sh`: a TLS round trip + carrying a signed chunked blob byte-for-byte, the TOFU + pin/mismatch/strict/pre-pin lifecycle, and the tightened plaintext gate — + run twice, zero residue. (ADR-0042.) + ## Active -**None.** The agent/workspace-depth horizon opened by P30 is complete; the -next horizon is TBD. +**None.** The agent/workspace-depth horizon opened by P30 is complete; P32 +closed the standalone TLS gap the P25–P31 horizon left open; the next +horizon is TBD. ## Completed phases (usability-first ordering) @@ -709,10 +742,21 @@ scale-&-reach horizon): - **Token expiry/rotation metadata (P29).** `sct-` tokens have no expiry field; rotation today is add-new + remove-old with no automation or reminder. Deferred. -- **`sc+https://` / TLS (P29).** `sc serve --http` is plaintext-only; a +- ~~**`sc+https://` / TLS (P29).** `sc serve --http` is plaintext-only; a bearer token crosses the wire in cleartext, so a public deployment must front with a TLS reverse proxy today. A first-party TLS dependency is - deferred, against the P25/P26 dep-free grain. + deferred, against the P25/P26 dep-free grain.~~ **Done.** Shipped in P32 + (ADR-0042): `sc+https://` via in-binary rustls, accept-new TOFU pinning, + a narrowed non-loopback bind gate (`--tls` + token, not token alone). See + the Phase 32 entry above. Follow-ons this phase named rather than closed: + CA-path validation as an additive trust option for PEM-provisioned + deployments (v1 is pin-only, names/validity ignored); opt-in SNI/ + certificate-name validation; pin-management UX (`sc tls` list/remove + entries in `~/.config/sc/known_hosts` — today only `sc serve fingerprint` + and first-connect/`SC_HTTPS_FINGERPRINT` write pins, nothing removes one); + TLS session-resumption knobs (not evaluated this phase — a later + throughput pass if repeated short-lived connections to the same host prove + handshake cost material). - ~~**Full OS-assigned-port de-flake for the CLI http-serving tests (launch prep).**~~ **Done.** `sc serve --http` now announces its bound address on stdout (`listening on `) right after `TcpListener::bind` diff --git a/demo/run_http_auth_demo.sh b/demo/run_http_auth_demo.sh index cc9d0edb..73837b3d 100755 --- a/demo/run_http_auth_demo.sh +++ b/demo/run_http_auth_demo.sh @@ -11,6 +11,15 @@ # a valid `Authorization: Bearer` is required on every connection, loopback # included; an `ro`-scope token floors the connection read-only. # +# P32 (ADR-0042) note: the non-loopback bind gate this demo exercises in +# step 8 was narrowed by the in-binary-TLS phase. Before P32, a configured +# token alone justified a plaintext non-loopback bind; as of P32 that combo +# is refused (the error names --tls as the fix) — a non-loopback bind now +# needs --read-only, --allow-public, or (--tls AND >=1 token). Step 9 below +# proves both halves of that change: the now-refused token-only plaintext +# case, and the --tls+token case that replaces it. The fuller TLS round trip +# (TOFU pinning, mismatch, strict mode) lives in demo/run_tls_demo.sh. +# # Self-checking: every claim is an assertion; any failure exits non-zero # before the RESULT line. set -euo pipefail @@ -26,9 +35,10 @@ A="$W/A" A2="$W/A2" SERVER_PID="" SERVER2_PID="" +SERVER3_PID="" cleanup() { - for pid in "$SERVER_PID" "$SERVER2_PID"; do + for pid in "$SERVER_PID" "$SERVER2_PID" "$SERVER3_PID"; do if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then kill "$pid" 2>/dev/null || true wait "$pid" 2>/dev/null || true @@ -38,12 +48,15 @@ cleanup() { } trap cleanup EXIT -# --- Pick two distinct free ports; probe with `nc -z` first. --- +# --- Pick distinct free ports; probe with `nc -z` first. --- pick_port() { - local exclude="$1" local candidate - for candidate in 8741 8742 8743 8901 18741 18742; do - [ "$candidate" = "$exclude" ] && continue + for candidate in 8741 8742 8743 8901 18741 18742 18743; do + local skip="" + for exclude in "$@"; do + [ "$candidate" = "$exclude" ] && skip=1 + done + [ -n "$skip" ] && continue if ! nc -z 127.0.0.1 "$candidate" 2>/dev/null; then echo "$candidate" return 0 @@ -51,8 +64,9 @@ pick_port() { done fail "no free port found among the candidates" } -PORT="$(pick_port '')" +PORT="$(pick_port)" PORT2="$(pick_port "$PORT")" +PORT3="$(pick_port "$PORT" "$PORT2")" wait_for_port() { local port="$1" tries=0 @@ -177,9 +191,37 @@ echo "server: stopped ✔" check_no_tmp_residue "$A2" "origin A2" +echo +echo "=== 9: P32 gate (ADR-0042) — tokens alone no longer justify a plaintext" +echo " public bind; --tls + a token does ===" +cd "$A2" +"$SC" serve token add --label a2-writer --scope rw >/dev/null 2>&1 +cd "$W" + +if "$SC" serve --http "0.0.0.0:$PORT3" "$A2" 2>"$W/err-tokenonly.txt"; then + fail "plaintext non-loopback bind justified by a token alone should now be refused (P32)" +fi +grep -qi -- "--tls" "$W/err-tokenonly.txt" \ + || fail "the P32 refusal must name --tls as the fix, got: $(cat "$W/err-tokenonly.txt")" +echo "public bind (0.0.0.0, token configured, no --tls): refused, error names --tls ✔" + +"$SC" serve --http "0.0.0.0:$PORT3" --tls "$A2" & +SERVER3_PID=$! +wait_for_port "$PORT3" +echo "public bind (0.0.0.0, token configured, --tls): accepted ✔" +kill "$SERVER3_PID" +wait "$SERVER3_PID" 2>/dev/null || true +SERVER3_PID="" +! nc -z 127.0.0.1 "$PORT3" 2>/dev/null || fail "third server still accepting connections after kill" +echo "server: stopped ✔" + +check_no_tmp_residue "$A2" "origin A2 (after TLS bind)" + echo echo "RESULT: sc+http access control — a no-token clone is rejected (401," echo "authentication error), an ro-token clone reads but its push is rejected" echo "read-only, an rw-token push lands and a later ro-token clone sees it," echo "an unjustified non-loopback bind is refused while --allow-public opens" -echo "it deliberately, and zero .sc/tmp residue is left anywhere. OK" +echo "it deliberately, the P32 gate refuses a token-only plaintext public bind" +echo "while --tls + a token opens it, and zero .sc/tmp residue is left" +echo "anywhere. OK" diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index e2165cc4..6cb341a3 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -101,20 +101,81 @@ too, not just the src-control-side metadata. scanner warns at attach time but never blocks; rotation of the underlying secret remedies). -## Network transport (`sc serve --http` / `sc+http://`) — ADR-0036/0040/0041 - -- **No TLS.** `sc serve --http` is **plaintext**. When bearer-token auth is - configured, the token crosses the wire in the clear — a public deployment MUST - be fronted by a TLS reverse proxy. `sc+https://` is deferred. -- **Auth is opt-in.** With no tokens configured, a loopback bind is unauthenticated - by design (local-dev ergonomics); a non-loopback bind is **fail-closed** (refused - unless `--read-only`, `--allow-public`, or ≥1 configured token). `--allow-public` - with no tokens is a sanctioned foot-gun: a deliberately open server. -- **Minor pre-auth information leak:** 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. -- **Not proxy/CDN-safe** (raw post-opening protocol) — this remains open, deferred - in [`ROADMAP.md`](../ROADMAP.md). +## Network transport (`sc serve --http` / `sc+http(s)://` / `ssh://`) — ADR-0022/0036/0040/0041/0042 + +- **Two confidential transports now ship: `sc+https://` (P32, ADR-0042) and + `ssh://` (P12, ADR-0022).** `ssh://` has been fully confidential since + Phase 12 — it delegates the entire channel to the user's `ssh`, inheriting + whatever SSH's own transport security and host-key trust already provide; + it was never plaintext and needed no separate TLS story. `sc+https://` adds + the same property for operators without ssh reach: a bearer token (P29) and + all repository traffic are encrypted end to end, at both the server and the + client leg, with no reverse proxy required. +- **`sc+https://` trust model is accept-new TOFU, stated plainly (the SSH + `known_hosts` trade).** The server auto-mints a self-signed identity + (`.sc/serve-tls/`) or loads an operator-supplied PEM cert; the client pins + `SHA-256(SPKI)` on first connect into `~/.config/sc/known_hosts` + (`SC_HTTPS_KNOWN_HOSTS` overrides the path). **The first connection to any + host is unverified by construction** — an active MITM present on that + specific first connection is not detected. Every subsequent connection + compares against the stored pin and hard-fails on any mismatch, never + silently proceeding and never prompting to accept a changed key. + `SC_HTTPS_FINGERPRINT` lets an operator (e.g. CI) pre-pin out-of-band + instead of trusting the first connection; `SC_HTTPS_STRICT=1` refuses to + connect to any host with no existing pin, closing the accept-new window + entirely for operators who need it. Trust is **pin-only in v1** — + certificate names and validity periods are deliberately ignored, and + CA-path validation as an additional trust option is deferred (see + `ROADMAP.md`). The TLS handshake signature is still verified even though + names/validity are not, so a captured certificate replayed without its + matching private key is rejected regardless of pin state. +- **`sc+http://` (plaintext) still exists, and its boundary is now narrower + and clearer: it applies only to loopback or explicitly `--allow-public` + deployments.** A public (non-loopback) plaintext deployment protected by + bearer tokens *alone* is no longer a supported configuration — as of P32, + `--tls` is required alongside ≥1 token to justify a non-loopback bind (the + P29 gate is narrowed; a token guarding only a plaintext channel was always + a weaker guarantee than the earlier wording implied). A loopback bind with + no tokens configured stays unauthenticated by design (local-dev + ergonomics, unchanged since P29); `--allow-public` with no tokens is still + a sanctioned foot-gun — a deliberately open, unauthenticated, plaintext + server, for the operator who explicitly asked for it. +- **Reverse-proxy guidance, corrected to cover both legs.** Prior guidance + ("front with a TLS reverse proxy") was incomplete: a server-side proxy + terminates TLS toward clients that speak TLS, but before P32 the `sc` + client itself spoke only plaintext TCP, so a server-side proxy alone + encrypted nothing end-to-end. Now that `sc+https://` and `ssh://` both + exist, the reverse-proxy path is a **fallback for operators who want their + existing proxy infrastructure in front of `sc`**, not the primary + confidentiality answer. If used, both legs need care: + - **Server side — raw-TCP/stream-mode TLS termination, never HTTP mode.** + The post-opening protocol is not HTTP, so an HTTP-mode reverse proxy will + not tunnel it. Use nginx `stream {}` + `ngx_stream_ssl_module` (note: + neither the `stream` block nor its ssl module is built into a default + nginx — check `nginx -V`; and `ssl_preread` is routing-only, it does + **not** terminate TLS), or HAProxy `mode tcp` with `bind : ssl crt + `, or stunnel's server mode (`accept`/`connect`/`cert`). Forward + decrypted bytes to a **loopback-bound** `sc serve --http 127.0.0.1:…` + and keep bearer tokens configured — the proxy→`sc serve` hop is + plaintext loopback, and the token is still the authorization mechanism + on that hop. + - **Client side — the leg the old guidance omitted.** The unmodified `sc` + client cannot itself speak TLS to a plaintext `sc+http://` remote it + thinks it's talking to over an encrypted channel; a client-side tunnel + (stunnel's client mode, `client = yes`, or `ssh -L`) is required to give + that plaintext client an encrypted leg to the proxy. **Simplest fix: + skip both legs of this section entirely and use `sc+https://` or + `ssh://` directly** — they exist now specifically so this two-legged + proxy dance is no longer the only option. + - 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. +- **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- hardening items this section used to track as open are now closed, plus a fourth gap P31's own research pass first named: diff --git a/docs/adr/0042-in-binary-tls-sc-https.md b/docs/adr/0042-in-binary-tls-sc-https.md new file mode 100644 index 00000000..482f547d --- /dev/null +++ b/docs/adr/0042-in-binary-tls-sc-https.md @@ -0,0 +1,194 @@ +# ADR-0042: In-binary TLS — `sc+https://` via rustls + +- **Status:** Accepted +- **Date:** 2026-07-10 +- **Phase:** 32 +- **Builds on:** ADR-0036 (P26 sc-native HTTP transport — reserved the + `sc+https://` scheme and named "no TLS" as an accepted consequence), + ADR-0040 (P29 access control — the bearer-token/bind-gate machinery this + phase extends), ADR-0041 (P31 listener resource limits — the + `--max-connections` accept-thread-never-blocks property this phase must not + violate) +- **Research:** `docs/research/tls-options-sc-http.md` (ticket #25 — measured + dependency counts, provider survey, reverse-proxy both-legs correction). + Decided at the decision level via tickets #26 (transport choice) and #35 + (provisioning + TOFU UX), the phase spec at #39. + +## Context + +The 2026-07-09 security audit's High #1 (ticket #24's map) is that +`sc serve --http` is plaintext: a bearer token (ADR-0040) and all repository +traffic cross the wire in the clear. ADR-0036 named this as an accepted +consequence and reserved the `sc+https://` scheme for "when TLS lands"; +`docs/THREAT-MODEL.md` told operators to front a public deployment with a TLS +reverse proxy. The #25 research pass found that guidance incomplete: a +server-side proxy terminates TLS toward clients that speak TLS, but `sc`'s own +client spoke only plaintext TCP, so the proxy alone protected nothing +end-to-end — the *client* leg needed wrapping too (client-mode stunnel, +`ssh -L`), a burden the existing docs never stated. + +## Decision — why in-binary TLS is load-bearing here + +Two comparable Rust servers, Vaultwarden and Garage, both decline to ship +in-binary TLS and instead punt to a reverse proxy +(`docs/research/tls-options-sc-http.md` §"Maturity / prior art"). That +precedent does not transfer to `sc`. Vaultwarden and Garage are server-only: +their proxy story covers the one leg that exists. `sc` is a CLI whose +**client** half must reach servers its operator does not control — a +teammate's laptop, a CI runner hitting a third-party host, a contributor +cloning from an unfamiliar remote. No reverse proxy can be placed in front of +a server the `sc` operator doesn't administer. This is the case a +"front it with nginx" answer structurally cannot cover, and it is why this +phase ships TLS in the binary rather than only writing the (still necessary) +both-legs proxy documentation. + +## Provider + +rustls **0.23.41**, **ring** crypto provider (`default-features = false`, +`features = ["ring", "std", "tls12", "logging"]`): ~14 new crates measured +against this repo's `Cargo.lock`, requiring a C compiler only (ring is +mostly assembly/C, no cmake). The rustls-default provider, **aws-lc-rs**, was +rejected as the primary choice — 18 new crates plus a cmake + C-compiler build +requirement — but is recorded here as the **swap-in fallback** if ring's +build story ever proves insufficient on a target platform, since both +providers implement the same `rustls::crypto::CryptoProvider` trait and the +swap touches no call site above it. Pure-Rust providers (`graviola`, +`rustls-rustcrypto`) were rejected as immature: `rustls-rustcrypto` +self-describes as "experimental," covering an estimated 70% of usage; +`graviola`'s RSA support is limited to five fixed key sizes. + +## Quarantine + +`crates/tlsio` (`scl-tlsio`) is the only crate linking `rustls`, `rcgen`, +`ring`, and `rustls-pki-types` — the same discipline as `gix`→`gitio` and +RustCrypto→`crypto`. It is a workspace dependency **leaf**: unlike its +sibling crates (`vfs`, `gitio`, `crypto`), it depends on no other workspace +crate, not even `core`. The dependency rule gains one edge: +`cli → repo → {vfs, gitio, crypto, tlsio}`, with `{vfs, gitio, crypto} → core` +unchanged. `repo` is the sole consumer. SHA-256 (used for the SPKI +fingerprint) goes through `ring::digest`, **not** the RustCrypto stack in +`crates/crypto` — that quarantine holds; `tlsio` never links `sha2` or any +other RustCrypto crate. + +## Trust model + +Accept-new TOFU, the SSH `known_hosts` shape, stated plainly: **the first +connection to a host is unverified by construction.** The pin is +`SHA-256(full SPKI DER TLV)` — the standard "pin the public key, not the +cert" approach, which survives a same-key certificate renewal and is +independently verifiable with `openssl x509 -pubkey | openssl pkey -pubin | +openssl dgst -sha256`. Pinning is **pin-only in v1**: certificate names and +validity periods are deliberately ignored (the pin itself carries the trust +decision), but the TLS **handshake signature is still verified** — a +pinned-certificate MITM replaying a captured cert without the matching +private key is rejected at the handshake, not just at pin comparison. A pin +mismatch **always hard-fails** — it never prompts or offers to proceed, the +same discipline SSH's `StrictHostKeyChecking` enforces for a changed key. + +Storage and knobs: + +- `~/.config/sc/known_hosts` (`$XDG_CONFIG_HOME/sc/known_hosts` if set, + falling back to `$HOME/.config/sc/known_hosts`; `SC_HTTPS_KNOWN_HOSTS` + overrides the path outright), one `host:port sha256:` line per pin. +- `SC_HTTPS_FINGERPRINT=sha256:` pre-pins a connection (e.g. for CI) + without ever writing to the known_hosts file — an explicit, non-persisted + override. +- `SC_HTTPS_STRICT=1` refuses to connect to any host with no existing pin, + closing the accept-new window for operators who want SSH's + `StrictHostKeyChecking=yes` equivalent. +- The server prints its SPKI fingerprint on the `sc serve --tls` startup + banner and via `sc serve fingerprint []` (which mints the identity if + missing), so an operator can distribute the pin out-of-band before a + client's first connection. + +## Server lifecycle + +`sc serve --http --tls [--tls-cert --tls-key ]`. +Without PEM flags, the server auto-mints a self-signed identity into +`.sc/serve-tls/` (`cert.pem` + `key.pem`, key file `0600` — the key file IS +the server's identity, so its permissions matter the same way an SSH host key +does), regenerating only when the material is missing, `not_after` set to +2126 (a century out — self-signed + TOFU pinning makes expiry-driven rotation +meaningless without a CA, so the cert's own validity window is not the trust +mechanism; the pin is). `--tls` is refused together with `--stdio` (ssh +already provides that channel's confidentiality end to end — layering TLS +under it protects nothing new). ACME was rejected outright: `rustls-acme` +pulls in `async-io` and an async HTTP + `serde_json` stack, wrong-shaped for +this project's blocking, minimal-dependency design; an operator who wants a +CA-issued cert runs certbot or a proxy and supplies `--tls-cert`/`--tls-key`. + +## Gate change (breaking) + +Per decide ticket #26/#35 and the phase spec: a non-loopback bind is allowed +iff `--read-only` OR `--allow-public` OR (`--tls` AND ≥1 serve token +configured). **Tokens alone no longer justify a plaintext public bind** — the +P29 gate (`--read-only | --allow-public | ≥1 token`) is narrowed so a +plaintext token-only deployment, which used to be accepted, is now refused +with a message naming `--tls` as the fix. This is a deliberate breaking +change to `bind_is_allowed`'s contract, justified by the same reasoning as +the rest of this ADR: a bearer token protecting nothing but a plaintext +channel was always a weaker guarantee than the gate's wording implied. + +## Accepted consequences + +1. **Under TLS, the `--max-connections` busy-shed closes silently instead of + writing a readable `503`.** Writing a `503 Service Unavailable` requires a + TLS handshake, and performing that handshake on the accept thread would let + one slow or hostile client stall every subsequent `accept()` — exactly the + property ADR-0041 exists to protect (accepts must never block). Plaintext + connections keep the readable `503`; TLS connections at the connection cap + simply see the socket close. This is a real behavior gap from the phase + spec's first draft (which assumed the busy status could ride the TLS + channel) — deliberately kept as a silent close rather than adding + handshake work to the accept path, and recorded in the amended spec + (`docs/superpowers/specs/2026-07-10-p32-tls-sc-https-design.md` §3). +2. **A new API-break tracking burden.** This is the project's first TLS + dependency; rustls's 0.23→0.24 boundary is a real API break to track on + the next upgrade, unlike the additive minor-version bumps most of this + project's dependencies see. +3. **CA-path validation is deferred.** v1 ships pin-only trust; validating a + presented cert against a system/operator-supplied CA bundle as an + *additional* trust path (for PEM-provisioned deployments that already have + a real CA-issued cert) is additive follow-on work, not a v1 requirement. +4. **The plaintext-gate break is pre-1.0 and narrow.** The project is not yet + at a stability guarantee, and the affected case — a plaintext non-loopback + bind justified by tokens alone — is exactly the case this phase's own + audit finding targets; narrowing it is the fix, not incidental breakage. + +## Consequences + +- `PROTOCOL_VERSION` stays 3: TLS wraps below the opening codec, which is + unchanged (`wire.rs`, `read_bounded_opening`, `serve_tokens.rs` all + untouched). +- `sc+https://` joins `sc+http://` and `ssh://` as a third confidential-or-not + transport choice; `ssh://` (ADR-0022) was already fully confidential and is + now explicitly promoted as such in `docs/THREAT-MODEL.md`. +- The both-legs reverse-proxy documentation gap the #25 research pass found + is closed by this ADR's existence — but is now also a fallback rather than + the only confidential option, since `sc+https://` and `ssh://` both exist. +- Proven by `demo/run_tls_demo.sh`: a TLS round trip carrying a signed chunked + blob, the TOFU pin/mismatch/strict/pre-pin lifecycle, and the tightened + plaintext gate refusing a token-only public bind — run twice, zero residue. + +## Rejected alternatives + +- **aws-lc-rs as the primary provider.** Rejected for its heavier build + requirement (cmake + C compiler vs. ring's C-compiler-only); recorded above + as the fallback, not eliminated from consideration. +- **A pure-Rust provider (`graviola`, `rustls-rustcrypto`).** Rejected as + immature for a security-load-bearing path — see Provider above. +- **The `rustls-pin` crate for fingerprint pinning.** Targets rustls 0.19, + five majors stale; a hand-written ~100-line `ServerCertVerifier` was + written instead, following the pattern in rustls's own test suite. +- **ACME in-binary (`rustls-acme`).** Rejected — async-only dependency + stack, wrong fit for this project's blocking I/O design. +- **Sending a readable `503` under TLS at the connection cap.** Rejected — + would require an accept-thread TLS handshake, violating ADR-0041's + accepts-never-block property. See Accepted consequences #1. +- **Noise protocol (`sc+noise://` via `snow`) instead of TLS.** Considered in + the research doc (Option C3) as the smallest marginal dependency footprint, + but rejected for this decision: no operator/proxy/certificate ecosystem + (no path for an operator who wants to terminate at nginx or use a + CA-issued cert), an explicitly unaudited crate, and a bigger architectural + bet (sc's network identity becoming scl-id keys rather than x509) than this + ticket scoped. diff --git a/docs/adr/README.md b/docs/adr/README.md index 018bd416..975d580c 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -52,6 +52,7 @@ supersedes the old one and update the **Status** line of the superseded record. | [0039](0039-security-hardening-sweep.md) | Security hardening sweep (audit fix-now items) | Accepted | 28 | | [0040](0040-sc-http-access-control.md) | sc+http access control (read-only, fail-closed bind, bearer tokens) | Accepted | 29 | | [0041](0041-listener-resource-limits.md) | Listener resource limits (connection cap, session timeout, spool cap, accept backoff) | Accepted | 31 | +| [0042](0042-in-binary-tls-sc-https.md) | In-binary TLS — `sc+https://` via rustls | Accepted | 32 | See [`ROADMAP.md`](../../ROADMAP.md) for the phase sequence. ADR numbers are assigned in creation order, so 0017 — the secret scanner — sequences as phase P5, diff --git a/docs/superpowers/specs/2026-07-10-p32-tls-sc-https-design.md b/docs/superpowers/specs/2026-07-10-p32-tls-sc-https-design.md index 332686a8..293d9f7e 100644 --- a/docs/superpowers/specs/2026-07-10-p32-tls-sc-https-design.md +++ b/docs/superpowers/specs/2026-07-10-p32-tls-sc-https-design.md @@ -95,10 +95,16 @@ Public surface: fingerprint. - `handle_http_connection` wraps the accepted socket in `TlsServerStream` before the opening read; P31's whole-session read/write timeouts still apply - (set on the inner `TcpStream`). The `--max-connections` busy-shed changes - shape under TLS only: the handshake completes first, then the busy status - goes over TLS so the client still gets the clean message (bounded work — the - 30s opening timeout covers the handshake). + (set on the inner `TcpStream`). **Amended (implementation deviation, plan- + approved — see ADR-0042):** the `--max-connections` busy-shed does NOT send + a status under TLS. Sending a readable busy response would require + performing the TLS handshake on the accept thread itself, which would let + one slow or hostile client stall every subsequent `accept()` — exactly the + accepts-never-block property ADR-0041 exists to protect. So at the + connection cap, a TLS connection is simply closed with no handshake and no + status; only plaintext (`--tls` unset) connections still get the readable + `503` written before any read. This is a deliberate, accepted asymmetry + between the two modes, not a bug. - New subcommand `sc serve fingerprint []`: prints the repo's `.sc/serve-tls/` SPKI fingerprint, **minting if missing** (so an operator can distribute the pin before first serve) — same `load_or_mint` path, no drift. From e87c2923fea7eeb99baed6e14dbddf8d421349c1 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 10 Jul 2026 16:24:18 +0300 Subject: [PATCH 12/14] fix: P32 final-review fix wave (I1-I3, T1, T7, T9, Recommendation 2) Closes the P32 final whole-branch review's findings: stale CLAUDE.md gate text (I1), SC_HTTPS_STRICT failing open on unrecognized values (I2), the THREAT-MODEL + serve-startup warning for residual plaintext-token exposure on --read-only/--allow-public public binds (I3), missing test asserts in scl-tlsio (T1), enforcement-implying doc-comment wording in the CLI (T7), missing P31/P32 ROADMAP rows (T9), and four named P32 follow-ons recorded in ROADMAP Deferred (Recommendation 2). --- CLAUDE.md | 8 ++-- ROADMAP.md | 15 +++++++ crates/cli/src/main.rs | 6 ++- crates/repo/src/http_transport.rs | 56 +++++++++++++++++++++++++ crates/repo/src/tls_pins.rs | 30 ++++++++++++- crates/tlsio/src/identity.rs | 2 + docs/THREAT-MODEL.md | 7 ++++ docs/adr/0042-in-binary-tls-sc-https.md | 4 +- 8 files changed, 120 insertions(+), 8 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 7ad295c2..b75ec144 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -289,8 +289,8 @@ cargo run --bin sc -- serve --http [--read-only] [--allow-public] # `sc serve --http 127.0.0.1:8730 .`; exactly one # of --stdio/--http is required; a non-loopback # is refused unless --read-only, - # --allow-public, or a configured serve token - # justifies it (P29) + # --allow-public, or — with --tls — a configured + # serve token justifies it (P29, narrowed by P32) # --max-connections : concurrent-connection cap, # --http only, default 32, 0=unlimited; at the limit # a new connection gets busy-status-and-close, no @@ -1336,7 +1336,9 @@ Trust model is accept-new TOFU, the SSH `known_hosts` shape: the pin is `SHA-256(SPKI DER)`, stored one `host:port sha256:` line per host in `~/.config/sc/known_hosts` (`SC_HTTPS_KNOWN_HOSTS` overrides the path); `SC_HTTPS_FINGERPRINT` pre-pins without persisting (CI); `SC_HTTPS_STRICT=1` -refuses an unknown host outright. Pinning is deliberately pin-only in v1 — +refuses an unknown host outright (`=1` enables it; any non-empty value other +than `0` is also treated as enabled — fails closed on a typo). Pinning is +deliberately pin-only in v1 — certificate names/validity are ignored — but the handshake signature is still verified, so a captured cert replayed without its private key is rejected even against a matching pin; a pin mismatch always hard-fails, never diff --git a/ROADMAP.md b/ROADMAP.md index 8eab7ce3..14c770bd 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -544,6 +544,8 @@ horizon is TBD. | **P28 — Security hardening sweep** | Close the audit's concrete-bug Highs + surface the accepted Mediums | strict ref-name validation at the write/read boundary (hostile wire `UpdateRef` + git-remote branch names rejected); `MAX_OBJECT_SIZE` caps every untrusted frame/pack-record/zstd-output length; `sc protect` nudges low-entropy secret filenames toward `sc secret`; secret env-var boundary documented as authorized-local-process-context + plaintext stays `Zeroizing`; every prior demo green + new pinned regression tests, no new dependency | [0039](docs/adr/0039-security-hardening-sweep.md) | | **P29 — sc+http access control** | Close the audit's remaining unauthenticated-server High | fail-closed non-loopback bind; `sc serve token add/remove/list` + `SC_HTTP_TOKEN` bearer auth at the HTTP opening (constant-time `BLAKE3` compare, `401` before the wire handoff); `--read-only`/`ro`-scope tokens reject mutating verbs before any store write via `EC_READONLY`; proven by `demo/run_http_auth_demo.sh` | [0040](docs/adr/0040-sc-http-access-control.md) | | **P30 — Agent session transcripts** | Attach a sealed, provenance-checked agent-session record to a commit | `sc transcript attach --agent claude --sign`; a keyless clone gets ciphertext only, the recipient's identity decrypts byte-exact; `sc log` shows a non-decrypting presence marker; `sc gc` prunes a transcript once its only snapshot is unreachable; proven by `demo/run_transcript_demo.sh` | [0038](docs/adr/0038-agent-session-transcripts.md) | +| **P31 — Listener resource limits** | Bound `sc serve --http`/`--stdio` against a hostile or overloaded peer | `--max-connections`/`--timeout`/`--max-pack-size` close ADR-0036's three named-but-open accepted consequences plus an aggregate-pack-spool gap this phase's own research pass found; busy-status-and-close at the connection cap, connection-fatal session timeout, `EC_TOO_LARGE` mid-stream abort on both transports, capped read-only pre-drain, Go-shaped exponential accept backoff; proven by `demo/run_limits_demo.sh` plus unit-test-proven timeout/backoff | [0041](docs/adr/0041-listener-resource-limits.md) | +| **P32 — In-binary TLS (`sc+https://`)** | Confidential `sc+http` transport without a reverse proxy | `sc serve --http --tls` auto-mints (or loads PEM) a serve identity; `sc clone sc+https://host/repo` with accept-new TOFU pinning into `~/.config/sc/known_hosts`; gate tightened so a plaintext public bind can no longer be justified by tokens alone (`--tls` + ≥1 token now required); proven by `demo/run_tls_demo.sh` (signed chunked blob over TLS, pin/mismatch/strict/pre-pin, tightened plaintext gate) | [0042](docs/adr/0042-in-binary-tls-sc-https.md) | > **Prior art.** Phases P5–P9 adapt decisions from the sibling project > [git.agentic](https://github.com/git-agentic/git.agentic) (same BLAKE3 @@ -828,6 +830,19 @@ 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. ## How a phase gets built diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 084f568e..8adb400f 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -318,10 +318,12 @@ enum Cmd { /// `--http` only. (P32) #[arg(long)] tls: bool, - /// PEM certificate chain for --tls (requires --tls-key). + /// PEM certificate chain for --tls; must be given together with + /// --tls-key (enforced in run_serve, not by clap). #[arg(long)] tls_cert: Option, - /// PEM private key for --tls (requires --tls-cert). + /// PEM private key for --tls; must be given together with + /// --tls-cert (enforced in run_serve, not by clap). #[arg(long)] tls_key: Option, /// Repo root to serve (the directory containing `.sc/`). Required diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index 1ac37887..0b3cbb2d 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -677,6 +677,29 @@ fn auth_is_mandatory(addr: &str, read_only: bool, allow_public: bool) -> bool { !is_loopback_host(host) && !read_only && !allow_public } +/// Does an already-accepted bind owe its operator a plaintext-token +/// warning? True when the bind is non-loopback, TLS is off, and ≥1 serve +/// token is configured — i.e. the bind proceeded justified by +/// `--read-only`/`--allow-public` (never by tokens alone; [`bind_is_allowed`] +/// already refuses that combination), but bearer tokens still exist and will +/// be presented/accepted in cleartext on every connection (I3, P32 +/// final-review fix wave). Mirrors [`bind_is_allowed`]'s host extraction; +/// kept as a separate function since the two answer different questions — +/// may this bind proceed vs. should we warn about one that already has. +fn plaintext_token_warning_applies(addr: &str, root: &std::path::Path, tls: bool) -> Result { + if tls { + return Ok(false); + } + let host = addr.rsplit_once(':').map(|(h, _)| h).unwrap_or(addr); + let host = host.trim_start_matches('[').trim_end_matches(']'); + if is_loopback_host(host) { + return Ok(false); + } + let tokens_configured = + !crate::serve_tokens::load(&crate::layout::Layout::at(root))?.is_empty(); + Ok(tokens_configured) +} + /// How `sc serve --http` provides its TLS identity (P32). `Off` keeps the /// P26 plaintext listener; `AutoMint` loads-or-mints `.sc/serve-tls/`; /// `Pem` loads an operator-supplied pair (certbot etc.). @@ -745,6 +768,13 @@ pub fn serve_http( ))); } let mandatory_auth = auth_is_mandatory(addr, read_only, allow_public); + if plaintext_token_warning_applies(addr, root, tls_config.is_some())? { + eprintln!( + "warning: {addr} is bound publicly with serve tokens configured but no --tls — \ + bearer tokens will cross the wire in cleartext on every connection; use --tls for \ + confidential token transport" + ); + } let listener = TcpListener::bind(addr) .map_err(|e| Error::ConnectionLost(format!("sc+http bind {addr}: {e}")))?; // Announce the actually-bound address on stdout, then flush. stdout is @@ -1526,6 +1556,32 @@ mod tests { let _ = std::fs::remove_dir_all(&root); } + /// The I3 plaintext-token warning decision: only fires for a + /// non-loopback, TLS-off bind that has ≥1 token configured (the + /// `--read-only`/`--allow-public`-justified case `bind_is_allowed` + /// already lets through) — never for loopback, never once `--tls` is on, + /// never with no tokens configured. + #[test] + fn plaintext_token_warning_fires_only_for_public_plaintext_with_tokens() { + let root = tmp_repo("tokenwarn"); + + // No tokens configured yet: never warns, loopback or not, TLS or not. + assert!(!plaintext_token_warning_applies("0.0.0.0:0", &root, false).unwrap()); + assert!(!plaintext_token_warning_applies("127.0.0.1:0", &root, false).unwrap()); + + add_test_token(&root); + + // Public, plaintext, tokens configured: warns. + assert!(plaintext_token_warning_applies("0.0.0.0:0", &root, false).unwrap()); + // Same, but TLS on: no warning (tokens are confidential in transit). + assert!(!plaintext_token_warning_applies("0.0.0.0:0", &root, true).unwrap()); + // Loopback: never warns regardless of TLS. + assert!(!plaintext_token_warning_applies("127.0.0.1:0", &root, false).unwrap()); + assert!(!plaintext_token_warning_applies("[::1]:0", &root, false).unwrap()); + + let _ = std::fs::remove_dir_all(&root); + } + /// Configure one `rw` serve token on `root`'s `.sc/serve-tokens.toml`, /// via the same mechanism the P29 gate tests use — shared here so the /// P32 gate-lattice test doesn't duplicate the inline setup. diff --git a/crates/repo/src/tls_pins.rs b/crates/repo/src/tls_pins.rs index 628d1623..ba16ef39 100644 --- a/crates/repo/src/tls_pins.rs +++ b/crates/repo/src/tls_pins.rs @@ -18,7 +18,10 @@ pub struct TlsClientPolicy { /// `SC_HTTPS_FINGERPRINT`: verified for this process only, never /// persisted (CI-friendly). pub pre_pin: Option<[u8; 32]>, - /// `SC_HTTPS_STRICT=1`: refuse unknown hosts instead of accept-new. + /// `SC_HTTPS_STRICT`: refuse unknown hosts instead of accept-new. + /// `=1` enables it; any non-empty value other than `0` is also treated + /// as enabled (fails closed on typos like `=true`/`=yes` rather than + /// silently falling back to accept-new — see [`strict_from`]). pub strict: bool, } @@ -32,7 +35,7 @@ impl TlsClientPolicy { Ok(s) if !s.is_empty() => Some(parse_fingerprint(&s)?), _ => None, }; - let strict = std::env::var("SC_HTTPS_STRICT").map(|v| v == "1").unwrap_or(false); + let strict = strict_from(std::env::var("SC_HTTPS_STRICT").ok().as_deref()); Ok(TlsClientPolicy { known_hosts, pre_pin, @@ -41,6 +44,20 @@ impl TlsClientPolicy { } } +/// Pure decision for `SC_HTTPS_STRICT`: unset, empty, or `"0"` means not +/// strict; any other value (including typos like `"true"`/`"yes"`) means +/// strict. Fails closed rather than silently disabling strict mode on an +/// unrecognized value. Factored out of [`TlsClientPolicy::from_env`] so it +/// can be unit-tested without mutating process env (racy under parallel +/// tests). +fn strict_from(value: Option<&str>) -> bool { + match value { + None => false, + Some(v) if v.is_empty() || v == "0" => false, + Some(_) => true, + } +} + /// `$XDG_CONFIG_HOME/sc/known_hosts`, falling back to /// `$HOME/.config/sc/known_hosts`. pub fn default_known_hosts_path() -> Result { @@ -162,6 +179,15 @@ mod tests { assert!(!dir.exists()); } + #[test] + fn strict_from_fails_closed_on_unrecognized_values() { + assert!(strict_from(Some("1"))); + assert!(strict_from(Some("true"))); + assert!(!strict_from(Some("0"))); + assert!(!strict_from(Some(""))); + assert!(!strict_from(None)); + } + #[test] fn parse_fingerprint_accepts_both_forms_and_rejects_junk() { let hexs = "ab".repeat(32); diff --git a/crates/tlsio/src/identity.rs b/crates/tlsio/src/identity.rs index 38fe0ebb..03d226a2 100644 --- a/crates/tlsio/src/identity.rs +++ b/crates/tlsio/src/identity.rs @@ -120,6 +120,7 @@ mod tests { let err = load_or_mint(&dir).unwrap_err(); assert!(err.to_string().contains("half a TLS identity"), "got: {err}"); std::fs::remove_dir_all(&dir).unwrap(); + assert!(!dir.exists()); } #[test] @@ -129,5 +130,6 @@ mod tests { let loaded = load_pem(&dir.join("cert.pem"), &dir.join("key.pem")).unwrap(); assert_eq!(minted.spki_sha256, loaded.spki_sha256); std::fs::remove_dir_all(&dir).unwrap(); + assert!(!dir.exists()); } } diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index 6cb341a3..7a80f4d8 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -140,6 +140,13 @@ too, not just the src-control-side metadata. ergonomics, unchanged since P29); `--allow-public` with no tokens is still a sanctioned foot-gun — a deliberately open, unauthenticated, plaintext server, for the operator who explicitly asked for it. + **Residual plaintext-token exposure, stated plainly:** a public bind + justified by `--read-only` or `--allow-public` *with* tokens configured + still requires the bearer on every connection — the P32 gate closes only + the tokens-ALONE justification, not plaintext token use in general — so + those tokens still cross the wire in cleartext on such a deployment. Use + `sc+https://` (or `ssh://`) instead of `sc+http://` whenever a public bind + needs its bearer tokens to stay confidential in transit. - **Reverse-proxy guidance, corrected to cover both legs.** Prior guidance ("front with a TLS reverse proxy") was incomplete: a server-side proxy terminates TLS toward clients that speak TLS, but before P32 the `sc` diff --git a/docs/adr/0042-in-binary-tls-sc-https.md b/docs/adr/0042-in-binary-tls-sc-https.md index 482f547d..a1c7afa7 100644 --- a/docs/adr/0042-in-binary-tls-sc-https.md +++ b/docs/adr/0042-in-binary-tls-sc-https.md @@ -95,7 +95,9 @@ Storage and knobs: override. - `SC_HTTPS_STRICT=1` refuses to connect to any host with no existing pin, closing the accept-new window for operators who want SSH's - `StrictHostKeyChecking=yes` equivalent. + `StrictHostKeyChecking=yes` equivalent (`=1` enables it; any non-empty + value other than `0` is also treated as enabled — it fails closed on a + typo like `=true` rather than silently falling back to accept-new). - The server prints its SPKI fingerprint on the `sc serve --tls` startup banner and via `sc serve fingerprint []` (which mints the identity if missing), so an operator can distribute the pin out-of-band before a From 363729189afd2b609a83849640b931cf04c0f070 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 10 Jul 2026 16:27:08 +0300 Subject: [PATCH 13/14] docs(adr): annotate ADR-0040's bind gate with the P32 narrowing (final-review minor) Claude-Session: https://claude.ai/code/session_01LhadyW9scQL95h3ag9yySB --- docs/adr/0040-sc-http-access-control.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/adr/0040-sc-http-access-control.md b/docs/adr/0040-sc-http-access-control.md index 47c207a4..bc76aa7e 100644 --- a/docs/adr/0040-sc-http-access-control.md +++ b/docs/adr/0040-sc-http-access-control.md @@ -24,7 +24,8 @@ Access control for `sc serve --http` composes three gates, dep-free (BLAKE3 is a dependency; constant-time compare is std-implementable — no TLS, no new crate): 1. **Fail-closed bind.** A non-loopback bind is refused unless justified by `--read-only`, - `--allow-public`, or ≥1 configured token; loopback always binds. + `--allow-public`, or ≥1 configured token; loopback always binds. *(Narrowed by P32: a + token justifies only together with `--tls` — see ADR-0042.)* 2. **Bearer-token auth at the HTTP opening.** When `.sc/serve-tokens.toml` holds ≥1 token, a valid `Authorization: Bearer ` is required on **every** connection (loopback included). The check sits at the opening — `read_client_opening` returns From c18a97d5b94f40fade43294afa435dff7348896e Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 10 Jul 2026 17:17:20 +0300 Subject: [PATCH 14/14] style: cargo fmt (CI fmt gate) Claude-Session: https://claude.ai/code/session_01LhadyW9scQL95h3ag9yySB --- crates/cli/src/main.rs | 5 ++++- crates/cli/tests/https_remote.rs | 3 +-- crates/repo/src/http_transport.rs | 15 +++++++++++---- crates/repo/src/tls_pins.rs | 10 ++++++++-- crates/tlsio/src/identity.rs | 10 ++++++++-- crates/tlsio/src/spki.rs | 10 ++++++++-- crates/tlsio/src/stream.rs | 15 +++++++++++---- 7 files changed, 51 insertions(+), 17 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 8adb400f..5d612b0e 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -862,7 +862,10 @@ fn main() -> Result<()> { Some(p) => p, None => std::env::current_dir()?, }; - println!("{}", scl_repo::http_transport::serve_tls_fingerprint(&root)?); + println!( + "{}", + scl_repo::http_transport::serve_tls_fingerprint(&root)? + ); Ok(()) } None => { diff --git a/crates/cli/tests/https_remote.rs b/crates/cli/tests/https_remote.rs index 6702939c..49e45f34 100644 --- a/crates/cli/tests/https_remote.rs +++ b/crates/cli/tests/https_remote.rs @@ -292,8 +292,7 @@ fn https_clone_push_fetch_round_trip_with_signed_chunked_blob() { // Zero .sc/tmp residue on every end. for repo in [&origin, &clone_dir, &clone2] { let tmp_dir = repo.join(".sc").join("tmp"); - let empty = - !tmp_dir.exists() || std::fs::read_dir(&tmp_dir).unwrap().next().is_none(); + let empty = !tmp_dir.exists() || std::fs::read_dir(&tmp_dir).unwrap().next().is_none(); assert!(empty, ".sc/tmp residue in {}", repo.display()); } diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index 0b3cbb2d..2565004f 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -794,7 +794,14 @@ pub fn serve_http( ); } std::io::Write::flush(&mut std::io::stdout()).ok(); - serve_http_listener(listener, root, read_only, mandatory_auth, limits, tls_config) + serve_http_listener( + listener, + root, + read_only, + mandatory_auth, + limits, + tls_config, + ) } /// Accept-loop core, factored out of [`serve_http`] so tests can bind @@ -2077,7 +2084,8 @@ mod tests { // A TLS client (raw tlsio, pinned) gets a 200 through the TLS channel. let tcp = std::net::TcpStream::connect(&addr).unwrap(); - let (stream, seen) = scl_tlsio::client_connect(tcp, "127.0.0.1", Some(spki), false).unwrap(); + let (stream, seen) = + scl_tlsio::client_connect(tcp, "127.0.0.1", Some(spki), false).unwrap(); assert_eq!(seen, spki); let (r, mut w) = stream.split(); write_client_opening(&mut w, "127.0.0.1", "/", None).unwrap(); @@ -2245,8 +2253,7 @@ mod tests { tls: true, }; let policy = crate::tls_pins::TlsClientPolicy { - known_hosts: std::env::temp_dir() - .join(format!("scl-verbs-kh-{}", std::process::id())), + known_hosts: std::env::temp_dir().join(format!("scl-verbs-kh-{}", std::process::id())), pre_pin: Some(spki), strict: false, }; diff --git a/crates/repo/src/tls_pins.rs b/crates/repo/src/tls_pins.rs index ba16ef39..84883679 100644 --- a/crates/repo/src/tls_pins.rs +++ b/crates/repo/src/tls_pins.rs @@ -65,7 +65,10 @@ pub fn default_known_hosts_path() -> Result { return Ok(PathBuf::from(xdg).join("sc").join("known_hosts")); } match std::env::var_os("HOME").filter(|v| !v.is_empty()) { - Some(home) => Ok(PathBuf::from(home).join(".config").join("sc").join("known_hosts")), + Some(home) => Ok(PathBuf::from(home) + .join(".config") + .join("sc") + .join("known_hosts")), None => Err(Error::InvalidArgument( "cannot resolve the sc+https pin file: set HOME, XDG_CONFIG_HOME, \ or SC_HTTPS_KNOWN_HOSTS" @@ -192,7 +195,10 @@ mod tests { fn parse_fingerprint_accepts_both_forms_and_rejects_junk() { let hexs = "ab".repeat(32); let expected = [0xabu8; 32]; - assert_eq!(parse_fingerprint(&format!("sha256:{hexs}")).unwrap(), expected); + assert_eq!( + parse_fingerprint(&format!("sha256:{hexs}")).unwrap(), + expected + ); assert_eq!(parse_fingerprint(&hexs).unwrap(), expected); assert!(parse_fingerprint("sha256:short").is_err()); assert!(parse_fingerprint("md5:abcd").is_err()); diff --git a/crates/tlsio/src/identity.rs b/crates/tlsio/src/identity.rs index 03d226a2..7f33e306 100644 --- a/crates/tlsio/src/identity.rs +++ b/crates/tlsio/src/identity.rs @@ -105,7 +105,10 @@ mod tests { #[cfg(unix)] { use std::os::unix::fs::PermissionsExt; - let mode = std::fs::metadata(dir.join("key.pem")).unwrap().permissions().mode(); + let mode = std::fs::metadata(dir.join("key.pem")) + .unwrap() + .permissions() + .mode(); assert_eq!(mode & 0o777, 0o600, "key.pem must be 0600"); } std::fs::remove_dir_all(&dir).unwrap(); @@ -118,7 +121,10 @@ mod tests { load_or_mint(&dir).unwrap(); std::fs::remove_file(dir.join("key.pem")).unwrap(); let err = load_or_mint(&dir).unwrap_err(); - assert!(err.to_string().contains("half a TLS identity"), "got: {err}"); + assert!( + err.to_string().contains("half a TLS identity"), + "got: {err}" + ); std::fs::remove_dir_all(&dir).unwrap(); assert!(!dir.exists()); } diff --git a/crates/tlsio/src/spki.rs b/crates/tlsio/src/spki.rs index 0f63ff63..fabb7bc3 100644 --- a/crates/tlsio/src/spki.rs +++ b/crates/tlsio/src/spki.rs @@ -92,7 +92,10 @@ mod tests { let params = rcgen::CertificateParams::new(vec!["sc-serve".to_string()]).unwrap(); let cert = params.self_signed(&key).unwrap(); let extracted = spki_der(cert.der()).unwrap(); - assert_eq!(extracted, rcgen::PublicKeyData::subject_public_key_info(&key).as_slice()); + assert_eq!( + extracted, + rcgen::PublicKeyData::subject_public_key_info(&key).as_slice() + ); } #[test] @@ -109,7 +112,10 @@ mod tests { // Different cert bytes, same key → same SPKI hash (the whole point // of pinning the SPKI, not the cert). assert_ne!(c1.der().as_ref(), c2.der().as_ref()); - assert_eq!(spki_sha256(c1.der()).unwrap(), spki_sha256(c2.der()).unwrap()); + assert_eq!( + spki_sha256(c1.der()).unwrap(), + spki_sha256(c2.der()).unwrap() + ); } #[test] diff --git a/crates/tlsio/src/stream.rs b/crates/tlsio/src/stream.rs index c23c4631..37f91d39 100644 --- a/crates/tlsio/src/stream.rs +++ b/crates/tlsio/src/stream.rs @@ -50,7 +50,9 @@ impl rustls::client::danger::ServerCertVerifier for PinVerifier { *self.seen.lock().unwrap() = Some(hash); match self.expected { Some(p) if p == hash => Ok(rustls::client::danger::ServerCertVerified::assertion()), - Some(_) => Err(rustls::Error::General("sc: pinned fingerprint mismatch".into())), + Some(_) => Err(rustls::Error::General( + "sc: pinned fingerprint mismatch".into(), + )), None if self.strict => Err(rustls::Error::General("sc: unknown host (strict)".into())), None => Ok(rustls::client::danger::ServerCertVerified::assertion()), } @@ -179,7 +181,9 @@ pub fn client_connect( if expected_pin.is_none() && strict && seen.is_some() { return Err(Error::UnknownHostStrict); } - return Err(Error::Handshake(format!("client handshake with {host}: {e}"))); + return Err(Error::Handshake(format!( + "client handshake with {host}: {e}" + ))); } } let seen = verifier @@ -287,7 +291,9 @@ mod tests { let (tcp, _) = listener.accept().unwrap(); // A client that aborts its handshake (mismatch/strict tests) // surfaces as Err here — that's fine, just return. - let Ok(stream) = server_stream(&cfg, tcp) else { return }; + let Ok(stream) = server_stream(&cfg, tcp) else { + return; + }; let (mut r, mut w) = stream.split(); let mut buf = [0u8; 5]; if r.read_exact(&mut buf).is_err() { @@ -370,7 +376,8 @@ mod tests { // (covered by echo_server's `else return`), and the client just sees // a dead/garbled connection — no hang. tcp.write_all(b"POST / HTTP/1.1\r\n\r\n").ok(); - tcp.set_read_timeout(Some(std::time::Duration::from_secs(5))).unwrap(); + tcp.set_read_timeout(Some(std::time::Duration::from_secs(5))) + .unwrap(); let mut buf = [0u8; 16]; // Read either 0 (close) or a TLS alert — anything but a hang. let _ = tcp.read(&mut buf);