mirror of
https://github.com/yuangyaa/openclaw.git
synced 2026-07-14 11:07:29 +08:00
fix(security): add optional workspace-only path guards for fs tools
This commit is contained in:
@@ -40,6 +40,7 @@ import {
|
||||
createSandboxedWriteTool,
|
||||
normalizeToolParams,
|
||||
patchToolSchemaForClaudeCompatibility,
|
||||
wrapToolWorkspaceRootGuard,
|
||||
wrapToolParamNormalization,
|
||||
} from "./pi-tools.read.js";
|
||||
import { cleanToolSchemaForGemini, normalizeToolParameters } from "./pi-tools.schema.js";
|
||||
@@ -108,6 +109,16 @@ function resolveExecConfig(params: { cfg?: OpenClawConfig; agentId?: string }) {
|
||||
};
|
||||
}
|
||||
|
||||
function resolveFsConfig(params: { cfg?: OpenClawConfig; agentId?: string }) {
|
||||
const cfg = params.cfg;
|
||||
const globalFs = cfg?.tools?.fs;
|
||||
const agentFs =
|
||||
cfg && params.agentId ? resolveAgentConfig(cfg, params.agentId)?.tools?.fs : undefined;
|
||||
return {
|
||||
workspaceOnly: agentFs?.workspaceOnly ?? globalFs?.workspaceOnly,
|
||||
};
|
||||
}
|
||||
|
||||
export const __testing = {
|
||||
cleanToolSchemaForGemini,
|
||||
normalizeToolParams,
|
||||
@@ -236,11 +247,14 @@ export function createOpenClawCodingTools(options?: {
|
||||
subagentPolicy,
|
||||
]);
|
||||
const execConfig = resolveExecConfig({ cfg: options?.config, agentId });
|
||||
const fsConfig = resolveFsConfig({ cfg: options?.config, agentId });
|
||||
const sandboxRoot = sandbox?.workspaceDir;
|
||||
const sandboxFsBridge = sandbox?.fsBridge;
|
||||
const allowWorkspaceWrites = sandbox?.workspaceAccess !== "ro";
|
||||
const workspaceRoot = options?.workspaceDir ?? process.cwd();
|
||||
const applyPatchConfig = options?.config?.tools?.exec?.applyPatch;
|
||||
const workspaceOnly = fsConfig.workspaceOnly === true;
|
||||
const applyPatchConfig = execConfig.applyPatch;
|
||||
const applyPatchWorkspaceOnly = workspaceOnly || applyPatchConfig?.workspaceOnly === true;
|
||||
const applyPatchEnabled =
|
||||
!!applyPatchConfig?.enabled &&
|
||||
isOpenAIProvider(options?.modelProvider) &&
|
||||
@@ -265,7 +279,8 @@ export function createOpenClawCodingTools(options?: {
|
||||
];
|
||||
}
|
||||
const freshReadTool = createReadTool(workspaceRoot);
|
||||
return [createOpenClawReadTool(freshReadTool)];
|
||||
const wrapped = createOpenClawReadTool(freshReadTool);
|
||||
return [workspaceOnly ? wrapToolWorkspaceRootGuard(wrapped, workspaceRoot) : wrapped];
|
||||
}
|
||||
if (tool.name === "bash" || tool.name === execToolName) {
|
||||
return [];
|
||||
@@ -275,16 +290,22 @@ export function createOpenClawCodingTools(options?: {
|
||||
return [];
|
||||
}
|
||||
// Wrap with param normalization for Claude Code compatibility
|
||||
return [
|
||||
wrapToolParamNormalization(createWriteTool(workspaceRoot), CLAUDE_PARAM_GROUPS.write),
|
||||
];
|
||||
const wrapped = wrapToolParamNormalization(
|
||||
createWriteTool(workspaceRoot),
|
||||
CLAUDE_PARAM_GROUPS.write,
|
||||
);
|
||||
return [workspaceOnly ? wrapToolWorkspaceRootGuard(wrapped, workspaceRoot) : wrapped];
|
||||
}
|
||||
if (tool.name === "edit") {
|
||||
if (sandboxRoot) {
|
||||
return [];
|
||||
}
|
||||
// Wrap with param normalization for Claude Code compatibility
|
||||
return [wrapToolParamNormalization(createEditTool(workspaceRoot), CLAUDE_PARAM_GROUPS.edit)];
|
||||
const wrapped = wrapToolParamNormalization(
|
||||
createEditTool(workspaceRoot),
|
||||
CLAUDE_PARAM_GROUPS.edit,
|
||||
);
|
||||
return [workspaceOnly ? wrapToolWorkspaceRootGuard(wrapped, workspaceRoot) : wrapped];
|
||||
}
|
||||
return [tool];
|
||||
});
|
||||
@@ -330,6 +351,7 @@ export function createOpenClawCodingTools(options?: {
|
||||
sandboxRoot && allowWorkspaceWrites
|
||||
? { root: sandboxRoot, bridge: sandboxFsBridge! }
|
||||
: undefined,
|
||||
workspaceOnly: applyPatchWorkspaceOnly,
|
||||
});
|
||||
const tools: AnyAgentTool[] = [
|
||||
...base,
|
||||
|
||||
Reference in New Issue
Block a user