@@ -25,6 +25,29 @@ describe("media understanding scope", () => {
2525
2626const originalFetch = globalThis . fetch ;
2727
28+ async function withLocalAttachmentCache (
29+ prefix : string ,
30+ run : ( params : {
31+ cache : MediaAttachmentCache ;
32+ attachmentPath : string ;
33+ canonicalAttachmentPath : string ;
34+ } ) => Promise < void > ,
35+ ) {
36+ await withTempDir ( { prefix } , async ( base ) => {
37+ const allowedRoot = path . join ( base , "allowed" ) ;
38+ const attachmentPath = path . join ( allowedRoot , "voice-note.m4a" ) ;
39+ await fs . mkdir ( allowedRoot , { recursive : true } ) ;
40+ await fs . writeFile ( attachmentPath , "ok" ) ;
41+ const canonicalAttachmentPath = await fs . realpath ( attachmentPath ) . catch ( ( ) => attachmentPath ) ;
42+
43+ const cache = new MediaAttachmentCache ( [ { index : 0 , path : attachmentPath } ] , {
44+ localPathRoots : [ allowedRoot ] ,
45+ } ) ;
46+
47+ await run ( { cache, attachmentPath, canonicalAttachmentPath } ) ;
48+ } ) ;
49+ }
50+
2851describe ( "media understanding attachments SSRF" , ( ) => {
2952 afterEach ( ( ) => {
3053 globalThis . fetch = originalFetch ;
@@ -45,16 +68,7 @@ describe("media understanding attachments SSRF", () => {
4568 } ) ;
4669
4770 it ( "reads local attachments inside configured roots" , async ( ) => {
48- await withTempDir ( { prefix : "openclaw-media-cache-allowed-" } , async ( base ) => {
49- const allowedRoot = path . join ( base , "allowed" ) ;
50- const attachmentPath = path . join ( allowedRoot , "voice-note.m4a" ) ;
51- await fs . mkdir ( allowedRoot , { recursive : true } ) ;
52- await fs . writeFile ( attachmentPath , "ok" ) ;
53-
54- const cache = new MediaAttachmentCache ( [ { index : 0 , path : attachmentPath } ] , {
55- localPathRoots : [ allowedRoot ] ,
56- } ) ;
57-
71+ await withLocalAttachmentCache ( "openclaw-media-cache-allowed-" , async ( { cache } ) => {
5872 const result = await cache . getBuffer ( { attachmentIndex : 0 , maxBytes : 1024 , timeoutMs : 1000 } ) ;
5973 expect ( result . buffer . toString ( ) ) . toBe ( "ok" ) ;
6074 } ) ;
@@ -111,63 +125,51 @@ describe("media understanding attachments SSRF", () => {
111125 } ) ;
112126
113127 it ( "enforces maxBytes after reading local attachments" , async ( ) => {
114- await withTempDir ( { prefix : "openclaw-media-cache-max-bytes-" } , async ( base ) => {
115- const allowedRoot = path . join ( base , "allowed" ) ;
116- const attachmentPath = path . join ( allowedRoot , "voice-note.m4a" ) ;
117- await fs . mkdir ( allowedRoot , { recursive : true } ) ;
118- await fs . writeFile ( attachmentPath , "ok" ) ;
119- const canonicalAttachmentPath = await fs . realpath ( attachmentPath ) . catch ( ( ) => attachmentPath ) ;
120-
121- const cache = new MediaAttachmentCache ( [ { index : 0 , path : attachmentPath } ] , {
122- localPathRoots : [ allowedRoot ] ,
123- } ) ;
124- const originalOpen = fs . open . bind ( fs ) ;
125- const openSpy = vi . spyOn ( fs , "open" ) ;
126-
127- openSpy . mockImplementation ( async ( filePath , flags ) => {
128- const handle = await originalOpen ( filePath , flags ) ;
129- const candidatePath = await fs . realpath ( String ( filePath ) ) . catch ( ( ) => String ( filePath ) ) ;
130- if ( candidatePath !== canonicalAttachmentPath ) {
131- return handle ;
132- }
133- const mockedHandle = handle as typeof handle & {
134- readFile : typeof handle . readFile ;
135- } ;
136- mockedHandle . readFile = ( async ( ) => Buffer . alloc ( 2048 , 1 ) ) as typeof handle . readFile ;
137- return mockedHandle ;
138- } ) ;
139-
140- await expect (
141- cache . getBuffer ( { attachmentIndex : 0 , maxBytes : 1024 , timeoutMs : 1000 } ) ,
142- ) . rejects . toThrow ( / e x c e e d s m a x B y t e s 1 0 2 4 / i) ;
143- } ) ;
128+ await withLocalAttachmentCache (
129+ "openclaw-media-cache-max-bytes-" ,
130+ async ( { cache, canonicalAttachmentPath } ) => {
131+ const originalOpen = fs . open . bind ( fs ) ;
132+ const openSpy = vi . spyOn ( fs , "open" ) ;
133+
134+ openSpy . mockImplementation ( async ( filePath , flags ) => {
135+ const handle = await originalOpen ( filePath , flags ) ;
136+ const candidatePath = await fs . realpath ( String ( filePath ) ) . catch ( ( ) => String ( filePath ) ) ;
137+ if ( candidatePath !== canonicalAttachmentPath ) {
138+ return handle ;
139+ }
140+ const mockedHandle = handle as typeof handle & {
141+ readFile : typeof handle . readFile ;
142+ } ;
143+ mockedHandle . readFile = ( async ( ) => Buffer . alloc ( 2048 , 1 ) ) as typeof handle . readFile ;
144+ return mockedHandle ;
145+ } ) ;
146+
147+ await expect (
148+ cache . getBuffer ( { attachmentIndex : 0 , maxBytes : 1024 , timeoutMs : 1000 } ) ,
149+ ) . rejects . toThrow ( / e x c e e d s m a x B y t e s 1 0 2 4 / i) ;
150+ } ,
151+ ) ;
144152 } ) ;
145153
146154 it ( "opens local attachments with nofollow on posix" , async ( ) => {
147155 if ( process . platform === "win32" ) {
148156 return ;
149157 }
150- await withTempDir ( { prefix : "openclaw-media-cache-flags-" } , async ( base ) => {
151- const allowedRoot = path . join ( base , "allowed" ) ;
152- const attachmentPath = path . join ( allowedRoot , "voice-note.m4a" ) ;
153- await fs . mkdir ( allowedRoot , { recursive : true } ) ;
154- await fs . writeFile ( attachmentPath , "ok" ) ;
155- const canonicalAttachmentPath = await fs . realpath ( attachmentPath ) . catch ( ( ) => attachmentPath ) ;
156-
157- const cache = new MediaAttachmentCache ( [ { index : 0 , path : attachmentPath } ] , {
158- localPathRoots : [ allowedRoot ] ,
159- } ) ;
160- const openSpy = vi . spyOn ( fs , "open" ) ;
161-
162- await cache . getBuffer ( { attachmentIndex : 0 , maxBytes : 1024 , timeoutMs : 1000 } ) ;
163-
164- expect ( openSpy ) . toHaveBeenCalled ( ) ;
165- const [ openedPath , openedFlags ] = openSpy . mock . calls [ 0 ] ?? [ ] ;
166- expect ( await fs . realpath ( String ( openedPath ) ) . catch ( ( ) => String ( openedPath ) ) ) . toBe (
167- canonicalAttachmentPath ,
168- ) ;
169- expect ( openedFlags ) . toBe ( fsConstants . O_RDONLY | fsConstants . O_NOFOLLOW ) ;
170- } ) ;
158+ await withLocalAttachmentCache (
159+ "openclaw-media-cache-flags-" ,
160+ async ( { cache, canonicalAttachmentPath } ) => {
161+ const openSpy = vi . spyOn ( fs , "open" ) ;
162+
163+ await cache . getBuffer ( { attachmentIndex : 0 , maxBytes : 1024 , timeoutMs : 1000 } ) ;
164+
165+ expect ( openSpy ) . toHaveBeenCalled ( ) ;
166+ const [ openedPath , openedFlags ] = openSpy . mock . calls [ 0 ] ?? [ ] ;
167+ expect ( await fs . realpath ( String ( openedPath ) ) . catch ( ( ) => String ( openedPath ) ) ) . toBe (
168+ canonicalAttachmentPath ,
169+ ) ;
170+ expect ( openedFlags ) . toBe ( fsConstants . O_RDONLY | fsConstants . O_NOFOLLOW ) ;
171+ } ,
172+ ) ;
171173 } ) ;
172174
173175 it ( "rejects local attachments when canonicalization fails" , async ( ) => {
0 commit comments