@@ -27,7 +27,7 @@ function createStatRuntime(
2727 return shellResult ( "1\n" ) ;
2828 }
2929 if ( command . script . includes ( 'readlink -f -- "$cursor"' ) ) {
30- return shellResult ( `${ workspaceDir } /note.txt\n` ) ;
30+ return shellResult ( `${ workspaceDir } /note.txt\n${ workspaceDir } \n ` ) ;
3131 }
3232 if ( command . script . includes ( 'stat -c "%F|%h"' ) ) {
3333 return shellResult ( `${ outputs . hardlinks ( command . script ) } \n` ) ;
@@ -168,6 +168,53 @@ describe("remote sandbox fs bridge", () => {
168168 } ,
169169 ) ;
170170
171+ it . runIf ( process . platform !== "win32" ) (
172+ "accepts a symlinked mount root while rejecting escapes through it" ,
173+ async ( ) => {
174+ await withTempDir ( "openclaw-remote-fs-linked-root-" , async ( stateDir ) => {
175+ const realWorkspaceDir = path . join ( stateDir , "real-workspace" ) ;
176+ const linkedWorkspaceDir = path . join ( stateDir , "linked-workspace" ) ;
177+ const outsideDir = path . join ( stateDir , "outside" ) ;
178+ await fs . mkdir ( realWorkspaceDir ) ;
179+ await fs . mkdir ( outsideDir ) ;
180+ await fs . symlink ( realWorkspaceDir , linkedWorkspaceDir , "dir" ) ;
181+ await fs . symlink ( outsideDir , path . join ( realWorkspaceDir , "escape" ) , "dir" ) ;
182+ const { runtime } = createLocalRemoteRuntime ( {
183+ remoteWorkspaceDir : linkedWorkspaceDir ,
184+ remoteAgentWorkspaceDir : linkedWorkspaceDir ,
185+ } ) ;
186+ const bridge = createRemoteShellSandboxFsBridge ( {
187+ sandbox : createSandbox ( {
188+ workspaceDir : linkedWorkspaceDir ,
189+ agentWorkspaceDir : linkedWorkspaceDir ,
190+ } ) ,
191+ runtime,
192+ } ) ;
193+ const createFileExclusive = bridge . createFileExclusive ?. bind ( bridge ) ;
194+ expect ( createFileExclusive ) . toBeTypeOf ( "function" ) ;
195+
196+ await expect (
197+ createFileExclusive ! ( { filePath : "inside.txt" , data : "inside" } ) ,
198+ ) . resolves . toBe ( "created" ) ;
199+ await expect ( bridge . readFile ( { filePath : "inside.txt" } ) ) . resolves . toEqual (
200+ Buffer . from ( "inside" ) ,
201+ ) ;
202+ await expect ( bridge . mkdirp ( { filePath : "nested/dir" } ) ) . resolves . toBeUndefined ( ) ;
203+ await expect ( fs . readFile ( path . join ( realWorkspaceDir , "inside.txt" ) , "utf8" ) ) . resolves . toBe (
204+ "inside" ,
205+ ) ;
206+ await expect (
207+ fs
208+ . stat ( path . join ( realWorkspaceDir , "nested" , "dir" ) )
209+ . then ( ( entry ) => entry . isDirectory ( ) ) ,
210+ ) . resolves . toBe ( true ) ;
211+ await expect (
212+ createFileExclusive ! ( { filePath : "escape/outside.txt" , data : "blocked" } ) ,
213+ ) . rejects . toThrow ( / e s c a p e s a l l o w e d m o u n t s / ) ;
214+ } ) ;
215+ } ,
216+ ) ;
217+
171218 it . runIf ( process . platform !== "win32" ) (
172219 "reads files with the pinned mutation helper" ,
173220 async ( ) => {
@@ -192,11 +239,11 @@ describe("remote sandbox fs bridge", () => {
192239 await expect ( bridge . readFile ( { filePath : "note.txt" } ) ) . resolves . toEqual (
193240 Buffer . from ( "hello" ) ,
194241 ) ;
195- expect ( calls ) . toHaveLength ( 1 ) ;
196- expect ( calls [ 0 ] ?. args ?. [ 0 ] ) . toBe ( "read" ) ;
197- expect ( calls [ 0 ] ?. script ) . toContain ( "python3 /dev/fd/3 \"$@\" 3<<'PY'" ) ;
198- expect ( calls [ 0 ] ?. script ) . toContain ( "read_file(parent_fd, basename)" ) ;
199- expect ( calls [ 0 ] ?. script ) . not . toContain ( 'cat -- "$1"' ) ;
242+ expect ( calls ) . toHaveLength ( 2 ) ;
243+ const readCall = calls . find ( ( call ) => call . args ?. [ 0 ] === "read" ) ;
244+ expect ( readCall ?. script ) . toContain ( "python3 /dev/fd/3 \"$@\" 3<<'PY'" ) ;
245+ expect ( readCall ?. script ) . toContain ( "read_file(parent_fd, basename)" ) ;
246+ expect ( readCall ?. script ) . not . toContain ( 'cat -- "$1"' ) ;
200247 } ) ;
201248 } ,
202249 ) ;
@@ -222,14 +269,20 @@ describe("remote sandbox fs bridge", () => {
222269 await expect ( bridge . readFile ( { filePath : "note.txt" , maxBytes : 5 } ) ) . resolves . toEqual (
223270 Buffer . from ( "hello" ) ,
224271 ) ;
225- expect ( calls [ 0 ] ?. args ) . toEqual ( [ "read" , workspaceDir , "" , "note.txt" , "5" ] ) ;
272+ expect ( calls . find ( ( call ) => call . args ?. [ 0 ] === "read" ) ?. args ) . toEqual ( [
273+ "read" ,
274+ workspaceDir ,
275+ "" ,
276+ "note.txt" ,
277+ "5" ,
278+ ] ) ;
226279 await expect ( bridge . readFile ( { filePath : "note.txt" , maxBytes : 4 } ) ) . rejects . toThrow (
227280 / b o u n d e d r e a d l i m i t / i,
228281 ) ;
229282 await expect ( bridge . readFile ( { filePath : "note.txt" , maxBytes : - 1 } ) ) . rejects . toThrow (
230283 / n o n - n e g a t i v e s a f e i n t e g e r / i,
231284 ) ;
232- expect ( calls ) . toHaveLength ( 2 ) ;
285+ expect ( calls ) . toHaveLength ( 4 ) ;
233286 } ) ;
234287 } ,
235288 ) ;
0 commit comments