Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Video frames: read an attachment only as the container its type names

milind-soni75129f2aaeb9
diff --git a/server/turn-video.test.ts b/server/turn-video.test.ts
index c095c77289..55d1faa267 100644
--- a/server/turn-video.test.ts
+++ b/server/turn-video.test.ts
@@ -1,5 +1,5 @@
import { execFileSync, spawnSync } from "node:child_process";
-import { mkdtempSync, readFileSync, rmSync } from "node:fs";
+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
@@ -76,6 +76,15 @@ describe.skipIf(!hasFfmpeg())("video frame sampling", () => {
expect(frames).toHaveLength(2);
});
+ it("never follows a playlist named as a video to the files it points at", async () => {
+ const root = store();
+ syntheticClip(root, 2);
+ const path = join(root, "disguised.mp4");
+ writeFileSync(path, "ffconcat version 1.0\nfile 'clip.mp4'\nduration 2\n");
+
+ await expect(videoFrameImages([{ path, mime: "video/mp4", bytes: 0 }])).resolves.toEqual([]);
+ });
+
it("drops a video ffmpeg cannot read instead of failing the turn", async () => {
const root = store();
const notAVideo: TurnVideoInput = { path: join(root, "missing.mp4"), mime: "video/mp4", bytes: 0 };
diff --git a/server/turn-video.ts b/server/turn-video.ts
index 6c755b8d3c..e6a863c062 100644
--- a/server/turn-video.ts
+++ b/server/turn-video.ts
@@ -13,9 +13,17 @@ const FFMPEG_TIMEOUT_MS = 20_000;
// Generous past any driver's turn timeout, so a slow tool loop still finds its frames; this is a leak bound, not a lease.
const FRAME_CLEANUP_DELAY_MS = 10 * 60 * 1000;
+// Security invariant: an attachment comes from anyone who can reach a bot, and ffmpeg otherwise picks its reader from the
+// file's contents, so a playlist named .mp4 could read other files or the network into a frame the model sees.
+const VIDEO_READER: Record<TurnVideoInput["mime"], readonly string[]> = {
+ "video/mp4": ["-protocol_whitelist", "file", "-f", "mov"],
+ "video/quicktime": ["-protocol_whitelist", "file", "-f", "mov"],
+ "video/webm": ["-protocol_whitelist", "file", "-f", "matroska"],
+};
+
function run(command: string, args: readonly string[], timeoutMs: number): Promise<string> {
return new Promise((resolvePromise, reject) => {
- const child = spawn(command, args, { stdio: ["ignore", "pipe", "pipe"] });
+ const child = spawn(command, args, { stdio: ["ignore", "pipe", "pipe"], windowsHide: true });
let stdout = "";
let stderr = "";
let settled = false;
@@ -43,10 +51,10 @@ function run(command: string, args: readonly string[], timeoutMs: number): Promi
});
}
-async function videoDurationSeconds(path: string): Promise<number> {
+async function videoDurationSeconds(video: TurnVideoInput): Promise<number> {
const stdout = await run(
"ffprobe",
- ["-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", path],
+ ["-v", "error", ...VIDEO_READER[video.mime], "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", video.path],
FFPROBE_TIMEOUT_MS,
);
const seconds = Number(stdout.trim());
@@ -54,10 +62,11 @@ async function videoDurationSeconds(path: string): Promise<number> {
return seconds;
}
-async function extractFrame(videoPath: string, atSeconds: number, outPath: string): Promise<void> {
+async function extractFrame(video: TurnVideoInput, atSeconds: number, outPath: string): Promise<void> {
await run("ffmpeg", [
"-ss", atSeconds.toFixed(3),
- "-i", videoPath,
+ ...VIDEO_READER[video.mime],
+ "-i", video.path,
"-frames:v", "1",
"-q:v", "4",
"-vf", "scale='min(1024,iw)':-2",
@@ -76,13 +85,13 @@ export async function videoFrameImages(videos: readonly TurnVideoInput[] | undef
for (const video of videos) {
let dir: string | undefined;
try {
- const duration = await videoDurationSeconds(video.path);
+ const duration = await videoDurationSeconds(video);
const count = Math.min(VIDEO_SAMPLE_FRAME_COUNT, Math.max(1, Math.ceil(duration)));
dir = await mkdtemp(join(tmpdir(), "omb-video-frame-"));
for (let index = 0; index < count; index += 1) {
const atSeconds = (duration * (index + 0.5)) / count;
const outPath = join(dir, `frame-${index}.jpg`);
- await extractFrame(video.path, atSeconds, outPath);
+ await extractFrame(video, atSeconds, outPath);
const bytes = await readFile(outPath);
frames.push({ path: outPath, mime: "image/jpeg", bytes: bytes.byteLength, source: "video-frame" });
}