Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ This project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

Packages without a separate changelog are covered by the cross-package notes below.

## [Unreleased]
## [Unreleased - Patch]

### Fixed

- DM and group DM participant rules now apply consistently for agent tokens across the channel, message, search and console routes, matching the `/v1/dm/…` routes. Workspace keys and observer tokens are unchanged.

## [12.13.0] - 2026-11-17

Expand Down
6 changes: 5 additions & 1 deletion packages/engine/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ See the [root changelog](../../CHANGELOG.md) for cross-package release highlight
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [Unreleased - Patch]

### Fixed

- DM participant rules are applied consistently for agent tokens. For an agent that is not an active participant of a DM or group DM, `/v1/channels/:name` (detail, messages, members, read-status, update, topic, archive, leave, mute, unmute) and `/v1/messages/:id` (get, replies, reactions, readers) answer `404`, as does posting to the channel, including a node token posting as a non-participant `from`. `join` and `invite` are refused on DM-backed channels for every agent; add group DM members through `/v1/dm/:conversation_id/participants`. `GET /v1/search` and `GET /v1/console/messages` drop DM rows the agent does not participate in.

## [12.13.0] - 2026-11-17

Expand Down
248 changes: 248 additions & 0 deletions packages/engine/src/__tests__/conformance/dmChannelRouteAccess.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import {
createWorkspace,
D1_MAX_BOUND_PARAMETERS,
enforceD1ParameterLimit,
makeNodeStack,
registerAgent,
type TestStack,
} from './harness.js';
import { conformancePgDb } from './pgMode.js';

// DM and group-DM conversations live on ordinary channel rows, so the
// channel/message/search/console routes reach them. An agent that is not a
// participant must see none of it, and must not be able to write into it.

interface Envelope<T> { ok: boolean; data: T; error?: { code: string } }

describe('DM content through channel and message routes', () => {
let stack: TestStack;

beforeEach(() => { stack = makeNodeStack(); });
afterEach(() => stack.close());

async function call<T = unknown>(method: string, path: string, token: string, body?: unknown) {
const res = await stack.app.request(`/v1${path}`, {
method,
headers: {
authorization: `Bearer ${token}`,
...(body === undefined ? {} : { 'content-type': 'application/json' }),
},
body: body === undefined ? undefined : JSON.stringify(body),
});
const text = await res.text();
const json = (text ? JSON.parse(text) : {}) as Envelope<T>;
return { status: res.status, data: json.data, text };
}

async function setup() {
const ws = await createWorkspace(stack.app, 'dm-route-access-ws');
const alice = await registerAgent(stack.app, ws.workspaceKey, 'alice');
const bob = await registerAgent(stack.app, ws.workspaceKey, 'bob');
const carol = await registerAgent(stack.app, ws.workspaceKey, 'carol');
const dave = await registerAgent(stack.app, ws.workspaceKey, 'dave');

const dm = await call<{ id: string; conversation_id: string }>(
'POST', '/dm', alice.token, { to: 'bob', text: 'pairsecret one' },
);
expect(dm.status).toBe(201);
const group = await call<{ id: string }>(
'POST', '/dm/group', alice.token, { participants: ['bob', 'dave'], name: 'private' },
);
expect(group.status).toBe(201);
const groupMessage = await call<{ id: string }>(
'POST', `/dm/${group.data.id}/messages`, alice.token, { text: 'groupsecret two' },
);
expect(groupMessage.status).toBe(201);

// The workspace key is the admin view; use it to learn the backing channel names.
const all = await call<Array<{ id: string; type: string }>>('GET', '/dm/conversations/all', ws.workspaceKey);
const pairConversationId = all.data.find((conversation) => conversation.type === '1:1')!.id;
const pairChannel = `dm-${pairConversationId.replace(/^dm_/, '')}`;
const groupChannel = `group-dm-${group.data.id}`;

return {
ws, alice, bob, carol, dave,
pair: { channel: pairChannel, conversationId: pairConversationId, messageId: String(dm.data.id) },
group: { channel: groupChannel, conversationId: group.data.id, messageId: String(groupMessage.data.id) },
};
}

it('hides DM-backed channels and their messages from a non-participant agent', async () => {
const { carol, pair, group } = await setup();

for (const target of [pair, group]) {
for (const path of [
`/channels/${target.channel}`,
`/channels/${target.channel}/messages`,
`/channels/${target.channel}/members`,
`/channels/${target.channel}/read-status`,
`/messages/${target.messageId}`,
`/messages/${target.messageId}/replies`,
`/messages/${target.messageId}/reactions`,
`/messages/${target.messageId}/readers`,
]) {
const res = await call('GET', path, carol.token);
expect(res.status, `GET ${path}`).toBe(404);
expect(res.text).not.toContain('secret');
}
}
});

it('keeps DM content out of search and console logs for a non-participant agent', async () => {
const { carol, bob, alice } = await setup();
const publicPost = await call('POST', '/channels/general/messages', alice.token, { text: 'pairsecret in public' });
expect(publicPost.status).toBe(201);

for (const q of ['pairsecret', 'groupsecret']) {
const outsider = await call<Array<{ text: string; conversation_id: string | null }>>(
'GET', `/search?q=${q}`, carol.token,
);
expect(outsider.status).toBe(200);
expect(outsider.data.every((hit) => !hit.conversation_id)).toBe(true);
expect(outsider.text).not.toContain('one');
expect(outsider.text).not.toContain('two');
}
// Regular channel hits are unaffected.
const publicHits = await call<Array<{ text: string }>>('GET', '/search?q=pairsecret', carol.token);
expect(publicHits.data.map((hit) => hit.text)).toEqual(['pairsecret in public']);

const participant = await call<Array<{ text: string }>>('GET', '/search?q=groupsecret', bob.token);
expect(participant.data.map((hit) => hit.text)).toContain('groupsecret two');

const outsiderLogs = await call<Array<{ conversation_id?: string | null }>>(
'GET', '/console/messages', carol.token,
);
expect(outsiderLogs.status).toBe(200);
expect(outsiderLogs.data.length).toBeGreaterThan(0);
expect(outsiderLogs.data.every((log) => !log.conversation_id)).toBe(true);
const dmOnly = await call<unknown[]>('GET', '/console/messages?delivery_kind=dm', carol.token);
expect(dmOnly.data).toHaveLength(0);

const participantLogs = await call<Array<{ conversation_id?: string | null }>>(
'GET', '/console/messages?delivery_kind=dm', bob.token,
);
expect(participantLogs.data.length).toBeGreaterThan(0);
});

it('refuses writes into a DM by a non-participant agent', async () => {
const { bob, carol, pair, group } = await setup();

for (const target of [pair, group]) {
const writes: Array<[string, string, unknown?]> = [
['POST', `/channels/${target.channel}/messages`, { text: 'injected' }],
['POST', `/channels/${target.channel}/join`],
['POST', `/channels/${target.channel}/leave`],
['POST', `/channels/${target.channel}/invite`, { agent_name: 'carol' }],
['POST', `/channels/${target.channel}/mute`],
['PATCH', `/channels/${target.channel}`, { topic: 'x' }],
['DELETE', `/channels/${target.channel}`],
['POST', `/messages/${target.messageId}/replies`, { text: 'injected' }],
['POST', `/messages/${target.messageId}/reactions`, { emoji: 'eyes' }],
['POST', `/messages/${target.messageId}/read`],
];
for (const [method, path, body] of writes) {
const res = await call(method, path, carol.token, body);
expect(res.status, `${method} ${path}`).toBe(404);
}

const history = await call('GET', `/dm/${target.conversationId}/messages`, bob.token);
expect(history.status).toBe(200);
expect(history.text).not.toContain('injected');
}
});

it('does not let a participant pull an outsider in through channel membership', async () => {
const { alice, carol, pair, group } = await setup();

for (const target of [pair, group]) {
const join = await call('POST', `/channels/${target.channel}/join`, alice.token);
expect(join.status).toBe(404);
const invite = await call('POST', `/channels/${target.channel}/invite`, alice.token, { agent_name: 'carol' });
expect(invite.status).toBe(404);
const stillHidden = await call('GET', `/channels/${target.channel}/messages`, carol.token);
expect(stillHidden.status).toBe(404);
}
});

it('refuses a channel leave on a DM-backed channel and keeps the participant in the DM', async () => {
const { alice, pair, group } = await setup();

for (const target of [pair, group]) {
const leave = await call('POST', `/channels/${target.channel}/leave`, alice.token);
expect(leave.status).toBe(404);
const history = await call('GET', `/channels/${target.channel}/messages`, alice.token);
expect(history.status).toBe(200);
}
});

it('leaves participants and the workspace key able to read by channel name and message id', async () => {
const { ws, bob, pair, group } = await setup();

for (const token of [bob.token, ws.workspaceKey]) {
for (const target of [pair, group]) {
const history = await call('GET', `/channels/${target.channel}/messages`, token);
expect(history.status).toBe(200);
expect(history.text).toContain('secret');
const message = await call('GET', `/messages/${target.messageId}`, token);
expect(message.status).toBe(200);
}
}
});

it('stops applying once a participant has left a group DM', async () => {
const { dave, group } = await setup();
const before = await call('GET', `/messages/${group.messageId}`, dave.token);
expect(before.status).toBe(200);

const leave = await call('DELETE', `/dm/${group.conversationId}/participants/dave`, dave.token);
expect(leave.status).toBeLessThan(300);

const after = await call('GET', `/messages/${group.messageId}`, dave.token);
expect(after.status).toBe(404);
});

it('keeps a DM posted through the channel route in its participants\' console logs', async () => {
const { alice, bob, carol, pair } = await setup();
const post = await call('POST', `/channels/${pair.channel}/messages`, alice.token, { text: 'viachannel three' });
expect(post.status).toBe(201);

for (const participant of [alice, bob]) {
const logs = await call<Array<{ text: string }>>('GET', '/console/messages?limit=100', participant.token);
expect(logs.status).toBe(200);
expect(logs.data.map((log) => log.text)).toContain('viachannel three');
}
const outsider = await call('GET', '/console/messages?limit=100', carol.token);
expect(outsider.status).toBe(200);
expect(outsider.text).not.toContain('viachannel');
});

it('filters a full page of distinct DM conversations inside the D1 parameter limit', async () => {
const ws = await createWorkspace(stack.app, 'dm-route-access-d1-ws');
const hub = await registerAgent(stack.app, ws.workspaceKey, 'hub');
const outsider = await registerAgent(stack.app, ws.workspaceKey, 'outsider');
const PEERS = 100;
for (let index = 0; index < PEERS; index += 1) {
const peer = await registerAgent(stack.app, ws.workspaceKey, `peer-${index}`);
const dm = await call('POST', '/dm', peer.token, { to: 'hub', text: `fanword ${index}` });
expect(dm.status).toBe(201);
}

// The guard patches the SQLite handle, which PostgreSQL mode does not have;
// the result checks below still run there.
const guard = conformancePgDb() ? undefined : enforceD1ParameterLimit(stack);
try {
for (const path of ['/search?q=fanword&limit=100', '/console/messages?limit=100&delivery_kind=dm']) {
const mine = await call<unknown[]>('GET', path, hub.token);
expect(mine.status, path).toBe(200);
expect(mine.data, path).toHaveLength(PEERS);
const theirs = await call<unknown[]>('GET', path, outsider.token);
expect(theirs.status, path).toBe(200);
expect(theirs.data, path).toHaveLength(0);
}
if (guard) expect(guard.maxObserved()).toBeLessThanOrEqual(D1_MAX_BOUND_PARAMETERS);
} finally {
guard?.restore();
}
});
});
Loading
Loading