diff --git a/workers/server/.sqlx/query-29b298c1752fbe9e4220a3464b283a3a10ca3ea2c8f7d67e0832bdc865ebb256.json b/workers/server/.sqlx/query-29b298c1752fbe9e4220a3464b283a3a10ca3ea2c8f7d67e0832bdc865ebb256.json deleted file mode 100644 index 568084e..0000000 --- a/workers/server/.sqlx/query-29b298c1752fbe9e4220a3464b283a3a10ca3ea2c8f7d67e0832bdc865ebb256.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n SELECT store_path_hash\n FROM upstream_paths\n WHERE store_path_hash = ANY($1)\n AND upstream = ANY($2)\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "store_path_hash", - "type_info": "Text", - "origin": { - "Table": { - "table": "upstream_paths", - "name": "store_path_hash" - } - } - } - ], - "parameters": { - "Left": [ - "TextArray", - "TextArray" - ] - }, - "nullable": [ - false - ] - }, - "hash": "29b298c1752fbe9e4220a3464b283a3a10ca3ea2c8f7d67e0832bdc865ebb256" -} diff --git a/workers/server/.sqlx/query-690f3e03e2f723475251396abb658e6ecb1c9c5c907fd6ef196c36a171b5c01c.json b/workers/server/.sqlx/query-690f3e03e2f723475251396abb658e6ecb1c9c5c907fd6ef196c36a171b5c01c.json new file mode 100644 index 0000000..26ee072 --- /dev/null +++ b/workers/server/.sqlx/query-690f3e03e2f723475251396abb658e6ecb1c9c5c907fd6ef196c36a171b5c01c.json @@ -0,0 +1,47 @@ +{ + "db_name": "PostgreSQL", + "query": "\n WITH target AS (\n SELECT c.id, ($4 OR c.id = ANY($5)) AS permitted\n FROM caches c\n WHERE c.org_id = $1\n AND c.name = $2\n AND EXISTS (\n SELECT 1\n FROM cache_protocols p\n WHERE p.cache_id = c.id\n AND p.protocol = 'nix'\n )\n ), refreshed AS (\n UPDATE published_paths\n SET last_pushed_at = NOW()\n WHERE cache_id = (SELECT id FROM target WHERE permitted)\n AND store_path_hash = ANY($3)\n AND last_pushed_at < NOW() - INTERVAL '1 day'\n RETURNING id\n )\n SELECT\n t.id AS \"id!\",\n ARRAY(\n SELECT p.store_path_hash\n FROM published_paths p\n WHERE t.permitted\n AND p.cache_id = t.id\n AND p.store_path_hash = ANY($3)\n ) AS \"existing!\",\n -- Without configured upstreams this lookup never starts.\n ARRAY(\n SELECT u.store_path_hash\n FROM upstream_paths u\n WHERE t.permitted\n AND CARDINALITY($6::text[]) > 0\n AND u.store_path_hash = ANY($3)\n AND u.upstream = ANY($6)\n ) AS \"served!\"\n FROM target t\n ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "id!", + "type_info": "Int4", + "origin": { + "Table": { + "table": "caches", + "name": "id" + } + } + }, + { + "ordinal": 1, + "name": "existing!", + "type_info": "TextArray", + "origin": "Expression" + }, + { + "ordinal": 2, + "name": "served!", + "type_info": "TextArray", + "origin": "Expression" + } + ], + "parameters": { + "Left": [ + "Text", + "Text", + "TextArray", + "Bool", + "Int4Array", + "TextArray" + ] + }, + "nullable": [ + false, + null, + null + ] + }, + "hash": "690f3e03e2f723475251396abb658e6ecb1c9c5c907fd6ef196c36a171b5c01c" +} diff --git a/workers/server/.sqlx/query-99b150c227347a6d0c116593cab156e9a80133962a52a8f645feeca877cdb73d.json b/workers/server/.sqlx/query-99b150c227347a6d0c116593cab156e9a80133962a52a8f645feeca877cdb73d.json deleted file mode 100644 index 9bfa4a3..0000000 --- a/workers/server/.sqlx/query-99b150c227347a6d0c116593cab156e9a80133962a52a8f645feeca877cdb73d.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n SELECT c.id\n FROM caches c\n WHERE c.org_id = $1\n AND c.name = $2\n AND EXISTS (\n SELECT 1\n FROM cache_protocols p\n WHERE p.cache_id = c.id\n AND p.protocol = 'nix'\n )\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "id", - "type_info": "Int4", - "origin": { - "Table": { - "table": "caches", - "name": "id" - } - } - } - ], - "parameters": { - "Left": [ - "Text", - "Text" - ] - }, - "nullable": [ - false - ] - }, - "hash": "99b150c227347a6d0c116593cab156e9a80133962a52a8f645feeca877cdb73d" -} diff --git a/workers/server/.sqlx/query-f492261ce077def7a8e0f59633b798ecc4542545c4d32a61ea31e29e447de6c0.json b/workers/server/.sqlx/query-f492261ce077def7a8e0f59633b798ecc4542545c4d32a61ea31e29e447de6c0.json deleted file mode 100644 index ff7332f..0000000 --- a/workers/server/.sqlx/query-f492261ce077def7a8e0f59633b798ecc4542545c4d32a61ea31e29e447de6c0.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n WITH refreshed AS (\n UPDATE published_paths\n SET last_pushed_at = NOW()\n WHERE cache_id = $1\n AND store_path_hash = ANY($2)\n AND last_pushed_at < NOW() - INTERVAL '1 day'\n RETURNING id\n )\n SELECT store_path_hash\n FROM published_paths\n WHERE cache_id = $1\n AND store_path_hash = ANY($2)\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "store_path_hash", - "type_info": "Text", - "origin": { - "Table": { - "table": "published_paths", - "name": "store_path_hash" - } - } - } - ], - "parameters": { - "Left": [ - "Int4", - "TextArray" - ] - }, - "nullable": [ - false - ] - }, - "hash": "f492261ce077def7a8e0f59633b798ecc4542545c4d32a61ea31e29e447de6c0" -} diff --git a/workers/server/src/nix.rs b/workers/server/src/nix.rs index 125ce15..e935467 100644 --- a/workers/server/src/nix.rs +++ b/workers/server/src/nix.rs @@ -15,7 +15,7 @@ use snowstead_protocol::{MissingPaths, limits, routes}; use crate::{ AppState, - authz::{self, Action, Principal as Identity, Resource}, + authz::{self, Action, CacheGrant, Principal as Identity, Resource}, cache, error::ApiError, protocol_policy::CallerLimit, @@ -55,6 +55,8 @@ pub(crate) async fn write_access( authz::authorize(&identity, Action::NixPush, &Resource::Org(identity.org())) { if matches!(identity, Identity::ApiToken(_)) { + // A token reaching past its permissions is the security record + // here: write it before the refusal is sent. audit( &state.pool, &identity, @@ -99,15 +101,22 @@ pub(crate) async fn write_access( }; request.extensions_mut().insert(identity.clone()); let response = next.run(request).await; - audit( - &state.pool, - &identity, - cache.as_deref(), - response.status(), - json!({"endpoint":endpoint}), - ip.as_deref(), - ) - .await; + // The response is decided, so recording it must not hold it back. Tracked + // work still finishes before a graceful shutdown closes SQL; only a crash + // between the response and this insert loses the row. + let status = response.status(); + let pool = state.pool.clone(); + state.tasks.spawn(async move { + audit( + &pool, + &identity, + cache.as_deref(), + status, + json!({"endpoint":endpoint}), + ip.as_deref(), + ) + .await; + }); Ok(response) } @@ -154,76 +163,73 @@ async fn get_missing_paths( let body: Value = serde_json::from_slice(&bytes) .map_err(|_| ApiError::new(StatusCode::BAD_REQUEST, "Invalid JSON in request body"))?; let (cache_name, hashes) = parse_missing(&body)?; - let cache = sqlx::query_scalar!( - r#" - SELECT c.id - FROM caches c - WHERE c.org_id = $1 - AND c.name = $2 - AND EXISTS ( - SELECT 1 - FROM cache_protocols p - WHERE p.cache_id = c.id - AND p.protocol = 'nix' - ) - "#, - identity.org(), - cache_name, - ) - .fetch_optional(&state.pool) - .await?; - let cache_id = cache.ok_or_else(|| ApiError::new(StatusCode::NOT_FOUND, "Cache not found"))?; - authz::authorize( - &identity, - Action::NixPush, - &Resource::cache(identity.org(), cache_id, cache_name), - )?; - // Refresh retention and return membership in one statement, without - // rewriting paths whose daily retention refresh is not due. - let existing: HashSet = sqlx::query_scalar!( + let (by_name, by_id) = push_scope(&identity, cache_name); + // Resolve the cache, refresh retention, and return membership in one + // statement, without rewriting paths whose daily retention refresh is not + // due. The cache's ID is unknown until the statement runs, so it receives + // the caller's scope and neither reads nor refreshes paths outside it. + let found = sqlx::query!( r#" - WITH refreshed AS ( + WITH target AS ( + SELECT c.id, ($4 OR c.id = ANY($5)) AS permitted + FROM caches c + WHERE c.org_id = $1 + AND c.name = $2 + AND EXISTS ( + SELECT 1 + FROM cache_protocols p + WHERE p.cache_id = c.id + AND p.protocol = 'nix' + ) + ), refreshed AS ( UPDATE published_paths SET last_pushed_at = NOW() - WHERE cache_id = $1 - AND store_path_hash = ANY($2) + WHERE cache_id = (SELECT id FROM target WHERE permitted) + AND store_path_hash = ANY($3) AND last_pushed_at < NOW() - INTERVAL '1 day' RETURNING id ) - SELECT store_path_hash - FROM published_paths - WHERE cache_id = $1 - AND store_path_hash = ANY($2) + SELECT + t.id AS "id!", + ARRAY( + SELECT p.store_path_hash + FROM published_paths p + WHERE t.permitted + AND p.cache_id = t.id + AND p.store_path_hash = ANY($3) + ) AS "existing!", + -- Without configured upstreams this lookup never starts. + ARRAY( + SELECT u.store_path_hash + FROM upstream_paths u + WHERE t.permitted + AND CARDINALITY($6::text[]) > 0 + AND u.store_path_hash = ANY($3) + AND u.upstream = ANY($6) + ) AS "served!" + FROM target t "#, - cache_id, + identity.org(), + cache_name, &hashes, + by_name, + &by_id, + &state.config.upstreams, ) - .fetch_all(&state.pool) + .fetch_optional(&state.pool) .await? - .into_iter() - .collect(); + .ok_or_else(|| ApiError::new(StatusCode::NOT_FOUND, "Cache not found"))?; + authz::authorize( + &identity, + Action::NixPush, + &Resource::cache(identity.org(), found.id, cache_name), + )?; + let existing: HashSet = found.existing.into_iter().collect(); + let served: HashSet = found.served.into_iter().collect(); let absent: Vec = hashes .into_iter() .filter(|hash| !existing.contains(hash)) .collect(); - let served: HashSet = if absent.is_empty() || state.config.upstreams.is_empty() { - HashSet::new() - } else { - sqlx::query_scalar!( - r#" - SELECT store_path_hash - FROM upstream_paths - WHERE store_path_hash = ANY($1) - AND upstream = ANY($2) - "#, - &absent, - &state.config.upstreams, - ) - .fetch_all(&state.pool) - .await? - .into_iter() - .collect() - }; let missing: Vec<_> = absent .iter() .filter(|hash| !served.contains(*hash)) @@ -250,6 +256,36 @@ async fn get_missing_paths( )) } +/// The caller's push scope for a cache known only by name: whether the name +/// alone authorizes it, and otherwise the cache IDs that would. `authorize` +/// makes each decision; a statement that resolves the name itself uses the +/// answer to stay inside that scope. +fn push_scope(identity: &Identity, name: &str) -> (bool, Vec) { + let allowed = |id| { + let resource = Resource::Cache { + org: identity.org(), + id, + name, + }; + authz::authorize(identity, Action::NixPush, &resource).is_ok() + }; + if allowed(None) { + return (true, Vec::new()); + } + let Identity::ApiToken(token) = identity else { + return (false, Vec::new()); + }; + let ids = token + .caches + .iter() + .filter_map(|grant| match grant { + CacheGrant::Id(id) if allowed(Some(*id)) => Some(*id), + CacheGrant::Id(_) | CacheGrant::Pattern(_) => None, + }) + .collect(); + (false, ids) +} + fn parse_missing(body: &Value) -> Result<(&str, Vec), ApiError> { let invalid = || { ApiError::new( diff --git a/workers/server/tests/protocol.rs b/workers/server/tests/protocol.rs index 4aec465..29da99b 100644 --- a/workers/server/tests/protocol.rs +++ b/workers/server/tests/protocol.rs @@ -450,6 +450,172 @@ async fn hosted_v1_missing_paths_and_authorization(pool: PgPool) { server.await.unwrap(); } +/// The lookup, refresh, and membership answer share one statement, so a +/// request it rejects must leave retention exactly as it found it. +#[sqlx::test(migrator = "snowstead_server::migrations::MIGRATOR")] +async fn missing_paths_resolves_the_cache_and_refreshes_only_within_scope(pool: PgPool) { + sqlx::raw_sql("INSERT INTO orgs (id,slug) VALUES ('org_alpha','alpha'),('org_beta','beta'); + INSERT INTO caches (name,org_id) VALUES ('main','org_alpha'),('other','org_alpha'),('go-only','org_alpha'),('main','org_beta'); + INSERT INTO cache_protocols (cache_id,protocol) SELECT id, CASE WHEN name='go-only' THEN 'go' ELSE 'nix' END FROM caches; + INSERT INTO blob_objects (file_hash,file_size,compression,r2_key) VALUES (repeat('a',64),17,'zstd','nar/existing'); + INSERT INTO published_paths (cache_id,store_path_hash,store_path,nar_hash,nar_size,blob_object_id,last_pushed_at) + SELECT c.id, repeat('0',32), '/nix/store/test', 'sha256:test', 41, b.id, now()-interval '3 days' + FROM caches c CROSS JOIN blob_objects b; + INSERT INTO upstream_paths (store_path_hash,upstream) VALUES (repeat('2',32),'https://upstream.example.test');") + .execute(&pool).await.unwrap(); + let state = AppState::new( + pool.clone(), + Config { + jwt_secret: "contract-secret".into(), + ..Default::default() + }, + common::storage(), + reqwest::Client::new(), + ) + .unwrap(); + let app = router(state); + let cache_id = async |name: &str| -> i32 { + sqlx::query_scalar("SELECT id FROM caches WHERE org_id='org_alpha' AND name=$1") + .bind(name) + .fetch_one(&pool) + .await + .unwrap() + }; + let (main, other, go) = ( + cache_id("main").await, + cache_id("other").await, + cache_id("go-only").await, + ); + let refreshed = async || -> Vec<(String, String)> { + sqlx::query_as( + "SELECT c.org_id, c.name FROM published_paths p JOIN caches c ON c.id=p.cache_id \ + WHERE p.last_pushed_at > now()-interval '1 minute' ORDER BY 1, 2", + ) + .fetch_all(&pool) + .await + .unwrap() + }; + let path = "/_api/v1/get-missing-paths"; + let known = "0".repeat(32); + let recorded_upstream = "2".repeat(32); + let absent = "3".repeat(32); + let other_id = format!("id:{other}"); + let go_id = format!("id:{go}"); + let main_id = format!("id:{main}"); + for (jti, caches, cache, expected, message) in [ + // A cache that is unknown, in another org, or not serving Nix is + // reported before the caller's scope is. + ( + "unknown", + vec!["*"], + "absent", + StatusCode::NOT_FOUND, + "Cache not found", + ), + ( + "unknown-unscoped", + vec!["other"], + "absent", + StatusCode::NOT_FOUND, + "Cache not found", + ), + ( + "wrong-protocol", + vec!["*"], + "go-only", + StatusCode::NOT_FOUND, + "Cache not found", + ), + ( + "wrong-protocol-by-id", + vec![go_id.as_str()], + "go-only", + StatusCode::NOT_FOUND, + "Cache not found", + ), + ( + "other-name", + vec!["other"], + "main", + StatusCode::FORBIDDEN, + "Forbidden", + ), + ( + "other-id", + vec![other_id.as_str()], + "main", + StatusCode::FORBIDDEN, + "Forbidden", + ), + ] { + let credential = token(&pool, Some("org_alpha"), &caches, &["push"], jti).await; + let (status, _, result) = request( + &app, + "POST", + path, + Some(&credential), + json!({"cache":cache,"storePathHashes":[known, absent]}), + ) + .await; + assert_eq!(status, expected, "{jti}"); + assert_eq!(result, json!({"error":message}), "{jti}"); + assert!(refreshed().await.is_empty(), "{jti}"); + } + // A grant by ID authorizes the cache whatever else the token names, and + // refreshes only that cache. Upstream records are ignored when no + // upstream is configured. + let by_id = token( + &pool, + Some("org_alpha"), + &["elsewhere", &main_id], + &["push"], + "main-id", + ) + .await; + let (status, _, result) = request( + &app, + "POST", + path, + Some(&by_id), + json!({"cache":"main","storePathHashes":[known, recorded_upstream, absent]}), + ) + .await; + assert_eq!(status, StatusCode::OK); + assert_eq!( + result, + json!({"missing":[recorded_upstream, absent],"upstream":[],"unchecked":[],"upstreams":[]}) + ); + assert_eq!( + refreshed().await, + [("org_alpha".to_owned(), "main".to_owned())] + ); + let by_name = token( + &pool, + Some("org_alpha"), + &["oth*"], + &["push"], + "other-name-ok", + ) + .await; + let (status, _, result) = request( + &app, + "POST", + path, + Some(&by_name), + json!({"cache":"other","storePathHashes":[known]}), + ) + .await; + assert_eq!(status, StatusCode::OK); + assert_eq!(result["missing"], json!([])); + assert_eq!( + refreshed().await, + [ + ("org_alpha".to_owned(), "main".to_owned()), + ("org_alpha".to_owned(), "other".to_owned()) + ] + ); +} + #[sqlx::test(migrator = "snowstead_server::migrations::MIGRATOR")] async fn nix_push_audits_permission_failures_and_endpoint_responses(pool: PgPool) { sqlx::raw_sql( @@ -471,7 +637,7 @@ async fn nix_push_audits_permission_failures_and_endpoint_responses(pool: PgPool reqwest::Client::new(), ) .unwrap(); - let app = router(state); + let app = router(state.clone()); let pull = token(&pool, None, &["*"], &["pull"], "pull-only").await; let response = app .clone() @@ -521,6 +687,8 @@ async fn nix_push_audits_permission_failures_and_endpoint_responses(pool: PgPool ) .await; assert_eq!(status, StatusCode::OK); + // Endpoint audits are recorded after the response, as tracked work. + state.finish_background().await; for (method, path, endpoint, cache, expected) in [ ( "POST", @@ -567,6 +735,7 @@ async fn nix_push_audits_permission_failures_and_endpoint_responses(pool: PgPool ] { let (status, _, _) = request(&app, method, path, Some("legacy-secret"), json!({})).await; assert_eq!(status, expected, "{endpoint}"); + state.finish_background().await; let row: (String, String, Option, i32, String) = sqlx::query_as( "SELECT actor,action,cache_name,status,detail FROM audit_logs ORDER BY id DESC LIMIT 1", ) @@ -597,6 +766,7 @@ async fn nix_push_audits_permission_failures_and_endpoint_responses(pool: PgPool ] { request(&app, method, path, credential, json!({})).await; } + state.finish_background().await; let count: i64 = sqlx::query_scalar("SELECT count(*) FROM audit_logs") .fetch_one(&pool) .await @@ -617,4 +787,5 @@ async fn nix_push_audits_permission_failures_and_endpoint_responses(pool: PgPool .await; assert_eq!(status, StatusCode::OK); assert_eq!(body["missing"], json!([])); + state.finish_background().await; } diff --git a/workers/server/tests/uploads.rs b/workers/server/tests/uploads.rs index 8955a3c..55675c8 100644 --- a/workers/server/tests/uploads.rs +++ b/workers/server/tests/uploads.rs @@ -4034,14 +4034,16 @@ async fn inline_batch_cli_contract_validates_before_work_and_protects_capacity(p .0, StatusCode::NOT_FOUND ); - let audit: (String, i32, String) = sqlx::query_as( - "SELECT action, status, detail::jsonb->>'endpoint' FROM audit_logs ORDER BY id DESC LIMIT 1", + // The audit is recorded after the response, as tracked work. + state.finish_background().await; + let audit: (String, i32) = sqlx::query_as( + "SELECT action, status FROM audit_logs \ + WHERE detail::jsonb->>'endpoint' = 'inline-upload-batch' ORDER BY id DESC LIMIT 1", ) .fetch_one(&pool) .await .unwrap(); - assert_eq!(audit, ("push".into(), 200, "inline-upload-batch".into())); - state.finish_background().await; + assert_eq!(audit, ("push".into(), 200)); } async fn upload_inline(app: &Router, cache: &str, input: Value, compressed: &[u8]) -> Value {