mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 08:53:13 +00:00
fix(packing): anyone on the trip can be put down as the bringer of a shared item, and taken off again (#3098)
A common item only let you pledge yourself. Nobody else could be named, and a bringer could never be removed, so an item that went personal and back kept its owner's avatar for good. The item menu now lists every member to tick on or off, a click on the avatar opens it there, and the add route takes an optional user_id limited to the trip's members. Taking the owner off clears the owner. The remove route now checks visibility for the caller, not for the user named in the path.
This commit is contained in:
@@ -245,6 +245,49 @@ describe('Three-tier packing sharing (#858)', () => {
|
||||
expect(res.body.item.contributors.map((c: any) => c.user_id)).toContain(helper.id);
|
||||
});
|
||||
|
||||
it('PACK-3T-004b — a member puts someone else down as the bringer, and takes the owner off again (#3098)', async () => {
|
||||
const { user: owner } = createUser(testDb);
|
||||
const { user: helper } = createUser(testDb);
|
||||
const { user: stranger } = createUser(testDb);
|
||||
const trip = createTrip(testDb, owner.id);
|
||||
addTripMember(testDb, trip.id, helper.id);
|
||||
const created = await request(app)
|
||||
.post(`/api/trips/${trip.id}/packing`)
|
||||
.set('Cookie', authCookie(owner.id))
|
||||
.send({ name: 'Stove', visibility: 'personal' });
|
||||
const id = created.body.item.id;
|
||||
await request(app)
|
||||
.put(`/api/trips/${trip.id}/packing/${id}/sharing`)
|
||||
.set('Cookie', authCookie(owner.id))
|
||||
.send({ visibility: 'common' });
|
||||
|
||||
const assigned = await request(app)
|
||||
.post(`/api/trips/${trip.id}/packing/${id}/contributors`)
|
||||
.set('Cookie', authCookie(owner.id))
|
||||
.send({ user_id: helper.id });
|
||||
expect(assigned.status).toBe(201);
|
||||
expect(assigned.body.item.contributors.map((c: any) => c.user_id)).toEqual([helper.id]);
|
||||
|
||||
const outsider = await request(app)
|
||||
.post(`/api/trips/${trip.id}/packing/${id}/contributors`)
|
||||
.set('Cookie', authCookie(owner.id))
|
||||
.send({ user_id: stranger.id });
|
||||
expect(outsider.status).toBe(404);
|
||||
|
||||
const bad = await request(app)
|
||||
.post(`/api/trips/${trip.id}/packing/${id}/contributors`)
|
||||
.set('Cookie', authCookie(owner.id))
|
||||
.send({ user_id: 'x' });
|
||||
expect(bad.status).toBe(400);
|
||||
|
||||
const ownerOff = await request(app)
|
||||
.delete(`/api/trips/${trip.id}/packing/${id}/contributors/${owner.id}`)
|
||||
.set('Cookie', authCookie(helper.id));
|
||||
expect(ownerOff.status).toBe(200);
|
||||
expect(ownerOff.body.item.owner_id).toBeNull();
|
||||
expect(ownerOff.body.item.contributors.map((c: any) => c.user_id)).toEqual([helper.id]);
|
||||
});
|
||||
|
||||
it('PACK-3T-005 — sharing can only be changed by the owner', async () => {
|
||||
const { user: owner } = createUser(testDb);
|
||||
const { user: member } = createUser(testDb);
|
||||
|
||||
@@ -112,3 +112,22 @@ describe('PackingItemsRepository — share.service.ts SH13 read', () => {
|
||||
expect(await packingItemsRepo.listPublicForShare(trip.id)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('PackingItemsRepository — clearOwner (#3098)', () => {
|
||||
it('drops the owner of one item, keeps its tier and leaves the other items alone', async () => {
|
||||
const { user: owner } = createUser(testDb);
|
||||
const trip = createTrip(testDb, owner.id);
|
||||
const item = createPackingItem(testDb, trip.id, { name: 'Stove' });
|
||||
const other = createPackingItem(testDb, trip.id, { name: 'Pot' });
|
||||
await updateRows(t, PackingItems, { id: item.id }, { is_private: 0, owner: owner.id, updated_at: null });
|
||||
await updateRows(t, PackingItems, { id: other.id }, { is_private: 0, owner: owner.id });
|
||||
|
||||
await packingItemsRepo.clearOwner(item.id);
|
||||
|
||||
const row = await packingItemsRepo.findById(item.id);
|
||||
expect(row?.owner_id).toBeNull();
|
||||
expect(row?.is_private).toBe(0);
|
||||
expect(row?.updated_at).not.toBeNull();
|
||||
expect((await packingItemsRepo.findById(other.id))?.owner_id).toBe(owner.id);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -498,10 +498,18 @@ describe('PackingController (parity with the legacy /api/trips/:tripId/packing r
|
||||
const svc = makeService({ addContributor, broadcast } as Partial<PackingService>);
|
||||
await new PackingController(svc).addContributor(user, '5', '9', 'sock');
|
||||
// Plan 4 Task 8b (U6) — :id is now parsed ONCE at the controller gate (toRowId).
|
||||
expect(addContributor).toHaveBeenCalledWith('5', 9, user.id);
|
||||
expect(addContributor).toHaveBeenCalledWith('5', 9, user.id, user.id);
|
||||
expect(broadcast).toHaveBeenCalledWith('5', 'packing:updated', { item }, 'sock');
|
||||
});
|
||||
|
||||
it('POST /:id/contributors with a user_id puts that person down as a bringer (#3098)', async () => {
|
||||
const item = { id: 9, is_private: 0, contributors: [{ user_id: 7 }] };
|
||||
const addContributor = vi.fn().mockReturnValue(item);
|
||||
const svc = makeService({ addContributor, broadcast: vi.fn() } as Partial<PackingService>);
|
||||
await new PackingController(svc).addContributor(user, '5', '9', 'sock', { user_id: 7 });
|
||||
expect(addContributor).toHaveBeenCalledWith('5', 9, 7, user.id);
|
||||
});
|
||||
|
||||
it('DELETE /:id/contributors/:userId removes the contributor + broadcasts', async () => {
|
||||
const item = { id: 9, is_private: 0, contributors: [] };
|
||||
const removeContributor = vi.fn().mockReturnValue(item);
|
||||
@@ -509,7 +517,7 @@ describe('PackingController (parity with the legacy /api/trips/:tripId/packing r
|
||||
const svc = makeService({ removeContributor, broadcast } as Partial<PackingService>);
|
||||
await new PackingController(svc).removeContributor(user, '5', '9', '2', 'sock');
|
||||
// Plan 4 Task 8b (U6) — :id/:userId are now parsed ONCE at the controller gate (toRowId).
|
||||
expect(removeContributor).toHaveBeenCalledWith('5', 9, 2);
|
||||
expect(removeContributor).toHaveBeenCalledWith('5', 9, 2, user.id);
|
||||
expect(broadcast).toHaveBeenCalledWith('5', 'packing:updated', { item }, 'sock');
|
||||
});
|
||||
|
||||
|
||||
@@ -728,6 +728,37 @@ describe('three-tier packing sharing (#858)', () => {
|
||||
expect(cleared.contributors).toEqual([]);
|
||||
});
|
||||
|
||||
it('PACK-SVC-044b: anyone on the trip can be put down as the bringer of a Common item, nobody else (#3098)', async () => {
|
||||
const { user: owner } = createUser(testDb);
|
||||
const { user: friend } = createUser(testDb);
|
||||
const { user: stranger } = createUser(testDb);
|
||||
const trip = createTrip(testDb, owner.id);
|
||||
addTripMember(testDb, trip.id, friend.id);
|
||||
const common = (await svc.createItem(trip.id, { name: 'Tent', visibility: 'common' }, owner.id)) as any;
|
||||
|
||||
const assigned = (await svc.addContributor(trip.id, common.id, friend.id, owner.id)) as any;
|
||||
expect(assigned.contributors.map((c: any) => c.user_id)).toEqual([friend.id]);
|
||||
expect(await svc.addContributor(trip.id, common.id, stranger.id, owner.id)).toBeNull();
|
||||
});
|
||||
|
||||
it('PACK-SVC-044c: taking the owner off a Common item clears them as its bringer and leaves the others (#3098)', async () => {
|
||||
const { user: owner } = createUser(testDb);
|
||||
const { user: friend } = createUser(testDb);
|
||||
const trip = createTrip(testDb, owner.id);
|
||||
addTripMember(testDb, trip.id, friend.id);
|
||||
// Personal first, then back to the shared list: the owner stays on as its bringer.
|
||||
const item = (await svc.createItem(trip.id, { name: 'Stove', visibility: 'personal' }, owner.id)) as any;
|
||||
await svc.setItemSharing(trip.id, item.id, owner.id, 'common', []);
|
||||
await svc.addContributor(trip.id, item.id, friend.id, friend.id);
|
||||
expect((await itemRow(item.id)).owner_id).toBe(owner.id);
|
||||
|
||||
const after = (await svc.removeContributor(trip.id, item.id, owner.id, friend.id)) as any;
|
||||
expect(after.owner_id).toBeNull();
|
||||
expect(after.owner_username).toBeNull();
|
||||
expect(after.contributors.map((c: any) => c.user_id)).toEqual([friend.id]);
|
||||
expect(after.is_private).toBe(0);
|
||||
});
|
||||
|
||||
it("PACK-SVC-045: cloneItem copies an item onto the cloner's personal list", async () => {
|
||||
const { user: owner } = createUser(testDb);
|
||||
const { user: cloner } = createUser(testDb);
|
||||
@@ -1436,30 +1467,13 @@ describe('packing item object-level authorization', () => {
|
||||
expect((await itemRow(personal.id)).owner_id).toBe(owner.id);
|
||||
});
|
||||
|
||||
it("U3 HOLE — contributor removal checks the path user's visibility, not the caller's: an intruder who cannot see a Personal item still gets it back by naming the owner as :userId", async () => {
|
||||
// PRE-EXISTING on base (packing.controller.ts ~250 → packing.service.ts
|
||||
// removeContributor; base carries the identical shape). The controller
|
||||
// passes the PATH `:userId` — not `user.id`, the caller — as the third
|
||||
// argument to `PackingService.removeContributor`, which uses that same
|
||||
// id both to run the visibility check AND as the contributor row to
|
||||
// delete. An intruder who cannot see `owner`'s Personal item at all can
|
||||
// still name `owner` as `:userId`: the visibility check passes because
|
||||
// OWNERS can always see their own item, and the full enriched item comes
|
||||
// back in the response — an IDOR read of content the intruder has no
|
||||
// business seeing, RIGHTS-gated only by `packing_edit`, not by the
|
||||
// three-tier privacy model. Deliberately NOT fixed here (controller
|
||||
// ruling U3, pinned so it cannot be "fixed" silently); the one-line fix
|
||||
// is in the fix-wave report for the user: check `findVisibleInTrip` for
|
||||
// the CALLER, and additionally require `userId === callerId ||
|
||||
// item.owner_id === callerId`.
|
||||
// The intruder is the caller in the real route (RequirePermission checks
|
||||
// only packing_edit, never visibility) — but the service signature never
|
||||
// receives the caller's id at all, only the path :userId. That gap is
|
||||
// the bug, so there is nothing of the intruder's identity to pass here.
|
||||
const { owner, trip, personal } = await restrictedTrip();
|
||||
const leaked = await svc.removeContributor(trip.id, personal.id, owner.id);
|
||||
expect(leaked).not.toBeNull();
|
||||
expect(leaked.name).toBe('Diary');
|
||||
it("U3: contributor removal checks the caller's visibility, so naming the owner as :userId no longer hands an intruder their Personal item", async () => {
|
||||
// The route used to pass only the path :userId, which ran the visibility
|
||||
// check as the owner and returned the item to whoever asked. The caller now
|
||||
// rides along (#3098 needed it to take the owner off a Common item).
|
||||
const { owner, intruder, trip, personal } = await restrictedTrip();
|
||||
expect(await svc.removeContributor(trip.id, personal.id, owner.id, intruder.id)).toBeNull();
|
||||
expect((await itemRow(personal.id)).owner_id).toBe(owner.id);
|
||||
});
|
||||
|
||||
it('PACK-SVC-105b: a visible item the caller does not own still reports forbidden, not missing', async () => {
|
||||
|
||||
Reference in New Issue
Block a user