@@ -3566,49 +3566,76 @@ fn event_mentions_agent(event: &nostr::Event, agent_pubkey_hex: &str) -> bool {
35663566/// tag emitted by `workflow_sink.rs`, and it has already verified that owner's
35673567/// access to the destination channel before emitting the event.
35683568///
3569- /// Recognition requires ALL of:
3570- /// 1. a known relay `self` pubkey (fetched from NIP-11 at startup) — no
3571- /// `relay_self`, no exemption (fail closed);
3572- /// 2. `event.pubkey` == relay `self` — only the relay holds that key, and the
3573- /// relay verifies signatures on submitted events, so this cannot be forged
3574- /// by another member;
3575- /// 3. a `buzz:workflow` tag — the marker the relay puts on workflow-emitted
3576- /// messages (also used for workflow-loop prevention relay-side);
3577- /// 4. a `buzz:workflow-owner` tag carrying a valid 64-hex pubkey — the
3578- /// explicit ownership attribution. Mention `p` tags are never used for
3579- /// attribution, so who is @mentioned in the message text has no bearing
3580- /// on whose authority the gate evaluates.
3569+ /// Recognition requires ALL of the following, failing closed otherwise:
3570+ /// 1. kind `9` (stream message) — the only kind the workflow sink emits;
3571+ /// 2. a known, syntactically valid relay `self` pubkey (fetched from NIP-11
3572+ /// at startup) — no `relay_self`, no exemption;
3573+ /// 3. `event.pubkey` == relay `self`, with a **valid event signature**
3574+ /// verified here. The relay verifies signatures on submission, but this
3575+ /// gate re-checks locally so the exemption never rests on an upstream
3576+ /// guarantee it can't see;
3577+ /// 4. **exactly one** tag exactly equal to `["buzz:workflow", "true"]` — no
3578+ /// duplicates, no extra fields, no other value;
3579+ /// 5. **exactly one** tag exactly equal to `["buzz:workflow-owner", <pubkey>]`
3580+ /// where the owner parses as a full pubkey — no duplicates, no extra
3581+ /// fields. Mention `p` tags are never used for attribution, so who is
3582+ /// @mentioned in the message text has no bearing on whose authority the
3583+ /// gate evaluates.
35813584///
35823585/// The returned pubkey is gated exactly like a direct author: owner/sibling
35833586/// under `owner-only`, plus the explicit list under `allowlist`. A workflow
35843587/// owned by a random channel member therefore still cannot wake an
35853588/// owner-only agent.
35863589fn workflow_attributed_author ( event : & nostr:: Event , relay_self : Option < & str > ) -> Option < String > {
3587- let relay_self = relay_self?;
3588- if event. pubkey . to_hex ( ) != relay_self {
3590+ // 1. Kind gate first — cheapest check, and everything below only makes
3591+ // sense for the kind:9 messages the workflow sink emits.
3592+ if event. kind . as_u16 ( ) as u32 != KIND_STREAM_MESSAGE {
35893593 return None ;
35903594 }
3591- let is_workflow = event
3595+
3596+ // 2. Relay identity must be known AND syntactically valid.
3597+ let relay_self = nostr:: PublicKey :: from_hex ( relay_self?) . ok ( ) ?;
3598+ if event. pubkey != relay_self {
3599+ return None ;
3600+ }
3601+
3602+ // 4. Exactly one marker tag, exactly ["buzz:workflow", "true"]. Collect
3603+ // every tag with the marker key so duplicates or shape/value mismatches
3604+ // (extra fields, wrong value) disqualify instead of being skipped over.
3605+ let markers: Vec < & [ String ] > = event
35923606 . tags
35933607 . iter ( )
3594- . any ( |t| t. as_slice ( ) . first ( ) . map ( |s| s. as_str ( ) ) == Some ( "buzz:workflow" ) ) ;
3595- if !is_workflow {
3608+ . map ( |t| t. as_slice ( ) )
3609+ . filter ( |s| s. first ( ) . map ( |k| k. as_str ( ) ) == Some ( "buzz:workflow" ) )
3610+ . collect ( ) ;
3611+ if markers. len ( ) != 1 || markers[ 0 ] != [ "buzz:workflow" , "true" ] {
35963612 return None ;
35973613 }
3598- event. tags . iter ( ) . find_map ( |t| {
3599- let s = t. as_slice ( ) ;
3600- if s. first ( ) . map ( |k| k. as_str ( ) ) != Some ( "buzz:workflow-owner" ) {
3601- return None ;
3602- }
3603- let owner = s. get ( 1 ) ?;
3604- // Defensive shape check: the gate compares pubkey hex strings, so
3605- // reject anything that is not a 64-hex pubkey rather than feeding
3606- // arbitrary bytes into the owner/sibling/allowlist comparison.
3607- if owner. len ( ) != 64 || !owner. chars ( ) . all ( |c| c. is_ascii_hexdigit ( ) ) {
3608- return None ;
3609- }
3610- Some ( owner. to_ascii_lowercase ( ) )
3611- } )
3614+
3615+ // 5. Exactly one owner tag, exactly ["buzz:workflow-owner", <pubkey>].
3616+ // The owner must parse as a full pubkey — not merely look hex-ish —
3617+ // before it is fed into the owner/sibling/allowlist comparison.
3618+ let owners: Vec < & [ String ] > = event
3619+ . tags
3620+ . iter ( )
3621+ . map ( |t| t. as_slice ( ) )
3622+ . filter ( |s| s. first ( ) . map ( |k| k. as_str ( ) ) == Some ( "buzz:workflow-owner" ) )
3623+ . collect ( ) ;
3624+ let [ owner_tag] = owners. as_slice ( ) else {
3625+ return None ;
3626+ } ;
3627+ let [ _, owner_value] = owner_tag else {
3628+ return None ;
3629+ } ;
3630+ let owner = nostr:: PublicKey :: from_hex ( owner_value) . ok ( ) ?;
3631+
3632+ // 3. Signature check last — it is the most expensive step, so only pay
3633+ // for it once every structural requirement has already passed.
3634+ if event. verify ( ) . is_err ( ) {
3635+ return None ;
3636+ }
3637+
3638+ Some ( owner. to_hex ( ) )
36123639}
36133640
36143641fn is_owner_control_command (
@@ -5894,6 +5921,144 @@ mod workflow_attributed_author_tests {
58945921 author and must fall through to the plain (relay-pubkey) author gate"
58955922 ) ;
58965923 }
5924+
5925+ #[ test]
5926+ fn duplicate_marker_tags_disqualify ( ) {
5927+ let relay = Keys :: generate ( ) ;
5928+ let owner = Keys :: generate ( ) . public_key ( ) . to_hex ( ) ;
5929+ let event = make_event (
5930+ & relay,
5931+ vec ! [
5932+ Tag :: parse( [ "buzz:workflow" , "true" ] ) . unwrap( ) ,
5933+ Tag :: parse( [ "buzz:workflow" , "true" ] ) . unwrap( ) ,
5934+ Tag :: parse( [ "buzz:workflow-owner" , & owner] ) . unwrap( ) ,
5935+ ] ,
5936+ ) ;
5937+ assert_eq ! (
5938+ workflow_attributed_author( & event, Some ( & relay. public_key( ) . to_hex( ) ) ) ,
5939+ None ,
5940+ "more than one buzz:workflow marker tag must fail closed"
5941+ ) ;
5942+ }
5943+
5944+ #[ test]
5945+ fn marker_value_mismatch_disqualifies ( ) {
5946+ let relay = Keys :: generate ( ) ;
5947+ let owner = Keys :: generate ( ) . public_key ( ) . to_hex ( ) ;
5948+ for bad_marker in [
5949+ Tag :: parse ( [ "buzz:workflow" , "false" ] ) . unwrap ( ) ,
5950+ Tag :: parse ( [ "buzz:workflow" ] ) . unwrap ( ) ,
5951+ Tag :: parse ( [ "buzz:workflow" , "true" , "extra" ] ) . unwrap ( ) ,
5952+ ] {
5953+ let event = make_event (
5954+ & relay,
5955+ vec ! [
5956+ bad_marker. clone( ) ,
5957+ Tag :: parse( [ "buzz:workflow-owner" , & owner] ) . unwrap( ) ,
5958+ ] ,
5959+ ) ;
5960+ assert_eq ! (
5961+ workflow_attributed_author( & event, Some ( & relay. public_key( ) . to_hex( ) ) ) ,
5962+ None ,
5963+ "marker tag {:?} is not exactly [\" buzz:workflow\" , \" true\" ] and must fail closed" ,
5964+ bad_marker. as_slice( )
5965+ ) ;
5966+ }
5967+ }
5968+
5969+ #[ test]
5970+ fn duplicate_owner_tags_disqualify ( ) {
5971+ // Two owner tags — even with identical values — are ambiguous
5972+ // provenance and must not attribute to anyone.
5973+ let relay = Keys :: generate ( ) ;
5974+ let owner = Keys :: generate ( ) . public_key ( ) . to_hex ( ) ;
5975+ let other = Keys :: generate ( ) . public_key ( ) . to_hex ( ) ;
5976+ for second_owner in [ & owner, & other] {
5977+ let event = make_event (
5978+ & relay,
5979+ vec ! [
5980+ Tag :: parse( [ "buzz:workflow" , "true" ] ) . unwrap( ) ,
5981+ Tag :: parse( [ "buzz:workflow-owner" , & owner] ) . unwrap( ) ,
5982+ Tag :: parse( [ "buzz:workflow-owner" , second_owner] ) . unwrap( ) ,
5983+ ] ,
5984+ ) ;
5985+ assert_eq ! (
5986+ workflow_attributed_author( & event, Some ( & relay. public_key( ) . to_hex( ) ) ) ,
5987+ None ,
5988+ "duplicate buzz:workflow-owner tags must fail closed"
5989+ ) ;
5990+ }
5991+ }
5992+
5993+ #[ test]
5994+ fn owner_tag_with_extra_fields_disqualifies ( ) {
5995+ let relay = Keys :: generate ( ) ;
5996+ let owner = Keys :: generate ( ) . public_key ( ) . to_hex ( ) ;
5997+ let event = make_event (
5998+ & relay,
5999+ vec ! [
6000+ Tag :: parse( [ "buzz:workflow" , "true" ] ) . unwrap( ) ,
6001+ Tag :: parse( [ "buzz:workflow-owner" , & owner, "extra" ] ) . unwrap( ) ,
6002+ ] ,
6003+ ) ;
6004+ assert_eq ! (
6005+ workflow_attributed_author( & event, Some ( & relay. public_key( ) . to_hex( ) ) ) ,
6006+ None ,
6007+ "an owner tag with extra fields is not the exact shape the relay \
6008+ emits and must fail closed"
6009+ ) ;
6010+ }
6011+
6012+ #[ test]
6013+ fn wrong_kind_disqualifies ( ) {
6014+ let relay = Keys :: generate ( ) ;
6015+ let owner = Keys :: generate ( ) . public_key ( ) . to_hex ( ) ;
6016+ let agent = Keys :: generate ( ) . public_key ( ) . to_hex ( ) ;
6017+ let event = EventBuilder :: new ( Kind :: from ( 1u16 ) , "wake up" )
6018+ . tags ( workflow_tags ( & owner, & agent) )
6019+ . sign_with_keys ( & relay)
6020+ . expect ( "sign test event" ) ;
6021+ assert_eq ! (
6022+ workflow_attributed_author( & event, Some ( & relay. public_key( ) . to_hex( ) ) ) ,
6023+ None ,
6024+ "only kind:9 stream messages may use the workflow exemption"
6025+ ) ;
6026+ }
6027+
6028+ #[ test]
6029+ fn tampered_event_fails_signature_check ( ) {
6030+ // Alter the content after signing: pubkey still matches relay_self
6031+ // and the tags are pristine, but the signature no longer covers the
6032+ // event — the local verify must reject it.
6033+ let relay = Keys :: generate ( ) ;
6034+ let owner = Keys :: generate ( ) . public_key ( ) . to_hex ( ) ;
6035+ let agent = Keys :: generate ( ) . public_key ( ) . to_hex ( ) ;
6036+ let event = make_event ( & relay, workflow_tags ( & owner, & agent) ) ;
6037+ let mut json = serde_json:: to_value ( & event) . expect ( "event to JSON" ) ;
6038+ json[ "content" ] = serde_json:: Value :: String ( "tampered" . into ( ) ) ;
6039+ let tampered: nostr:: Event = serde_json:: from_value ( json) . expect ( "tampered event parses" ) ;
6040+ assert_eq ! (
6041+ workflow_attributed_author( & tampered, Some ( & relay. public_key( ) . to_hex( ) ) ) ,
6042+ None ,
6043+ "a tampered event must fail the local signature check"
6044+ ) ;
6045+ }
6046+
6047+ #[ test]
6048+ fn syntactically_invalid_relay_self_means_no_exemption ( ) {
6049+ let relay = Keys :: generate ( ) ;
6050+ let owner = Keys :: generate ( ) . public_key ( ) . to_hex ( ) ;
6051+ let agent = Keys :: generate ( ) . public_key ( ) . to_hex ( ) ;
6052+ let event = make_event ( & relay, workflow_tags ( & owner, & agent) ) ;
6053+ let long_not_hex = "zz" . repeat ( 32 ) ;
6054+ for bad_self in [ "" , "not-hex" , long_not_hex. as_str ( ) ] {
6055+ assert_eq ! (
6056+ workflow_attributed_author( & event, Some ( bad_self) ) ,
6057+ None ,
6058+ "an invalid NIP-11 self value {bad_self:?} must disable the exemption"
6059+ ) ;
6060+ }
6061+ }
58976062}
58986063
58996064#[ cfg( test) ]
0 commit comments