Skip to content

Commit 1ce727c

Browse files
joahgHertzclaude
committed
fix(relay): keep agents out of DMs in workflow send_message
A workflow's send_message posts as the workflow owner and checks may_post directly, skipping the shared channel gate, so an agent could save a workflow that posts into a DM its direct writes are refused in. The sink now applies the same owner-only DM rule before persisting. Co-authored-by: Hertz <c6741012416ef5c49a865db79682113385c5fb998e8c890234fb442b59e22748@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Joah Gerstenberg <[email protected]>
1 parent 2c6bcb9 commit 1ce727c

2 files changed

Lines changed: 102 additions & 1 deletion

File tree

‎crates/buzz-relay/src/handlers/ingest.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ pub(crate) fn agent_dm_allowed(agent: &[u8], owner: &[u8], participants: &[Vec<u
977977

978978
/// Agents never write in a DM, except one whose members are exactly the agent
979979
/// and its owner. Humans and non-DM channels pass untouched.
980-
async fn check_agent_dm_write(
980+
pub(crate) async fn check_agent_dm_write(
981981
community_id: CommunityId,
982982
state: &AppState,
983983
ch_id: Uuid,

‎crates/buzz-relay/src/workflow_sink.rs‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,21 @@ impl ActionSink for RelayActionSink {
298298
"workflow owner cannot post in destination channel".into(),
299299
));
300300
}
301+
crate::handlers::ingest::check_agent_dm_write(
302+
tenant.community(),
303+
&state,
304+
channel_uuid,
305+
&author_pubkey_bytes,
306+
&channel,
307+
)
308+
.await
309+
.map_err(|e| {
310+
if e == crate::handlers::ingest::AGENT_DM_REFUSED {
311+
ActionSinkError::InvalidInput(e)
312+
} else {
313+
ActionSinkError::Database(e)
314+
}
315+
})?;
301316

302317
// 3. Build kind:9 Nostr event
303318
// - Signed by relay keypair (event.pubkey = relay pubkey)
@@ -1372,6 +1387,92 @@ mod postgres_tests {
13721387
);
13731388
}
13741389

1390+
#[tokio::test]
1391+
#[ignore = "requires Postgres"]
1392+
async fn workflow_send_message_keeps_agents_out_of_dms() {
1393+
let state = test_state().await;
1394+
let (owner, agent, stranger) = (
1395+
nostr::Keys::generate(),
1396+
nostr::Keys::generate(),
1397+
nostr::Keys::generate(),
1398+
);
1399+
let host = format!("wf-agent-dm-{}.example", uuid::Uuid::new_v4().simple());
1400+
let community = match state
1401+
.db
1402+
.create_community_with_owner(
1403+
&host,
1404+
&owner.public_key().to_hex(),
1405+
&nostr::Keys::generate(),
1406+
)
1407+
.await
1408+
.map(|write| write.outcome)
1409+
.expect("create community")
1410+
{
1411+
CreateCommunityWithOwnerResult::Created(rec) => rec.id,
1412+
other => panic!("expected fresh community, got {other:?}"),
1413+
};
1414+
for keys in [&owner, &agent, &stranger] {
1415+
state
1416+
.db
1417+
.ensure_user(community, &keys.public_key().to_bytes())
1418+
.await
1419+
.expect("ensure user");
1420+
}
1421+
state
1422+
.db
1423+
.set_agent_owner_for_authorization(
1424+
community,
1425+
&agent.public_key().to_bytes(),
1426+
&owner.public_key().to_bytes(),
1427+
)
1428+
.await
1429+
.expect("link agent owner");
1430+
let open_dm = |members: [&nostr::Keys; 2]| {
1431+
let state = state.clone();
1432+
let keys: Vec<[u8; 32]> = members.iter().map(|k| k.public_key().to_bytes()).collect();
1433+
async move {
1434+
let refs: Vec<&[u8]> = keys.iter().map(|k| k.as_slice()).collect();
1435+
state
1436+
.db
1437+
.open_dm(community, &refs, refs[0])
1438+
.await
1439+
.expect("open dm")
1440+
.0
1441+
.id
1442+
}
1443+
};
1444+
let stranger_dm = open_dm([&agent, &stranger]).await;
1445+
let owner_dm = open_dm([&agent, &owner]).await;
1446+
let sink = RelayActionSink::new(&state);
1447+
let agent_hex = agent.public_key().to_hex();
1448+
1449+
let err = sink
1450+
.send_message(
1451+
community,
1452+
&stranger_dm.to_string(),
1453+
"hi",
1454+
"hi",
1455+
&agent_hex,
1456+
None,
1457+
)
1458+
.await
1459+
.expect_err("an agent's workflow must not post in a DM with a stranger");
1460+
assert!(
1461+
matches!(&err, ActionSinkError::InvalidInput(m) if m == crate::handlers::ingest::AGENT_DM_REFUSED),
1462+
"{err:?}"
1463+
);
1464+
sink.send_message(
1465+
community,
1466+
&owner_dm.to_string(),
1467+
"hi",
1468+
"hi",
1469+
&agent_hex,
1470+
None,
1471+
)
1472+
.await
1473+
.expect("an agent's workflow may post in its owner-only DM");
1474+
}
1475+
13751476
#[tokio::test]
13761477
#[ignore = "requires Postgres"]
13771478
async fn workflow_reply_to_missing_parent_errors() {

0 commit comments

Comments
 (0)