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
3036import { afterAll , beforeAll , describe , expect , it , vi } from 'vitest' ;
3137import { mkdtempSync , rmSync } from 'node:fs' ;
@@ -34,6 +40,9 @@ import { join } from 'node:path';
3440import type { VerifyStack } from '@objectstack/verify' ;
3541import { StorageServicePlugin } from '@objectstack/service-storage' ;
3642import { 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' ;
3746import { bootShowcase } from './showcase-boot.js' ;
3847import { PrintServicePlugin } from '../../../services/service-print/src/print-service-plugin.js' ;
3948import { principalSessionMinterOf } from '../../../services/service-print/src/principal-session.js' ;
@@ -54,6 +63,13 @@ const PAGE = 'showcase_invoice_document';
5463const PDF = Buffer . from ( '%PDF-1.4\n% print-service dogfood\n%%EOF\n' ) ;
5564const 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+
5773interface 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' ) ) ;
0 commit comments