fix(files): refuse a plugin upload that attaches nothing while Documents is off

files.list hides loose files on such a trip, so files.create now needs a reservation_id or place_id there instead of writing a file the plugin cannot list back.
This commit is contained in:
Maurice
2026-11-23 17:20:52 +01:00
parent b2ba89fe4f
commit e1147504c2
2 changed files with 33 additions and 3 deletions
+9 -2
View File
@@ -51,7 +51,8 @@ type CreateInput = {
*
* Documents gates it the way it gates the REST routes: while the trip has it off,
* the list leaves out the files only the file manager shows, and a call on such a
* file is refused unless it attaches the file. Uploads and deletes stay open.
* file is refused unless it attaches the file. An upload has to attach its file
* then too, so a plugin never writes a file it cannot list. Deletes stay open.
*/
@PluginController()
export class FilesRpc {
@@ -127,7 +128,13 @@ export class FilesRpc {
throw new BadParams('file exceeds the 10MB plugin upload cap');
}
await this.guards.requireTripEdit(tripId, actor, UPLOAD_ACTION);
return this.writeFile(tripId, input as unknown as CreateInput, actor);
const fields = input as unknown as CreateInput;
if (!attachesFile(fields) && !(await this.documentsOn(tripId))) {
throw new ForbiddenResource(
'the documents addon is disabled: pass reservation_id or place_id to attach the uploaded file',
);
}
return this.writeFile(tripId, fields, actor);
}
private async writeFile(tripId: number, input: CreateInput, actingUserId: number): Promise<unknown> {
+24 -1
View File
@@ -248,11 +248,19 @@ describe('FilesRpc with Documents off for the trip', () => {
).toMatchObject({ code: 'RESOURCE_FORBIDDEN', message: 'the documents addon is disabled' });
});
it('FILES-RPC-023 uploads and deletes stay open, a write on a loose file only when it attaches it', async () => {
it('FILES-RPC-023 deletes stay open, an upload or a write on a loose file only when it attaches it', async () => {
const f = build({ documents: false, attached: false });
const host = f.host('db:write:files');
const create = { tripId: 1, input: { name: 'a.pdf', content_base64: b64('x'), reservation_id: 4 } };
expect((await host.dispatch(req('files.create', create), 42)).ok).toBe(true);
expect(
(
await host.dispatch(
req('files.create', { ...create, input: { ...create.input, reservation_id: undefined, place_id: 3 } }),
42,
)
).ok,
).toBe(true);
expect((await host.dispatch(req('files.softDelete', { tripId: 1, fileId: 2 }), 42)).ok).toBe(true);
const describeOnly = req('files.update', { tripId: 1, fileId: 2, input: { description: 'x' } });
@@ -272,6 +280,21 @@ describe('FilesRpc with Documents off for the trip', () => {
true,
);
});
it('FILES-RPC-024 an upload that attaches nothing is refused, since the list would not show it', async () => {
const off = build({ documents: false });
const loose = req('files.create', { tripId: 1, input: { name: 'a.pdf', content_base64: b64('x') } });
expect(await forbidden(off.host('db:write:files').dispatch(loose, 42))).toMatchObject({
code: 'RESOURCE_FORBIDDEN',
message: 'the documents addon is disabled: pass reservation_id or place_id to attach the uploaded file',
});
expect(off.storage.put).not.toHaveBeenCalled();
expect(off.files.createFile).not.toHaveBeenCalled();
expect(off.realtime.broadcast).not.toHaveBeenCalled();
const on = build({ documents: true });
expect((await on.host('db:write:files').dispatch(loose, 42)).ok).toBe(true);
});
});
describe('FilesRpc writes', () => {