mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 01:28:27 +00:00
fix: execute sandboxed file ops inside containers (#4026)
Merged via /review-pr -> /prepare-pr -> /merge-pr.
Prepared head SHA: 795ec6aa2f
Co-authored-by: davidbors-snyk <240482518+davidbors-snyk@users.noreply.github.com>
Co-authored-by: steipete <58493+steipete@users.noreply.github.com>
Reviewed-by: @steipete
This commit is contained in:
74
src/agents/test-helpers/host-sandbox-fs-bridge.ts
Normal file
74
src/agents/test-helpers/host-sandbox-fs-bridge.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import type { SandboxFsBridge, SandboxFsStat, SandboxResolvedPath } from "../sandbox/fs-bridge.js";
|
||||
import { resolveSandboxPath } from "../sandbox-paths.js";
|
||||
|
||||
export function createHostSandboxFsBridge(rootDir: string): SandboxFsBridge {
|
||||
const root = path.resolve(rootDir);
|
||||
|
||||
const resolvePath = (filePath: string, cwd?: string): SandboxResolvedPath => {
|
||||
const resolved = resolveSandboxPath({
|
||||
filePath,
|
||||
cwd: cwd ?? root,
|
||||
root,
|
||||
});
|
||||
const relativePath = resolved.relative
|
||||
? resolved.relative.split(path.sep).filter(Boolean).join(path.posix.sep)
|
||||
: "";
|
||||
const containerPath = relativePath ? path.posix.join("/workspace", relativePath) : "/workspace";
|
||||
return {
|
||||
hostPath: resolved.resolved,
|
||||
relativePath,
|
||||
containerPath,
|
||||
};
|
||||
};
|
||||
|
||||
return {
|
||||
resolvePath: ({ filePath, cwd }) => resolvePath(filePath, cwd),
|
||||
readFile: async ({ filePath, cwd }) => {
|
||||
const target = resolvePath(filePath, cwd);
|
||||
return fs.readFile(target.hostPath);
|
||||
},
|
||||
writeFile: async ({ filePath, cwd, data, mkdir = true }) => {
|
||||
const target = resolvePath(filePath, cwd);
|
||||
if (mkdir) {
|
||||
await fs.mkdir(path.dirname(target.hostPath), { recursive: true });
|
||||
}
|
||||
const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
||||
await fs.writeFile(target.hostPath, buffer);
|
||||
},
|
||||
mkdirp: async ({ filePath, cwd }) => {
|
||||
const target = resolvePath(filePath, cwd);
|
||||
await fs.mkdir(target.hostPath, { recursive: true });
|
||||
},
|
||||
remove: async ({ filePath, cwd, recursive, force }) => {
|
||||
const target = resolvePath(filePath, cwd);
|
||||
await fs.rm(target.hostPath, {
|
||||
recursive: recursive ?? false,
|
||||
force: force ?? false,
|
||||
});
|
||||
},
|
||||
rename: async ({ from, to, cwd }) => {
|
||||
const source = resolvePath(from, cwd);
|
||||
const target = resolvePath(to, cwd);
|
||||
await fs.mkdir(path.dirname(target.hostPath), { recursive: true });
|
||||
await fs.rename(source.hostPath, target.hostPath);
|
||||
},
|
||||
stat: async ({ filePath, cwd }) => {
|
||||
try {
|
||||
const target = resolvePath(filePath, cwd);
|
||||
const stats = await fs.stat(target.hostPath);
|
||||
return {
|
||||
type: stats.isDirectory() ? "directory" : stats.isFile() ? "file" : "other",
|
||||
size: stats.size,
|
||||
mtimeMs: stats.mtimeMs,
|
||||
} satisfies SandboxFsStat;
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
||||
return null;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user