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:
Maurice
2026-11-15 11:00:21 +01:00
parent a1bf57b71a
commit 7bbb5a9f71
15 changed files with 238 additions and 49 deletions
@@ -407,6 +407,12 @@ export class PackingItemsRepository extends TrekRepository<PackingItems> {
await this.nativeUpdate({ id }, { is_private, ...data, updated_at: currentTimestamp(platform) });
}
/** `removeContributor` on a Common item's owner (#3098): the item stays on the list, nobody's in particular. */
async clearOwner(id: number): Promise<void> {
const platform = this.getEntityManager().getPlatform();
await this.nativeUpdate({ id }, { owner: null, updated_at: currentTimestamp(platform) });
}
/** PK16 (`setItemSharing`'s replace-all) — `DELETE FROM packing_item_recipients WHERE item_id = ?`. */
async deleteRecipientsForItem(item_id: number): Promise<void> {
await this.db().deleteFrom('packing_item_recipients').where('item_id', '=', item_id).execute();
@@ -14,6 +14,7 @@ import {
PackingReorderDto,
PackingSaveTemplateDto,
PackingSetSharingDto,
PackingAddContributorDto,
PackingUpdateBagDto,
PackingUpdateItemDto,
} from './packing.dto';
@@ -248,6 +249,7 @@ export class PackingController {
@Param('tripId') tripId: string,
@Param('id') id: string,
@Headers('x-socket-id') socketId?: string,
@Body() body?: PackingAddContributorDto,
) {
// Plan 4 Task 8b (U6) — :id parsed ONCE here (toRowId). #858's
// contributors/sharing routes are native Nest code with no pre-ORM
@@ -258,7 +260,8 @@ export class PackingController {
if (itemId === null) {
throw new HttpException({ error: 'Item not found or not a shared list item' }, 404);
}
const item = await this.packing.addContributor(tripId, itemId, user.id);
// No body is the caller's own pledge; a user_id puts someone else down (#3098).
const item = await this.packing.addContributor(tripId, itemId, body?.user_id ?? user.id, user.id);
if (!item) {
throw new HttpException({ error: 'Item not found or not a shared list item' }, 404);
}
@@ -286,7 +289,7 @@ export class PackingController {
throw new HttpException({ error: 'Item not found' }, 404);
}
// You can drop your own pledge; the owner can remove anyone's.
const item = await this.packing.removeContributor(tripId, itemId, target);
const item = await this.packing.removeContributor(tripId, itemId, target, user.id);
if (!item) {
throw new HttpException({ error: 'Item not found' }, 404);
}
+2
View File
@@ -3,6 +3,7 @@ import {
packingCreateItemRequestSchema,
packingUpdateItemRequestSchema,
packingSetSharingRequestSchema,
packingAddContributorRequestSchema,
packingImportRequestSchema,
packingReorderRequestSchema,
packingCreateBagRequestSchema,
@@ -24,6 +25,7 @@ import { createZodDto } from 'nestjs-zod';
export class PackingCreateItemDto extends createZodDto(packingCreateItemRequestSchema) {}
export class PackingUpdateItemDto extends createZodDto(packingUpdateItemRequestSchema) {}
export class PackingSetSharingDto extends createZodDto(packingSetSharingRequestSchema) {}
export class PackingAddContributorDto extends createZodDto(packingAddContributorRequestSchema) {}
export class PackingImportDto extends createZodDto(packingImportRequestSchema) {}
export class PackingReorderDto extends createZodDto(packingReorderRequestSchema) {}
export class PackingCreateBagDto extends createZodDto(packingCreateBagRequestSchema) {}
+9 -4
View File
@@ -406,18 +406,23 @@ export class PackingService {
* getItemPrivacy} (`PackingController.addContributor`, native Nest code
* with no pre-ORM Express precedent — #858 landed post-migration).
*/
async addContributor(tripId: string | number, id: number, userId: number) {
const item = await this.itemsRepo.findVisibleInTrip(id, tripId, userId);
async addContributor(tripId: string | number, id: number, userId: number, actingUserId: number = userId) {
const item = await this.itemsRepo.findVisibleInTrip(id, tripId, actingUserId);
if (!item || item.is_private !== 0) return null; // co-contribution is a Common-list concept
if (item.owner_id === userId) return null; // the bringer is already covering it
// Putting someone else down as a bringer (#3098) only reaches people on the trip.
if (userId !== actingUserId && !(await this.tripRosterIds(tripId)).has(userId)) return null;
await this.contributorsRepo.insertIgnore(id, userId);
return (await this.enrichItems([await this.itemsRepo.findById(id)]))[0];
}
/** `id: number` — same Plan 4 Task 8b (U6) narrowing as {@link addContributor}; `userId` here is the target being removed, not the acting viewer (`PackingController.removeContributor` now parses both `:id`/`:userId` via `toRowId` instead of a bare `Number.parseInt`). */
async removeContributor(tripId: string | number, id: number, userId: number) {
const item = await this.itemsRepo.findVisibleInTrip(id, tripId, userId);
async removeContributor(tripId: string | number, id: number, userId: number, actingUserId: number = userId) {
const item = await this.itemsRepo.findVisibleInTrip(id, tripId, actingUserId);
if (!item) return null;
// On a Common item the owner is its first bringer, so taking them off the
// item has to clear the owner too; otherwise their avatar stuck for good (#3098).
if (item.is_private === 0 && item.owner_id === userId) await this.itemsRepo.clearOwner(id);
await this.contributorsRepo.deleteOne(id, userId);
return (await this.enrichItems([await this.itemsRepo.findById(id)]))[0];
}
+43
View File
@@ -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');
});
+38 -24
View File
@@ -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 () => {