mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 01:33:29 +00:00
refactor: centralize delivery/path/media/version lifecycle
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
||||
import { detectMime } from "../media/mime.js";
|
||||
import { sniffMimeFromBase64 } from "../media/sniff-mime-from-base64.js";
|
||||
import type { ImageSanitizationLimits } from "./image-sanitization.js";
|
||||
import { toRelativeWorkspacePath } from "./path-policy.js";
|
||||
import type { AnyAgentTool } from "./pi-tools.types.js";
|
||||
import { assertSandboxPath } from "./sandbox-paths.js";
|
||||
import type { SandboxFsBridge } from "./sandbox/fs-bridge.js";
|
||||
@@ -784,13 +785,13 @@ function createHostWriteOperations(root: string, options?: { workspaceOnly?: boo
|
||||
// When workspaceOnly is true, enforce workspace boundary
|
||||
return {
|
||||
mkdir: async (dir: string) => {
|
||||
const relative = toRelativePathInRoot(root, dir, { allowRoot: true });
|
||||
const relative = toRelativeWorkspacePath(root, dir, { allowRoot: true });
|
||||
const resolved = relative ? path.resolve(root, relative) : path.resolve(root);
|
||||
await assertSandboxPath({ filePath: resolved, cwd: root, root });
|
||||
await fs.mkdir(resolved, { recursive: true });
|
||||
},
|
||||
writeFile: async (absolutePath: string, content: string) => {
|
||||
const relative = toRelativePathInRoot(root, absolutePath);
|
||||
const relative = toRelativeWorkspacePath(root, absolutePath);
|
||||
await writeFileWithinRoot({
|
||||
rootDir: root,
|
||||
relativePath: relative,
|
||||
@@ -827,7 +828,7 @@ function createHostEditOperations(root: string, options?: { workspaceOnly?: bool
|
||||
// When workspaceOnly is true, enforce workspace boundary
|
||||
return {
|
||||
readFile: async (absolutePath: string) => {
|
||||
const relative = toRelativePathInRoot(root, absolutePath);
|
||||
const relative = toRelativeWorkspacePath(root, absolutePath);
|
||||
const safeRead = await readFileWithinRoot({
|
||||
rootDir: root,
|
||||
relativePath: relative,
|
||||
@@ -835,7 +836,7 @@ function createHostEditOperations(root: string, options?: { workspaceOnly?: bool
|
||||
return safeRead.buffer;
|
||||
},
|
||||
writeFile: async (absolutePath: string, content: string) => {
|
||||
const relative = toRelativePathInRoot(root, absolutePath);
|
||||
const relative = toRelativeWorkspacePath(root, absolutePath);
|
||||
await writeFileWithinRoot({
|
||||
rootDir: root,
|
||||
relativePath: relative,
|
||||
@@ -846,7 +847,7 @@ function createHostEditOperations(root: string, options?: { workspaceOnly?: bool
|
||||
access: async (absolutePath: string) => {
|
||||
let relative: string;
|
||||
try {
|
||||
relative = toRelativePathInRoot(root, absolutePath);
|
||||
relative = toRelativeWorkspacePath(root, absolutePath);
|
||||
} catch {
|
||||
// Path escapes workspace root. Don't throw here – the upstream
|
||||
// library replaces any `access` error with a misleading "File not
|
||||
@@ -876,26 +877,6 @@ function createHostEditOperations(root: string, options?: { workspaceOnly?: bool
|
||||
} as const;
|
||||
}
|
||||
|
||||
function toRelativePathInRoot(
|
||||
root: string,
|
||||
candidate: string,
|
||||
options?: { allowRoot?: boolean },
|
||||
): string {
|
||||
const rootResolved = path.resolve(root);
|
||||
const resolved = path.resolve(candidate);
|
||||
const relative = path.relative(rootResolved, resolved);
|
||||
if (relative === "" || relative === ".") {
|
||||
if (options?.allowRoot) {
|
||||
return "";
|
||||
}
|
||||
throw new Error(`Path escapes workspace root: ${candidate}`);
|
||||
}
|
||||
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
||||
throw new Error(`Path escapes workspace root: ${candidate}`);
|
||||
}
|
||||
return relative;
|
||||
}
|
||||
|
||||
function createFsAccessError(code: string, filePath: string): NodeJS.ErrnoException {
|
||||
const error = new Error(`Sandbox FS error (${code}): ${filePath}`) as NodeJS.ErrnoException;
|
||||
error.code = code;
|
||||
|
||||
Reference in New Issue
Block a user