fix(reservations): keep every id in a booking inside its own trip

reservation_edit on the trip in the URL says the caller may write here. It
says nothing about the ids they put in the body, and the write contracts are
open records, so day_id, place_id, assignment_id and accommodation_id all
arrived unchecked. A booking created in one trip could carry another trip's
accommodation_id, and deleting it ran the cascade against that foreign row.

The MCP tools have refused foreign ids since they were written. Put the same
check where every writer reaches it, and scope the two day_accommodations
statements to the trip so a row stored before this still cannot be followed
out of it.
This commit is contained in:
Maurice
2026-08-23 09:12:46 +02:00
committed by Maurice
parent 39c8a22352
commit d91eda690a
4 changed files with 153 additions and 3 deletions
@@ -15,6 +15,7 @@ function makeService(overrides: Partial<ReservationsService> = {}): Reservations
return {
verifyTripAccess: vi.fn().mockReturnValue(trip),
canEdit: vi.fn().mockReturnValue(true),
referencesOutsideTrip: vi.fn().mockReturnValue([]),
broadcast: vi.fn(),
syncBudgetOnCreate: vi.fn(),
syncBudgetOnUpdate: vi.fn(),
@@ -55,6 +56,18 @@ describe('ReservationsController (parity with the legacy /api/trips/:tripId/rese
expect(broadcast).toHaveBeenCalledWith('5', 'reservation:created', { reservation: { id: 9 } }, 'sock');
expect(notifyBookingChange).toHaveBeenCalledWith('5', user.id, 'Hotel', 'lodging');
});
it('400s on a body id belonging to another trip, without writing', () => {
const create = vi.fn();
const svc = makeService({
create,
referencesOutsideTrip: vi.fn().mockReturnValue(['accommodation_id']),
} as Partial<ReservationsService>);
const body = { title: 'Hotel', accommodation_id: 4711 };
expect(thrown(() => new ReservationsController(svc, airtrailLink).create(user, '5', body)))
.toEqual({ status: 400, body: { error: 'Not part of this trip: accommodation_id' } });
expect(create).not.toHaveBeenCalled();
});
});
describe('PUT /positions', () => {
@@ -87,6 +100,18 @@ describe('ReservationsController (parity with the legacy /api/trips/:tripId/rese
expect(syncBudgetOnUpdate).toHaveBeenCalledWith('5', '9', '', undefined, 'Old', 'lodging', { total_price: 50 }, 'sock');
expect(notifyBookingChange).toHaveBeenCalledWith('5', user.id, 'Old', 'lodging');
});
it('400s on a body id belonging to another trip, without writing', () => {
const update = vi.fn();
const svc = makeService({
getReservation: vi.fn().mockReturnValue({ title: 'Old', type: 'lodging' }),
update,
referencesOutsideTrip: vi.fn().mockReturnValue(['day_id', 'place_id']),
} as Partial<ReservationsService>);
expect(thrown(() => new ReservationsController(svc, airtrailLink).update(user, '5', '9', { day_id: 1, place_id: 2 })))
.toEqual({ status: 400, body: { error: 'Not part of this trip: day_id, place_id' } });
expect(update).not.toHaveBeenCalled();
});
});
describe('PUT /:id/travelers', () => {
@@ -816,3 +816,65 @@ describe('ReservationsService — quirk fixes (post-fold)', () => {
expect(testDb.prepare('SELECT title FROM reservations WHERE id = ?').get(res.id)).toEqual({ title: 'Old' });
});
});
describe('ReservationsService — referenced ids stay inside the trip', () => {
/** Attacker's trip and a victim trip they are not a member of. */
function twoTrips() {
const { user: attacker } = createUser(testDb);
const { user: victim } = createUser(testDb, { email: 'victim@example.test' });
const mine = createTrip(testDb, attacker.id, { start_date: '2030-05-01', end_date: '2030-05-03' });
const theirs = createTrip(testDb, victim.id, { start_date: '2030-05-01', end_date: '2030-05-03' });
return { mine, theirs };
}
it('RESV-SCOPE-001: names every foreign id in the body', () => {
const { mine, theirs } = twoTrips();
const foreignPlace = createPlace(testDb, theirs.id);
const foreignDay = testDb.prepare('SELECT id FROM days WHERE trip_id = ? ORDER BY day_number').get(theirs.id) as { id: number };
const foreignAcc = createDayAccommodation(testDb, theirs.id, foreignPlace.id, foreignDay.id, foreignDay.id);
expect(svc.referencesOutsideTrip(String(mine.id), {
title: 'x', day_id: foreignDay.id, place_id: foreignPlace.id, accommodation_id: foreignAcc.id,
})).toEqual(['day_id', 'place_id', 'accommodation_id']);
});
it('RESV-SCOPE-002: passes a body whose ids all live in the trip', () => {
const { mine } = twoTrips();
const place = createPlace(testDb, mine.id);
const day = testDb.prepare('SELECT id FROM days WHERE trip_id = ? ORDER BY day_number').get(mine.id) as { id: number };
expect(svc.referencesOutsideTrip(String(mine.id), { title: 'x', day_id: day.id, place_id: place.id })).toEqual([]);
expect(svc.referencesOutsideTrip(String(mine.id), { title: 'x' })).toEqual([]);
});
it('RESV-SCOPE-003: a stored foreign accommodation_id is not deleted with the reservation', () => {
const { mine, theirs } = twoTrips();
const foreignPlace = createPlace(testDb, theirs.id);
const foreignDay = testDb.prepare('SELECT id FROM days WHERE trip_id = ? ORDER BY day_number').get(theirs.id) as { id: number };
const foreignAcc = createDayAccommodation(testDb, theirs.id, foreignPlace.id, foreignDay.id, foreignDay.id);
// A row from before the check existed: it already carries the foreign id.
const res = createReservation(testDb, mine.id, { title: 'Hotel', type: 'hotel' });
testDb.prepare('UPDATE reservations SET accommodation_id = ? WHERE id = ?').run(foreignAcc.id, res.id);
const { accommodationDeleted } = svc.remove(String(res.id), String(mine.id));
expect(accommodationDeleted).toBe(false);
expect(testDb.prepare('SELECT id FROM day_accommodations WHERE id = ?').get(foreignAcc.id)).toBeTruthy();
});
it('RESV-SCOPE-004: an update carrying a foreign accommodation_id does not write through to it', () => {
const { mine, theirs } = twoTrips();
const foreignPlace = createPlace(testDb, theirs.id);
const foreignDay = testDb.prepare('SELECT id FROM days WHERE trip_id = ? ORDER BY day_number').get(theirs.id) as { id: number };
const foreignAcc = createDayAccommodation(testDb, theirs.id, foreignPlace.id, foreignDay.id, foreignDay.id, { check_in: '14:00' });
const res = createReservation(testDb, mine.id, { title: 'Hotel', type: 'hotel' });
const current = svc.getReservation(String(res.id), String(mine.id))!;
svc.update(String(res.id), String(mine.id), {
accommodation_id: foreignAcc.id, metadata: { check_in_time: '23:00' },
} as never, current);
expect(testDb.prepare('SELECT check_in FROM day_accommodations WHERE id = ?').get(foreignAcc.id)).toEqual({ check_in: '14:00' });
expect(testDb.prepare('SELECT accommodation_id FROM reservations WHERE id = ?').get(res.id)).toEqual({ accommodation_id: null });
});
});