@@ -302,15 +302,26 @@ pub async fn hook_policy_check(
302302
303303 // 6. Resolve channel binding via the shared resolver (same first-tag,
304304 // fail-closed semantics as the read gate) and check archived state
305- // (applies to ALL pushers including owner). On the push side, NotBound
306- // and Broken collapse into the same bucket — both historically produced
307- // the "no channel binding" denial for non-owners, and a pusher is past
308- // NIP-98 auth so the body leaks nothing new. Only the read gate
309- // distinguishes them (author remediation is NotBound-only).
305+ // (applies to ALL pushers including owner).
306+ //
307+ // `Broken` denies HERE, before owner resolution: a malformed or
308+ // ambiguous first binding fails closed for *everyone*, exactly like the
309+ // read gate. Letting it fall through as "unbound" would hand the owner
310+ // short-circuit below a push path through a binding the read gate
311+ // refuses to honor — the tri-state exists precisely so Broken and
312+ // NotBound cannot collapse. Only genuinely-NotBound repos proceed, and
313+ // only they may earn the remediation-token denial.
310314 let channel_id = match crate :: api:: git:: binding:: resolve_repo_binding ( & repo_event. event ) {
311315 crate :: api:: git:: binding:: RepoBinding :: Bound ( id) => Some ( id) ,
312- crate :: api:: git:: binding:: RepoBinding :: NotBound
313- | crate :: api:: git:: binding:: RepoBinding :: Broken => None ,
316+ crate :: api:: git:: binding:: RepoBinding :: NotBound => None ,
317+ crate :: api:: git:: binding:: RepoBinding :: Broken => {
318+ warn ! ( repo = %req. repo_id, "hook callback: broken buzz-channel binding" ) ;
319+ // Deliberately NOT the no_channel_binding token body: the
320+ // remediation contract is NotBound-only. A broken binding is
321+ // ambiguity, and ambiguity gets a generic denial (matching the
322+ // read gate's posture for the same announcement).
323+ return ( StatusCode :: FORBIDDEN , "invalid channel binding" ) . into_response ( ) ;
324+ }
314325 } ;
315326
316327 if let Some ( ch_id) = channel_id {
@@ -808,4 +819,169 @@ printf '%s' "$HMAC_INPUT" | openssl dgst -sha256 -hmac "{secret}" -hex 2>/dev/nu
808819 "Single-ref HMAC mismatch!\n Rust: {rust_sig}\n Bash: {bash_sig}"
809820 ) ;
810821 }
822+
823+ // ── hook_policy_check binding gate (requires Postgres) ──────────────
824+
825+ const TEST_DB_URL : & str = "postgres://buzz:buzz_dev@localhost:5432/buzz" ; // sadscan:disable np.postgres.1
826+
827+ async fn policy_test_state ( ) -> Arc < AppState > {
828+ let mut config = crate :: config:: Config :: from_env ( ) . expect ( "default config loads" ) ;
829+ config. require_relay_membership = false ;
830+ config. redis_url = "redis://127.0.0.1:1" . to_string ( ) ;
831+ config. database_url = std:: env:: var ( "BUZZ_TEST_DATABASE_URL" )
832+ . or_else ( |_| std:: env:: var ( "DATABASE_URL" ) )
833+ . unwrap_or_else ( |_| TEST_DB_URL . to_string ( ) ) ;
834+ let pool = sqlx:: PgPool :: connect ( & config. database_url )
835+ . await
836+ . expect ( "connect test DB" ) ;
837+ let db = buzz_db:: Db :: from_pool ( pool. clone ( ) ) ;
838+ let redis_pool = deadpool_redis:: Config :: from_url ( & config. redis_url )
839+ . create_pool ( Some ( deadpool_redis:: Runtime :: Tokio1 ) )
840+ . expect ( "redis pool" ) ;
841+ let pubsub = Arc :: new (
842+ buzz_pubsub:: PubSubManager :: new ( & config. redis_url , redis_pool. clone ( ) )
843+ . await
844+ . expect ( "pubsub manager" ) ,
845+ ) ;
846+ let audit = buzz_audit:: AuditService :: new ( pool. clone ( ) ) ;
847+ let auth = buzz_auth:: AuthService :: new ( config. auth . clone ( ) ) ;
848+ let search = buzz_search:: SearchService :: new ( pool. clone ( ) ) ;
849+ let workflow_engine = Arc :: new ( buzz_workflow:: WorkflowEngine :: new (
850+ db. clone ( ) ,
851+ buzz_workflow:: WorkflowConfig :: default ( ) ,
852+ ) ) ;
853+ let media_storage = buzz_media:: MediaStorage :: new ( & config. media ) . expect ( "media storage" ) ;
854+ let ( state, _audit_shutdown) = AppState :: new (
855+ config,
856+ db,
857+ redis_pool,
858+ audit,
859+ pubsub,
860+ auth,
861+ search,
862+ workflow_engine,
863+ nostr:: Keys :: generate ( ) ,
864+ media_storage,
865+ ) ;
866+ Arc :: new ( state)
867+ }
868+
869+ /// Announce `repo_id` with the given tags, then push to it as its own
870+ /// announcement author and return the response.
871+ async fn owner_push_response (
872+ state : & Arc < AppState > ,
873+ community : buzz_core:: CommunityId ,
874+ keys : & nostr:: Keys ,
875+ repo_id : & str ,
876+ binding_tags : Vec < nostr:: Tag > ,
877+ ) -> axum:: response:: Response {
878+ use nostr:: { EventBuilder , Kind , Tag } ;
879+
880+ let mut tags = vec ! [ Tag :: parse( [ "d" , repo_id] ) . unwrap( ) ] ;
881+ tags. extend ( binding_tags) ;
882+ let event = EventBuilder :: new ( Kind :: Custom ( 30617 ) , "" )
883+ . tags ( tags)
884+ . sign_with_keys ( keys)
885+ . expect ( "sign 30617" ) ;
886+ state
887+ . db
888+ . insert_event ( community, & event, None )
889+ . await
890+ . expect ( "insert 30617" ) ;
891+
892+ let owner_hex = keys. public_key ( ) . to_hex ( ) ;
893+ let mut req = HookCallbackRequest {
894+ repo_id : repo_id. to_string ( ) ,
895+ repo_owner : owner_hex. clone ( ) ,
896+ community_id : community. as_uuid ( ) . to_string ( ) ,
897+ pusher_pubkey : owner_hex,
898+ ref_updates : vec ! [ HookRefUpdate {
899+ old_oid: "0" . repeat( 40 ) ,
900+ new_oid: "2" . repeat( 40 ) ,
901+ ref_name: "refs/heads/main" . to_string( ) ,
902+ is_ancestor: false ,
903+ } ] ,
904+ timestamp : SystemTime :: now ( )
905+ . duration_since ( UNIX_EPOCH )
906+ . unwrap ( )
907+ . as_secs ( ) ,
908+ signature : String :: new ( ) ,
909+ } ;
910+ let secret = state. config . git_hook_hmac_secret . clone ( ) ;
911+ sign_request ( & mut req, secret. as_bytes ( ) ) ;
912+ hook_policy_check ( State ( Arc :: clone ( state) ) , Json ( req) ) . await
913+ }
914+
915+ async fn body_string ( response : axum:: response:: Response ) -> ( StatusCode , String ) {
916+ let status = response. status ( ) ;
917+ let bytes = axum:: body:: to_bytes ( response. into_body ( ) , usize:: MAX )
918+ . await
919+ . expect ( "read body" ) ;
920+ ( status, String :: from_utf8 ( bytes. to_vec ( ) ) . expect ( "utf-8" ) )
921+ }
922+
923+ /// The tri-state trap the resolver exists to prevent: a broken (malformed
924+ /// or ambiguous-first) binding must fail closed for EVERYONE on push —
925+ /// including the announcement author — *before* the owner short-circuit
926+ /// grants `MemberRole::Owner`. Collapsing `Broken` into "unbound" hands
927+ /// the owner a push path through a binding the read gate refuses to
928+ /// honor. The remediation token stays reserved for genuinely NotBound.
929+ #[ tokio:: test]
930+ #[ ignore = "requires Postgres" ]
931+ async fn push_gate_denies_owner_through_broken_binding ( ) {
932+ use nostr:: { Keys , Tag } ;
933+
934+ let state = policy_test_state ( ) . await ;
935+ let host = format ! ( "policy-{}.example" , uuid:: Uuid :: new_v4( ) . simple( ) ) ;
936+ let community = state
937+ . db
938+ . ensure_configured_community ( & host)
939+ . await
940+ . expect ( "community" )
941+ . id ;
942+ let keys = Keys :: generate ( ) ;
943+
944+ // Malformed first + valid-looking second: the ambiguity must deny,
945+ // and the parseable duplicate must not rescue the push.
946+ let response = owner_push_response (
947+ & state,
948+ community,
949+ & keys,
950+ & format ! ( "repo-{}" , uuid:: Uuid :: new_v4( ) . simple( ) ) ,
951+ vec ! [
952+ Tag :: parse( [ "buzz-channel" , "not-a-uuid" ] ) . unwrap( ) ,
953+ Tag :: parse( [ "buzz-channel" , & uuid:: Uuid :: new_v4( ) . to_string( ) ] ) . unwrap( ) ,
954+ ] ,
955+ )
956+ . await ;
957+ let ( status, body) = body_string ( response) . await ;
958+ assert_eq ! ( status, StatusCode :: FORBIDDEN ) ;
959+ assert_eq ! (
960+ body, "invalid channel binding" ,
961+ "owner pushing through a broken binding must be denied generically"
962+ ) ;
963+ assert ! (
964+ !body. contains( buzz_core:: git_perms:: GIT_NO_CHANNEL_BINDING_TOKEN ) ,
965+ "remediation token is NotBound-only; Broken must never earn it"
966+ ) ;
967+
968+ // Control: the same owner pushing a genuinely NEVER-BOUND repo is
969+ // allowed (owner authority over an unbound announcement is the
970+ // long-standing push semantics). This pins the denial above to
971+ // Broken specifically, not to some broader regression.
972+ let response = owner_push_response (
973+ & state,
974+ community,
975+ & keys,
976+ & format ! ( "repo-{}" , uuid:: Uuid :: new_v4( ) . simple( ) ) ,
977+ vec ! [ ] ,
978+ )
979+ . await ;
980+ let ( status, body) = body_string ( response) . await ;
981+ assert_eq ! (
982+ status,
983+ StatusCode :: OK ,
984+ "owner push to a never-bound repo must remain allowed (got body: {body})"
985+ ) ;
986+ }
811987}
0 commit comments