fix(plugins): require budget_edit for a plugin switching a trip's costs mode

trips.update checked trip_edit and the archive and cover permissions, but
let costs_mode through without the budget_edit check REST and MCP apply.
This commit is contained in:
Maurice
2026-11-16 10:45:23 +01:00
parent 40dea84085
commit ea5542f8eb
2 changed files with 35 additions and 9 deletions
+12 -6
View File
@@ -27,11 +27,12 @@ const TRIP_STR_LIMITS: Record<string, number> = { title: 200, description: 2000
* The trip surface a plugin may reach (#plugins), including the cross-trip feeds and
* the member roster.
*
* Two things are specific to trips. updateTrip gates two individual FIELDS behind
* Two things are specific to trips. updateTrip gates three individual FIELDS behind
* their own admin-configurable permissions, on top of trip_edit, so a plugin cannot
* archive or re-cover a trip it may otherwise edit. And adding a member GRANTS TRIP
* ACCESS, which is why it sits behind its own permission and the app's member_manage
* right rather than being bundled with a lower-risk write.
* archive or re-cover a trip it may otherwise edit, nor switch how it keeps its costs.
* And adding a member GRANTS TRIP ACCESS, which is why it sits behind its own
* permission and the app's member_manage right rather than being bundled with a
* lower-risk write.
*/
@PluginController()
export class TripsRpc {
@@ -145,14 +146,19 @@ export class TripsRpc {
this.guards.capStrings(parsed.data as Record<string, unknown>, TRIP_STR_LIMITS);
await this.guards.requireTripEdit(tripId, actor, TRIP_EDIT_ACTION);
const input = parsed.data as Record<string, unknown>;
// Two fields carry their own permissions on top of trip_edit, exactly as the REST
// controller gates them.
// Three fields carry their own permissions on top of trip_edit, exactly as the REST
// controller and update_trip gate them (tripUpdatePermissions).
if ('is_archived' in input && !(await this.guards.canEditAs('trip_archive', tripId, actor))) {
throw new ForbiddenResource(`no permission to archive trip ${tripId}`);
}
if ('cover_image' in input && !(await this.guards.canEditAs('trip_cover_upload', tripId, actor))) {
throw new ForbiddenResource(`no permission to change the cover of trip ${tripId}`);
}
// The costs mode decides what Costs shows, so it belongs to whoever may edit the
// costs (#1714), not to whoever may edit the trip's details.
if ('costs_mode' in input && !(await this.guards.canEditAs('budget_edit', tripId, actor))) {
throw new ForbiddenResource(`no permission to change how trip ${tripId} keeps its costs`);
}
const role = await this.em.getRepository(Users).getRole(actor); // RP4 — UsersRepository.getRole
try {
// The no-rebase core, parity with the legacy host path, which never
+23 -3
View File
@@ -3,7 +3,7 @@
* the two cross-trip feeds, the roster, the update, the create and the two member
* operations.
*
* Two things get their own cases. trips.update gates two individual FIELDS behind
* Two things get their own cases. trips.update gates three individual FIELDS behind
* permissions of their own on top of trip_edit, and adding a member GRANTS TRIP
* ACCESS, which is why it sits behind its own permission rather than any other write.
*/
@@ -271,11 +271,31 @@ describe('TripsRpc writes', () => {
expect(res.error.message).toBe('no permission to change the cover of trip 1');
});
it('TRIPS-RPC-008 a plain edit is unaffected by those two field permissions', async () => {
const f = build({ allow: (a) => a !== 'trip_archive' && a !== 'trip_cover_upload' });
it('TRIPS-RPC-008 a plain edit is unaffected by those field permissions', async () => {
const f = build({ allow: (a) => a !== 'trip_archive' && a !== 'trip_cover_upload' && a !== 'budget_edit' });
expect((await f.host().dispatch(req('trips.update', { tripId: 1, input: { title: 'New' } }), 42)).ok).toBe(true);
});
it('TRIPS-RPC-022 switching the costs mode needs budget_edit on top of trip_edit', async () => {
const f = build({ allow: (a) => a !== 'budget_edit' });
const res = (await f
.host()
.dispatch(req('trips.update', { tripId: 1, input: { costs_mode: 'totals' } }), 42)) as RpcError;
expect(res.error).toMatchObject({
code: 'RESOURCE_FORBIDDEN',
message: 'no permission to change how trip 1 keeps its costs',
});
expect(f.trips.updateTrip).not.toHaveBeenCalled();
});
it('TRIPS-RPC-023 with budget_edit the costs mode reaches the service', async () => {
const f = build();
expect((await f.host().dispatch(req('trips.update', { tripId: 1, input: { costs_mode: 'totals' } }), 42)).ok).toBe(
true,
);
expect(f.trips.updateTrip).toHaveBeenCalledWith(1, 42, { costs_mode: 'totals' }, 'user');
});
it('TRIPS-RPC-009 service errors map onto the right RPC codes', async () => {
const invalid = build({ updateThrows: new ValidationError('bad dates') });
expect(