mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 15:28:27 +00:00
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:
committed by
GitHub
parent
5e025c4ba3
commit
4434cae565
@@ -1,8 +1,11 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const UNICODE_SPACES = /[\u00A0\u2000-\u200A\u202F\u205F\u3000]/g;
|
||||
const HTTP_URL_RE = /^https?:\/\//i;
|
||||
const DATA_URL_RE = /^data:/i;
|
||||
|
||||
function normalizeUnicodeSpaces(str: string): string {
|
||||
return str.replace(UNICODE_SPACES, " ");
|
||||
@@ -49,6 +52,40 @@ export async function assertSandboxPath(params: { filePath: string; cwd: string;
|
||||
return resolved;
|
||||
}
|
||||
|
||||
export function assertMediaNotDataUrl(media: string): void {
|
||||
const raw = media.trim();
|
||||
if (DATA_URL_RE.test(raw)) {
|
||||
throw new Error("data: URLs are not supported for media. Use buffer instead.");
|
||||
}
|
||||
}
|
||||
|
||||
export async function resolveSandboxedMediaSource(params: {
|
||||
media: string;
|
||||
sandboxRoot: string;
|
||||
}): Promise<string> {
|
||||
const raw = params.media.trim();
|
||||
if (!raw) {
|
||||
return raw;
|
||||
}
|
||||
if (HTTP_URL_RE.test(raw)) {
|
||||
return raw;
|
||||
}
|
||||
let candidate = raw;
|
||||
if (/^file:\/\//i.test(candidate)) {
|
||||
try {
|
||||
candidate = fileURLToPath(candidate);
|
||||
} catch {
|
||||
throw new Error(`Invalid file:// URL for sandboxed media: ${raw}`);
|
||||
}
|
||||
}
|
||||
const resolved = await assertSandboxPath({
|
||||
filePath: candidate,
|
||||
cwd: params.sandboxRoot,
|
||||
root: params.sandboxRoot,
|
||||
});
|
||||
return resolved.resolved;
|
||||
}
|
||||
|
||||
async function assertNoSymlink(relative: string, root: string) {
|
||||
if (!relative) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user