Skip to content

The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.

shared: a sound's file stays inside the film's folder; its numbers are numbers (#33)

xyd9459d82e89700d9
diff --git a/.changeset/sound-path-containment.md b/.changeset/sound-path-containment.md
new file mode 100644
index 0000000..c601fb0
--- /dev/null
+++ b/.changeset/sound-path-containment.md
@@ -0,0 +1,5 @@
+---
+"openfilm": patch
+---
+
+A sound's file is always read from inside the film's folder. A path with a slash written as `%2F` was decoded after the URL had resolved its `..`, so a page's script could name a file above the folder and `look`, `render` and Studio's exports read it; such a path, or a link that leads out of the folder, is now refused with its name. A link to media the person linked (Studio's linked places) is still read, as the file server reads it. A sound's timing and volume have to be numbers before they go into ffmpeg's filters.
diff --git a/packages/openfilm/src/look.mjs b/packages/openfilm/src/look.mjs
index c90facd..0a138ef 100644
--- a/packages/openfilm/src/look.mjs
+++ b/packages/openfilm/src/look.mjs
@@ -4,7 +4,7 @@ import { spawnSync } from 'node:child_process';
import { existsSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
import { basename, dirname, extname, join, resolve } from 'node:path';
import { FrameError, launch } from './host.mjs';
-import { blockDiff, cutText, fileOf, ffmpeg, filmSound, fmt, hash, homeFor, mix, open, outDir, parseTarget, SAME_LIMIT, shown, shuffled, spread, UsageError } from './shared.mjs';
+import { blockDiff, checkSound, cutText, fileOf, ffmpeg, filmSound, fmt, hash, homeFor, mix, open, outDir, parseTarget, SAME_LIMIT, shown, shuffled, spread, UsageError } from './shared.mjs';
const MEDIA = /\.(mp4|mov|m4v|webm|mkv|avi|png|jpe?g|webp|gif|mp3|wav|m4a|aac|ogg|flac)$/i;
@@ -218,6 +218,7 @@ function joinPieces(list = []) {
function listSounds(s, m, [a, b], warnings) {
const lines = [];
for (const c of joinPieces(m.sounds)) {
+ checkSound(c);
const file = c?.src ? fileOf(s.root, s.entry, c.src) : '';
if (!c?.src || !existsSync(file)) continue;
const at = c.at ?? 0;
diff --git a/packages/openfilm/src/shared.d.mts b/packages/openfilm/src/shared.d.mts
index 788e6ce..654c6dc 100644
--- a/packages/openfilm/src/shared.d.mts
+++ b/packages/openfilm/src/shared.d.mts
@@ -20,3 +20,8 @@ export declare class UsageError extends Error {}
export declare function kill(proc: import('node:child_process').ChildProcess): void;
/** Run ffmpeg (`-nostdin`, errors only) with `args`; rejects with its error, or when it runs past `timeoutMs`. */
export declare function ffmpeg(args: string[], opts?: { input?: boolean; timeoutMs?: number; what?: string }): { proc: import('node:child_process').ChildProcess; done: Promise<void> };
+
+/** Where a path the film asks for (a sound's `src`, resolved from the entry as the browser would) lives on disk; throws for one outside the film's folder, or a link out of it that is not media the person linked (src/media-links.mjs). */
+export declare function fileOf(root: string, entry: string, src: string): string;
+/** A sound as a film or a page lists it: a path, and numbers where numbers go; throws otherwise. */
+export declare function checkSound<T extends { src: string }>(sound: T): T;
diff --git a/packages/openfilm/src/shared.mjs b/packages/openfilm/src/shared.mjs
index 4350f42..59965a9 100644
--- a/packages/openfilm/src/shared.mjs
+++ b/packages/openfilm/src/shared.mjs
@@ -1,9 +1,10 @@
import { spawn, spawnSync } from 'node:child_process';
import { createHash } from 'node:crypto';
-import { existsSync, mkdirSync, readFileSync, statSync } from 'node:fs';
+import { existsSync, mkdirSync, readFileSync, realpathSync, statSync } from 'node:fs';
import { basename, dirname, join, relative, resolve, sep } from 'node:path';
import { FilmPage, FrameError, launch, serve } from './host.mjs';
import { FILM_FILE, readFilmFile } from './film-doc.mjs';
+import { readableLink } from './media-links.mjs';
/**
* A film argument (a project folder with film.html, a folder inside one, a folder with index.html, or an .html file)
@@ -143,10 +144,32 @@ export async function write(stream, buf) {
if (!stream.write(buf)) await new Promise((r) => stream.once('drain', r));
}
-/** Where a path the film asks for (a sound's `src`, resolved from the entry as the browser would) lives on disk. */
+/**
+ * Where a path the film asks for (a sound's `src`, resolved from the entry as the browser would) lives on disk. The
+ * file is inside the film's folder; a link out of it is followed only to media the person linked (`readableLink`,
+ * src/media-links.mjs), as the file server does.
+ */
export function fileOf(root, entry, src) {
- const path = decodeURIComponent(new URL(src, `http://film/${entry}`).pathname);
- return join(root, path);
+ const path = decodeURIComponent(new URL(String(src), `http://film/${entry}`).pathname);
+ /* the URL takes care of `..` before the path is decoded: `%2F` decoded after it is a `/` the join resolves, so
+ the file is checked to be inside the folder, and, when it is there, not a link out of it */
+ const home = resolve(root);
+ const file = resolve(join(root, path));
+ if (file !== home && !file.startsWith(home + sep)) throw new Error(`${src}: outside the film's folder`);
+ if (existsSync(file)) {
+ const real = realpathSync(file), base = realpathSync(home);
+ if (real !== base && !real.startsWith(base + sep) && !readableLink(home, file)) throw new Error(`${src}: a link out of the film's folder`);
+ }
+ return file;
+}
+
+/** A sound as a film or a page lists it: a path, and numbers where numbers go (they end up in ffmpeg's filters). */
+export function checkSound(c) {
+ if (!c || typeof c.src !== 'string' || !c.src) throw new Error(`sound ${c?.clip ?? '?'}: src must be a path in the film's folder`);
+ const finite = (v) => typeof v === 'number' && Number.isFinite(v);
+ for (const key of ['at', 'from', 'to', 'volume', 'speed']) if (c[key] != null && !finite(c[key])) throw new Error(`sound ${c.src}: ${key} must be a number`);
+ if (c.fade != null && !(Array.isArray(c.fade) && c.fade.length === 2 && c.fade.every(finite))) throw new Error(`sound ${c.src}: fade must be two numbers`);
+ return c;
}
/**
@@ -156,6 +179,7 @@ export function fileOf(root, entry, src) {
*/
export function audioClips(root, entry, clips, from = 0) {
return (clips ?? []).map((c) => {
+ checkSound(c);
const speed = c.speed ?? 1;
let at = (c.at ?? 0) - from;
let start = c.from ?? 0;
diff --git a/packages/openfilm/src/shared.test.mjs b/packages/openfilm/src/shared.test.mjs
index b81f5f4..3a00795 100644
--- a/packages/openfilm/src/shared.test.mjs
+++ b/packages/openfilm/src/shared.test.mjs
@@ -1,10 +1,11 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
-import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
+import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { spawnSync } from 'node:child_process';
-import { fadeFilter, ffmpeg, mix } from './shared.mjs';
+import { audioClips, checkSound, fadeFilter, ffmpeg, fileOf, mix } from './shared.mjs';
+import { addLinkedPlaces } from './media-links.mjs';
test('an ffmpeg that does not end is stopped, and says what did not finish', { timeout: 20_000 }, async () => {
/* an endless silent source, written nowhere: it never ends by itself */
@@ -79,3 +80,63 @@ test('fadeFilter: the ramps as a per-sample expression, nothing without fades',
assert.equal(fadeFilter({ in: 0.5, out: 0, length: 3, skip: 0 }), "aeval='val(ch)*clip(min(1,t/0.5),0,1)':c=same");
assert.equal(fadeFilter({ in: 0, out: 1, length: 3, skip: 0.25 }), "aeval='val(ch)*clip(min(1,(3-(t+0.25))/1),0,1)':c=same");
});
+
+test('fileOf: a sound stays inside the film\'s folder, however its path is written', () => {
+ const root = mkdtempSync(join(tmpdir(), 'openfilm-fileof-'));
+ try {
+ const ws = join(root, 'ws');
+ mkdirSync(join(ws, 'assets'), { recursive: true });
+ writeFileSync(join(ws, 'assets', 'a.mp3'), 'inside');
+ writeFileSync(join(root, 'outside.mp3'), 'outside');
+ assert.equal(fileOf(ws, 'film.html', 'assets/a.mp3'), join(ws, 'assets', 'a.mp3'));
+ assert.equal(fileOf(ws, 'scenes/one.html', '../assets/a.mp3'), join(ws, 'assets', 'a.mp3'));
+ /* the URL resolves `..` above the root to the root: still inside */
+ assert.equal(fileOf(ws, 'film.html', '../../outside.mp3'), join(ws, 'outside.mp3'));
+ /* a slash hidden as %2F is decoded after the URL resolved its `..`: the join must not walk out */
+ assert.throws(() => fileOf(ws, 'film.html', 'x%2F..%2F..%2Foutside.mp3'), /outside the film's folder/);
+ assert.throws(() => fileOf(ws, 'film.html', '%2E%2E%2Foutside.mp3'), /outside the film's folder/);
+ /* a link inside the folder that leads out of it */
+ symlinkSync(join(root, 'outside.mp3'), join(ws, 'assets', 'link.mp3'));
+ assert.throws(() => fileOf(ws, 'film.html', 'assets/link.mp3'), /link out of the film's folder/);
+ } finally { rmSync(root, { recursive: true, force: true }); }
+});
+
+test('fileOf: a link to media the person linked is followed; a link a project brought from elsewhere is not', () => {
+ /* the places the person linked are kept in Studio's home: this test's own */
+ const home = process.env.OPENFILM_HOME;
+ const root = mkdtempSync(join(tmpdir(), 'openfilm-fileof-links-'));
+ process.env.OPENFILM_HOME = join(root, 'home');
+ try {
+ const ws = join(root, 'ws'), nas = join(root, 'nas'), away = join(root, 'away');
+ mkdirSync(join(ws, 'assets'), { recursive: true });
+ mkdirSync(nas); mkdirSync(away);
+ writeFileSync(join(nas, 'bed.mp3'), 'on the nas');
+ writeFileSync(join(away, 'secret.mp3'), 'theirs');
+ symlinkSync(join(nas, 'bed.mp3'), join(ws, 'assets', 'bed.mp3'));
+ symlinkSync(join(away, 'secret.mp3'), join(ws, 'assets', 'secret.mp3'));
+ /* nothing linked yet: neither is followed */
+ assert.throws(() => fileOf(ws, 'film.html', 'assets/bed.mp3'), /link out of the film's folder/);
+ /* the person links the NAS file: its link in assets/ is read, as the file server reads it */
+ addLinkedPlaces([join(nas, 'bed.mp3')]);
+ assert.equal(fileOf(ws, 'film.html', 'assets/bed.mp3'), join(ws, 'assets', 'bed.mp3'));
+ assert.equal(readFileSync(fileOf(ws, 'scenes/one.html', '../assets/bed.mp3'), 'utf8'), 'on the nas');
+ /* a link a project brought from elsewhere, to a place never linked, still is not */
+ assert.throws(() => fileOf(ws, 'film.html', 'assets/secret.mp3'), /link out of the film's folder/);
+ /* outside assets/ no link is followed, linked or not */
+ symlinkSync(join(nas, 'bed.mp3'), join(ws, 'bed.mp3'));
+ assert.throws(() => fileOf(ws, 'film.html', 'bed.mp3'), /link out of the film's folder/);
+ } finally {
+ if (home === undefined) delete process.env.OPENFILM_HOME; else process.env.OPENFILM_HOME = home;
+ rmSync(root, { recursive: true, force: true });
+ }
+});
+
+test('checkSound: a sound\'s numbers are numbers before they reach ffmpeg', () => {
+ const sound = { clip: 'bed', src: 'assets/a.mp3', at: 0, from: 0, to: 6, volume: 1, speed: 1 };
+ assert.equal(checkSound(sound), sound);
+ assert.throws(() => checkSound({ ...sound, volume: '1,amovie=/etc/passwd' }), /volume must be a number/);
+ assert.throws(() => checkSound({ ...sound, to: NaN }), /to must be a number/);
+ assert.throws(() => checkSound({ ...sound, fade: [0.5] }), /fade must be two numbers/);
+ assert.throws(() => checkSound({ ...sound, src: 7 }), /src must be a path/);
+ assert.throws(() => audioClips('/nowhere', 'film.html', [{ ...sound, speed: 'fast' }]), /speed must be a number/);
+});