mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 16:31:23 +00:00
fix(security): block env depth-overflow approval bypass
This commit is contained in:
@@ -299,6 +299,36 @@ describe("exec approvals command resolution", () => {
|
||||
expect(allowlistEval.segmentSatisfiedBy).toEqual([null]);
|
||||
});
|
||||
|
||||
it("fails closed when transparent env wrappers exceed unwrap depth", () => {
|
||||
if (process.platform === "win32") {
|
||||
return;
|
||||
}
|
||||
const dir = makeTempDir();
|
||||
const binDir = path.join(dir, "bin");
|
||||
fs.mkdirSync(binDir, { recursive: true });
|
||||
const envPath = path.join(binDir, "env");
|
||||
fs.writeFileSync(envPath, "#!/bin/sh\n");
|
||||
fs.chmodSync(envPath, 0o755);
|
||||
|
||||
const analysis = analyzeArgvCommand({
|
||||
argv: [envPath, envPath, envPath, envPath, envPath, "/bin/sh", "-c", "echo pwned"],
|
||||
cwd: dir,
|
||||
env: makePathEnv(binDir),
|
||||
});
|
||||
const allowlistEval = evaluateExecAllowlist({
|
||||
analysis,
|
||||
allowlist: [{ pattern: envPath }],
|
||||
safeBins: normalizeSafeBins([]),
|
||||
cwd: dir,
|
||||
});
|
||||
|
||||
expect(analysis.ok).toBe(true);
|
||||
expect(analysis.segments[0]?.resolution?.policyBlocked).toBe(true);
|
||||
expect(analysis.segments[0]?.resolution?.blockedWrapper).toBe("env");
|
||||
expect(allowlistEval.allowlistSatisfied).toBe(false);
|
||||
expect(allowlistEval.segmentSatisfiedBy).toEqual([null]);
|
||||
});
|
||||
|
||||
it("unwraps env wrapper with shell inner executable", () => {
|
||||
const resolution = resolveCommandResolutionFromArgv(["/usr/bin/env", "bash", "-lc", "echo hi"]);
|
||||
expect(resolution?.rawExecutable).toBe("bash");
|
||||
|
||||
@@ -478,6 +478,17 @@ export function resolveDispatchWrapperExecutionPlan(
|
||||
}
|
||||
current = unwrap.argv;
|
||||
}
|
||||
if (wrappers.length >= maxDepth) {
|
||||
const overflow = unwrapKnownDispatchWrapperInvocation(current);
|
||||
if (overflow.kind === "blocked" || overflow.kind === "unwrapped") {
|
||||
return {
|
||||
argv: current,
|
||||
wrappers,
|
||||
policyBlocked: true,
|
||||
blockedWrapper: overflow.wrapper,
|
||||
};
|
||||
}
|
||||
}
|
||||
return { argv: current, wrappers, policyBlocked: false };
|
||||
}
|
||||
|
||||
|
||||
@@ -348,4 +348,99 @@ describe("handleSystemRunInvoke mac app exec host routing", () => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("denies nested env shell payloads when wrapper depth is exceeded", async () => {
|
||||
if (process.platform === "win32") {
|
||||
return;
|
||||
}
|
||||
const tempHome = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-env-depth-overflow-"));
|
||||
const previousOpenClawHome = process.env.OPENCLAW_HOME;
|
||||
const marker = path.join(tempHome, "pwned.txt");
|
||||
process.env.OPENCLAW_HOME = tempHome;
|
||||
saveExecApprovals({
|
||||
version: 1,
|
||||
defaults: {
|
||||
security: "allowlist",
|
||||
ask: "on-miss",
|
||||
askFallback: "deny",
|
||||
},
|
||||
agents: {
|
||||
main: {
|
||||
allowlist: [{ pattern: "/usr/bin/env" }],
|
||||
},
|
||||
},
|
||||
});
|
||||
const runCommand = vi.fn(async () => {
|
||||
fs.writeFileSync(marker, "executed");
|
||||
return {
|
||||
success: true,
|
||||
stdout: "local-ok",
|
||||
stderr: "",
|
||||
timedOut: false,
|
||||
truncated: false,
|
||||
exitCode: 0,
|
||||
error: null,
|
||||
};
|
||||
});
|
||||
const sendInvokeResult = vi.fn(async () => {});
|
||||
const sendNodeEvent = vi.fn(async () => {});
|
||||
|
||||
try {
|
||||
await handleSystemRunInvoke({
|
||||
client: {} as never,
|
||||
params: {
|
||||
command: [
|
||||
"/usr/bin/env",
|
||||
"/usr/bin/env",
|
||||
"/usr/bin/env",
|
||||
"/usr/bin/env",
|
||||
"/usr/bin/env",
|
||||
"/bin/sh",
|
||||
"-c",
|
||||
`echo PWNED > ${marker}`,
|
||||
],
|
||||
sessionKey: "agent:main:main",
|
||||
},
|
||||
skillBins: {
|
||||
current: async () => [],
|
||||
},
|
||||
execHostEnforced: false,
|
||||
execHostFallbackAllowed: true,
|
||||
resolveExecSecurity: () => "allowlist",
|
||||
resolveExecAsk: () => "on-miss",
|
||||
isCmdExeInvocation: () => false,
|
||||
sanitizeEnv: () => undefined,
|
||||
runCommand,
|
||||
runViaMacAppExecHost: vi.fn(async () => null),
|
||||
sendNodeEvent,
|
||||
buildExecEventPayload: (payload) => payload,
|
||||
sendInvokeResult,
|
||||
sendExecFinishedEvent: vi.fn(async () => {}),
|
||||
preferMacAppExecHost: false,
|
||||
});
|
||||
} finally {
|
||||
if (previousOpenClawHome === undefined) {
|
||||
delete process.env.OPENCLAW_HOME;
|
||||
} else {
|
||||
process.env.OPENCLAW_HOME = previousOpenClawHome;
|
||||
}
|
||||
fs.rmSync(tempHome, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
expect(runCommand).not.toHaveBeenCalled();
|
||||
expect(fs.existsSync(marker)).toBe(false);
|
||||
expect(sendNodeEvent).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
"exec.denied",
|
||||
expect.objectContaining({ reason: "approval-required" }),
|
||||
);
|
||||
expect(sendInvokeResult).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
ok: false,
|
||||
error: expect.objectContaining({
|
||||
message: "SYSTEM_RUN_DENIED: approval required",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user