fix(dms): close the moderation, agent-policy and one-person identity gaps

- Opening a DM can no longer put anyone else in the relay's moderation DM.
- Opening a DM checks each agent's add policy unless it is just the caller
  and that one agent.
- An agent joining a DM of at most one person moves its identity to all its
  members, so reopening the original pair starts a new DM.

Signed-off-by: morgmart <98432065+morgmart@users.noreply.github.com>
Co-authored-by: Carl <c217fe6b9d958f41c3a5e030dccc7f626775a923089cb6491305eade75ea1f1b@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
morgmart
2026-11-05 13:52:37 -08:00
parent 4b1ad7f5c4
commit fa2842048c
3 changed files with 92 additions and 14 deletions

View File

@@ -496,9 +496,9 @@ pub enum DmGrowth {
/// Add `additions` to the DM `channel_id` in place, keeping its history.
///
/// Agents join without changing which DM this is. When people join, the DM's
/// identity moves to the grown set of people ([`dm_identity_hash`]), so
/// opening a DM with the original people afterwards starts a new one. If a DM
/// Agents join a DM of two or more people without changing which DM it is.
/// Otherwise the DM's identity moves to the grown set ([`dm_identity_hash`]), so
/// opening a DM with the original members afterwards starts a new one. If a DM
/// with the grown set already exists, nothing is written and that DM is
/// returned as [`DmGrowth::Existing`], provided `actor` is in it.
///
@@ -586,8 +586,11 @@ pub async fn grow_dm(
)));
}
if count_people(&added, &agents) > 0 {
let hash = dm_identity_hash(&grown, &agents);
// People move the DM's identity to the grown set of people. So do agents
// joining a DM of at most one person, which is identified by all members.
let hash = dm_identity_hash(&grown, &agents);
let current: Vec<&[u8]> = members.iter().map(Vec::as_slice).collect();
if hash != dm_identity_hash(&current, &agents) {
let existing = sqlx::query_scalar::<_, Uuid>(
"SELECT id FROM channels \
WHERE community_id = $1 AND participant_hash = $2 AND id <> $3 \

View File

@@ -412,6 +412,9 @@ async fn announce_dm_created(
}
}
/// Why the relay's moderation DM cannot take anyone else.
const MODERATION_DM_REFUSAL: &str = "invalid: this DM cannot be changed";
/// The agents among `pubkeys` (users with an owner): each one's
/// `channel_add_policy` and owner.
async fn dm_agents(
@@ -467,12 +470,21 @@ async fn handle_dm_open(
));
}
// 2. At most 8 other people (9 total); agents do not count. Putting an
// agent in a DM with other people needs its channel_add_policy; a DM
// of just the caller and agents does not.
// 2. The relay's moderation DM stays two-party. At most 8 other people
// (9 total); agents do not count. Putting an agent in a DM with anyone
// besides the caller needs its channel_add_policy; messaging one alone
// does not.
let relay_pubkey = state.relay_keypair.public_key().to_bytes();
if others.len() > 1
&& others
.iter()
.any(|pk| pk.as_slice() == relay_pubkey.as_slice())
{
return Err(IngestError::Rejected(MODERATION_DM_REFUSAL.into()));
}
let agents = dm_agents(tenant, state, &others).await?;
let other_people = others.iter().filter(|pk| !agents.contains_key(*pk)).count();
if other_people > 0 {
if others.len() > 1 {
check_dm_agent_policies(&agents, &self_bytes)?;
}
if other_people > MAX_DM_PEOPLE - 1 {
@@ -641,9 +653,7 @@ async fn handle_dm_add_member(
.iter()
.any(|m| m.as_slice() == relay_pubkey.as_slice())
{
return Err(IngestError::Rejected(
"invalid: this DM cannot be changed".into(),
));
return Err(IngestError::Rejected(MODERATION_DM_REFUSAL.into()));
}
let mut additions: Vec<Vec<u8>> = Vec::new();

View File

@@ -477,8 +477,9 @@ async fn growing_does_not_reveal_a_dm_the_actor_is_not_in() {
);
}
/// The relay's moderation DM stays two-party.
/// Mutation: drop the relay-key guard → a person joins → RED.
/// The relay's moderation DM stays two-party, whether added to or opened with
/// someone else.
/// Mutation: drop either relay-key guard → a member joins → RED.
#[tokio::test]
#[ignore = "requires Postgres"]
async fn the_moderation_dm_cannot_grow() {
@@ -500,6 +501,16 @@ async fn the_moderation_dm_cannot_grow() {
add(&state, &tenant, &a, dm.id, &[&b], false).await,
"cannot be changed",
);
let agent = Keys::generate();
make_agent(&state, &tenant, &agent, &a, "anyone").await;
rejected(
open(&state, &tenant, &a, &[&state.relay_keypair, &agent]).await,
"cannot be changed",
);
assert_eq!(
members(&state, &tenant, dm.id).await,
set_of(&[&a, &state.relay_keypair])
);
}
/// Only `include` is a valid history value.
@@ -713,3 +724,57 @@ async fn kicking_an_agent_keeps_a_group_dm_its_identity() {
own
);
}
/// Opening a DM that puts someone else's agent with anyone besides the caller
/// follows its add policy, even when no other person is in it.
/// Mutation: check policies only when people are named → the DM opens → RED.
#[tokio::test]
#[ignore = "requires Postgres"]
async fn opening_with_several_agents_follows_their_add_policy() {
let state = state().await;
let tenant = community(&state, "dm-agents-policy").await;
let (a, b) = (Keys::generate(), Keys::generate());
let (theirs, mine) = (Keys::generate(), Keys::generate());
make_agent(&state, &tenant, &theirs, &b, "nobody").await;
make_agent(&state, &tenant, &mine, &a, "owner_only").await;
rejected(
open(&state, &tenant, &a, &[&theirs, &mine]).await,
"policy:nobody",
);
}
/// Adding an agent to a DM of one person and their agent moves its identity
/// to all its members: the original pair gets a new DM.
/// Mutation: keep the old identity → reopening the pair finds the grown DM →
/// RED.
#[tokio::test]
#[ignore = "requires Postgres"]
async fn an_agent_joining_a_one_person_dm_moves_its_identity() {
let state = state().await;
let tenant = community(&state, "dm-one-person-grow").await;
let (a, b) = (Keys::generate(), Keys::generate());
let (mine, theirs) = (Keys::generate(), Keys::generate());
make_agent(&state, &tenant, &mine, &a, "anyone").await;
make_agent(&state, &tenant, &theirs, &b, "anyone").await;
let dm = open(&state, &tenant, &a, &[&mine]).await.expect("open");
let (target, _) = add(&state, &tenant, &a, dm, &[&theirs], false)
.await
.expect("add agent");
assert_eq!(target, dm);
assert_eq!(
members(&state, &tenant, dm).await,
set_of(&[&a, &mine, &theirs])
);
assert_ne!(
open(&state, &tenant, &a, &[&mine]).await.expect("reopen"),
dm
);
assert_eq!(
open(&state, &tenant, &a, &[&mine, &theirs])
.await
.expect("grown"),
dm
);
}