|
29 | 29 | // service and the security plugin alone hands the grant out, with the opt-in |
30 | 30 | // (`enable.files`) still refusing: every gate the grant relies on is the |
31 | 31 | // storage service's own. |
| 32 | +// |
| 33 | +// The set carries no delete. The uploader withdraws an attachment they |
| 34 | +// uploaded through the storage service's removal door |
| 35 | +// (`DELETE /api/v1/attachments/:id`), which admits only the row's |
| 36 | +// `uploaded_by`: another member is refused even on a record they edit, the |
| 37 | +// data door's delete stays refused to a member, and an administrator still |
| 38 | +// deletes through it. |
32 | 39 |
|
33 | 40 | import { createHash, randomBytes } from 'node:crypto'; |
34 | 41 | import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
@@ -354,6 +361,79 @@ describe('a member attaches to the records they edit and sees the files of the r |
354 | 361 | expect([out.status, out.body.error?.code]).toEqual([403, 'FILES_DISABLED']); |
355 | 362 | expect(await linksOn(nofilesId)).toEqual([]); |
356 | 363 | }); |
| 364 | + |
| 365 | + // The uploader withdraws an attachment they uploaded (maintainer ruling |
| 366 | + // 6879388034): own uploads only, through the storage service's removal door |
| 367 | + // (`DELETE /api/v1/attachments/:id`), and with no delete bit on the member |
| 368 | + // grant — the data door's delete stays what it was, refused to a member and |
| 369 | + // served to an administrator. |
| 370 | + describe('the uploader withdraws their own attachment through the removal door, and nobody else\'s', () => { |
| 371 | + const OTHER_EMAIL = 'att-baseline-other-member@verify.test'; |
| 372 | + let otherTok: string; |
| 373 | + |
| 374 | + const row = (id: string) => ql.findOne('sys_attachment', { where: { id }, context: SYS }); |
| 375 | + const answerOf = async (res: Response) => { |
| 376 | + const body = (await res.json()) as any; |
| 377 | + return { status: res.status, code: (body?.error?.code ?? body?.code) as string | undefined, body }; |
| 378 | + }; |
| 379 | + const removeAs = async (token: string, id: string) => answerOf(await stack.apiAs(token, 'DELETE', `/attachments/${id}`)); |
| 380 | + const dataDeleteAs = async (token: string, id: string) => answerOf(await stack.apiAs(token, 'DELETE', `/data/sys_attachment/${id}`)); |
| 381 | + /** The member attaches a file to their case, through the panel's door: the row's uploader is the member. */ |
| 382 | + const memberAttachment = async () => { |
| 383 | + const out = await completeOnto(memberTok, 'att_case', caseId); |
| 384 | + expect(out.status, JSON.stringify(out.body)).toBe(200); |
| 385 | + const id = String(out.body.data.attachmentId); |
| 386 | + expect((await row(id))?.uploaded_by).toBe(memberId); |
| 387 | + return id; |
| 388 | + }; |
| 389 | + |
| 390 | + beforeAll(async () => { |
| 391 | + otherTok = await stack.signUp(OTHER_EMAIL, MEMBER_PASSWORD); |
| 392 | + }); |
| 393 | + |
| 394 | + it('the uploader removes their own attachment through the door: 200, and the row is gone', async () => { |
| 395 | + const id = await memberAttachment(); |
| 396 | + const answer = await removeAs(memberTok, id); |
| 397 | + expect(answer.status, JSON.stringify(answer.body)).toBe(200); |
| 398 | + expect(answer.body).toEqual({ success: true, data: { id, deleted: true } }); |
| 399 | + expect(await row(id)).toBeNull(); |
| 400 | + }); |
| 401 | + |
| 402 | + it('CONTROL: the member grant gains no delete bit — the data door still refuses the uploader, and read and create are unchanged', async () => { |
| 403 | + const id = await memberAttachment(); |
| 404 | + expect(await dataDeleteAs(memberTok, id)).toMatchObject({ status: 403, code: 'PERMISSION_DENIED' }); |
| 405 | + expect(await row(id)).toMatchObject({ id, uploaded_by: memberId }); |
| 406 | + const explain = async (operation: string) => |
| 407 | + ((await (await stack.apiAs(memberTok, 'GET', `/security/explain?object=sys_attachment&operation=${operation}`)).json()) as any).allowed; |
| 408 | + expect({ read: await explain('read'), create: await explain('create'), delete: await explain('delete') }).toEqual({ |
| 409 | + read: true, |
| 410 | + create: true, |
| 411 | + delete: false, |
| 412 | + }); |
| 413 | + expect((await stack.apiAs(memberTok, 'GET', `/data/sys_attachment/${id}`)).status).toBe(200); |
| 414 | + }); |
| 415 | + |
| 416 | + it('another member is refused on the door and on the data door, and the row stays — though they can edit the record it is on', async () => { |
| 417 | + const id = await memberAttachment(); |
| 418 | + // The other member edits the case: their own attach to it is admitted. |
| 419 | + const theirs = await completeOnto(otherTok, 'att_case', caseId); |
| 420 | + expect(theirs.status, JSON.stringify(theirs.body)).toBe(200); |
| 421 | + |
| 422 | + expect(await removeAs(otherTok, id)).toMatchObject({ status: 403, code: 'PERMISSION_DENIED' }); |
| 423 | + expect(await dataDeleteAs(otherTok, id)).toMatchObject({ status: 403, code: 'PERMISSION_DENIED' }); |
| 424 | + expect(await row(id)).toMatchObject({ id, uploaded_by: memberId }); |
| 425 | + }); |
| 426 | + |
| 427 | + it('an administrator still deletes any attachment through the data door; the door grants them nothing more', async () => { |
| 428 | + const id = await memberAttachment(); |
| 429 | + expect(await removeAs(adminTok, id)).toMatchObject({ status: 403, code: 'PERMISSION_DENIED' }); |
| 430 | + expect(await row(id)).toMatchObject({ id, uploaded_by: memberId }); |
| 431 | + |
| 432 | + const answer = await dataDeleteAs(adminTok, id); |
| 433 | + expect(answer.status, JSON.stringify(answer.body)).toBeLessThan(300); |
| 434 | + expect(await row(id)).toBeNull(); |
| 435 | + }); |
| 436 | + }); |
357 | 437 | }); |
358 | 438 |
|
359 | 439 | // A host that composes `sys_attachment` WITHOUT the storage service: the object |
|
0 commit comments