From 1a990a09fdab3f0f366431adad399ba9f587d08d Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 2 Dec 2026 19:35:55 +0100 Subject: [PATCH] Auth surface: kill the surviving mutants on token expiry, store custody, body limit, fatal signalling and the pointer move Co-Authored-By: Claude Sonnet 5.5 --- crates/ghostlight-dungeon/src/api.rs | 40 ++++++++++++++++- crates/ghostlight-dungeon/src/app_session.rs | 15 +++++++ crates/ghostlight-dungeon/src/auth_guard.rs | 10 +++++ crates/ghostlight-dungeon/src/catalog.rs | 47 ++++++++++++++++++++ crates/ghostlight-dungeon/src/invite.rs | 9 ++++ 5 files changed, 120 insertions(+), 1 deletion(-) diff --git a/crates/ghostlight-dungeon/src/api.rs b/crates/ghostlight-dungeon/src/api.rs index 63141fa..15ef1ba 100644 --- a/crates/ghostlight-dungeon/src/api.rs +++ b/crates/ghostlight-dungeon/src/api.rs @@ -794,6 +794,7 @@ mod tests { router: Router, state: AppState, heimdall: Arc, + fatal_events: std::sync::Mutex>, rig: Rig, cookies: Vec, } @@ -824,7 +825,7 @@ mod tests { }) .collect(); let heimdall = Arc::new(FakeHeimdall::default()); - let (fatal, _) = tokio::sync::mpsc::unbounded_channel(); + let (fatal, fatal_events) = tokio::sync::mpsc::unbounded_channel(); let state = AppState { catalog: rig.catalog.clone(), timelines: rig.timelines.clone(), @@ -840,6 +841,7 @@ mod tests { router: service_router(state.clone()), state, heimdall, + fatal_events: std::sync::Mutex::new(fatal_events), rig, cookies, } @@ -1266,6 +1268,42 @@ mod tests { assert_eq!(client_key(&headers), "203.0.113.9"); } + #[tokio::test] + async fn a_body_over_the_limit_is_refused_and_one_under_it_is_not() { + let app = app(vec![], &[]).await; + let key = |len: usize| json!({"key": "k".repeat(len), "provider": "google"}); + let (status, _, _) = auth_call(&app, "/api/auth/begin", &[], "198.51.100.3", key(5_000)).await; + assert_eq!(status, StatusCode::OK, "a few kilobytes is an ordinary body"); + let (status, _, _) = auth_call(&app, "/api/auth/begin", &[], "198.51.100.3", key(BODY_LIMIT + 1)).await; + assert_eq!(status, StatusCode::PAYLOAD_TOO_LARGE); + } + + #[tokio::test] + async fn a_fatal_custody_signal_reaches_the_runtime_owner_with_its_organ() { + let app = app(vec![], &[]).await; + signal_fatal(&app.state, "app-session custody", &"the store moved"); + assert_eq!( + app.fatal_events.lock().unwrap().try_recv().unwrap(), + "app-session custody: the store moved" + ); + } + + #[tokio::test] + async fn playing_an_older_timeline_moves_the_pointer_to_it() { + let app = app(vec![], &["alice"]).await; + let cookie = app.cookies[0].clone(); + let first = begin_one(&app, &cookie).await; + let second = begin_one(&app, &cookie).await; + let (_, _, me) = call(&app, Method::GET, "/api/me", Some(&cookie), None).await; + assert_eq!(me["currentTimeline"], second.as_str()); + play_text(&app, &cookie, &first, "back to the start", None).await; + let (_, _, me) = call(&app, Method::GET, "/api/me", Some(&cookie), None).await; + assert_eq!(me["currentTimeline"], first.as_str()); + play_text(&app, &cookie, &first, "and again", None).await; + let (_, _, me) = call(&app, Method::GET, "/api/me", Some(&cookie), None).await; + assert_eq!(me["currentTimeline"], first.as_str(), "playing the current timeline leaves it"); + } + #[tokio::test] async fn only_the_owner_reaches_a_timeline() { let app = app(vec![], &["alice", "mallory"]).await; diff --git a/crates/ghostlight-dungeon/src/app_session.rs b/crates/ghostlight-dungeon/src/app_session.rs index f35a449..6cb6ad9 100644 --- a/crates/ghostlight-dungeon/src/app_session.rs +++ b/crates/ghostlight-dungeon/src/app_session.rs @@ -704,6 +704,21 @@ mod tests { sites[0] ); } + #[test] + fn a_cookie_authenticates_until_the_instant_its_access_expires() { + let directory = tempfile::tempdir().unwrap(); + let key = directory.path().join("session.key"); + std::fs::write(&key, [23_u8; 32]).unwrap(); + let mut owner = AppSessionOwner::open(directory.path().join("app-sessions-v2.cc"), &key).unwrap(); + let now = Utc::now(); + let (access, refresh) = (now + chrono::Duration::minutes(5), now + chrono::Duration::days(7)); + let cookie = owner + .create_session(VerifiedSessionAdmission::fixture("a", "s1", 1, access, refresh, "c1")) + .unwrap(); + assert!(owner.account_for_cookie(&cookie, access - chrono::Duration::seconds(1)).unwrap().is_some()); + assert!(owner.account_for_cookie(&cookie, access).unwrap().is_none(), "dead at the instant"); + assert!(owner.account_for_cookie("not-a-cookie", now).unwrap().is_none()); + } fn session(claim: &str) -> VerifiedSessionAdmission { VerifiedSessionAdmission::fixture( diff --git a/crates/ghostlight-dungeon/src/auth_guard.rs b/crates/ghostlight-dungeon/src/auth_guard.rs index 4705e90..c31ec52 100644 --- a/crates/ghostlight-dungeon/src/auth_guard.rs +++ b/crates/ghostlight-dungeon/src/auth_guard.rs @@ -181,4 +181,14 @@ mod tests { assert!(guard.windows.lock().unwrap().len() <= MAX_CLIENTS); assert!(!guard.allow("brand-new", later), "a full table refuses a stranger rather than forget everyone"); } + + #[test] + fn a_binding_is_dead_at_its_expiry_instant_and_dropped_by_the_next_start() { + let guard = AuthGuard::default(); + let now = Utc::now(); + guard.start("old", now + Duration::minutes(1), now).unwrap(); + let at_expiry = now + Duration::minutes(1); + guard.start("new", at_expiry + Duration::minutes(1), at_expiry).unwrap(); + assert_eq!(guard.pending.lock().unwrap().len(), 1, "expiry is exclusive"); + } } diff --git a/crates/ghostlight-dungeon/src/catalog.rs b/crates/ghostlight-dungeon/src/catalog.rs index 6ec466d..2a6697b 100644 --- a/crates/ghostlight-dungeon/src/catalog.rs +++ b/crates/ghostlight-dungeon/src/catalog.rs @@ -564,4 +564,51 @@ mod sign_in_tests { assert_eq!(account.created_at, now); assert!(account_id.starts_with("account:")); } + + #[test] + fn mint_and_redemption_drop_invites_dead_at_that_very_instant() { + let (_d, mut catalog) = catalog(); + let now = Utc::now(); + let dying = catalog.mint_invite(now, Duration::hours(1)).unwrap(); + let at = now + Duration::hours(1); + let minted = catalog.mint_invite(at, Duration::hours(1)).unwrap(); + assert!(!catalog.state.invites.contains_key(&token_hash(&dying)), "mint prunes at the instant"); + let dying = catalog.mint_invite(at, Duration::hours(1) - Duration::seconds(1)).unwrap(); + let later = at + Duration::hours(1) - Duration::seconds(1); + assert!(catalog.sign_in("a", Some(&minted), later).unwrap().is_ok()); + assert!( + !catalog.state.invites.contains_key(&token_hash(&dying)), + "redemption prunes an invite that expires at that instant" + ); + } + + #[test] + fn a_store_holding_a_row_of_the_wrong_type_key_or_schema_is_refused() { + for (row_type, key, schema) in [ + ("ghostlight.other.v1", STORE_KEY, STORE_TYPE), + (STORE_TYPE, "secondary", STORE_TYPE), + (STORE_TYPE, STORE_KEY, "ghostlight.other.v1"), + ] { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("catalog.cc"); + let store = OwnedRedbMessagePackBackingStore::new(&path).unwrap(); + let mut row = envelope(&CatalogState::default()).unwrap(); + row.r#type = row_type.into(); + row.key = key.into(); + row.schema_id = Some(schema.into()); + assert!(store.compare_and_swap_batch(&[], vec![row]).unwrap()); + drop(store); + assert!(Catalog::open(&path).is_err(), "{row_type} {key} {schema}"); + } + } + + #[test] + fn custody_fails_once_the_store_path_is_replaced() { + let (directory, catalog) = catalog(); + catalog.validate_custody().unwrap(); + let path = directory.path().join("catalog.cc"); + std::fs::rename(&path, directory.path().join("displaced.cc")).unwrap(); + std::fs::File::create(&path).unwrap(); + assert!(catalog.validate_custody().is_err()); + } } diff --git a/crates/ghostlight-dungeon/src/invite.rs b/crates/ghostlight-dungeon/src/invite.rs index dddaa2e..e0c199e 100644 --- a/crates/ghostlight-dungeon/src/invite.rs +++ b/crates/ghostlight-dungeon/src/invite.rs @@ -199,4 +199,13 @@ mod tests { assert!(reply.contains("error"), "{reply}"); assert!(catalog.lock().await.sign_in("x", Some("anything"), Utc::now()).unwrap().is_err()); } + + #[tokio::test] + async fn a_socket_path_that_cannot_be_inspected_is_an_error_not_a_free_path() { + let directory = tempfile::tempdir().unwrap(); + let file = directory.path().join("file"); + std::fs::write(&file, "x").unwrap(); + let error = bind(&file.join("invite.sock")).unwrap_err(); + assert!(format!("{error:#}").contains("inspecting the invite socket path"), "{error:#}"); + } }