mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
fix(packing): keep a private item off the other members' screens over MCP
A restricted packing item, changed through an MCP tool, went to every socket in the trip room. Ticking something off your own list through an assistant pushed that row to every other member. The REST and RPC surfaces have scoped these events since #858 — both go through emitToViewers, which delivers to the owner and the recipients and nobody else. The MCP tools called safeBroadcast, which took three arguments and had no way to say who may see the thing that changed. So safeBroadcast learned that fourth argument, and the packing tools pass viewersOf(item), which is the same answer the REST route asks for. A shared item still answers null and still goes to the room in a three-argument call, so nothing about it changes — including what the hundred-odd broadcast mocks in the suite record. The client stops trusting the wire on this too. It used to `put` whatever arrived, and the offline read hands back every cached row for the trip with no privacy filter, so a single stray event lodged another member's item in that browser permanently. It now drops a restricted item that is neither mine nor shared with me — deleting rather than skipping, so a list that was already leaked into heals itself the next time an event about that item comes past. Closes #1976
This commit is contained in:
@@ -19,15 +19,41 @@ export class McpToolGuardsService {
|
||||
private readonly realtime: RealtimeService,
|
||||
) {}
|
||||
|
||||
safeBroadcast(tripId: number, event: string, payload: Record<string, unknown>): void {
|
||||
/**
|
||||
* Broadcast a tool's change to the trip room.
|
||||
*
|
||||
* `onlyUserIds` scopes delivery the way the REST routes already scope theirs:
|
||||
* pass the users who may see the thing that changed, and the event is sent to
|
||||
* those users' sockets rather than to everyone in the room. Omitting it keeps
|
||||
* the room-wide behaviour, which is right for anything the whole trip shares.
|
||||
*
|
||||
* It is optional rather than required because most tools change something
|
||||
* everybody can see — but a tool that touches a restricted row and forgets it
|
||||
* pushes that row onto every other member's screen, which is #1976: a private
|
||||
* packing item, ticked off through MCP, landed in every other member's
|
||||
* IndexedDB and stayed there.
|
||||
*/
|
||||
safeBroadcast(tripId: number, event: string, payload: Record<string, unknown>, onlyUserIds?: number[] | null): void {
|
||||
try {
|
||||
// Legacy MCP call sites pass event names as plain strings; route through
|
||||
// the facade's implementation signature (its typed overloads are for new
|
||||
// call sites). Runtime is a pure pass-through to src/websocket's
|
||||
// broadcast, so the 100+ per-file vi.mock stubs keep intercepting.
|
||||
(this.realtime.broadcast as (t: number | string, e: string, p: Record<string, unknown>) => void)(
|
||||
tripId, event, { ...payload, _source: 'mcp' },
|
||||
);
|
||||
const send = this.realtime.broadcast as (
|
||||
t: number | string, e: string, p: Record<string, unknown>, sid?: number | string, uid?: number,
|
||||
) => void;
|
||||
const body = { ...payload, _source: 'mcp' };
|
||||
|
||||
// Room-wide, and called with three arguments exactly as before: the
|
||||
// facade preserves its caller's arity, and padding the optionals with
|
||||
// explicit undefineds would change what every existing mock records.
|
||||
if (onlyUserIds == null) {
|
||||
send(tripId, event, body);
|
||||
return;
|
||||
}
|
||||
for (const uid of new Set(onlyUserIds)) {
|
||||
if (uid != null) send(tripId, event, body, undefined, uid);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`[MCP] broadcast failed for ${event}:`, (err as Error | undefined)?.message ?? err);
|
||||
}
|
||||
|
||||
@@ -61,7 +61,10 @@ export class PackingMcp {
|
||||
if (!this.packing.verifyTripAccess(tripId, ctx.userId)) return noAccess();
|
||||
if (!this.guards.hasTripPermission('packing_edit', tripId, ctx.userId)) return permissionDenied();
|
||||
const item = this.packing.createItem(tripId, { name, category: category || 'General' }, ctx.userId);
|
||||
this.guards.safeBroadcast(tripId, 'packing:created', { item });
|
||||
// A tool-made item is Common today, so this asks the room. Routed through
|
||||
// viewersOf anyway, so the day createItem learns to make a restricted one
|
||||
// this does not have to be remembered a second time.
|
||||
this.guards.safeBroadcast(tripId, 'packing:created', { item }, this.packing.viewersOf(item));
|
||||
return ok({ item });
|
||||
}
|
||||
|
||||
@@ -83,7 +86,9 @@ export class PackingMcp {
|
||||
if (!this.guards.hasTripPermission('packing_edit', tripId, ctx.userId)) return permissionDenied();
|
||||
const item = this.packing.updateItem(tripId, itemId, { checked: checked ? 1 : 0 }, ['checked'], undefined, ctx.userId);
|
||||
if (!item) return errorResult('Packing item not found.');
|
||||
this.guards.safeBroadcast(tripId, 'packing:updated', { item });
|
||||
// Scoped to the people who may see it, exactly as the REST route does
|
||||
// (#858, #1976). A Common item answers null here and goes to the room.
|
||||
this.guards.safeBroadcast(tripId, 'packing:updated', { item }, this.packing.viewersOf(item));
|
||||
return ok({ item });
|
||||
}
|
||||
|
||||
@@ -104,7 +109,9 @@ export class PackingMcp {
|
||||
if (!this.guards.hasTripPermission('packing_edit', tripId, ctx.userId)) return permissionDenied();
|
||||
const deleted = this.packing.deleteItem(tripId, itemId, ctx.userId);
|
||||
if (!deleted) return errorResult('Packing item not found.');
|
||||
this.guards.safeBroadcast(tripId, 'packing:deleted', { itemId });
|
||||
// deleteItem hands back the row it removed, so the delete can be scoped to
|
||||
// the same people the item was ever visible to (#1976).
|
||||
this.guards.safeBroadcast(tripId, 'packing:deleted', { itemId }, this.packing.viewersOf(deleted));
|
||||
return ok({ success: true });
|
||||
}
|
||||
|
||||
@@ -130,7 +137,7 @@ export class PackingMcp {
|
||||
const bodyKeys = ['name', 'category'].filter(k => k === 'name' ? name !== undefined : category !== undefined);
|
||||
const item = this.packing.updateItem(tripId, itemId, { name, category }, bodyKeys, undefined, ctx.userId);
|
||||
if (!item) return errorResult('Packing item not found.');
|
||||
this.guards.safeBroadcast(tripId, 'packing:updated', { item });
|
||||
this.guards.safeBroadcast(tripId, 'packing:updated', { item }, this.packing.viewersOf(item));
|
||||
return ok({ item });
|
||||
}
|
||||
|
||||
@@ -408,7 +415,9 @@ export class PackingMcp {
|
||||
if (!this.packing.verifyTripAccess(tripId, ctx.userId)) return noAccess();
|
||||
if (!this.guards.hasTripPermission('packing_edit', tripId, ctx.userId)) return permissionDenied();
|
||||
const created = this.packing.bulkImport(tripId, items, ctx.userId);
|
||||
for (const item of created) this.guards.safeBroadcast(tripId, 'packing:created', { item });
|
||||
for (const item of created) {
|
||||
this.guards.safeBroadcast(tripId, 'packing:created', { item }, this.packing.viewersOf(item));
|
||||
}
|
||||
return ok({ items: created, count: created.length });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user