From a5399357012aa53e01ed37a10cf528bdcf69b4f4 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:30:41 +0200 Subject: [PATCH 01/14] fix(forge): only receive-pack registers as a writer, not a dumb-protocol GET --- crates/forge/src/cgi.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/forge/src/cgi.rs b/crates/forge/src/cgi.rs index 4d10c641..03880c4e 100644 --- a/crates/forge/src/cgi.rs +++ b/crates/forge/src/cgi.rs @@ -510,6 +510,12 @@ fn environment(l: &Launch, marker: &std::path::Path) -> Vec<(String, String)> { env } +/// Only a receive-pack run is a writer. A dumb-protocol GET has no service and needs only Read, so +/// counting it would let a slow read-only client stall `PATCH default_branch` and lock cleanup. +fn registers_as_writer(service: Option) -> bool { + service == Some(Service::Receive) +} + fn launch(l: Launch) -> Result { let repo_id = l.plan.access.repo.id; let cfg = l.app.transports.cfg.clone(); @@ -520,8 +526,7 @@ fn launch(l: Launch) -> Result { .registry .register( repo_id, - // Only a fetch is known to write no lock and no quarantine directory. - l.plan.service != Some(Service::Upload), + registers_as_writer(l.plan.service), || matches!(repos::find_by_id(&conn, repo_id), Ok(Some(r)) if r.state == "live"), ) .ok_or_else(|| { @@ -1051,3 +1056,15 @@ pub async fn handle(app: Arc, req: Req, body: Incoming, path: GitPath) -> R pub fn answer(r: crate::http::Resp) -> Response { buffered(r) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn only_receive_pack_registers_as_a_writer() { + assert!(registers_as_writer(Some(Service::Receive))); + assert!(!registers_as_writer(Some(Service::Upload))); + assert!(!registers_as_writer(None), "a dumb GET is a reader"); + } +} From 64a6db864d1a38aab4da081d1ad6da75a461ef05 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:32:41 +0200 Subject: [PATCH 02/14] fix(forge): bound the wait for a password-verification slot, answer 503 --- crates/forge/src/auth.rs | 11 ++++++++++- crates/forge/tests/e2e_a.rs | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/crates/forge/src/auth.rs b/crates/forge/src/auth.rs index 2ea56dd1..8f1223cd 100644 --- a/crates/forge/src/auth.rs +++ b/crates/forge/src/auth.rs @@ -127,6 +127,9 @@ pub fn by_token(app: &App, conn: &Connection, cred: &Cred, now: i64) -> Result Resu } }; let ok = { - let _slot = app.gate.acquire(); + // Bounded: an unbounded wait would pin a blocking-pool thread per queued login. + let Some(_slot) = app.gate.acquire_timeout(VERIFY_WAIT) else { + // Our own load says nothing about the credential: give the reservation back. + app.backoff.cancel(req.source); + return Err(error(&app.base, 503, "too many password checks in flight, try again") + .with("retry-after", "5".into())); + }; verify_password(pass, found.as_ref().map(|u| u.password_hash.as_str())) }; match (ok, found) { diff --git a/crates/forge/tests/e2e_a.rs b/crates/forge/tests/e2e_a.rs index 2502c1b1..dc74364a 100644 --- a/crates/forge/tests/e2e_a.rs +++ b/crates/forge/tests/e2e_a.rs @@ -1224,3 +1224,19 @@ fn serve_refuses_unusable_settings_at_startup() { } let _ = std::fs::remove_dir_all(&dir); } + +#[test] +fn a_busy_password_gate_answers_503_and_refunds_the_attempt() { + let s = Srv::start(); + let pw = s.mk_user("alice"); + let body = json!({"name": "t", "scopes": ["all"]}); + // Hold every Argon2 slot (the harness builds the app with 4): the check must give up, not queue. + let held: Vec<_> = (0..4).map(|_| s.app.gate.acquire()).collect(); + let r = s.post("/api/v1/users/alice/tokens", &basic("alice", &pw), body.clone()); + assert_eq!(r.status, 503, "{}", r.msg()); + assert_eq!(r.headers["retry-after"], "5"); + drop(held); + // The refused attempt left no failure behind and returned its source reservation. + let r = s.post("/api/v1/users/alice/tokens", &basic("alice", &pw), body); + assert_eq!(r.status, 201, "{}", r.msg()); +} From 7bdcfd05ad4250bc8ea07041cefcf6fa8bfc5ede Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:44:30 +0200 Subject: [PATCH 03/14] fix(forge): read SC_FORGE_MERGE_PAUSE_MS in debug builds only --- crates/forge/src/merges.rs | 5 +++-- crates/forge/tests/e2e_crash.rs | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/forge/src/merges.rs b/crates/forge/src/merges.rs index b510cbf2..3dda901e 100644 --- a/crates/forge/src/merges.rs +++ b/crates/forge/src/merges.rs @@ -470,9 +470,10 @@ fn publish(git: &Git, dir: &Path, i: &Intent) -> Result { } /// Test-only knob: `SC_FORGE_MERGE_PAUSE_MS` stretches the two crash windows of a merge (after the -/// journal row, and after the base moved) so a test can SIGKILL the server inside them. Unset in -/// production. +/// journal row, and after the base moved) so a test can SIGKILL the server inside them. Compiled +/// into debug builds only, so a release server never reads it. fn pause_for_tests() { + #[cfg(debug_assertions)] if let Some(ms) = std::env::var("SC_FORGE_MERGE_PAUSE_MS") .ok() .and_then(|v| v.parse::().ok()) diff --git a/crates/forge/tests/e2e_crash.rs b/crates/forge/tests/e2e_crash.rs index 8c1a23d5..3f681e27 100644 --- a/crates/forge/tests/e2e_crash.rs +++ b/crates/forge/tests/e2e_crash.rs @@ -3,7 +3,7 @@ //! reconciler must leave the forge consistent: no pending intent, no receipt ref, and every pull //! request either wholly merged (its merge commit on the base, two parents) or wholly open (its //! head not on the base). -#![cfg(unix)] +#![cfg(all(unix, debug_assertions))] // the merge pause knob is compiled into debug builds only use serde_json::{json, Value}; use std::io::{Read, Write}; From 745293fd5067ca32c89f906c6c7e7da836308378 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:44:30 +0200 Subject: [PATCH 04/14] fix(forge): parse ip:port forwarded-for entries and stop the walk at an unparsable one --- crates/forge/src/limits.rs | 58 ++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/crates/forge/src/limits.rs b/crates/forge/src/limits.rs index bdc201e9..411257d7 100644 --- a/crates/forge/src/limits.rs +++ b/crates/forge/src/limits.rs @@ -253,14 +253,29 @@ impl Proxies { return peer; } // Walk from the right: the first address that is not one of our own - // trusted proxies is the client as the outermost proxy saw it. - v.rsplit(',') - .filter_map(|s| s.trim().parse::().ok()) - .find(|a| !self.cidrs.iter().any(|c| c.contains(*a))) - .unwrap_or(peer) + // trusted proxies is the client as the outermost proxy saw it. An entry that + // does not parse is client-controlled text: stop there and key on the last + // trusted hop, never on anything to its left. + let mut hop = peer; + for entry in v.rsplit(',') { + match hop_addr(entry.trim()) { + None => return hop, + Some(a) if self.cidrs.iter().any(|c| c.contains(a)) => hop = a, + Some(a) => return a, + } + } + peer } } +/// An address as a proxy may append it: `ip`, `ip:port`, `[v6]` or `[v6]:port`. +fn hop_addr(s: &str) -> Option { + s.parse::() + .ok() + .or_else(|| s.parse::().ok().map(|a| a.ip())) + .or_else(|| s.strip_prefix('[')?.strip_suffix(']')?.parse().ok()) +} + #[cfg(test)] mod tests { use super::*; @@ -396,6 +411,39 @@ mod tests { ); } + #[test] + fn forwarded_for_ports_are_parsed_and_junk_stops_the_walk() { + let p = Proxies { + header: Some("x-forwarded-for".into()), + cidrs: vec![Cidr::parse("127.0.0.1/32").unwrap()], + }; + let peer = ip("127.0.0.1"); + // a proxy that appends ip:port, in either family + assert_eq!( + p.resolve(peer, Some("6.6.6.6, 5.5.5.5:4711")), + ip("5.5.5.5") + ); + assert_eq!( + p.resolve(peer, Some("6.6.6.6, [2001:db8::5]:80")), + ip("2001:db8::5") + ); + assert_eq!( + p.resolve(peer, Some("6.6.6.6, [2001:db8::5]")), + ip("2001:db8::5") + ); + assert_eq!( + p.resolve(peer, Some("6.6.6.6, 127.0.0.1:9, 5.5.5.5:1")), + ip("5.5.5.5") + ); + // an unparsable entry no longer lets the walk fall through to a client-chosen one + assert_eq!(p.resolve(peer, Some("6.6.6.6, unknown")), peer); + assert_eq!( + p.resolve(peer, Some("6.6.6.6, 5.5.5.5, unknown, 127.0.0.1")), + peer + ); + assert_eq!(p.resolve(peer, Some("6.6.6.6, junk, 127.0.0.1")), peer); + } + #[test] fn gate_acquire_times_out_instead_of_queueing() { let g = Gate::new(1); From fdd3f8fb4db75de491a563cbe519ea1276a8c2cc Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:44:37 +0200 Subject: [PATCH 05/14] style(forge): cargo fmt the password gate refusal --- crates/forge/src/auth.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/forge/src/auth.rs b/crates/forge/src/auth.rs index 8f1223cd..23e9e6b0 100644 --- a/crates/forge/src/auth.rs +++ b/crates/forge/src/auth.rs @@ -162,8 +162,12 @@ pub fn by_password(app: &App, conn: &Connection, req: &Req, cred: &Cred) -> Resu let Some(_slot) = app.gate.acquire_timeout(VERIFY_WAIT) else { // Our own load says nothing about the credential: give the reservation back. app.backoff.cancel(req.source); - return Err(error(&app.base, 503, "too many password checks in flight, try again") - .with("retry-after", "5".into())); + return Err(error( + &app.base, + 503, + "too many password checks in flight, try again", + ) + .with("retry-after", "5".into())); }; verify_password(pass, found.as_ref().map(|u| u.password_hash.as_str())) }; From 044715284f4513c489ac4922d81a77cf1dc338d3 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:58:45 +0200 Subject: [PATCH 06/14] fix(forge): a private organization the caller cannot see answers like a missing one --- crates/forge/conformance/departures.json | 39 ++++++++++++++++++- crates/forge/src/api.rs | 6 ++- crates/forge/src/repo_api.rs | 35 ++++++++++++----- crates/forge/tests/e2e_a.rs | 48 +++++++++++++++++++----- crates/forge/tests/e2e_repos.rs | 20 ++++++---- 5 files changed, 118 insertions(+), 30 deletions(-) diff --git a/crates/forge/conformance/departures.json b/crates/forge/conformance/departures.json index 7cf1a679..823909dd 100644 --- a/crates/forge/conformance/departures.json +++ b/crates/forge/conformance/departures.json @@ -1748,11 +1748,16 @@ "kind": "http", "match": "GET /api/v1/orgs/acme/repos", "expect": { + "status": 404, + "message_contains": [ + "getorgbyname" + ], + "ignore_body": true, "ignore_headers": [ "x-total-count" ] }, - "reason": "Gitea leaks all 11 organization repositories in X-Total-Count even when the body is access-filtered. The complete body stays compared; e2e_repos::listings_are_filtered_by_access_with_visible_counts checks visible-count filtering for a read collaborator and an outsider." + "reason": "sc-forge answers a caller who cannot see a private organization and can read nothing in it like a missing organization (404 GetOrgByName); Gitea answers 200 with an empty list [ignore_body: the recorded 200 body has another shape than the departed answer]. K#56 (a collaborator) still lists what it can read." }, { "scenario": "K", @@ -1765,5 +1770,37 @@ ] }, "reason": "Gitea leaks all 11 organization repositories in X-Total-Count even when the body is access-filtered. The complete body stays compared; e2e_repos::listings_are_filtered_by_access_with_visible_counts checks visible-count filtering for a read collaborator and an outsider." + }, + { + "scenario": "B", + "exchange": 32, + "kind": "http", + "match": "GET /api/v1/orgs/acme", + "expect": { + "status": 404, + "message_contains": [ + "getorgbyname" + ], + "ignore_fields": [ + "errors" + ] + }, + "reason": "sc-forge answers a private organization the caller cannot see exactly like a missing one (404 GetOrgByName); Gitea's 'HasOrgOrUserVisible' tells the two apart [ignore_fields: errors is the missing-organization list, not null]" + }, + { + "scenario": "K", + "exchange": 54, + "kind": "http", + "match": "GET /api/v1/orgs/acme", + "expect": { + "status": 404, + "message_contains": [ + "getorgbyname" + ], + "ignore_fields": [ + "errors" + ] + }, + "reason": "sc-forge answers a private organization the caller cannot see exactly like a missing one (404 GetOrgByName); Gitea's 'HasOrgOrUserVisible' tells the two apart [ignore_fields: errors is the missing-organization list, not null]" } ] diff --git a/crates/forge/src/api.rs b/crates/forge/src/api.rs index 1122b804..5cd46346 100644 --- a/crates/forge/src/api.rs +++ b/crates/forge/src/api.rs @@ -297,7 +297,9 @@ pub fn get_org(ctx: &mut Ctx) -> Resp { }; match org_visible(ctx, &org) { Ok(true) => Resp::json(200, &org_json(&ctx.app.base, &org)), - Ok(false) => not_found(&ctx.app.base, "HasOrgOrUserVisible", None), + // Not Gitea's `HasOrgOrUserVisible`: that text tells a private organization from a missing + // one (recorded departure). + Ok(false) => no_such_org(ctx, &name), Err(e) => ctx.app.internal(&e), } } @@ -326,7 +328,7 @@ pub fn org_permissions(ctx: &mut Ctx) -> Resp { // hidden like on `GET /users/{u}`. match org_visible(ctx, &org) { Ok(true) => {} - Ok(false) => return not_found(&ctx.app.base, "HasOrgOrUserVisible", None), + Ok(false) => return no_such_org(ctx, &oname), Err(e) => return ctx.app.internal(&e), } let full = match store::is_org_owner(ctx.conn, org.id, subject.id) { diff --git a/crates/forge/src/repo_api.rs b/crates/forge/src/repo_api.rs index c6b7c5c3..91fb653b 100644 --- a/crates/forge/src/repo_api.rs +++ b/crates/forge/src/repo_api.rs @@ -491,7 +491,7 @@ pub fn list_user_repos(ctx: &mut Ctx) -> Resp { query_list(ctx, &Filter::default(), Sort::Id, "/user/repos", false) } -/// `GET /orgs/{o}/repos`: 200 for any existing organization, listing only what the caller can read. +/// `GET /orgs/{o}/repos`: lists only what the caller can read; 404 like a missing organization when the caller cannot see a private one and can read nothing in it. pub fn list_org_repos(ctx: &mut Ctx) -> Resp { let name = ctx.param("o").to_string(); match store::find_by_login(ctx.conn, &name) { @@ -500,16 +500,31 @@ pub fn list_org_repos(ctx: &mut Ctx) -> Resp { // visible: a collaborator of a repository inside a private organization still sees it // (recorded K#55), and the counts are the visible count. let path = format!("/orgs/{}/repos", org.login); - query_list( - ctx, - &Filter { - owner_id: Some(org.id), - ..Default::default() + let filter = Filter { + owner_id: Some(org.id), + ..Default::default() + }; + // A caller who cannot see the organization and can read nothing in it gets what a + // missing organization gets (recorded departure from K#55); one who can read a + // repository there already knows it exists. + match crate::api::org_visible(ctx, &org) { + Ok(true) => {} + Ok(false) => match repos::list_visible( + ctx.conn, + &ctx.who.user, + token_public_only(ctx), + &filter, + Sort::Id, + 1, + 1, + ) { + Ok((_, 0)) => return crate::api::org_missing(ctx.app, &name), + Ok(_) => {} + Err(e) => return ctx.app.internal(&e), }, - Sort::Id, - &path, - false, - ) + Err(e) => return ctx.app.internal(&e), + } + query_list(ctx, &filter, Sort::Id, &path, false) } Ok(_) => crate::api::org_missing(ctx.app, &name), Err(e) => ctx.app.internal(&e), diff --git a/crates/forge/tests/e2e_a.rs b/crates/forge/tests/e2e_a.rs index dc74364a..085f7279 100644 --- a/crates/forge/tests/e2e_a.rs +++ b/crates/forge/tests/e2e_a.rs @@ -151,13 +151,18 @@ fn users_orgs_and_visibility() { nf.json()["errors"], json!(["user redirect does not exist [name: nobody-org]"]) ); - // Private org: owner and admin see it, others get the recorded 404. + // Private org: owner and admin see it, others get exactly what a missing organization gets + // (a recorded departure from Gitea's `HasOrgOrUserVisible`, which told the two apart). let hidden = s.get("/api/v1/orgs/acme", &token(&bob)); assert_eq!( (hidden.status, hidden.msg().as_str()), - (404, "HasOrgOrUserVisible") + (404, "GetOrgByName") + ); + let missing = s.get("/api/v1/orgs/acmf", &token(&bob)); + assert_eq!( + (hidden.status, hidden.s_text().replace("acme", "")), + (missing.status, missing.s_text().replace("acmf", "")) ); - assert_eq!(hidden.json()["errors"], serde_json::Value::Null); let perms = s.get("/api/v1/users/siteadmin/orgs/acme/permissions", &adm); assert_eq!( perms.json(), @@ -963,11 +968,32 @@ fn private_org_permissions_are_hidden_from_outsiders() { "/api/v1/users/siteadmin/orgs/secret/permissions", &token(&eve), ); - assert_eq!( - (r.status, r.msg().as_str()), - (404, "HasOrgOrUserVisible"), - "no membership oracle for outsiders" - ); + // An outsider cannot tell a private organization from a missing one on any org route. + for (hidden, missing) in [ + ( + "/api/v1/users/siteadmin/orgs/secret/permissions", + "/api/v1/users/siteadmin/orgs/secrex/permissions", + ), + ("/api/v1/orgs/secret", "/api/v1/orgs/secrex"), + ("/api/v1/orgs/secret/repos", "/api/v1/orgs/secrex/repos"), + ] { + let (h, m) = (s.get(hidden, &token(&eve)), s.get(missing, &token(&eve))); + assert_eq!(h.status, 404, "{hidden}"); + assert_eq!( + ( + h.status, + h.s_text().replace("secret", ""), + h.header("content-type") + ), + ( + m.status, + m.s_text().replace("secrex", ""), + m.header("content-type") + ), + "{hidden}" + ); + } + assert_eq!(r.status, 404, "no membership oracle for outsiders"); assert_eq!( s.get("/api/v1/users/siteadmin/orgs/secret/permissions", &adm) .status, @@ -1232,7 +1258,11 @@ fn a_busy_password_gate_answers_503_and_refunds_the_attempt() { let body = json!({"name": "t", "scopes": ["all"]}); // Hold every Argon2 slot (the harness builds the app with 4): the check must give up, not queue. let held: Vec<_> = (0..4).map(|_| s.app.gate.acquire()).collect(); - let r = s.post("/api/v1/users/alice/tokens", &basic("alice", &pw), body.clone()); + let r = s.post( + "/api/v1/users/alice/tokens", + &basic("alice", &pw), + body.clone(), + ); assert_eq!(r.status, 503, "{}", r.msg()); assert_eq!(r.headers["retry-after"], "5"); drop(held); diff --git a/crates/forge/tests/e2e_repos.rs b/crates/forge/tests/e2e_repos.rs index 6379f725..88697cf6 100644 --- a/crates/forge/tests/e2e_repos.rs +++ b/crates/forge/tests/e2e_repos.rs @@ -455,11 +455,9 @@ fn a_public_only_token_sees_and_creates_nothing() { ); let r = w.s.get("/api/v1/repos/acme/ghost", &po); assert_eq!(r.status, 403, "refused before existence is looked at"); - for path in [ - "/api/v1/repos/search?limit=50", - "/api/v1/user/repos", - "/api/v1/orgs/acme/repos", - ] { + // acme is private and `pat` is no member: with nothing readable it is a missing organization + assert_eq!(w.s.get("/api/v1/orgs/acme/repos", &po).status, 404); + for path in ["/api/v1/repos/search?limit=50", "/api/v1/user/repos"] { let l = w.s.get(path, &po); assert_eq!(l.status, 200, "{path}"); assert_eq!(l.headers["x-total-count"], "0", "{path}"); @@ -508,14 +506,20 @@ fn listings_are_filtered_by_access_with_visible_counts() { list(&t["bob"], "/api/v1/user/repos"), (vec!["acme/a2".to_string()], "1".to_string()) ); - // an invisible org still answers 200 with only what the caller can read (recorded K#55) + // an invisible org still answers 200 with what the caller can read, once there is any (recorded K#55) assert_eq!( list(&t["bob"], "/api/v1/orgs/acme/repos"), (vec!["acme/a2".to_string()], "1".to_string()) ); + // dave can read nothing in acme and cannot see it: the answer is a missing organization's + let (hidden, missing) = ( + w.s.get("/api/v1/orgs/acme/repos", &token(&t["dave"])), + w.s.get("/api/v1/orgs/acmf/repos", &token(&t["dave"])), + ); + assert_eq!(hidden.status, 404); assert_eq!( - list(&t["dave"], "/api/v1/orgs/acme/repos"), - (vec![], "0".to_string()) + (hidden.status, hidden.s_text().replace("acme", "")), + (missing.status, missing.s_text().replace("acmf", "")) ); assert_eq!( w.s.get("/api/v1/orgs/ghost/repos", &token(&t["bob"])) From f08ebe603e9ede80f4702ed57c2d299e2e188fc1 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:58:45 +0200 Subject: [PATCH 07/14] feat(forge): admin user create and change-password take --password-stdin (#337) --- crates/forge/src/cli.rs | 44 +++++++++++++++++++- crates/forge/tests/e2e_a.rs | 81 +++++++++++++++++++++++++++++++++++++ docs/FORGE-OPERATIONS.md | 8 ++++ 3 files changed, 131 insertions(+), 2 deletions(-) diff --git a/crates/forge/src/cli.rs b/crates/forge/src/cli.rs index 8ab51266..aa3c45fb 100644 --- a/crates/forge/src/cli.rs +++ b/crates/forge/src/cli.rs @@ -3,6 +3,7 @@ use clap::{Args, Parser, Subcommand}; use scl_forge::limits::{Cidr, Proxies}; use scl_forge::server::{bind_gate, make_app, serve, Limits}; use scl_forge::{admin, db}; +use std::io::BufRead; use std::net::SocketAddr; use std::path::PathBuf; use std::time::Duration; @@ -148,8 +149,16 @@ enum UserCmd { Create { #[arg(long)] username: String, + /// Visible in the process list and shell history; prefer `--password-stdin`. + #[arg( + long, + required_unless_present = "password_stdin", + conflicts_with = "password_stdin" + )] + password: Option, + /// Read the password from one line of standard input. #[arg(long)] - password: String, + password_stdin: bool, #[arg(long)] email: String, #[arg(long)] @@ -163,8 +172,16 @@ enum UserCmd { ChangePassword { #[arg(long)] username: String, + /// Visible in the process list and shell history; prefer `--password-stdin`. + #[arg( + long, + required_unless_present = "password_stdin", + conflicts_with = "password_stdin" + )] + password: Option, + /// Read the password from one line of standard input. #[arg(long)] - password: String, + password_stdin: bool, #[arg(long, num_args = 0..=1, default_missing_value = "true", default_value = "true", action = clap::ArgAction::Set)] must_change_password: bool, #[arg(long)] @@ -218,6 +235,25 @@ pub fn run() -> Result<()> { } } +/// The password from `--password`, or one line of stdin for `--password-stdin` (the trailing +/// newline is stripped; an empty line is refused). +fn password_arg(arg: Option, from_stdin: bool) -> Result { + if !from_stdin { + return arg.context("--password or --password-stdin is required"); + } + let mut line = String::new(); + std::io::stdin() + .lock() + .read_line(&mut line) + .context("read the password from stdin")?; + let pw = line.strip_suffix('\n').unwrap_or(&line); + let pw = pw.strip_suffix('\r').unwrap_or(pw); + if pw.is_empty() { + bail!("the password read from stdin is empty"); + } + Ok(pw.to_string()) +} + fn run_admin(cmd: AdminCmd) -> Result<()> { match cmd { AdminCmd::User { @@ -225,12 +261,14 @@ fn run_admin(cmd: AdminCmd) -> Result<()> { UserCmd::Create { username, password, + password_stdin, email, admin: a, must_change_password, data_dir, }, } => { + let password = password_arg(password, password_stdin)?; admin::user_create( &data_dir, &username, @@ -246,10 +284,12 @@ fn run_admin(cmd: AdminCmd) -> Result<()> { UserCmd::ChangePassword { username, password, + password_stdin, must_change_password, data_dir, }, } => { + let password = password_arg(password, password_stdin)?; admin::user_change_password(&data_dir, &username, &password, must_change_password)?; println!("{username}'s password has been successfully updated!"); } diff --git a/crates/forge/tests/e2e_a.rs b/crates/forge/tests/e2e_a.rs index 085f7279..ad372691 100644 --- a/crates/forge/tests/e2e_a.rs +++ b/crates/forge/tests/e2e_a.rs @@ -1270,3 +1270,84 @@ fn a_busy_password_gate_answers_503_and_refunds_the_attempt() { let r = s.post("/api/v1/users/alice/tokens", &basic("alice", &pw), body); assert_eq!(r.status, 201, "{}", r.msg()); } + +#[test] +fn admin_passwords_can_come_from_stdin_but_not_both_ways() { + use std::io::Write; + let dir = std::env::temp_dir().join(format!("scl-forge-stdin-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let run = |args: &[&str], stdin: &str| { + let mut c = std::process::Command::new(env!("CARGO_BIN_EXE_sc-forge")) + .args(args) + .arg("--data-dir") + .arg(&dir) + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + c.stdin.take().unwrap().write_all(stdin.as_bytes()).unwrap(); + let o = c.wait_with_output().unwrap(); + ( + o.status.success(), + String::from_utf8_lossy(&o.stderr).to_string(), + ) + }; + let create = |extra: &[&str], stdin: &str| { + let mut a = vec![ + "admin", + "user", + "create", + "--username", + "eng", + "--email", + "e@example.invalid", + ]; + a.extend_from_slice(extra); + run(&a, stdin) + }; + // The trailing newline is not part of the password; an empty line is refused. + assert!(!create(&["--password-stdin"], "\n").0, "empty password"); + assert!(!create(&["--password-stdin"], "").0, "no line at all"); + let (ok, err) = create( + &["--password-stdin", "--must-change-password=false"], + "stdin-pw-1\r\n", + ); + assert!(ok, "{err}"); + let (ok, err) = create(&["--password", "x", "--password-stdin"], "y\n"); + assert!( + !ok && err.contains("cannot be used with"), + "exclusive: {err}" + ); + let (ok, err) = create(&[], "y\n"); + assert!( + !ok && err.contains("--password"), + "one of them is required: {err}" + ); + let (ok, err) = run( + &[ + "admin", + "user", + "change-password", + "--username", + "eng", + "--password-stdin", + "--must-change-password=false", + ], + "stdin-pw-2\n", + ); + assert!(ok, "{err}"); + let hash_of = |pw: &str| { + let conn = scl_forge::db::open_for_admin(&dir).unwrap(); + let u = scl_forge::store::find_by_login(&conn, "eng") + .unwrap() + .unwrap(); + scl_crypto::verify_password(pw, Some(&u.password_hash)) + }; + assert!( + hash_of("stdin-pw-2"), + "the stdin password was stored, without its newline" + ); + assert!(!hash_of("stdin-pw-1")); + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/docs/FORGE-OPERATIONS.md b/docs/FORGE-OPERATIONS.md index 5874d12e..2b0afb64 100644 --- a/docs/FORGE-OPERATIONS.md +++ b/docs/FORGE-OPERATIONS.md @@ -22,6 +22,14 @@ sc-forge admin user create --data-dir /data --username --password - sc-forge admin user generate-access-token --data-dir /data --username --token-name bootstrap --scopes all --raw ``` +`--password ` puts the password on the command line, where the process list and shell history show it. `admin user create` and +`admin user change-password` also take `--password-stdin` (one line on standard input, the trailing newline stripped, an empty line +refused; under `docker run` add `-i`), which cannot be combined with `--password`: + +```sh +printf '%s\n' "$ADMIN_PASSWORD" | sc-forge admin user create --data-dir /data --username --password-stdin --email --admin --must-change-password=false +``` + Migrating a repository from another forge: after pushing its branches and tags, retire its old pull request numbers so references to them answer 404 instead of naming a new pull request (works while the server runs): From 70feee254076a35c4ebdc2f089e79df7a1e283f8 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:58:45 +0200 Subject: [PATCH 08/14] docs(threat-model): record the forge hardening decisions --- docs/THREAT-MODEL.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index d3d0ab68..5d69c5a6 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -893,7 +893,11 @@ unless said otherwise). the start of the request: revoking a token or a collaborator does not cancel a push already running (it is bounded by `--git-timeout`, 30 minutes by default). - **Existence oracles.** A repository the caller may not see, one that does not exist, and one being deleted give the - same 404 body on both services (`missing_and_invisible_repositories_answer_identically`); a token restricted to + same 404 body on both services (`missing_and_invisible_repositories_answer_identically`); a private organization the + caller cannot see answers like a missing one on `GET /orgs/{o}`, `GET /users/{u}/orgs/{o}/permissions` and + `GET /orgs/{o}/repos` (the last only while the caller can read nothing in it; a collaborator of a repository there still + lists it), where Gitea 1.22 answers `HasOrgOrUserVisible` or 200 and so confirms the name (recorded departures for + B#32, K#54 and K#55; `private_org_permissions_are_hidden_from_outsiders`); a token restricted to public repositories is refused before the repository is looked up, and a read-only collaborator is refused at the `receive-pack` advertisement (`scopes_and_permissions_gate_each_service`). - **Tenant isolation.** The directory a transport can reach is built from the integer repository id @@ -914,7 +918,9 @@ unless said otherwise). `the_hook_binary_fails_closed`). Known gap: NFC/NFD-equivalent spellings of a non-ASCII default branch are not folded (a macOS-filesystem quirk; marked `shortcut:` in `policy.rs`). A push in flight read the default branch when it started, so `PATCH default_branch` commits only while no push has registered on the repository since it checked the - branch, else 503 (`a_push_in_flight_keeps_the_default_branch_off_a_branch_it_deletes`, #316). + branch, else 503 (`a_push_in_flight_keeps_the_default_branch_off_a_branch_it_deletes`, #316). Only a `receive-pack` + run registers as a writer: a fetch or a dumb-protocol GET (read permission, no service) does not, so a slow reader can + neither stall that change nor hold off run-time lock cleanup (`cgi::tests::only_receive_pack_registers_as_a_writer`). - **Hook integrity.** `core.hooksPath` is pinned on every spawn to `/hooks`, whose `pre-receive` is a symlink to the `sc-forge` binary, re-pointed at every start; repositories are created with `--template=` and carry no hooks of their own. The hook reads the database through a read-only opener (no create, `query_only`, schema version checked), @@ -937,6 +943,15 @@ unless said otherwise). (`gzip_request_bodies_are_inflated_by_the_server_under_a_cap`; other encodings and gzip on a push are 415). Server memory is bounded per transport by about twice the cap while a gzip body is inflated (the buffer plus its chunked copy) and a few fixed channel buffers otherwise; with `--max-transports` 64 the worst case is the product. +- **Password verification.** Argon2 checks run under a global gate (`--argon2-concurrency`); a check waits at most 2 s for a + slot, then answers 503 + `Retry-After: 5` and hands its per-source attempt back, so a burst of logins cannot pin the + blocking pool or count against honest sources (`a_busy_password_gate_answers_503_and_refunds_the_attempt`). +- **Client address behind a proxy.** The forwarded-for header is read only from a trusted peer, from the right: addresses + of our own proxies are skipped, entries may carry a port (`ip:port`, `[v6]:port`), and the first entry that does not + parse ends the walk on the last trusted hop instead of falling through to text the client chose + (`limits::tests::forwarded_for_ports_are_parsed_and_junk_stops_the_walk`). +- **Decided and unchanged.** Repository scopes are not required on `/user/repos` and `/orgs/{o}/repos` (Gitea's requirement is + unrecorded, see #316). Access tokens never expire and survive a password change (Gitea 1.22 parity); revoke tokens by hand. - **Cache policy.** Everything served is authenticated, so nothing may be cached publicly: git's `public, max-age=31536000` on loose objects and packs is replaced with Gitea's private policy and its `Expires` dropped, and RPC results and 401s carry the same private policy (`authenticated_object_reads_are_never_publicly_cacheable`). From 154176942b14d74c94b46a660bea084edbf0bbc8 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 10:09:29 +0200 Subject: [PATCH 09/14] test(forge): record the private-organization departures with ignore_body --- crates/forge/conformance/departures.json | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/crates/forge/conformance/departures.json b/crates/forge/conformance/departures.json index 823909dd..55029fd2 100644 --- a/crates/forge/conformance/departures.json +++ b/crates/forge/conformance/departures.json @@ -1781,11 +1781,9 @@ "message_contains": [ "getorgbyname" ], - "ignore_fields": [ - "errors" - ] + "ignore_body": true }, - "reason": "sc-forge answers a private organization the caller cannot see exactly like a missing one (404 GetOrgByName); Gitea's 'HasOrgOrUserVisible' tells the two apart [ignore_fields: errors is the missing-organization list, not null]" + "reason": "sc-forge answers a private organization the caller cannot see exactly like a missing one (404 GetOrgByName); Gitea's 'HasOrgOrUserVisible' tells the two apart [ignore_body: `errors` is the missing-organization list, the recorded one is null]" }, { "scenario": "K", @@ -1797,10 +1795,8 @@ "message_contains": [ "getorgbyname" ], - "ignore_fields": [ - "errors" - ] + "ignore_body": true }, - "reason": "sc-forge answers a private organization the caller cannot see exactly like a missing one (404 GetOrgByName); Gitea's 'HasOrgOrUserVisible' tells the two apart [ignore_fields: errors is the missing-organization list, not null]" + "reason": "sc-forge answers a private organization the caller cannot see exactly like a missing one (404 GetOrgByName); Gitea's 'HasOrgOrUserVisible' tells the two apart [ignore_body: `errors` is the missing-organization list, the recorded one is null]" } ] From b5841d4a989042befaf58310adabecf30ae005fd Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 10:40:23 +0200 Subject: [PATCH 10/14] fix(forge): refund the exact reservation; resolve the client once from raw forwarded-for fields --- crates/forge/src/auth.rs | 12 +-- crates/forge/src/limits.rs | 208 ++++++++++++++++++++++++++---------- crates/forge/src/server.rs | 38 +++---- crates/forge/tests/e2e_a.rs | 6 +- 4 files changed, 178 insertions(+), 86 deletions(-) diff --git a/crates/forge/src/auth.rs b/crates/forge/src/auth.rs index 23e9e6b0..d02408da 100644 --- a/crates/forge/src/auth.rs +++ b/crates/forge/src/auth.rs @@ -142,26 +142,26 @@ pub fn by_password(app: &App, conn: &Connection, req: &Req, cred: &Cred) -> Resu ) .with("www-authenticate", "Basic realm=\"sc-forge\"".into())); }; - if !app.backoff.begin(req.source) { + let Some(slot) = app.backoff.begin(req.source) else { return Err(error( &app.base, 429, "too many failed password attempts from this address", ) .with("retry-after", "60".into())); - } + }; let found = match store::find_by_login(conn, login) { Ok(u) => u.filter(|u| !u.is_org), Err(e) => { - app.backoff.cancel(req.source); + app.backoff.cancel(&slot); return Err(app.internal(&e)); } }; let ok = { // Bounded: an unbounded wait would pin a blocking-pool thread per queued login. - let Some(_slot) = app.gate.acquire_timeout(VERIFY_WAIT) else { + let Some(_permit) = app.gate.acquire_timeout(VERIFY_WAIT) else { // Our own load says nothing about the credential: give the reservation back. - app.backoff.cancel(req.source); + app.backoff.cancel(&slot); return Err(error( &app.base, 503, @@ -173,7 +173,7 @@ pub fn by_password(app: &App, conn: &Connection, req: &Req, cred: &Cred) -> Resu }; match (ok, found) { (true, Some(user)) => { - app.backoff.success(login, req.source); + app.backoff.success(login, &slot); if user.must_change_password { return Err(error( &app.base, diff --git a/crates/forge/src/limits.rs b/crates/forge/src/limits.rs index 411257d7..7306a12f 100644 --- a/crates/forge/src/limits.rs +++ b/crates/forge/src/limits.rs @@ -74,7 +74,8 @@ pub struct Backoff { source_cap: usize, max_delay: Duration, by_login: Mutex>>, - by_source: Mutex>>, + by_source: Mutex>>, + next_id: std::sync::atomic::AtomicU64, } impl Default for Backoff { @@ -84,15 +85,40 @@ impl Default for Backoff { } } -fn prune(v: &mut Vec, window: Duration) { +fn prune(v: &mut Vec, window: Duration) { let now = Instant::now(); - v.retain(|t| now.duration_since(*t) < window); + v.retain(|t| now.duration_since(t.at()) < window); +} + +/// An entry of a failure list: when it was recorded. +trait Stamped { + fn at(&self) -> Instant; +} + +impl Stamped for Instant { + fn at(&self) -> Instant { + *self + } +} + +impl Stamped for (u64, Instant) { + fn at(&self) -> Instant { + self.1 + } +} + +/// One reserved password attempt of a source, returned by [`Backoff::begin`] and given back by +/// exactly this value, so a refund never erases another request's reservation. +#[derive(Debug)] +pub struct Reservation { + ip: IpAddr, + id: u64, } /// Keep a map bounded: drop expired entries, and if it is still over budget /// start over. shortcut: a distributed flood of more than MAX_ENTRIES live /// sources resets everyone's counters; move to an LRU if that is ever seen. -fn bound(m: &mut HashMap>, window: Duration) { +fn bound(m: &mut HashMap>, window: Duration) { if m.len() > MAX_ENTRIES { m.retain(|_, v| { prune(v, window); @@ -123,24 +149,39 @@ impl Backoff { max_delay, by_login: Mutex::default(), by_source: Mutex::default(), + next_id: std::sync::atomic::AtomicU64::new(0), } } - /// Reserve one password attempt for `ip`. `false` when the source has used + /// Reserve one password attempt for `ip`. `None` when the source has used /// its per-minute budget; the check and the reservation are one step, so /// concurrent requests cannot all slip under the cap. A failure keeps the - /// reservation as its record; [`Backoff::success`] gives it back. - pub fn begin(&self, ip: IpAddr) -> bool { + /// reservation as its record; [`Backoff::success`] and [`Backoff::cancel`] give it back. + pub fn begin(&self, ip: IpAddr) -> Option { let ip = source_key(ip); let mut m = self.by_source.lock().unwrap_or_else(|e| e.into_inner()); bound(&mut m, SOURCE_WINDOW); let v = m.entry(ip).or_default(); prune(v, SOURCE_WINDOW); if v.len() >= self.source_cap { - return false; + return None; + } + let id = self + .next_id + .fetch_add(1, std::sync::atomic::Ordering::Relaxed); + v.push((id, Instant::now())); + Some(Reservation { ip, id }) + } + + fn release(&self, r: &Reservation) { + if let Some(v) = self + .by_source + .lock() + .unwrap_or_else(|e| e.into_inner()) + .get_mut(&r.ip) + { + v.retain(|(id, _)| *id != r.id); } - v.push(Instant::now()); - true } /// Record a failed attempt; returns how long to delay the failure response @@ -157,35 +198,20 @@ impl Backoff { } /// Give back a reservation for an attempt that failed on our side (a database - /// error), which says nothing about the credential and must not count against - /// the source. - pub fn cancel(&self, ip: IpAddr) { - if let Some(v) = self - .by_source - .lock() - .unwrap_or_else(|e| e.into_inner()) - .get_mut(&source_key(ip)) - { - v.pop(); - } + /// error, a busy gate), which says nothing about the credential and must not + /// count against the source. + pub fn cancel(&self, r: &Reservation) { + self.release(r); } /// A success clears the login's failures and returns the source's reservation. - pub fn success(&self, login: &str, ip: IpAddr) { - let ip = source_key(ip); + pub fn success(&self, login: &str, r: &Reservation) { let key: String = login.to_lowercase().chars().take(MAX_KEY).collect(); self.by_login .lock() .unwrap_or_else(|e| e.into_inner()) .remove(&key); - if let Some(v) = self - .by_source - .lock() - .unwrap_or_else(|e| e.into_inner()) - .get_mut(&ip) - { - v.pop(); - } + self.release(r); } } @@ -248,8 +274,20 @@ impl Proxies { /// otherwise the peer. (A header from any other peer is ignored, so a /// client cannot spoof its address into the limiter.) pub fn resolve(&self, peer: IpAddr, header_value: Option<&str>) -> IpAddr { - let Some(v) = header_value else { return peer }; - if self.header.is_none() || !self.cidrs.iter().any(|c| c.contains(peer)) { + match header_value { + Some(v) => self.resolve_values(peer, &[v.as_bytes()]), + None => peer, + } + } + + /// [`Proxies::resolve`] over every field of a repeated header, in the order received, as raw + /// bytes: the fields join into one chain, and an entry that is not valid text is a stopping + /// point like any other unparsable one, never silently dropped. + pub fn resolve_values(&self, peer: IpAddr, values: &[&[u8]]) -> IpAddr { + if values.is_empty() + || self.header.is_none() + || !self.cidrs.iter().any(|c| c.contains(peer)) + { return peer; } // Walk from the right: the first address that is not one of our own @@ -257,8 +295,11 @@ impl Proxies { // does not parse is client-controlled text: stop there and key on the last // trusted hop, never on anything to its left. let mut hop = peer; - for entry in v.rsplit(',') { - match hop_addr(entry.trim()) { + for entry in values.iter().rev().flat_map(|v| v.rsplit(|b| *b == b',')) { + let addr = std::str::from_utf8(entry) + .ok() + .and_then(|s| hop_addr(s.trim())); + match addr { None => return hop, Some(a) if self.cidrs.iter().any(|c| c.contains(a)) => hop = a, Some(a) => return a, @@ -291,56 +332,74 @@ mod tests { .map(|_| b.failure("Alice", ip("1.1.1.1")).as_millis()) .collect(); assert_eq!(d, vec![50, 100, 200, 400, 800, 1600, 2000, 2000]); - b.success("alice", ip("1.1.1.1")); // case-insensitive key + let r = b.begin(ip("1.1.1.1")).unwrap(); + b.success("alice", &r); // case-insensitive key assert_eq!(b.failure("alice", ip("1.1.1.1")).as_millis(), 50); } #[test] fn the_source_budget_is_reserved_atomically_and_per_source() { let b = Backoff::new(3, Duration::ZERO); - assert!([ - b.begin(ip("9.9.9.9")), - b.begin(ip("9.9.9.9")), - b.begin(ip("9.9.9.9")) - ] - .iter() - .all(|x| *x)); + let held: Vec<_> = (0..3).map(|_| b.begin(ip("9.9.9.9"))).collect(); + assert!(held.iter().all(Option::is_some)); assert!( - !b.begin(ip("9.9.9.9")), + b.begin(ip("9.9.9.9")).is_none(), "the fourth attempt is refused before any verification ran" ); - assert!(b.begin(ip("8.8.8.8")), "other sources are unaffected"); - b.success("u", ip("9.9.9.9")); // a success gives its reservation back - assert!(b.begin(ip("9.9.9.9"))); + assert!( + b.begin(ip("8.8.8.8")).is_some(), + "other sources are unaffected" + ); + b.success("u", held[0].as_ref().unwrap()); // a success gives its reservation back + assert!(b.begin(ip("9.9.9.9")).is_some()); } #[test] fn a_cancelled_reservation_is_returned() { let b = Backoff::new(1, Duration::ZERO); - assert!(b.begin(ip("7.7.7.7"))); - assert!(!b.begin(ip("7.7.7.7"))); - b.cancel(ip("7.7.7.7")); - assert!(b.begin(ip("7.7.7.7"))); + let r = b.begin(ip("7.7.7.7")).unwrap(); + assert!(b.begin(ip("7.7.7.7")).is_none()); + b.cancel(&r); + assert!(b.begin(ip("7.7.7.7")).is_some()); + } + + #[test] + fn a_refund_gives_back_exactly_its_own_reservation() { + let b = Backoff::new(3, Duration::ZERO); + let first = b.begin(ip("6.6.6.6")).unwrap(); + let second = b.begin(ip("6.6.6.6")).unwrap(); // a later attempt that stays charged + b.cancel(&first); + let left: Vec = b.by_source.lock().unwrap()[&ip("6.6.6.6")] + .iter() + .map(|(id, _)| *id) + .collect(); + assert_eq!( + left, + vec![second.id], + "the newest reservation must survive an older refund" + ); + b.cancel(&first); // refunding twice changes nothing + assert_eq!(b.by_source.lock().unwrap()[&ip("6.6.6.6")].len(), 1); } #[test] fn one_ipv6_64_shares_a_budget() { let b = Backoff::new(2, Duration::ZERO); - assert!(b.begin(ip("2001:db8::1")) && b.begin(ip("2001:db8::2"))); - assert!(!b.begin(ip("2001:db8::ffff:1")), "same /64"); - assert!(b.begin(ip("2001:db8:0:1::1")), "a different /64"); + assert!(b.begin(ip("2001:db8::1")).is_some() && b.begin(ip("2001:db8::2")).is_some()); + assert!(b.begin(ip("2001:db8::ffff:1")).is_none(), "same /64"); + assert!(b.begin(ip("2001:db8:0:1::1")).is_some(), "a different /64"); } #[test] fn ipv4_mapped_addresses_keep_their_own_budget() { let b = Backoff::new(1, Duration::ZERO); - assert!(b.begin(ip("::ffff:1.2.3.4"))); + assert!(b.begin(ip("::ffff:1.2.3.4")).is_some()); assert!( - b.begin(ip("::ffff:5.6.7.8")), + b.begin(ip("::ffff:5.6.7.8")).is_some(), "a different IPv4 client is a different source" ); assert!( - !b.begin(ip("1.2.3.4")), + b.begin(ip("1.2.3.4")).is_none(), "mapped and plain forms of one client share a budget" ); } @@ -444,6 +503,41 @@ mod tests { assert_eq!(p.resolve(peer, Some("6.6.6.6, junk, 127.0.0.1")), peer); } + #[test] + fn repeated_and_non_utf8_forwarded_for_fields_are_one_ordered_chain() { + let p = Proxies { + header: Some("x-forwarded-for".into()), + cidrs: vec![Cidr::parse("127.0.0.1/32").unwrap()], + }; + let peer = ip("127.0.0.1"); + // repeated fields join in order: the last field is the rightmost hop + assert_eq!( + p.resolve_values(peer, &[b"6.6.6.6", b"5.5.5.5"]), + ip("5.5.5.5") + ); + assert_eq!( + p.resolve_values(peer, &[b"6.6.6.6, 5.5.5.5", b"127.0.0.1"]), + ip("5.5.5.5") + ); + // a later field that is not text stops the walk; the earlier entry is not reached + assert_eq!(p.resolve_values(peer, &[b"5.5.5.5", b"\xff\xfe"]), peer); + assert_eq!(p.resolve_values(peer, &[b"5.5.5.5", b"junk"]), peer); + assert_eq!( + p.resolve_values(peer, &[b"5.5.5.5, \xff", b"127.0.0.1"]), + peer + ); + // a non-text entry to the left of a good client address is never read + assert_eq!( + p.resolve_values(peer, &[b"\xff", b"5.5.5.5"]), + ip("5.5.5.5") + ); + assert_eq!( + p.resolve_values(ip("7.7.7.7"), &[b"5.5.5.5"]), + ip("7.7.7.7") + ); + assert_eq!(p.resolve_values(peer, &[]), peer); + } + #[test] fn gate_acquire_times_out_instead_of_queueing() { let g = Gate::new(1); diff --git a/crates/forge/src/server.rs b/crates/forge/src/server.rs index 62a36bc6..91a176b5 100644 --- a/crates/forge/src/server.rs +++ b/crates/forge/src/server.rs @@ -77,15 +77,21 @@ async fn handle( let started = std::time::Instant::now(); let method = req.method().as_str().to_string(); let path = req.uri().path().to_string(); - let source = app.proxies.resolve( - peer, - app.proxies - .header - .as_deref() - .and_then(|h| req.headers().get(h)) - .and_then(|v| v.to_str().ok()), - ); - let resp = handle_request(app, limits, peer, req).await; + // Resolved once, from every forwarded-for field in order and as raw bytes, and reused for the + // limiter and the access log: a malformed or repeated field must not change who they see. + let source = match app.proxies.header.as_deref() { + Some(h) => { + let values: Vec<&[u8]> = req + .headers() + .get_all(h) + .iter() + .map(|v| v.as_bytes()) + .collect(); + app.proxies.resolve_values(peer, &values) + } + None => peer, + }; + let resp = handle_request(app, limits, source, req).await; if let Ok(r) = &resp { let status = r.status().as_u16(); if !(status == 200 && matches!(path.as_str(), "/healthz" | "/api/healthz")) { @@ -108,7 +114,7 @@ async fn handle( async fn handle_request( app: Arc, limits: Limits, - peer: IpAddr, + source: IpAddr, req: Request, ) -> Result, std::convert::Infallible> { let (parts, body) = req.into_parts(); @@ -122,12 +128,6 @@ async fn handle_request( transport::Route::NotGit => {} transport::Route::Unknown => return Ok(cgi::answer(transport::not_found())), transport::Route::Git(g) => { - let source = app.proxies.resolve( - peer, - headers - .get(app.proxies.header.as_deref().unwrap_or("")) - .map(String::as_str), - ); let r = Req { method: parts.method.as_str().to_string(), path: parts.uri.path().to_string(), @@ -161,12 +161,6 @@ async fn handle_request( ))) } }; - let source = app.proxies.resolve( - peer, - headers - .get(app.proxies.header.as_deref().unwrap_or("")) - .map(String::as_str), - ); let r = Req { method: parts.method.as_str().to_string(), path: parts.uri.path().to_string(), diff --git a/crates/forge/tests/e2e_a.rs b/crates/forge/tests/e2e_a.rs index ad372691..2afcbfdc 100644 --- a/crates/forge/tests/e2e_a.rs +++ b/crates/forge/tests/e2e_a.rs @@ -1253,7 +1253,11 @@ fn serve_refuses_unusable_settings_at_startup() { #[test] fn a_busy_password_gate_answers_503_and_refunds_the_attempt() { - let s = Srv::start(); + // A budget of one attempt per source: without the refund the second request is a 429. + let s = Srv::start_backoff(scl_forge::limits::Backoff::new( + 1, + std::time::Duration::ZERO, + )); let pw = s.mk_user("alice"); let body = json!({"name": "t", "scopes": ["all"]}); // Hold every Argon2 slot (the harness builds the app with 4): the check must give up, not queue. From 713389d2eaf2821623c1c50a1e0cda16b7c88931 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 10:40:24 +0200 Subject: [PATCH 11/14] fix(forge): decide the hidden-organization 404 from the paginated listing --- crates/forge/src/repo_api.rs | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/crates/forge/src/repo_api.rs b/crates/forge/src/repo_api.rs index 91fb653b..86963ba5 100644 --- a/crates/forge/src/repo_api.rs +++ b/crates/forge/src/repo_api.rs @@ -506,25 +506,26 @@ pub fn list_org_repos(ctx: &mut Ctx) -> Resp { }; // A caller who cannot see the organization and can read nothing in it gets what a // missing organization gets (recorded departure from K#55); one who can read a - // repository there already knows it exists. - match crate::api::org_visible(ctx, &org) { - Ok(true) => {} - Ok(false) => match repos::list_visible( - ctx.conn, - &ctx.who.user, - token_public_only(ctx), - &filter, - Sort::Id, - 1, - 1, - ) { - Ok((_, 0)) => return crate::api::org_missing(ctx.app, &name), - Ok(_) => {} - Err(e) => return ctx.app.internal(&e), - }, + // repository there already knows it exists. Decided from the very listing that is + // paginated, so a repository lost in between cannot leave a `200 []`. + let hidden = match crate::api::org_visible(ctx, &org) { + Ok(visible) => !visible, Err(e) => return ctx.app.internal(&e), + }; + let (page, limit) = page_params(ctx); + match repos::list_visible( + ctx.conn, + &ctx.who.user, + token_public_only(ctx), + &filter, + Sort::Id, + page, + limit, + ) { + Ok((_, 0)) if hidden => crate::api::org_missing(ctx.app, &name), + Ok((rows, total)) => list_response(ctx, &path, &rows, total, page, limit, false), + Err(e) => ctx.app.internal(&e), } - query_list(ctx, &filter, Sort::Id, &path, false) } Ok(_) => crate::api::org_missing(ctx.app, &name), Err(e) => ctx.app.internal(&e), From 4b47f63714b7757872c04e12aa05aa8e72455e3d Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 11:00:04 +0200 Subject: [PATCH 12/14] test(forge): pin the missing-organization error shape and an absent count header in the departures --- crates/forge/conformance/README.md | 6 +++-- crates/forge/conformance/departures.json | 34 ++++++++++++++++++++---- crates/forge/conformance/run.py | 31 ++++++++++++++++++++- 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/crates/forge/conformance/README.md b/crates/forge/conformance/README.md index 9b574c42..08b03aae 100644 --- a/crates/forge/conformance/README.md +++ b/crates/forge/conformance/README.md @@ -158,11 +158,13 @@ success, ...), as well as every deliberate sc-forge difference. override is refused (`applied to a different exchange`) when the exchange actually produced differs, including for `skip`. - `expect` keys depend on `kind`: http `status`, `message_contains`, `fields`, `ignore_fields`, - `ignore_headers`, `ignore_body`, `skip`; git `ok`, `message_contains`, `skip`; note `value`, `skip`. + `ignore_headers`, `ignore_body`, `body`, `absent_headers`, `skip`; git `ok`, `message_contains`, `skip`; note `value`, `skip`. A key that does not apply to the entry's kind is a load error. The keys: `status` (number or list), `message_contains`, `ok` (git), `value` (note; works for `strict=False` notes too), `fields` (pin body values by dotted path, `$len` pins an array length), `ignore_fields` (dotted paths left out of the body comparison), `ignore_headers` (recorded headers left out), - `ignore_body` (no body comparison at all) and `skip` (only when unavoidable; say why in `reason`). + `ignore_body` (no body comparison at all), `body` (the exact JSON object the departed answer must have: the same + keys, every value compared, URLs included; use it with `ignore_body` to pin a changed error shape instead of + dropping the check), `absent_headers` (headers that must not be present on the departed answer) and `skip` (only when unavoidable; say why in `reason`). - A departure replaces only what it names. Whatever it sets (`status`, `fields`, `message_contains`, ...), the recorded headers (minus `ignore_headers`) and every JSON field not in `ignore_fields` or `fields` are still compared, and a top-level error `message` is checked by `message_contains` instead of the recorded text. diff --git a/crates/forge/conformance/departures.json b/crates/forge/conformance/departures.json index 55029fd2..2e9ec688 100644 --- a/crates/forge/conformance/departures.json +++ b/crates/forge/conformance/departures.json @@ -1755,9 +1755,19 @@ "ignore_body": true, "ignore_headers": [ "x-total-count" + ], + "body": { + "errors": [ + "user redirect does not exist [name: acme]" + ], + "message": "GetOrgByName", + "url": "/api/swagger" + }, + "absent_headers": [ + "x-total-count" ] }, - "reason": "sc-forge answers a caller who cannot see a private organization and can read nothing in it like a missing organization (404 GetOrgByName); Gitea answers 200 with an empty list [ignore_body: the recorded 200 body has another shape than the departed answer]. K#56 (a collaborator) still lists what it can read." + "reason": "sc-forge answers a caller who cannot see a private organization and can read nothing in it like a missing organization (404 GetOrgByName); Gitea answers 200 with an empty list [ignore_body: the recorded 200 body has another shape than the departed answer; `body` pins the missing-organization error, `absent_headers` that no count leaks]. K#56 (a collaborator) still lists what it can read." }, { "scenario": "K", @@ -1781,9 +1791,16 @@ "message_contains": [ "getorgbyname" ], - "ignore_body": true + "ignore_body": true, + "body": { + "errors": [ + "user redirect does not exist [name: acme]" + ], + "message": "GetOrgByName", + "url": "/api/swagger" + } }, - "reason": "sc-forge answers a private organization the caller cannot see exactly like a missing one (404 GetOrgByName); Gitea's 'HasOrgOrUserVisible' tells the two apart [ignore_body: `errors` is the missing-organization list, the recorded one is null]" + "reason": "sc-forge answers a private organization the caller cannot see exactly like a missing one (404 GetOrgByName); Gitea's 'HasOrgOrUserVisible' tells the two apart [ignore_body: the recorded `errors` is null; `body` pins the missing-organization error shape and URL]" }, { "scenario": "K", @@ -1795,8 +1812,15 @@ "message_contains": [ "getorgbyname" ], - "ignore_body": true + "ignore_body": true, + "body": { + "errors": [ + "user redirect does not exist [name: acme]" + ], + "message": "GetOrgByName", + "url": "/api/swagger" + } }, - "reason": "sc-forge answers a private organization the caller cannot see exactly like a missing one (404 GetOrgByName); Gitea's 'HasOrgOrUserVisible' tells the two apart [ignore_body: `errors` is the missing-organization list, the recorded one is null]" + "reason": "sc-forge answers a private organization the caller cannot see exactly like a missing one (404 GetOrgByName); Gitea's 'HasOrgOrUserVisible' tells the two apart [ignore_body: the recorded `errors` is null; `body` pins the missing-organization error shape and URL]" } ] diff --git a/crates/forge/conformance/run.py b/crates/forge/conformance/run.py index 0945c2d5..8b56cd15 100644 --- a/crates/forge/conformance/run.py +++ b/crates/forge/conformance/run.py @@ -367,7 +367,8 @@ def dep_indexes(spec, expected): EXPECT_KEYS = { - "http": {"status", "message_contains", "fields", "ignore_fields", "ignore_headers", "ignore_body", "skip"}, + "http": {"status", "message_contains", "fields", "ignore_fields", "ignore_headers", "ignore_body", "body", + "absent_headers", "skip"}, "git": {"ok", "message_contains", "skip"}, "note": {"value", "value_prefix", "skip"}, } @@ -490,6 +491,20 @@ def apply_override(exp, act, ov, bij, match=None): got = len(body) if k == "$len" and isinstance(body, list) else dget(body, k) if got != v: d.append(("fail", f"unexpected: {k} {v!r} expected, got {got!r}")) + # `body`: the exact JSON object the departed answer must have (same keys, by value, URLs included); + # `absent_headers`: recorded headers that must not appear on it + if "body" in e: + ab = act.get("body") + if not isinstance(ab, dict) or set(ab) != set(e["body"]): + d.append(("fail", f"unexpected: body keys {sorted(e['body'])} expected, got " + f"{sorted(ab) if isinstance(ab, dict) else type(ab).__name__}")) + else: + found = [] + cmp_json(e["body"], ab, "", found, set(), bij) + d += [x for x in found if x[0] == "fail"] + for h in e.get("absent_headers", []): + if h in act.get("headers", {}): + d.append(("fail", f"unexpected: header {h} must be absent, got {act['headers'][h]!r}")) pins = list((e.get("fields") or {})) status_ok = sts is None or act["status"] in sts if status_ok: @@ -1205,6 +1220,20 @@ def compare_pr(act): for key, value in (("mergeable", False), ("html_url", "/acme/wrong/pulls/1")): wrong = dict(pr, body=dict(pr["body"], **{key: value})) assert any(lv == "fail" and key in text for lv, text in compare_pr(wrong)), f"wrong {key} must fail" + # a departure's `body` pins an error shape and URL, `absent_headers` a header that must not appear + miss = {"errors": ["user redirect does not exist [name: acme]"], "message": "GetOrgByName", "url": "/api/swagger"} + rec = {"kind": "http", "method": "GET", "path": "/api/v1/orgs/acme", "status": 200, "headers": {"x-total-count": "1"}, + "body": []} + ov = {"kind": "http", "match": None, "expect": {"status": 404, "message_contains": ["getorgbyname"], + "ignore_body": True, "ignore_headers": ["x-total-count"], "body": miss, "absent_headers": ["x-total-count"]}} + base = {"kind": "http", "method": "GET", "path": "/api/v1/orgs/acme", "status": 404, "headers": {}, "body": miss} + def departed(act): + return [t for lv, t in apply_override(rec, act, ov, Bij()) if lv == "fail"] + assert not departed(base), "the pinned missing-organization answer is honoured" + assert departed(dict(base, body=dict(miss, url="/api/other"))), "a changed URL must fail" + assert departed(dict(base, body=dict(miss, errors=None))), "a changed errors list must fail" + assert departed(dict(base, body=dict(miss, extra=1))), "an extra key must fail" + assert departed(dict(base, headers={"x-total-count": "0"})), "a count header must fail" bad = [] for d in sorted(os.listdir(a.fixtures_root)): full = os.path.join(a.fixtures_root, d) From a12ae816d619de38cf6b9561c126f0a36e7d1c62 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 11:18:36 +0200 Subject: [PATCH 13/14] test(forge): compare exact-body departures with the strict comparator --- crates/forge/conformance/run.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/forge/conformance/run.py b/crates/forge/conformance/run.py index 8b56cd15..787b01cd 100644 --- a/crates/forge/conformance/run.py +++ b/crates/forge/conformance/run.py @@ -498,10 +498,8 @@ def apply_override(exp, act, ov, bij, match=None): if not isinstance(ab, dict) or set(ab) != set(e["body"]): d.append(("fail", f"unexpected: body keys {sorted(e['body'])} expected, got " f"{sorted(ab) if isinstance(ab, dict) else type(ab).__name__}")) - else: - found = [] - cmp_json(e["body"], ab, "", found, set(), bij) - d += [x for x in found if x[0] == "fail"] + elif not eq_val(e["body"], ab, bij): + d.append(("fail", f"unexpected: body {e['body']!r} expected, got {ab!r}")) for h in e.get("absent_headers", []): if h in act.get("headers", {}): d.append(("fail", f"unexpected: header {h} must be absent, got {act['headers'][h]!r}")) From 392f2102b4e91df513aa253a51edc8f7be294a4e Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 11:37:38 +0200 Subject: [PATCH 14/14] test(forge): generate the stdin test passwords --- crates/forge/tests/e2e_a.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/forge/tests/e2e_a.rs b/crates/forge/tests/e2e_a.rs index 2afcbfdc..f8f8012b 100644 --- a/crates/forge/tests/e2e_a.rs +++ b/crates/forge/tests/e2e_a.rs @@ -1310,12 +1310,15 @@ fn admin_passwords_can_come_from_stdin_but_not_both_ways() { a.extend_from_slice(extra); run(&a, stdin) }; + // Generated, so no password literal reaches the hashing code (CodeQL hard-coded-credential rule). + let pw1 = scl_crypto::random_hex(12); + let pw2 = scl_crypto::random_hex(12); // The trailing newline is not part of the password; an empty line is refused. assert!(!create(&["--password-stdin"], "\n").0, "empty password"); assert!(!create(&["--password-stdin"], "").0, "no line at all"); let (ok, err) = create( &["--password-stdin", "--must-change-password=false"], - "stdin-pw-1\r\n", + &format!("{pw1}\r\n"), ); assert!(ok, "{err}"); let (ok, err) = create(&["--password", "x", "--password-stdin"], "y\n"); @@ -1338,7 +1341,7 @@ fn admin_passwords_can_come_from_stdin_but_not_both_ways() { "--password-stdin", "--must-change-password=false", ], - "stdin-pw-2\n", + &format!("{pw2}\n"), ); assert!(ok, "{err}"); let hash_of = |pw: &str| { @@ -1349,9 +1352,9 @@ fn admin_passwords_can_come_from_stdin_but_not_both_ways() { scl_crypto::verify_password(pw, Some(&u.password_hash)) }; assert!( - hash_of("stdin-pw-2"), + hash_of(&pw2), "the stdin password was stored, without its newline" ); - assert!(!hash_of("stdin-pw-1")); + assert!(!hash_of(&pw1)); let _ = std::fs::remove_dir_all(&dir); }