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", () => {