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:
Maurice
2026-08-20 17:13:33 +02:00
committed by Maurice
parent eae8480728
commit 8f171453d3
5 changed files with 266 additions and 10 deletions
+24 -1
View File
@@ -3,6 +3,7 @@ import type { TrekWsTripEventName } from '@trek/shared'
import type { TripStoreState } from '../tripStore'
import type { Assignment, Place, Day, DayNote, PackingItem, TodoItem, BudgetItem, BudgetItemMember, Reservation, Trip, TripFile, WebSocketEvent } from '../../types'
import { offlineDb } from '../../db/offlineDb'
import { useAuthStore } from '../authStore'
type SetState = StoreApi<TripStoreState>['setState']
type GetState = StoreApi<TripStoreState>['getState']
@@ -27,8 +28,30 @@ const writeDayById: DexieWriter = async (payload, state) => {
const day = payload.day as Day
await _writeDayToDb(day.id, state)
}
/**
* Cache a packing item — unless it is somebody else's private one (#1976).
*
* The server scopes these events to the people who may see the item, so in
* ordinary running this never has anything to refuse. It refuses anyway, for
* two reasons. A leak on the wire used to become permanent here: the write is a
* put, the offline read hands back every cached row for the trip, and nothing
* prunes, so one stray event put another member's item into this browser for
* good. And a member whose access to a shared item is withdrawn should lose the
* copy they already have, which is the same check.
*
* Deleting rather than skipping is the part that matters: skipping would leave
* a row that arrived before this existed sitting there forever.
*/
const putPackingItem: DexieWriter = async payload => {
await offlineDb.packingItems.put(payload.item as PackingItem)
const item = payload.item as PackingItem
const me = useAuthStore.getState().user?.id
const mine = !item?.is_private || item.owner_id == null || item.owner_id === me
|| (item.recipients || []).some(r => r.user_id === me)
if (!mine) {
await offlineDb.packingItems.delete(item.id)
return
}
await offlineDb.packingItems.put(item)
}
const putTodoItem: DexieWriter = async payload => {
await offlineDb.todoItems.put(payload.item as TodoItem)