@@ -224,10 +224,95 @@ impl axum::extract::FromRequestParts<Arc<AppState>> for GitAuth {
224224 return Err ( ( StatusCode :: FORBIDDEN , "restricted: not a relay member" ) . into_response ( ) ) ;
225225 }
226226
227+ deny_banned_git_principal ( & state. db , tenant. community ( ) , & pubkey, auth_tag) . await ?;
228+
227229 Ok ( GitAuth { pubkey, tenant } )
228230 }
229231}
230232
233+ /// Deny banned principals on every Git HTTP request.
234+ ///
235+ /// Git runs outside the WebSocket authentication path, so a valid NIP-98
236+ /// credential and channel membership are not enough — neither reflects a
237+ /// moderation ban. Git credentials are also deliberately reused across a
238+ /// session (see the replay notes above), so no session expiry would close the
239+ /// gap on its own. Re-read the durable ban per request instead.
240+ ///
241+ /// Cascades to the proven NIP-OA owner, matching the NIP-42 gate in
242+ /// `handlers::auth`: banning a human must also revoke their agents, or the ban
243+ /// is bypassable by cloning and pushing through an agent key.
244+ async fn deny_banned_git_principal (
245+ db : & buzz_db:: Db ,
246+ community : buzz_core:: CommunityId ,
247+ pubkey : & nostr:: PublicKey ,
248+ auth_tag : Option < & str > ,
249+ ) -> Result < ( ) , Response > {
250+ let agent = git_restriction_state ( db, community, pubkey) . await ?;
251+
252+ // Skip the owner read when the agent is already banned: the denial is
253+ // identical either way. Mirrors the WebSocket cascade's short-circuit.
254+ let owner = if agent. banned {
255+ None
256+ } else {
257+ crate :: api:: relay_members:: extract_nip_oa_owner ( pubkey. as_bytes ( ) , auth_tag)
258+ } ;
259+ let owner_state = match owner {
260+ Some ( owner) => Some ( git_restriction_state ( db, community, & owner) . await ?) ,
261+ None => None ,
262+ } ;
263+
264+ enforce_git_ban_cascade ( & agent, owner_state. as_ref ( ) ) . map_err ( |status| {
265+ warn ! (
266+ pubkey = %pubkey. to_hex( ) ,
267+ owner = ?owner. map( |owner| owner. to_hex( ) ) ,
268+ "git: community ban denied request"
269+ ) ;
270+ ( status, "blocked: banned from this community" ) . into_response ( )
271+ } )
272+ }
273+
274+ /// One restriction read, failing closed with 503.
275+ ///
276+ /// A restriction-store outage must not be reported to the client as a
277+ /// permission decision — 503 says "retry", 403 would claim a ban that was
278+ /// never read.
279+ async fn git_restriction_state (
280+ db : & buzz_db:: Db ,
281+ community : buzz_core:: CommunityId ,
282+ pubkey : & nostr:: PublicKey ,
283+ ) -> Result < buzz_db:: moderation:: RestrictionState , Response > {
284+ db. moderation_restriction_state ( community, pubkey. as_bytes ( ) )
285+ . await
286+ . map_err ( |error| {
287+ warn ! ( pubkey = %pubkey. to_hex( ) , error = %error, "git: ban lookup failed closed" ) ;
288+ ( StatusCode :: SERVICE_UNAVAILABLE , "authorization unavailable" ) . into_response ( )
289+ } )
290+ }
291+
292+ fn enforce_git_ban ( restriction : & buzz_db:: moderation:: RestrictionState ) -> Result < ( ) , StatusCode > {
293+ if restriction. banned {
294+ Err ( StatusCode :: FORBIDDEN )
295+ } else {
296+ Ok ( ( ) )
297+ }
298+ }
299+
300+ /// Either principal's ban denies the request; `None` owner means no attested
301+ /// owner to inherit from.
302+ ///
303+ /// Split from the DB reads so agent→owner precedence stays unit-testable
304+ /// without Postgres.
305+ fn enforce_git_ban_cascade (
306+ agent : & buzz_db:: moderation:: RestrictionState ,
307+ owner : Option < & buzz_db:: moderation:: RestrictionState > ,
308+ ) -> Result < ( ) , StatusCode > {
309+ enforce_git_ban ( agent) ?;
310+ match owner {
311+ Some ( owner) => enforce_git_ban ( owner) ,
312+ None => Ok ( ( ) ) ,
313+ }
314+ }
315+
231316/// Construct the repo-root NIP-98 `u` URL expected for a git HTTP request.
232317///
233318/// The host is always the server-resolved tenant host. `config_relay_url` only
@@ -2610,6 +2695,76 @@ mod sec005_read_gate_tests {
26102695 assert ! ( !read_role_allows( Some ( "" ) ) , "empty role must deny" ) ;
26112696 }
26122697
2698+ #[ test]
2699+ fn durable_ban_denies_git_even_with_otherwise_valid_auth ( ) {
2700+ let restriction = buzz_db:: moderation:: RestrictionState {
2701+ banned : true ,
2702+ muted_until : None ,
2703+ } ;
2704+
2705+ assert_eq ! ( enforce_git_ban( & restriction) , Err ( StatusCode :: FORBIDDEN ) ) ;
2706+ }
2707+
2708+ #[ test]
2709+ fn timeout_without_ban_does_not_revoke_git_access ( ) {
2710+ let restriction = buzz_db:: moderation:: RestrictionState {
2711+ banned : false ,
2712+ muted_until : Some ( chrono:: Utc :: now ( ) ) ,
2713+ } ;
2714+
2715+ assert_eq ! ( enforce_git_ban( & restriction) , Ok ( ( ) ) ) ;
2716+ }
2717+
2718+ fn restriction ( banned : bool ) -> buzz_db:: moderation:: RestrictionState {
2719+ buzz_db:: moderation:: RestrictionState {
2720+ banned,
2721+ muted_until : None ,
2722+ }
2723+ }
2724+
2725+ // ── Agent → owner ban cascade ────────────────────────────────────────
2726+ //
2727+ // Git accepts NIP-OA attestations on the signed NIP-98 token, so an agent
2728+ // key can act for its owner (`deny_banned_git_principal`). The NIP-42 gate
2729+ // in `handlers::auth` cascades the ban check to the proven owner for that
2730+ // reason, and Git must agree: if only the presented key were checked, a
2731+ // banned human would keep clone and push access through any agent key.
2732+
2733+ #[ test]
2734+ fn banned_owner_denies_git_for_an_otherwise_clear_agent ( ) {
2735+ assert_eq ! (
2736+ enforce_git_ban_cascade( & restriction( false ) , Some ( & restriction( true ) ) ) ,
2737+ Err ( StatusCode :: FORBIDDEN ) ,
2738+ "an agent must inherit its proven owner's ban"
2739+ ) ;
2740+ }
2741+
2742+ #[ test]
2743+ fn banned_agent_denies_git_whatever_the_owner_state ( ) {
2744+ for owner in [ None , Some ( restriction ( false ) ) , Some ( restriction ( true ) ) ] {
2745+ assert_eq ! (
2746+ enforce_git_ban_cascade( & restriction( true ) , owner. as_ref( ) ) ,
2747+ Err ( StatusCode :: FORBIDDEN ) ,
2748+ "a directly banned agent must be denied"
2749+ ) ;
2750+ }
2751+ }
2752+
2753+ #[ test]
2754+ fn clear_agent_and_clear_owner_allow_git ( ) {
2755+ assert_eq ! (
2756+ enforce_git_ban_cascade( & restriction( false ) , Some ( & restriction( false ) ) ) ,
2757+ Ok ( ( ) )
2758+ ) ;
2759+ }
2760+
2761+ #[ test]
2762+ fn clear_agent_without_attested_owner_allows_git ( ) {
2763+ // No NIP-OA tag on the request: nothing to inherit, so the agent's own
2764+ // state decides. A missing owner must not read as a ban.
2765+ assert_eq ! ( enforce_git_ban_cascade( & restriction( false ) , None ) , Ok ( ( ) ) ) ;
2766+ }
2767+
26132768 fn announcement ( keys : & Keys , tags : Vec < Tag > ) -> nostr:: Event {
26142769 EventBuilder :: new ( Kind :: Custom ( 30617 ) , "" )
26152770 . tags ( tags)
@@ -2965,4 +3120,129 @@ mod sec005_read_gate_tests {
29653120 "deleted announcement must deny reads even for channel members"
29663121 ) ;
29673122 }
3123+
3124+ // ── Ban gate wiring (requires Postgres) ──────────────────────────────
3125+ //
3126+ // The pure tests above fix the decision table; these prove the gate is
3127+ // actually wired to the durable store — that it reads the real ban row,
3128+ // resolves the NIP-OA owner from a live attestation, and fails closed when
3129+ // the store is unreachable. `deny_banned_git_principal` runs inside the
3130+ // `GitAuth` extractor, which every Git route (`info/refs`, `git-upload-pack`,
3131+ // `git-receive-pack`) goes through, so advertise, fetch and push all
3132+ // inherit these outcomes.
3133+
3134+ /// Community + a ban actor, without the channel/repo fixture the read-gate
3135+ /// tests need — the ban gate runs before any repo is resolved.
3136+ async fn setup_ban_community ( ) -> ( buzz_db:: Db , buzz_core:: CommunityId , Vec < u8 > ) {
3137+ let db = setup_db ( ) . await ;
3138+ let host = format ! ( "ban-git-{}.example" , uuid:: Uuid :: new_v4( ) . simple( ) ) ;
3139+ let community = db
3140+ . ensure_configured_community ( & host)
3141+ . await
3142+ . expect ( "community" )
3143+ . id ;
3144+ let actor = Keys :: generate ( ) . public_key ( ) . to_bytes ( ) . to_vec ( ) ;
3145+ db. ensure_user ( community, & actor) . await . expect ( "actor" ) ;
3146+ ( db, community, actor)
3147+ }
3148+
3149+ #[ tokio:: test]
3150+ #[ ignore = "requires Postgres" ]
3151+ async fn ban_gate_denies_banned_member_and_allows_clear_member ( ) {
3152+ let ( db, community, actor) = setup_ban_community ( ) . await ;
3153+ let member = Keys :: generate ( ) ;
3154+ let member_pk = member. public_key ( ) . to_bytes ( ) . to_vec ( ) ;
3155+ db. ensure_user ( community, & member_pk) . await . expect ( "member" ) ;
3156+
3157+ assert ! (
3158+ deny_banned_git_principal( & db, community, & member. public_key( ) , None )
3159+ . await
3160+ . is_ok( ) ,
3161+ "precondition: an unbanned member passes the git ban gate"
3162+ ) ;
3163+
3164+ db. ban_community_member ( community, & member_pk, & actor, Some ( "test" ) , None )
3165+ . await
3166+ . expect ( "ban" ) ;
3167+
3168+ let ( status, body) = denial_parts (
3169+ deny_banned_git_principal ( & db, community, & member. public_key ( ) , None ) . await ,
3170+ )
3171+ . await ;
3172+ assert_eq ! ( status, StatusCode :: FORBIDDEN ) ;
3173+ assert_eq ! ( body, "blocked: banned from this community" ) ;
3174+ }
3175+
3176+ #[ tokio:: test]
3177+ #[ ignore = "requires Postgres" ]
3178+ async fn ban_gate_cascades_to_a_banned_nip_oa_owner ( ) {
3179+ let ( db, community, actor) = setup_ban_community ( ) . await ;
3180+ let owner = Keys :: generate ( ) ;
3181+ let agent = Keys :: generate ( ) ;
3182+ let owner_pk = owner. public_key ( ) . to_bytes ( ) . to_vec ( ) ;
3183+ let agent_pk = agent. public_key ( ) . to_bytes ( ) . to_vec ( ) ;
3184+ db. ensure_user ( community, & owner_pk) . await . expect ( "owner" ) ;
3185+ db. ensure_user ( community, & agent_pk) . await . expect ( "agent" ) ;
3186+
3187+ // A real attestation: the gate must verify it, not trust a claim.
3188+ let auth_tag = buzz_sdk:: nip_oa:: compute_auth_tag ( & owner, & agent. public_key ( ) , "kind=9" )
3189+ . expect ( "auth tag" ) ;
3190+
3191+ assert ! (
3192+ deny_banned_git_principal( & db, community, & agent. public_key( ) , Some ( & auth_tag) )
3193+ . await
3194+ . is_ok( ) ,
3195+ "precondition: neither agent nor owner is banned"
3196+ ) ;
3197+
3198+ // Ban the human only. The agent's own row stays clear.
3199+ db. ban_community_member ( community, & owner_pk, & actor, Some ( "test" ) , None )
3200+ . await
3201+ . expect ( "ban owner" ) ;
3202+
3203+ let ( status, _) = denial_parts (
3204+ deny_banned_git_principal ( & db, community, & agent. public_key ( ) , Some ( & auth_tag) ) . await ,
3205+ )
3206+ . await ;
3207+ assert_eq ! (
3208+ status,
3209+ StatusCode :: FORBIDDEN ,
3210+ "banning the owner must revoke its agent's git access"
3211+ ) ;
3212+
3213+ // An unattested request from the same agent key is unaffected: the
3214+ // cascade must follow a verified owner, not punish every agent.
3215+ assert ! (
3216+ deny_banned_git_principal( & db, community, & agent. public_key( ) , None )
3217+ . await
3218+ . is_ok( ) ,
3219+ "without an attestation there is no owner to inherit from"
3220+ ) ;
3221+ }
3222+
3223+ #[ tokio:: test]
3224+ #[ ignore = "requires Postgres" ]
3225+ async fn ban_gate_fails_closed_with_503_when_the_store_is_unreachable ( ) {
3226+ let url = std:: env:: var ( "BUZZ_TEST_DATABASE_URL" )
3227+ . or_else ( |_| std:: env:: var ( "DATABASE_URL" ) )
3228+ . unwrap_or_else ( |_| TEST_DB_URL . to_string ( ) ) ;
3229+ let pool = sqlx:: PgPool :: connect ( & url) . await . expect ( "connect test DB" ) ;
3230+ let db = buzz_db:: Db :: from_pool ( pool. clone ( ) ) ;
3231+
3232+ // Closing the pool is the cheapest faithful stand-in for the
3233+ // restriction store being unavailable mid-request.
3234+ pool. close ( ) . await ;
3235+
3236+ let community = buzz_core:: CommunityId :: from_uuid ( uuid:: Uuid :: new_v4 ( ) ) ;
3237+ let ( status, body) = denial_parts (
3238+ deny_banned_git_principal ( & db, community, & Keys :: generate ( ) . public_key ( ) , None ) . await ,
3239+ )
3240+ . await ;
3241+ assert_eq ! (
3242+ status,
3243+ StatusCode :: SERVICE_UNAVAILABLE ,
3244+ "a store outage must deny as retryable, never allow and never claim a 403"
3245+ ) ;
3246+ assert_eq ! ( body, "authorization unavailable" ) ;
3247+ }
29683248}
0 commit comments