Security: harden sandboxed media handling (#9182)

* Message: enforce sandbox for media param

* fix: harden sandboxed media handling (#8780) (thanks @victormier)

* chore: format message action runner (#8780) (thanks @victormier)

---------

Co-authored-by: Victor Mier <victormier@gmail.com>
This commit is contained in:
Gustavo Madeira Santana
2026-02-04 19:11:23 -05:00
committed by GitHub
parent 5e025c4ba3
commit 4434cae565
6 changed files with 278 additions and 80 deletions

View File

@@ -1,3 +1,6 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { ChannelPlugin } from "../../channels/plugins/types.js";
import type { OpenClawConfig } from "../../config/config.js";
@@ -446,6 +449,164 @@ describe("runMessageAction sendAttachment hydration", () => {
Buffer.from("hello").toString("base64"),
);
});
it("rewrites sandboxed media paths for sendAttachment", async () => {
const cfg = {
channels: {
bluebubbles: {
enabled: true,
serverUrl: "http://localhost:1234",
password: "test-password",
},
},
} as OpenClawConfig;
const sandboxDir = await fs.mkdtemp(path.join(os.tmpdir(), "msg-sandbox-"));
try {
await runMessageAction({
cfg,
action: "sendAttachment",
params: {
channel: "bluebubbles",
target: "+15551234567",
media: "./data/pic.png",
message: "caption",
},
sandboxRoot: sandboxDir,
});
const call = vi.mocked(loadWebMedia).mock.calls[0];
expect(call?.[0]).toBe(path.join(sandboxDir, "data", "pic.png"));
} finally {
await fs.rm(sandboxDir, { recursive: true, force: true });
}
});
});
describe("runMessageAction sandboxed media validation", () => {
beforeEach(async () => {
const { createPluginRuntime } = await import("../../plugins/runtime/index.js");
const { setSlackRuntime } = await import("../../../extensions/slack/src/runtime.js");
const runtime = createPluginRuntime();
setSlackRuntime(runtime);
setActivePluginRegistry(
createTestRegistry([
{
pluginId: "slack",
source: "test",
plugin: slackPlugin,
},
]),
);
});
afterEach(() => {
setActivePluginRegistry(createTestRegistry([]));
});
it("rejects media outside the sandbox root", async () => {
const sandboxDir = await fs.mkdtemp(path.join(os.tmpdir(), "msg-sandbox-"));
try {
await expect(
runMessageAction({
cfg: slackConfig,
action: "send",
params: {
channel: "slack",
target: "#C12345678",
media: "/etc/passwd",
message: "",
},
sandboxRoot: sandboxDir,
dryRun: true,
}),
).rejects.toThrow(/sandbox/i);
} finally {
await fs.rm(sandboxDir, { recursive: true, force: true });
}
});
it("rejects file:// media outside the sandbox root", async () => {
const sandboxDir = await fs.mkdtemp(path.join(os.tmpdir(), "msg-sandbox-"));
try {
await expect(
runMessageAction({
cfg: slackConfig,
action: "send",
params: {
channel: "slack",
target: "#C12345678",
media: "file:///etc/passwd",
message: "",
},
sandboxRoot: sandboxDir,
dryRun: true,
}),
).rejects.toThrow(/sandbox/i);
} finally {
await fs.rm(sandboxDir, { recursive: true, force: true });
}
});
it("rewrites sandbox-relative media paths", async () => {
const sandboxDir = await fs.mkdtemp(path.join(os.tmpdir(), "msg-sandbox-"));
try {
const result = await runMessageAction({
cfg: slackConfig,
action: "send",
params: {
channel: "slack",
target: "#C12345678",
media: "./data/file.txt",
message: "",
},
sandboxRoot: sandboxDir,
dryRun: true,
});
expect(result.kind).toBe("send");
expect(result.sendResult?.mediaUrl).toBe(path.join(sandboxDir, "data", "file.txt"));
} finally {
await fs.rm(sandboxDir, { recursive: true, force: true });
}
});
it("rewrites MEDIA directives under sandbox", async () => {
const sandboxDir = await fs.mkdtemp(path.join(os.tmpdir(), "msg-sandbox-"));
try {
const result = await runMessageAction({
cfg: slackConfig,
action: "send",
params: {
channel: "slack",
target: "#C12345678",
message: "Hello\\nMEDIA: ./data/note.ogg",
},
sandboxRoot: sandboxDir,
dryRun: true,
});
expect(result.kind).toBe("send");
expect(result.sendResult?.mediaUrl).toBe(path.join(sandboxDir, "data", "note.ogg"));
} finally {
await fs.rm(sandboxDir, { recursive: true, force: true });
}
});
it("rejects data URLs in media params", async () => {
await expect(
runMessageAction({
cfg: slackConfig,
action: "send",
params: {
channel: "slack",
target: "#C12345678",
media: "data:image/png;base64,abcd",
message: "",
},
dryRun: true,
}),
).rejects.toThrow(/data:/i);
});
});
describe("runMessageAction accountId defaults", () => {

View File

@@ -10,6 +10,7 @@ import type { OpenClawConfig } from "../../config/config.js";
import type { OutboundSendDeps } from "./deliver.js";
import type { MessagePollResult, MessageSendResult } from "./message.js";
import { resolveSessionAgentId } from "../../agents/agent-scope.js";
import { assertMediaNotDataUrl, resolveSandboxedMediaSource } from "../../agents/sandbox-paths.js";
import {
readNumberParam,
readStringArrayParam,
@@ -62,6 +63,7 @@ export type RunMessageActionParams = {
deps?: OutboundSendDeps;
sessionKey?: string;
agentId?: string;
sandboxRoot?: string;
dryRun?: boolean;
abortSignal?: AbortSignal;
};
@@ -326,6 +328,53 @@ function normalizeBase64Payload(params: { base64?: string; contentType?: string
};
}
async function normalizeSandboxMediaParams(params: {
args: Record<string, unknown>;
sandboxRoot?: string;
}): Promise<void> {
const sandboxRoot = params.sandboxRoot?.trim();
const mediaKeys: Array<"media" | "path" | "filePath"> = ["media", "path", "filePath"];
for (const key of mediaKeys) {
const raw = readStringParam(params.args, key, { trim: false });
if (!raw) {
continue;
}
assertMediaNotDataUrl(raw);
if (!sandboxRoot) {
continue;
}
const normalized = await resolveSandboxedMediaSource({ media: raw, sandboxRoot });
if (normalized !== raw) {
params.args[key] = normalized;
}
}
}
async function normalizeSandboxMediaList(params: {
values: string[];
sandboxRoot?: string;
}): Promise<string[]> {
const sandboxRoot = params.sandboxRoot?.trim();
const normalized: string[] = [];
const seen = new Set<string>();
for (const value of params.values) {
const raw = value?.trim();
if (!raw) {
continue;
}
assertMediaNotDataUrl(raw);
const resolved = sandboxRoot
? await resolveSandboxedMediaSource({ media: raw, sandboxRoot })
: raw;
if (seen.has(resolved)) {
continue;
}
seen.add(resolved);
normalized.push(resolved);
}
return normalized;
}
async function hydrateSetGroupIconParams(params: {
cfg: OpenClawConfig;
channel: ChannelId;
@@ -699,6 +748,14 @@ async function handleSendAction(ctx: ResolvedActionContext): Promise<MessageActi
pushMedia(url);
}
pushMedia(parsed.mediaUrl);
const normalizedMediaUrls = await normalizeSandboxMediaList({
values: mergedMediaUrls,
sandboxRoot: input.sandboxRoot,
});
mergedMediaUrls.length = 0;
mergedMediaUrls.push(...normalizedMediaUrls);
message = parsed.text;
params.message = message;
if (!params.replyTo && parsed.replyToId) {
@@ -967,6 +1024,11 @@ export async function runMessageAction(
}
const dryRun = Boolean(input.dryRun ?? readBooleanParam(params, "dryRun"));
await normalizeSandboxMediaParams({
args: params,
sandboxRoot: input.sandboxRoot,
});
await hydrateSendAttachmentParams({
cfg,
channel,