@@ -34,6 +34,7 @@ import { promises as fs } from 'node:fs';
3434import { join } from 'node:path' ;
3535import { tmpdir } from 'node:os' ;
3636import { ObjectQL } from '@objectstack/objectql' ;
37+ import { resolveThrownHttpError } from '@objectstack/types' ;
3738import { SqlDriver } from '@objectstack/driver-sql' ;
3839import { installAuditWriters } from '@objectstack/plugin-audit' ;
3940import { SysAttachment } from '@objectstack/platform-objects/audit' ;
@@ -520,3 +521,100 @@ describe('the file-attachments writer AS THE REQUESTING USER: assertMayAttach an
520521 await expectNothingLeft ( ql , storage ) ;
521522 } ) ;
522523} ) ;
524+
525+ describe ( 'attachFileAs for a member who cannot read sys_file: the verb vouches for the one file it created' , ( ) => {
526+ // A member as a real deployment holds one: the create grant on
527+ // `sys_attachment` and none on `sys_file`. The middleware below stands in for
528+ // the object-permission layer: a non-system read of `sys_file` is refused
529+ // outright, as an ungranted object read is (`PERMISSION_DENIED` / 403), and
530+ // so is Bob's read of the parent object. The write-time reference check asks
531+ // the caller's read door for every `lookup` value, `sys_attachment.file_id`
532+ // included.
533+ const ALICE = { userId : 'usr_alice' , tenantId : 'org_a' , positions : [ 'org_member' ] } ;
534+ const BOB = { userId : 'usr_bob' , tenantId : 'org_a' , positions : [ 'org_member' ] } ;
535+ const asCaller = ( a : IFileAttachmentService ) => a as IFileAttachmentService & ICallerFileAttachmentService ;
536+ const READS = new Set ( [ 'find' , 'findOne' , 'count' , 'aggregate' ] ) ;
537+ const readRefusal = ( object : string ) =>
538+ Object . assign ( new Error ( `[Security] Access denied: read on ${ object } ` ) , {
539+ name : 'PermissionDeniedError' , code : 'PERMISSION_DENIED' , status : 403 , statusCode : 403 ,
540+ } ) ;
541+
542+ async function bootMember ( opts : { sharing ?: unknown } = { } ) {
543+ const booted = await boot ( opts ) ;
544+ booted . ql . registerMiddleware ( async ( ctx : any , next : ( ) => Promise < void > ) => {
545+ if ( READS . has ( ctx . operation ) && ctx . context ?. isSystem !== true ) {
546+ if ( ctx . object === 'sys_file' ) throw readRefusal ( 'sys_file' ) ;
547+ if ( ctx . object === 'att_case' && ctx . context ?. userId === BOB . userId ) throw readRefusal ( 'att_case' ) ;
548+ }
549+ await next ( ) ;
550+ } ) ;
551+ return booted ;
552+ }
553+
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' , {
557+ id : 'file_alice' , key : 'attachments/file_alice/n.txt' , name : 'n.txt' , mime_type : 'text/plain' , size : 1 ,
558+ scope : 'attachments' , acl : 'private' , status : 'committed' , owner_id : 'usr_alice' , organization_id : 'org_a' ,
559+ } , SYS ) ;
560+ const err = await refusalOf ( ql . findOne ( 'sys_file' , { where : { id : 'file_alice' } , context : ALICE } as any ) ) ;
561+ expect ( [ err . code , err . status ] ) . toEqual ( [ 'PERMISSION_DENIED' , 403 ] ) ;
562+ } ) ;
563+
564+ it ( 'attaches as the member: one committed sys_file owned by them, one sys_attachment stamped uploaded_by them' , async ( ) => {
565+ const { ql, attachments, storage } = await bootMember ( ) ;
566+ const out = await asCaller ( attachments ) . attachFileAs ( input ( ) , ALICE as never ) ;
567+ const files = await rows ( ql , 'sys_file' ) ;
568+ expect ( files ) . toHaveLength ( 1 ) ;
569+ expect ( files [ 0 ] ) . toMatchObject ( { id : out . fileId , status : 'committed' , owner_id : 'usr_alice' , organization_id : 'org_a' } ) ;
570+ const links = await rows ( ql , 'sys_attachment' ) ;
571+ expect ( links ) . toHaveLength ( 1 ) ;
572+ expect ( links [ 0 ] ) . toMatchObject ( {
573+ id : out . attachmentId , parent_object : 'att_case' , parent_id : 'case_a' , file_id : out . fileId ,
574+ uploaded_by : 'usr_alice' , organization_id : 'org_a' ,
575+ } ) ;
576+ expect ( await storedKeys ( storage ) ) . toEqual ( [ files [ 0 ] . key ] ) ;
577+ } ) ;
578+
579+ it ( 'a producer’s sequence (assertMayAttach, then attachFileAs — the print archive’s) attaches as the member' , async ( ) => {
580+ const sharing = { checkEdit : vi . fn ( async ( ) => 'allow' ) } ;
581+ const { ql, attachments } = await bootMember ( { sharing } ) ;
582+ await asCaller ( attachments ) . assertMayAttach ( 'att_case' , 'case_a' , ALICE as never ) ;
583+ const out = await asCaller ( attachments ) . attachFileAs ( input ( { filename : 'printed.pdf' , mimeType : 'application/pdf' } ) , ALICE as never ) ;
584+ const [ link ] = await rows ( ql , 'sys_attachment' ) ;
585+ expect ( link ) . toMatchObject ( { id : out . attachmentId , file_id : out . fileId , uploaded_by : 'usr_alice' } ) ;
586+ } ) ;
587+
588+ it ( 'control: a member the attach rule refuses is still refused, by assertMayAttach and by attachFileAs, and nothing is left' , async ( ) => {
589+ const sharing = { checkEdit : vi . fn ( async ( ) => 'deny' ) } ;
590+ const { ql, attachments, storage } = await bootMember ( { sharing } ) ;
591+ const pre = await refusalOf ( asCaller ( attachments ) . assertMayAttach ( 'att_case' , 'case_a' , ALICE as never ) ) ;
592+ expect ( [ pre . code , pre . status ] ) . toEqual ( [ 'ATTACHMENT_PARENT_ACCESS' , 403 ] ) ;
593+ const r = await refusalOf ( asCaller ( attachments ) . attachFileAs ( input ( ) , ALICE as never ) ) ;
594+ expect ( [ r . code , r . status ] ) . toEqual ( [ 'ATTACHMENT_PARENT_ACCESS' , 403 ] ) ;
595+ await expectNothingLeft ( ql , storage ) ;
596+ } ) ;
597+
598+ it ( 'control: a parent the member cannot read is refused RECORD_NOT_FOUND / 404, and nothing is left' , async ( ) => {
599+ const { ql, attachments, storage } = await bootMember ( ) ;
600+ const r = await refusalOf ( asCaller ( attachments ) . attachFileAs ( input ( ) , BOB as never ) ) ;
601+ expect ( [ r . code , r . status ] ) . toEqual ( [ 'RECORD_NOT_FOUND' , 404 ] ) ;
602+ await expectNothingLeft ( ql , storage ) ;
603+ } ) ;
604+
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 ) ;
608+ const err = await refusalOf (
609+ ql . insert (
610+ 'sys_attachment' ,
611+ { parent_object : 'att_case' , parent_id : 'case_a' , file_id : out . fileId , file_name : 'again.eml' } ,
612+ { context : ALICE } as any ,
613+ ) ,
614+ ) ;
615+ 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' ] ] ) ;
618+ expect ( await rows ( ql , 'sys_attachment' ) ) . toHaveLength ( 1 ) ;
619+ } ) ;
620+ } ) ;
0 commit comments