Compare commits

...

3 Commits

Author SHA1 Message Date
Peter Steinberger
0dbe018aa9 fix: harden message-tool sandbox paths (#6398) (thanks @leszekszpunar) 2026-02-01 14:18:11 -08:00
Leszek Szpunar
d6842f1967 style: translate Polish comments to English for consistency 2026-02-01 14:12:35 -08:00
Leszek Szpunar
bf993e15ce security(message-tool): validate filePath/path against sandbox root 2026-02-01 14:12:35 -08:00
4 changed files with 233 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ Docs: https://docs.openclaw.ai
### Fixes
- Agents: enforce sandbox validation for message media/file paths. (#6398) Thanks @leszekszpunar.
- Docs: run oxfmt to fix format checks. (#6513) Thanks @app/clawdinator.
- Auto-reply: avoid referencing workspace files in /new greeting prompt. (#5706) Thanks @bravostation.
- Process: resolve Windows `spawn()` failures for npm-family CLIs by appending `.cmd` when needed. (#5815) Thanks @thejhinvirtuoso.

View File

@@ -92,6 +92,7 @@ export function createOpenClawTools(options?: {
currentThreadTs: options?.currentThreadTs,
replyToMode: options?.replyToMode,
hasRepliedRef: options?.hasRepliedRef,
sandboxRoot: options?.sandboxRoot,
}),
createTtsTool({
agentChannel: options?.agentChannel,

View File

@@ -1,3 +1,6 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import type { ChannelPlugin } from "../../channels/plugins/types.js";
import type { MessageActionRunResult } from "../../infra/outbound/message-action-runner.js";
@@ -161,3 +164,180 @@ describe("message tool description", () => {
setActivePluginRegistry(createTestRegistry([]));
});
});
describe("message tool sandbox path validation", () => {
it("rejects filePath that escapes sandbox root", async () => {
const sandboxDir = await fs.mkdtemp(path.join(os.tmpdir(), "msg-sandbox-"));
try {
const tool = createMessageTool({
config: {} as never,
sandboxRoot: sandboxDir,
});
await expect(
tool.execute("1", {
action: "send",
target: "telegram:123",
filePath: "/etc/passwd",
message: "",
}),
).rejects.toThrow(/sandbox/i);
} finally {
await fs.rm(sandboxDir, { recursive: true, force: true });
}
});
it("rejects filePath file URL that escapes sandbox root", async () => {
const sandboxDir = await fs.mkdtemp(path.join(os.tmpdir(), "msg-sandbox-"));
try {
const tool = createMessageTool({
config: {} as never,
sandboxRoot: sandboxDir,
});
await expect(
tool.execute("1", {
action: "send",
target: "telegram:123",
filePath: "file:///etc/passwd",
message: "",
}),
).rejects.toThrow(/sandbox/i);
} finally {
await fs.rm(sandboxDir, { recursive: true, force: true });
}
});
it("rejects path param with traversal sequence", async () => {
const sandboxDir = await fs.mkdtemp(path.join(os.tmpdir(), "msg-sandbox-"));
try {
const tool = createMessageTool({
config: {} as never,
sandboxRoot: sandboxDir,
});
await expect(
tool.execute("1", {
action: "send",
target: "telegram:123",
path: "../../../etc/shadow",
message: "",
}),
).rejects.toThrow(/sandbox/i);
} finally {
await fs.rm(sandboxDir, { recursive: true, force: true });
}
});
it("rejects media path that escapes sandbox root", async () => {
const sandboxDir = await fs.mkdtemp(path.join(os.tmpdir(), "msg-sandbox-"));
try {
const tool = createMessageTool({
config: {} as never,
sandboxRoot: sandboxDir,
});
await expect(
tool.execute("1", {
action: "send",
target: "telegram:123",
media: "/etc/passwd",
message: "",
}),
).rejects.toThrow(/sandbox/i);
} finally {
await fs.rm(sandboxDir, { recursive: true, force: true });
}
});
it("allows remote media URLs inside sandbox", async () => {
mocks.runMessageAction.mockClear();
mocks.runMessageAction.mockResolvedValue({
kind: "send",
action: "send",
channel: "telegram",
to: "telegram:123",
handledBy: "plugin",
payload: {},
dryRun: true,
} satisfies MessageActionRunResult);
const sandboxDir = await fs.mkdtemp(path.join(os.tmpdir(), "msg-sandbox-"));
try {
const tool = createMessageTool({
config: {} as never,
sandboxRoot: sandboxDir,
});
await tool.execute("1", {
action: "send",
target: "telegram:123",
media: "https://example.com/file.png",
message: "",
});
expect(mocks.runMessageAction).toHaveBeenCalledTimes(1);
} finally {
await fs.rm(sandboxDir, { recursive: true, force: true });
}
});
it("allows filePath inside sandbox root", async () => {
mocks.runMessageAction.mockClear();
mocks.runMessageAction.mockResolvedValue({
kind: "send",
action: "send",
channel: "telegram",
to: "telegram:123",
handledBy: "plugin",
payload: {},
dryRun: true,
} satisfies MessageActionRunResult);
const sandboxDir = await fs.mkdtemp(path.join(os.tmpdir(), "msg-sandbox-"));
try {
const tool = createMessageTool({
config: {} as never,
sandboxRoot: sandboxDir,
});
await tool.execute("1", {
action: "send",
target: "telegram:123",
filePath: "./data/file.txt",
message: "",
});
expect(mocks.runMessageAction).toHaveBeenCalledTimes(1);
} finally {
await fs.rm(sandboxDir, { recursive: true, force: true });
}
});
it("skips validation when no sandboxRoot is set", async () => {
mocks.runMessageAction.mockClear();
mocks.runMessageAction.mockResolvedValue({
kind: "send",
action: "send",
channel: "telegram",
to: "telegram:123",
handledBy: "plugin",
payload: {},
dryRun: true,
} satisfies MessageActionRunResult);
const tool = createMessageTool({
config: {} as never,
});
await tool.execute("1", {
action: "send",
target: "telegram:123",
filePath: "/etc/passwd",
message: "",
});
// Without sandboxRoot the validation is skipped — unsandboxed sessions work normally.
expect(mocks.runMessageAction).toHaveBeenCalledTimes(1);
});
});

View File

@@ -1,4 +1,5 @@
import { Type } from "@sinclair/typebox";
import { fileURLToPath } from "node:url";
import type { OpenClawConfig } from "../../config/config.js";
import type { AnyAgentTool } from "./common.js";
import { BLUEBUBBLES_GROUP_ACTIONS } from "../../channels/plugins/bluebubbles-actions.js";
@@ -19,10 +20,40 @@ import { normalizeAccountId } from "../../routing/session-key.js";
import { normalizeMessageChannel } from "../../utils/message-channel.js";
import { resolveSessionAgentId } from "../agent-scope.js";
import { listChannelSupportedActions } from "../channel-tools.js";
import { assertSandboxPath } from "../sandbox-paths.js";
import { channelTargetSchema, channelTargetsSchema, stringEnum } from "../schema/typebox.js";
import { jsonResult, readNumberParam, readStringParam } from "./common.js";
const AllMessageActions = CHANNEL_MESSAGE_ACTION_NAMES;
const REMOTE_SCHEME_PATTERN = /^[a-z][a-z0-9+.-]*:\/\//i;
function normalizeSandboxPathInput(raw: string): string {
if (!raw.startsWith("file://")) {
return raw;
}
try {
return fileURLToPath(raw);
} catch {
throw new Error(`Invalid file:// URL: ${raw}`);
}
}
function shouldValidateMediaPath(value: string): boolean {
const trimmed = value.trim();
if (!trimmed) {
return false;
}
if (trimmed.startsWith("data:") || trimmed.startsWith("blob:")) {
return false;
}
if (trimmed.startsWith("file://")) {
return true;
}
if (REMOTE_SCHEME_PATTERN.test(trimmed)) {
return false;
}
return true;
}
function buildRoutingSchema() {
return {
channel: Type.Optional(Type.String()),
@@ -252,6 +283,7 @@ type MessageToolOptions = {
currentThreadTs?: string;
replyToMode?: "off" | "first" | "all";
hasRepliedRef?: { value: boolean };
sandboxRoot?: string;
};
function buildMessageToolSchema(cfg: OpenClawConfig) {
@@ -362,6 +394,25 @@ export function createMessageTool(options?: MessageToolOptions): AnyAgentTool {
required: true,
}) as ChannelMessageActionName;
// Validate file paths against sandbox root to prevent host file access.
const sandboxRoot = options?.sandboxRoot?.trim();
if (sandboxRoot) {
const validatePath = async (raw: string) => {
const normalized = normalizeSandboxPathInput(raw);
await assertSandboxPath({ filePath: normalized, cwd: sandboxRoot, root: sandboxRoot });
};
for (const key of ["filePath", "path"] as const) {
const raw = readStringParam(params, key, { trim: false });
if (raw) {
await validatePath(raw);
}
}
const mediaRaw = readStringParam(params, "media", { trim: false });
if (mediaRaw && shouldValidateMediaPath(mediaRaw)) {
await validatePath(mediaRaw);
}
}
const accountId = readStringParam(params, "accountId") ?? agentAccountId;
if (accountId) {
params.accountId = accountId;