Skip to content

Commit 49f7749

Browse files
authored
fix(sandbox): stabilize remote hardlink checks (#108879)
1 parent 8090cb1 commit 49f7749

2 files changed

Lines changed: 60 additions & 66 deletions

File tree

‎src/agents/sandbox/remote-fs-bridge.test.ts‎

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,35 @@ import {
1111
type RemoteShellSandboxHandle,
1212
} from "./remote-fs-bridge.js";
1313

14+
function shellResult(stdout: string) {
15+
return { stdout: Buffer.from(stdout), stderr: Buffer.alloc(0), code: 0 };
16+
}
17+
18+
function createStatRuntime(
19+
workspaceDir: string,
20+
outputs: { hardlinks: (script: string) => string; stat: (script: string) => string },
21+
): RemoteShellSandboxHandle {
22+
return {
23+
remoteWorkspaceDir: workspaceDir,
24+
remoteAgentWorkspaceDir: workspaceDir,
25+
runRemoteShellScript: async (command) => {
26+
if (command.script.includes('if [ -e "$1" ] || [ -L "$1" ]')) {
27+
return shellResult("1\n");
28+
}
29+
if (command.script.includes('readlink -f -- "$cursor"')) {
30+
return shellResult(`${workspaceDir}/note.txt\n`);
31+
}
32+
if (command.script.includes('stat -c "%F|%h"')) {
33+
return shellResult(`${outputs.hardlinks(command.script)}\n`);
34+
}
35+
if (command.script.includes('stat -c "%F|%s|%y"')) {
36+
return shellResult(`${outputs.stat(command.script)}\n`);
37+
}
38+
throw new Error(`unexpected remote script: ${command.script}`);
39+
},
40+
};
41+
}
42+
1443
function createLocalRemoteRuntime(params: {
1544
remoteWorkspaceDir: string;
1645
remoteAgentWorkspaceDir: string;
@@ -194,40 +223,11 @@ describe("remote sandbox fs bridge", () => {
194223
await withTempDir("openclaw-remote-fs-bridge-stat-", async (stateDir) => {
195224
const workspaceDir = path.join(stateDir, "workspace");
196225
await fs.mkdir(workspaceDir, { recursive: true });
197-
const runtime: RemoteShellSandboxHandle = {
198-
remoteWorkspaceDir: workspaceDir,
199-
remoteAgentWorkspaceDir: workspaceDir,
200-
runRemoteShellScript: async (command) => {
201-
if (command.script.includes('if [ -e "$1" ] || [ -L "$1" ]')) {
202-
return { stdout: Buffer.from("1\n"), stderr: Buffer.alloc(0), code: 0 };
203-
}
204-
if (command.script.includes('readlink -f -- "$cursor"')) {
205-
return {
206-
stdout: Buffer.from(`${workspaceDir}/note.txt\n`),
207-
stderr: Buffer.alloc(0),
208-
code: 0,
209-
};
210-
}
211-
if (command.script.includes('stat -c "%F|%h"')) {
212-
return {
213-
stdout: Buffer.from("regular file|1\n"),
214-
stderr: Buffer.alloc(0),
215-
code: 0,
216-
};
217-
}
218-
if (command.script.includes('stat -c "%F|%s|%y"')) {
219-
const kind = command.script.includes('LC_ALL=C stat -c "%F|%s|%y"')
220-
? "regular file"
221-
: "reguläre Datei";
222-
return {
223-
stdout: Buffer.from(`${kind}|9007199254740992|8640000000001\n`),
224-
stderr: Buffer.alloc(0),
225-
code: 0,
226-
};
227-
}
228-
throw new Error(`unexpected remote script: ${command.script}`);
229-
},
230-
};
226+
const runtime = createStatRuntime(workspaceDir, {
227+
hardlinks: () => "regular file|1",
228+
stat: (script) =>
229+
`${script.includes('LC_ALL=C stat -c "%F|%s|%y"') ? "regular file" : "reguläre Datei"}|9007199254740992|8640000000001`,
230+
});
231231
const bridge = createRemoteShellSandboxFsBridge({
232232
sandbox: createSandbox({
233233
workspaceDir,
@@ -244,41 +244,35 @@ describe("remote sandbox fs bridge", () => {
244244
});
245245
});
246246

247+
it("rejects hardlinked files under localized remote shells", async () => {
248+
await withTempDir("openclaw-remote-fs-bridge-hardlink-locale-", async (stateDir) => {
249+
const workspaceDir = path.join(stateDir, "workspace");
250+
await fs.mkdir(workspaceDir, { recursive: true });
251+
const runtime = createStatRuntime(workspaceDir, {
252+
hardlinks: (script) =>
253+
`${script.includes('LC_ALL=C stat -c "%F|%h"') ? "regular file" : "reguläre Datei"}|2`,
254+
stat: () => "regular file|12|2026-05-29 12:00:00.000000000 +0000",
255+
});
256+
const bridge = createRemoteShellSandboxFsBridge({
257+
sandbox: createSandbox({
258+
workspaceDir,
259+
agentWorkspaceDir: workspaceDir,
260+
}),
261+
runtime,
262+
});
263+
264+
await expect(bridge.stat({ filePath: "note.txt" })).rejects.toThrow(/Hardlinked path/);
265+
});
266+
});
267+
247268
it("does not reject malformed non-decimal hardlink counts", async () => {
248269
await withTempDir("openclaw-remote-fs-bridge-hardlink-", async (stateDir) => {
249270
const workspaceDir = path.join(stateDir, "workspace");
250271
await fs.mkdir(workspaceDir, { recursive: true });
251-
const runtime: RemoteShellSandboxHandle = {
252-
remoteWorkspaceDir: workspaceDir,
253-
remoteAgentWorkspaceDir: workspaceDir,
254-
runRemoteShellScript: async (command) => {
255-
if (command.script.includes('if [ -e "$1" ] || [ -L "$1" ]')) {
256-
return { stdout: Buffer.from("1\n"), stderr: Buffer.alloc(0), code: 0 };
257-
}
258-
if (command.script.includes('readlink -f -- "$cursor"')) {
259-
return {
260-
stdout: Buffer.from(`${workspaceDir}/note.txt\n`),
261-
stderr: Buffer.alloc(0),
262-
code: 0,
263-
};
264-
}
265-
if (command.script.includes('stat -c "%F|%h"')) {
266-
return {
267-
stdout: Buffer.from("regular file|0x2\n"),
268-
stderr: Buffer.alloc(0),
269-
code: 0,
270-
};
271-
}
272-
if (command.script.includes('stat -c "%F|%s|%y"')) {
273-
return {
274-
stdout: Buffer.from("regular file|12|2026-05-29 12:00:00.000000000 +0000\n"),
275-
stderr: Buffer.alloc(0),
276-
code: 0,
277-
};
278-
}
279-
throw new Error(`unexpected remote script: ${command.script}`);
280-
},
281-
};
272+
const runtime = createStatRuntime(workspaceDir, {
273+
hardlinks: () => "regular file|0x2",
274+
stat: () => "regular file|12|2026-05-29 12:00:00.000000000 +0000",
275+
});
282276
const bridge = createRemoteShellSandboxFsBridge({
283277
sandbox: createSandbox({
284278
workspaceDir,

‎src/agents/sandbox/remote-fs-bridge.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ class RemoteShellSandboxFsBridge implements SandboxFsBridge {
529529
const result = await this.runRemoteScript({
530530
script: [
531531
'if [ ! -e "$1" ] && [ ! -L "$1" ]; then exit 0; fi',
532-
'stats=$(stat -c "%F|%h" -- "$1")',
532+
'stats=$(LC_ALL=C stat -c "%F|%h" -- "$1")',
533533
'printf "%s\\n" "$stats"',
534534
].join("\n"),
535535
args: [params.containerPath],

0 commit comments

Comments
 (0)