diff --git a/server/src/nest/files/files.rpc.ts b/server/src/nest/files/files.rpc.ts index c8a8f5ebe..ebd6612fe 100644 --- a/server/src/nest/files/files.rpc.ts +++ b/server/src/nest/files/files.rpc.ts @@ -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 { diff --git a/server/tests/unit/files/files.rpc.test.ts b/server/tests/unit/files/files.rpc.test.ts index b1cca157e..7268d27de 100644 --- a/server/tests/unit/files/files.rpc.test.ts +++ b/server/tests/unit/files/files.rpc.test.ts @@ -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', () => {