@@ -919,6 +919,165 @@ describe("AssetAccess", () => {
919919 } ) . pipe ( Effect . provide ( layerTest ) ) ,
920920 ) ;
921921
922+ it . effect (
923+ "serves Office bytes through signed workspace and host URLs without sibling access" ,
924+ ( ) =>
925+ Effect . gen ( function * ( ) {
926+ const fs = yield * FileSystem . FileSystem ;
927+ const path = yield * Path . Path ;
928+ const root = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-office-workspace-" } ) ;
929+ const outside = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-office-host-" } ) ;
930+ // Asset access transports bytes; parsing and format validation belong to the viewer.
931+ const bytes = new Uint8Array ( [ 0x50 , 0x4b , 3 , 4 , 0 , 0xff ] ) ;
932+ for ( const [ extension , mimeType ] of [
933+ [ "xlsx" , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ] ,
934+ [ "xls" , "application/vnd.ms-excel" ] ,
935+ [ "pptx" , "application/vnd.openxmlformats-officedocument.presentationml.presentation" ] ,
936+ ] as const ) {
937+ for ( const source of [ "workspace" , "draft" , "host" ] as const ) {
938+ const directory = source === "host" ? outside : root ;
939+ const name = `report.${ extension } ` ;
940+ const filePath = path . join ( directory , name ) ;
941+ yield * fs . writeFile ( filePath , bytes ) ;
942+ yield * fs . writeFile ( path . join ( directory , `sibling.${ extension } ` ) , bytes ) ;
943+ const resource =
944+ source === "workspace"
945+ ? {
946+ _tag : "workspace-file" as const ,
947+ threadId : ThreadId . make ( "thread-1" ) ,
948+ path : name ,
949+ }
950+ : source === "draft"
951+ ? { _tag : "draft-workspace-file" as const , cwd : root , path : name }
952+ : {
953+ _tag : "media-file" as const ,
954+ threadId : ThreadId . make ( "thread-1" ) ,
955+ path : filePath ,
956+ } ;
957+ const issued = yield * issueAssetUrl ( { resource, workspaceRoot : root } ) ;
958+ const suffix = issued . relativeUrl . slice ( `${ ASSET_ROUTE_PREFIX } /` . length ) ;
959+ const separator = suffix . indexOf ( "/" ) ;
960+ const token = suffix . slice ( 0 , separator ) ;
961+ const asset = yield * resolveAsset ( token , suffix . slice ( separator + 1 ) ) ;
962+ expect ( asset ) . toMatchObject ( { kind : "file" , path : yield * fs . realPath ( filePath ) } ) ;
963+ if ( asset ?. kind !== "file" ) throw new Error ( "Expected Office asset" ) ;
964+ const response = HttpServerResponse . toWeb ( yield * assetFileResponse ( asset ) ) ;
965+ expect ( response . headers . get ( "content-type" ) ) . toBe ( mimeType ) ;
966+ expect ( response . headers . get ( "content-disposition" ) ) . toBeNull ( ) ;
967+ expect ( new Uint8Array ( yield * Effect . promise ( ( ) => response . arrayBuffer ( ) ) ) ) . toEqual (
968+ bytes ,
969+ ) ;
970+ expect ( yield * resolveAsset ( token , `sibling.${ extension } ` ) ) . toBeNull ( ) ;
971+ expect ( yield * resolveAsset ( `${ token } tampered` , name ) ) . toBeNull ( ) ;
972+ expect ( yield * resolveAsset ( token , `../${ name } ` ) ) . toBeNull ( ) ;
973+ }
974+ }
975+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
976+ ) ;
977+
978+ it . effect (
979+ "does not issue workspace or host previews for Office-named directories or legacy ppt" ,
980+ ( ) =>
981+ Effect . gen ( function * ( ) {
982+ const fs = yield * FileSystem . FileSystem ;
983+ const path = yield * Path . Path ;
984+ const root = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-office-directory-" } ) ;
985+ for ( const extension of [ "xlsx" , "xls" , "pptx" ] ) {
986+ const filePath = path . join ( root , `folder.${ extension } ` ) ;
987+ yield * fs . makeDirectory ( filePath ) ;
988+ for ( const resource of [
989+ {
990+ _tag : "workspace-file" as const ,
991+ threadId : ThreadId . make ( "thread-1" ) ,
992+ path : filePath ,
993+ } ,
994+ { _tag : "draft-workspace-file" as const , cwd : root , path : filePath } ,
995+ { _tag : "media-file" as const , threadId : ThreadId . make ( "thread-1" ) , path : filePath } ,
996+ ] ) {
997+ const error = yield * issueAssetUrl ( { resource, workspaceRoot : root } ) . pipe ( Effect . flip ) ;
998+ expect ( error . _tag ) . toBe ( "AssetWorkspaceAssetNotFoundError" ) ;
999+ }
1000+ }
1001+ const legacyPath = path . join ( root , "legacy.ppt" ) ;
1002+ yield * fs . writeFile ( legacyPath , new Uint8Array ( [ 0xd0 , 0xcf , 0x11 , 0xe0 ] ) ) ;
1003+ const error = yield * issueAssetUrl ( {
1004+ resource : { _tag : "media-file" , threadId : ThreadId . make ( "thread-1" ) , path : legacyPath } ,
1005+ } ) . pipe ( Effect . flip ) ;
1006+ expect ( error ) . toBeInstanceOf ( AssetPreviewTypeValidationError ) ;
1007+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
1008+ ) ;
1009+
1010+ it . effect (
1011+ "serves Office attachment previews by stored extension and preserves explicit downloads" ,
1012+ ( ) =>
1013+ Effect . gen ( function * ( ) {
1014+ const config = yield * ServerConfig . ServerConfig ;
1015+ const fs = yield * FileSystem . FileSystem ;
1016+ const path = yield * Path . Path ;
1017+ const bytes = new Uint8Array ( [ 0x50 , 0x4b , 3 , 4 , 0 , 0xff ] ) ;
1018+ yield * fs . makeDirectory ( config . attachmentsDir , { recursive : true } ) ;
1019+ for ( const [ extension , mimeType ] of [
1020+ [ "xlsx" , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ] ,
1021+ [ "xls" , "application/vnd.ms-excel" ] ,
1022+ [ "pptx" , "application/vnd.openxmlformats-officedocument.presentationml.presentation" ] ,
1023+ ] as const ) {
1024+ const attachmentId = `thread-1-00000000-0000-4000-8000-000000000042-${ extension } ` ;
1025+ const attachmentPath = path . join ( config . attachmentsDir , `${ attachmentId } .${ extension } ` ) ;
1026+ yield * fs . writeFile ( attachmentPath , bytes ) ;
1027+ for ( const disposition of [ "inline" , "attachment" ] as const ) {
1028+ const issued = yield * issueAssetUrl ( {
1029+ resource : {
1030+ _tag : "attachment" ,
1031+ attachmentId,
1032+ fileName : `report.${ extension } ` ,
1033+ mimeType : "application/octet-stream" ,
1034+ disposition,
1035+ } ,
1036+ } ) ;
1037+ const suffix = issued . relativeUrl . slice ( `${ ASSET_ROUTE_PREFIX } /` . length ) ;
1038+ const separator = suffix . indexOf ( "/" ) ;
1039+ const token = suffix . slice ( 0 , separator ) ;
1040+ const asset = yield * resolveAsset ( token , suffix . slice ( separator + 1 ) ) ;
1041+ expect ( asset ) . toMatchObject ( { kind : "file" , path : attachmentPath } ) ;
1042+ if ( asset ?. kind !== "file" ) throw new Error ( "Expected Office attachment" ) ;
1043+ const response = HttpServerResponse . toWeb ( yield * assetFileResponse ( asset ) ) ;
1044+ expect ( response . headers . get ( "content-type" ) ) . toBe (
1045+ disposition === "inline" ? mimeType : "application/octet-stream" ,
1046+ ) ;
1047+ if ( disposition === "attachment" ) {
1048+ expect ( response . headers . get ( "content-disposition" ) ) . toContain ( "attachment;" ) ;
1049+ } else {
1050+ expect ( response . headers . get ( "content-disposition" ) ) . toBeNull ( ) ;
1051+ }
1052+ expect ( new Uint8Array ( yield * Effect . promise ( ( ) => response . arrayBuffer ( ) ) ) ) . toEqual (
1053+ bytes ,
1054+ ) ;
1055+ expect ( yield * resolveAsset ( `${ token } tampered` , `report.${ extension } ` ) ) . toBeNull ( ) ;
1056+ }
1057+ yield * fs . remove ( attachmentPath ) ;
1058+ yield * fs . makeDirectory ( attachmentPath ) ;
1059+ const issued = yield * issueAssetUrl ( {
1060+ resource : { _tag : "attachment" , attachmentId, disposition : "inline" } ,
1061+ } ) ;
1062+ const suffix = issued . relativeUrl . slice ( `${ ASSET_ROUTE_PREFIX } /` . length ) ;
1063+ const separator = suffix . indexOf ( "/" ) ;
1064+ expect (
1065+ yield * resolveAsset ( suffix . slice ( 0 , separator ) , suffix . slice ( separator + 1 ) ) ,
1066+ ) . toBeNull ( ) ;
1067+ }
1068+ const attachmentId = "thread-1-00000000-0000-4000-8000-000000000043-ppt" ;
1069+ yield * fs . writeFile ( path . join ( config . attachmentsDir , `${ attachmentId } .ppt` ) , bytes ) ;
1070+ const issued = yield * issueAssetUrl ( {
1071+ resource : { _tag : "attachment" , attachmentId, disposition : "inline" } ,
1072+ } ) ;
1073+ const suffix = issued . relativeUrl . slice ( `${ ASSET_ROUTE_PREFIX } /` . length ) ;
1074+ const separator = suffix . indexOf ( "/" ) ;
1075+ expect (
1076+ yield * resolveAsset ( suffix . slice ( 0 , separator ) , suffix . slice ( separator + 1 ) ) ,
1077+ ) . toMatchObject ( { download : true } ) ;
1078+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
1079+ ) ;
1080+
9221081 it . effect ( "previews workspace files with literal fragment characters in their paths" , ( ) =>
9231082 Effect . gen ( function * ( ) {
9241083 const fileSystem = yield * FileSystem . FileSystem ;
0 commit comments