Move packing onto the decorators and de-duplicate its privacy fan-out

Nine more methods, so the registry owns 24 of 113.

The #858 privacy logic existed three times: PackingService had the primitives, the
controller layered emitToViewers and broadcastUpdate on top privately, and the
plugin deps factory carried a standalone copy of all four, with a comment asking
for them to be kept in lockstep by hand. Migrating packing meant either copying
them a fourth time or fixing that, so the two private controller methods moved
into PackingService and the factory copy is gone. Controller and PackingRpc now
call the same code, and the controller suite proves it by passing with all 45
assertions unchanged.

The four public/private transitions get one test each, because each one is a way
to leak a private item to the whole trip room, plus a test that the privacy is
read before the write rather than after: reading it afterwards would report
"already private", skip the room delete, and leave the item on every member's
screen.
This commit is contained in:
Maurice
2026-08-09 12:29:34 +02:00
committed by Maurice
parent 62978aab5a
commit ca88676858
13 changed files with 467 additions and 357 deletions
@@ -585,13 +585,6 @@ describe('host-deps factory — planner write + metadata deps', () => {
expect((await call(h, 'costs.delete', { tripId: 1, itemId: 404 })).error.code).toBe('RESOURCE_FORBIDDEN');
});
it('packing/files read deps delegate to their services (trash excluded for files)', async () => {
const h = host('db:read:packing', 'db:read:files');
// acting user 5 is threaded to the packing service (#858 private-item filter); _uid proves it
expect((await call(h, 'packing.list', { tripId: 1 })).result).toEqual([{ id: 1, trip_id: 1, name: 'Socks', _uid: 5 }]);
expect((await call(h, 'files.list', { tripId: 1 })).result).toEqual([{ id: 2, trip_id: 1, trash: false }]);
});
it('users.getById is scoped to people the acting user shares a trip with', async () => {
const h = host('db:read:users');
expect((await call(h, 'users.getById', { id: 6 }, 5)).ok).toBe(true); // 5 (owner) + 6 (member) share trip 1
@@ -714,68 +707,9 @@ describe('host-deps factory — reservations, day notes, cross-trip + addon read
});
});
describe('host-deps factory — packing write with #858 privacy-scoped broadcasts', () => {
const host = () => createRealRpcHost('pk', new Set(['db:write:packing']))
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const call = async (method: string, params: Record<string, unknown>, uid = 5): Promise<any> =>
host().dispatch({ k: 'req', id: 'x', method, params }, uid)
// the onlyUserId (5th arg) of every broadcast for `event` since the last clear:
// undefined = whole trip room, a number = that user's sockets only.
const fanout = (event: string) => broadcast.mock.calls.filter((c) => c[1] === event).map((c) => c[4])
beforeEach(() => { checkPermission.mockReset(); checkPermission.mockReturnValue(true); isAddonEnabled.mockReset(); isAddonEnabled.mockReturnValue(true); broadcast.mockClear() })
afterAll(() => closePluginDataDb('pk'))
it('create: Common -> whole room; Personal -> owner-only; Shared -> owner + recipients', async () => {
expect((await call('packing.create', { tripId: 1, input: { name: 'Common', visibility: 'common' } })).ok).toBe(true)
expect(fanout('packing:created')).toEqual([undefined]) // whole room
broadcast.mockClear()
expect((await call('packing.create', { tripId: 1, input: { name: 'Mine', visibility: 'personal' } })).ok).toBe(true)
expect(fanout('packing:created')).toEqual([5]) // owner-only
broadcast.mockClear()
expect((await call('packing.create', { tripId: 1, input: { name: 'Ours', visibility: 'shared', recipient_ids: [6] } })).ok).toBe(true)
expect([...fanout('packing:created')].sort()).toEqual([5, 6]) // owner + recipient, never the room
})
it('update: the four public<->private transitions route correctly (never leaks a privatized item)', async () => {
await call('packing.update', { tripId: 1, itemId: 71, input: { is_private: true } }) // stays private (71 seeded private)
expect(fanout('packing:updated')).toEqual([5])
expect(fanout('packing:deleted')).toEqual([])
expect(fanout('packing:created')).toEqual([])
broadcast.mockClear()
await call('packing.update', { tripId: 1, itemId: 70, input: { is_private: true } }) // public -> private (70 seeded public)
expect(fanout('packing:deleted')).toEqual([undefined]) // drop from the room FIRST (the anti-leak)
expect(fanout('packing:created')).toEqual([5]) // then re-add owner-only
broadcast.mockClear()
await call('packing.update', { tripId: 1, itemId: 71, input: { is_private: false } }) // private -> public
expect(fanout('packing:created')).toEqual([undefined])
expect(fanout('packing:updated')).toEqual([undefined])
broadcast.mockClear()
await call('packing.update', { tripId: 1, itemId: 70, input: { is_private: false } }) // stays public
expect(fanout('packing:updated')).toEqual([undefined])
expect(fanout('packing:deleted')).toEqual([])
})
it('update: a stale-write conflict is BAD_PARAMS and never broadcasts', async () => {
const res = await call('packing.update', { tripId: 1, itemId: 99, input: { name: 'x' } })
expect((res as { error: { code: string } }).error.code).toBe('BAD_PARAMS')
expect(broadcast).not.toHaveBeenCalled()
})
it('delete: a private item is owner-scoped; a missing one is RESOURCE_FORBIDDEN', async () => {
await call('packing.delete', { tripId: 1, itemId: 71 })
expect(fanout('packing:deleted')).toEqual([5]) // owner-only (recipients get no packing:deleted)
broadcast.mockClear()
await call('packing.delete', { tripId: 1, itemId: 70 })
expect(fanout('packing:deleted')).toEqual([undefined]) // common -> room
const missing = await call('packing.delete', { tripId: 1, itemId: 404 })
expect((missing as { error: { code: string } }).error.code).toBe('RESOURCE_FORBIDDEN')
})
})
// The packing write deps left this factory with the decorator migration. The #858
// transitions are asserted in tests/unit/packing/packing.rpc.test.ts, against the
// PackingService methods the REST controller uses too, so there is one copy now.
describe('host-deps factory — Wave 1 wiring (weather/categories/tags/todos/roster/bags)', () => {
const host = (...perms: string[]) => createRealRpcHost('w1', new Set(perms))
@@ -800,15 +734,6 @@ describe('host-deps factory — Wave 1 wiring (weather/categories/tags/todos/ros
// todos.* left this factory with the decorator migration; the cases now run in
// tests/unit/todo/todo.rpc.test.ts against TodoRpc.
it('packing bags list/create/update/delete/setMembers run the wiring', async () => {
const h = host('db:write:packing')
expect((await call(h, 'packing.listBags', { tripId: 1 }, 5)).ok).toBe(true)
expect((await call(h, 'packing.createBag', { tripId: 1, input: { name: 'Bag' } }, 5)).ok).toBe(true)
expect((await call(h, 'packing.updateBag', { tripId: 1, bagId: 80, input: { name: 'X' } }, 5)).ok).toBe(true)
expect((await call(h, 'packing.setBagMembers', { tripId: 1, bagId: 80, userIds: [5] }, 5)).ok).toBe(true)
expect((await call(h, 'packing.deleteBag', { tripId: 1, bagId: 80 }, 5)).ok).toBe(true)
expect(((await call(h, 'packing.deleteBag', { tripId: 1, bagId: 404 }, 5)) as { error: { code: string } }).error.code).toBe('RESOURCE_FORBIDDEN')
})
})
describe('host-deps factory — Wave 2 wiring (atlas/vacay/journal/collections writes)', () => {
+11 -2
View File
@@ -54,8 +54,8 @@ describe('plugin RPC coverage ledger', () => {
// Update these two numbers in every rollout PR. They make the diff state the size
// of the move out loud, which is the number a reviewer wants to see.
const total = KNOWN_METHODS.length + UNCONDITIONAL_METHODS.length;
expect(fromRegistry.size).toBe(15);
expect(legacy.size).toBe(total - 15);
expect(fromRegistry.size).toBe(24);
expect(legacy.size).toBe(total - 24);
});
it('RPCLEDGER-006 the migrated methods are exactly the ones this PR claims', () => {
@@ -65,6 +65,15 @@ describe('plugin RPC coverage ledger', () => {
'daynotes.delete',
'daynotes.list',
'daynotes.update',
'packing.create',
'packing.createBag',
'packing.delete',
'packing.deleteBag',
'packing.list',
'packing.listBags',
'packing.setBagMembers',
'packing.update',
'packing.updateBag',
'rates.get',
'tags.create',
'tags.delete',
@@ -10,6 +10,11 @@ import { describe, it, expect, vi } from 'vitest';
import { PluginRpcHost } from '../../../src/nest/plugins/host/rpc-host';
import type { RpcRequest, RpcResponse } from '../../../src/nest/plugins/protocol/envelope';
import { makeDeps } from '../../helpers/rpc-host-deps';
import { createTestPluginRegistry } from '../../../src/nest/plugins/host/rpc-kit/testing';
import { PackingRpc } from '../../../src/nest/packing/packing.rpc';
import type { PackingService } from '../../../src/nest/packing/packing.service';
import type { PluginGuards } from '../../../src/nest/plugins/host/plugin-guards.service';
import type { RealtimeService } from '../../../src/nest/realtime/realtime.service';
const req = (method: string, params: Record<string, unknown>): RpcRequest => ({ k: 'req', id: 'x', method, params });
@@ -60,11 +65,23 @@ describe('dispatch strips the supervisor _inv marker', () => {
});
it('RPCINV-006 a handler reading the whole payload object no longer finds _inv in it', async () => {
const deps = makeDeps();
const host = new PluginRpcHost('p', new Set(['db:write:packing']), deps);
// packing.setBagMembers is the one handler that reads the params object as a
// whole (asPayload(p).userIds) rather than a named field off it.
// whole (asPayload(p).userIds) rather than a named field off it, which makes it
// the case where a stray _inv could actually change behaviour.
const setBagMembers = vi.fn(() => ({ bagId: 80, members: [3] }));
const packing = { setBagMembers } as unknown as PackingService;
const guards = {
requireActor: () => 42,
requireTripEdit: () => undefined,
} as unknown as PluginGuards;
const realtime = { broadcast: vi.fn() } as unknown as RealtimeService;
const host = new PluginRpcHost(
'p',
new Set(['db:write:packing']),
makeDeps(),
createTestPluginRegistry([new PackingRpc(packing, realtime, guards)]),
);
await host.dispatch(req('packing.setBagMembers', { tripId: 1, bagId: 80, userIds: [3], _inv: 'req-7' }), 42);
expect(deps.setPackingBagMembers).toHaveBeenCalledWith(1, 80, [3]);
expect(setBagMembers).toHaveBeenCalledWith('1', '80', [3]);
});
});
@@ -77,23 +77,6 @@ describe('PluginRpcHost — capability enforcement', () => {
expect((allowed as RpcResponse).result).toEqual([{ id: 7, name: 'Place' }]);
});
it('db:read:packing / db:read:files delegate to the service, membership-checked, and stay separate scopes', async () => {
const packing = new PluginRpcHost('p', new Set(['db:read:packing']), deps);
// no access to trip 2 → refused before the service is called
expect(((await packing.dispatch(req('packing.list', { tripId: 2 }), 42)) as RpcError).error.code).toBe('RESOURCE_FORBIDDEN');
expect(deps.listPackingItems).not.toHaveBeenCalled();
const okP = await packing.dispatch(req('packing.list', { tripId: 1 }), 42);
expect(ok(okP)).toBe(true);
// the acting user is threaded through so packing's #858 private-item filter applies
expect(deps.listPackingItems).toHaveBeenCalledWith(1, 42);
// the packing scope does NOT unlock files
expect(((await packing.dispatch(req('files.list', { tripId: 1 }), 42)) as RpcError).error.code).toBe('PERMISSION_DENIED');
const files = new PluginRpcHost('p', new Set(['db:read:files']), deps);
const okF = await files.dispatch(req('files.list', { tripId: 1 }), 42);
expect((okF as RpcResponse).result).toEqual([{ id: 2, trip_id: 1, filename: 'visa.pdf' }]);
});
it('db:read:users returns only the public projection for a visible user', async () => {
const host = new PluginRpcHost('p', new Set(['db:read:users']), deps);
const res = await host.dispatch(req('users.getById', { id: 3 }), 42);
@@ -334,26 +317,6 @@ describe('PluginRpcHost — capability enforcement', () => {
expect((await host.dispatch(req('collections.listMine'), undefined)).ok).toBe(false);
});
it('packing.create needs db:write:packing + packing_edit, membership-checked, name required', async () => {
const host = new PluginRpcHost('p', new Set(['db:write:packing']), deps);
const good = await host.dispatch(req('packing.create', { tripId: 1, input: { name: 'Socks' } }), 42);
expect(ok(good)).toBe(true);
expect(deps.createPackingItem).toHaveBeenCalledWith(1, expect.objectContaining({ name: 'Socks' }), 42);
expect((await host.dispatch(req('packing.create', { tripId: 1, input: { name: '' } }), 42)).ok).toBe(false); // schema: name min 1
expect(((await host.dispatch(req('packing.create', { tripId: 2, input: { name: 'x' } }), 42)) as RpcError).error.code).toBe('RESOURCE_FORBIDDEN');
expect((await host.dispatch(req('packing.create', { tripId: 1, input: { name: 'x' } }), undefined)).ok).toBe(false); // no acting user
});
it('packing.update/delete are gated the same way; nothing without the grant', async () => {
const host = new PluginRpcHost('p', new Set(['db:write:packing']), deps);
expect(ok(await host.dispatch(req('packing.update', { tripId: 1, itemId: 70, input: { checked: true } }), 42))).toBe(true);
expect(deps.updatePackingItem).toHaveBeenCalledWith(1, 70, expect.objectContaining({ checked: true }), 42);
expect(ok(await host.dispatch(req('packing.delete', { tripId: 1, itemId: 70 }), 42))).toBe(true);
expect(deps.deletePackingItem).toHaveBeenCalledWith(1, 70);
const noGrant = new PluginRpcHost('p', new Set(['db:read:packing']), deps);
expect((await noGrant.dispatch(req('packing.create', { tripId: 1, input: { name: 'x' } }), 42)).ok).toBe(false);
});
// weather.get, categories.list and rates.get moved to tests/unit/plugins/tenant-free.rpc.test.ts.
// The tags cases moved to tests/unit/tags/tags.rpc.test.ts with the handlers.
@@ -364,14 +327,6 @@ describe('PluginRpcHost — capability enforcement', () => {
expect(((await host.dispatch(req('trips.members', { tripId: 2 }), 42)) as RpcError).error.code).toBe('RESOURCE_FORBIDDEN');
});
it('packing-bags writes need the grant + edit right + a bound user', async () => {
const host = new PluginRpcHost('p', new Set(['db:write:packing']), deps);
expect(ok(await host.dispatch(req('packing.createBag', { tripId: 1, input: { name: 'Bag' } }), 42))).toBe(true);
expect(ok(await host.dispatch(req('packing.setBagMembers', { tripId: 1, bagId: 80, userIds: [5, 6] }), 42))).toBe(true);
expect(deps.setPackingBagMembers).toHaveBeenCalledWith(1, 80, [5, 6]);
expect((await host.dispatch(req('packing.createBag', { tripId: 1, input: { name: 'Bag' } }), undefined)).ok).toBe(false);
});
it('atlas writes are uid-bound (code-validated, userless refused)', async () => {
const host = new PluginRpcHost('p', new Set(['db:write:atlas']), deps);
expect(ok(await host.dispatch(req('atlas.markCountry', { code: 'jp' }), 42))).toBe(true);