Skip to content

Commit 98c5826

Browse files
committed
test(dogfood): a member who may attach files archives a print as their own write
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibvynRbCtMndW8EHNGc
1 parent 59d7107 commit 98c5826

2 files changed

Lines changed: 106 additions & 9 deletions

File tree

‎packages/qa/dogfood/test/print-service-archive.dogfood.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
// default record page; `showcase_invoice` declares `enable.files` for it, and
2727
// its select RLS (`owner == current_user.email`) is what hides another user's
2828
// invoice from a contributor.
29+
//
30+
// A second member, `print-attacher`, holds the contributor's set plus the one
31+
// grant an app ships when it lets members attach files: `sys_attachment`
32+
// read and create, and nothing on `sys_file`. Their print archives as THEIR
33+
// write — the attachment names them — while `sys_file` stays unreadable to
34+
// them through the data engine.
2935

3036
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
3137
import { mkdtempSync, rmSync } from 'node:fs';
@@ -34,6 +40,9 @@ import { join } from 'node:path';
3440
import type { VerifyStack } from '@objectstack/verify';
3541
import { StorageServicePlugin } from '@objectstack/service-storage';
3642
import { AuditPlugin } from '@objectstack/plugin-audit';
43+
import { SecurityPlugin, appSecurityPluginOptions, securityDefaultPermissionSets } from '@objectstack/plugin-security';
44+
import { PermissionSetSchema } from '@objectstack/spec/security';
45+
import showcaseStack from '@objectstack/example-showcase';
3746
import { bootShowcase } from './showcase-boot.js';
3847
import { PrintServicePlugin } from '../../../services/service-print/src/print-service-plugin.js';
3948
import { principalSessionMinterOf } from '../../../services/service-print/src/principal-session.js';
@@ -54,6 +63,13 @@ const PAGE = 'showcase_invoice_document';
5463
const PDF = Buffer.from('%PDF-1.4\n% print-service dogfood\n%%EOF\n');
5564
const PRINT_ENV = ['OS_PRINT_CHROMIUM_PATH', 'OS_PRINT_BROWSER_ENDPOINT', 'OS_PRINT_CONSOLE_URL'];
5665

66+
/** The grant an app ships to let members attach files: `sys_attachment` only, nothing on `sys_file`. */
67+
const attacherSet = PermissionSetSchema.parse({
68+
name: 'print_dogfood_attacher',
69+
label: 'Print dogfood: attach files',
70+
objects: { sys_attachment: { allowRead: true, allowCreate: true } },
71+
});
72+
5773
interface Refusal {
5874
code: unknown;
5975
status: unknown;
@@ -83,6 +99,8 @@ describe('print service on a real showcase boot', () => {
8399
}));
84100
const renderPdf = vi.fn(async () => PDF);
85101
let memberToken: string;
102+
let attacherToken: string;
103+
let attacherInvoice: string;
86104
let auditorToken: string;
87105
let adminToken: string;
88106
let memberEmail: string;
@@ -96,6 +114,12 @@ describe('print service on a real showcase boot', () => {
96114
}
97115
rootDir = mkdtempSync(join(tmpdir(), 'print-dogfood-'));
98116
stack = await bootShowcase({
117+
// The showcase's own composition (`appSecurityPluginOptions`, what the
118+
// harness passes by default), plus the attach grant above.
119+
security: new SecurityPlugin({
120+
...appSecurityPluginOptions(showcaseStack),
121+
defaultPermissionSets: [...securityDefaultPermissionSets, attacherSet],
122+
}),
99123
extraPlugins: [
100124
new StorageServicePlugin({ adapter: 'local', local: { rootDir }, bindToSettings: false }),
101125
new AuditPlugin(),
@@ -112,6 +136,13 @@ describe('print service on a real showcase boot', () => {
112136
const member = await ql.findOne('sys_user', { where: { email: memberEmail } }, { context: SYS });
113137
await ql.insert('sys_user_permission_set', { user_id: member!.id, permission_set: 'showcase_contributor' }, { context: SYS });
114138
adminToken = await stack.signIn();
139+
// The member who may attach files: the contributor's set and the attach grant.
140+
const attacherEmail = 'print-attacher@dogfood.test';
141+
attacherToken = await stack.signUp(attacherEmail);
142+
const attacher = await ql.findOne('sys_user', { where: { email: attacherEmail } }, { context: SYS });
143+
for (const permission_set of ['showcase_contributor', attacherSet.name]) {
144+
await ql.insert('sys_user_permission_set', { user_id: attacher!.id, permission_set }, { context: SYS });
145+
}
115146
// A reader who may read every invoice and edit none: the app's own `showcase_auditor`.
116147
const auditorEmail = 'print-auditor@dogfood.test';
117148
auditorToken = await stack.signUp(auditorEmail);
@@ -123,6 +154,7 @@ describe('print service on a real showcase boot', () => {
123154
ql.insert('showcase_invoice', { name, owner, account: seeded.account, status: 'draft' }, { context: SYS });
124155
ownInvoice = String((await invoice('INV-PRINT-OWN', memberEmail)).id);
125156
othersInvoice = String((await invoice('INV-PRINT-OTHER', 'someone-else@dogfood.test')).id);
157+
attacherInvoice = String((await invoice('INV-PRINT-ATTACHER', attacherEmail)).id);
126158

127159
service = new PrintService({
128160
placement: {
@@ -246,6 +278,51 @@ describe('print service on a real showcase boot', () => {
246278
expect(attachment!.uploaded_by).toBe(ctx.userId);
247279
});
248280

281+
it('a member who may attach files archives their own invoice: the attachment is their write, and sys_file stays unreadable to them', async () => {
282+
const ctx = await stack.contextFor(attacherToken);
283+
// Preconditions: the member reads their invoice, and is refused a read of `sys_file`.
284+
expect(await ql.findOne('showcase_invoice', { where: { id: attacherInvoice } }, { context: ctx })).not.toBeNull();
285+
expect(await refusalOf(ql.find('sys_file', { limit: 1 }, { context: ctx }))).toEqual({ code: 'PERMISSION_DENIED', status: 403 });
286+
const out = await service.printRecordToFile(request(attacherInvoice), ctx);
287+
const file = await ql.findOne('sys_file', { where: { id: out.fileId } }, { context: SYS });
288+
expect(file).toMatchObject({ status: 'committed', mime_type: 'application/pdf', scope: 'attachments', owner_id: ctx.userId });
289+
const attachment = await ql.findOne('sys_attachment', { where: { id: out.attachmentId } }, { context: SYS });
290+
expect(attachment).toMatchObject({
291+
parent_object: 'showcase_invoice', parent_id: attacherInvoice, file_id: out.fileId, uploaded_by: ctx.userId,
292+
});
293+
// Still not readable after the archive: the print widened no read.
294+
expect(await refusalOf(ql.findOne('sys_file', { where: { id: out.fileId } }, { context: ctx }))).toEqual({
295+
code: 'PERMISSION_DENIED',
296+
status: 403,
297+
});
298+
});
299+
300+
it('control: the same member’s own sys_attachment insert, naming a file they own and cannot read, is refused reference_not_found', async () => {
301+
const ctx = await stack.contextFor(attacherToken);
302+
// A file the member owns, written by the system as the upload doors write one.
303+
const fileId = 'print_dogfood_attacher_file';
304+
await ql.insert('sys_file', {
305+
id: fileId, key: `attachments/${fileId}/own.pdf`, name: 'own.pdf', mime_type: 'application/pdf', size: PDF.byteLength,
306+
scope: 'attachments', acl: 'private', status: 'committed', owner_id: ctx.userId,
307+
...(ctx.tenantId ? { organization_id: ctx.tenantId } : {}),
308+
}, { context: SYS });
309+
let refusal: { code?: unknown; fields?: Array<{ field?: unknown; code?: unknown }> } | undefined;
310+
try {
311+
await ql.insert(
312+
'sys_attachment',
313+
{ parent_object: 'showcase_invoice', parent_id: attacherInvoice, file_id: fileId, file_name: 'own.pdf' },
314+
{ context: ctx },
315+
);
316+
} catch (err) {
317+
refusal = err as typeof refusal;
318+
}
319+
expect(refusal, 'the insert was refused').toBeDefined();
320+
expect([refusal!.code, (refusal!.fields ?? []).map((f) => [f.field, f.code])]).toEqual([
321+
'VALIDATION_FAILED',
322+
[['file_id', 'reference_not_found']],
323+
]);
324+
});
325+
249326
it('with the real auth mint: the render loads as the caller, and the session is gone once the print returns', async () => {
250327
const ctx = await stack.contextFor(adminToken);
251328
const minter = principalSessionMinterOf(stack.kernel.getService<IAuthService>('auth'));

‎packages/services/service-storage/src/file-attachment-service.test.ts‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -551,12 +551,16 @@ describe('attachFileAs for a member who cannot read sys_file: the verb vouches f
551551
return booted;
552552
}
553553

554-
it('precondition: the member cannot read sys_file, not even the file they own', async () => {
555-
const { ql } = await bootMember();
556-
await ql.insert('sys_file', {
554+
/** A committed file the member owns, written by the system as the upload doors write one. */
555+
const seedOwnFile = (ql: ObjectQL) =>
556+
ql.insert('sys_file', {
557557
id: 'file_alice', key: 'attachments/file_alice/n.txt', name: 'n.txt', mime_type: 'text/plain', size: 1,
558558
scope: 'attachments', acl: 'private', status: 'committed', owner_id: 'usr_alice', organization_id: 'org_a',
559559
}, SYS);
560+
561+
it('precondition: the member cannot read sys_file, not even the file they own', async () => {
562+
const { ql } = await bootMember();
563+
await seedOwnFile(ql);
560564
const err = await refusalOf(ql.findOne('sys_file', { where: { id: 'file_alice' }, context: ALICE } as any));
561565
expect([err.code, err.status]).toEqual(['PERMISSION_DENIED', 403]);
562566
});
@@ -602,19 +606,35 @@ describe('attachFileAs for a member who cannot read sys_file: the verb vouches f
602606
await expectNothingLeft(ql, storage);
603607
});
604608

605-
it('control: the member’s own insert of sys_attachment through the data engine, naming a file they cannot read, is refused reference_not_found — the very file the verb attached included', async () => {
606-
const { ql, attachments } = await bootMember();
607-
const out = await asCaller(attachments).attachFileAs(input(), ALICE as never);
609+
/** What the member's own `sys_attachment` insert through the data engine answers. */
610+
async function directInsertRefusal(ql: ObjectQL, fileId: string) {
608611
const err = await refusalOf(
609612
ql.insert(
610613
'sys_attachment',
611-
{ parent_object: 'att_case', parent_id: 'case_a', file_id: out.fileId, file_name: 'again.eml' },
614+
{ parent_object: 'att_case', parent_id: 'case_a', file_id: fileId, file_name: 'direct.eml' },
612615
{ context: ALICE } as any,
613616
),
614617
);
615618
const envelope = resolveThrownHttpError(err);
616-
expect([envelope.code, envelope.status]).toEqual(['VALIDATION_FAILED', 400]);
617-
expect(((err as any).fields ?? []).map((f: any) => [f.field, f.code])).toEqual([['file_id', 'reference_not_found']]);
619+
return {
620+
code: envelope.code,
621+
status: envelope.status,
622+
fields: ((err as any).fields ?? []).map((f: any) => [f.field, f.code]),
623+
};
624+
}
625+
const REFERENCE_NOT_FOUND = { code: 'VALIDATION_FAILED', status: 400, fields: [['file_id', 'reference_not_found']] };
626+
627+
it('control: the member’s own insert of sys_attachment through the data engine, naming a file they cannot read, is refused reference_not_found', async () => {
628+
const { ql } = await bootMember();
629+
await seedOwnFile(ql);
630+
expect(await directInsertRefusal(ql, 'file_alice')).toEqual(REFERENCE_NOT_FOUND);
631+
expect(await rows(ql, 'sys_attachment')).toEqual([]);
632+
});
633+
634+
it('the vouch is the verb’s call alone: after attachFileAs, the member’s own insert naming the same file is refused reference_not_found', async () => {
635+
const { ql, attachments } = await bootMember();
636+
const out = await asCaller(attachments).attachFileAs(input(), ALICE as never);
637+
expect(await directInsertRefusal(ql, out.fileId)).toEqual(REFERENCE_NOT_FOUND);
618638
expect(await rows(ql, 'sys_attachment')).toHaveLength(1);
619639
});
620640
});

0 commit comments

Comments
 (0)