Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: bound and sniff a launch-video style reference (#8961)

[._.]/ Adam Eivy23e3f7bbb3cc
diff --git a/server/routes/apps/launchVideos.js b/server/routes/apps/launchVideos.js
index 90af2e9a6d..db9781414c 100644
--- a/server/routes/apps/launchVideos.js
+++ b/server/routes/apps/launchVideos.js
@@ -1,11 +1,11 @@
import { Router } from 'express';
import { randomUUID } from 'node:crypto';
-import { mkdir, realpath, rm, stat, writeFile } from 'node:fs/promises';
+import { mkdir, open, realpath, rm, stat, writeFile } from 'node:fs/promises';
import { join, dirname, extname, resolve } from 'node:path';
import { asyncHandler, ServerError } from '../../lib/errorHandler.js';
import { LAUNCH_VIDEO_FORMATS, appLaunchVideoRequestSchema, appLaunchVideoPublishSchema, validateRequest } from '../../lib/validation.js';
import { pullRequestProviderOverrideSchema } from '../../lib/cosValidation.js';
-import { PATHS } from '../../lib/fileUtils.js';
+import { PATHS, detectImageFormat } from '../../lib/fileUtils.js';
import { copyFileGuarded } from '../../lib/fileCore.js';
import { PORTOS_API_URL } from '../../lib/portosUrls.js';
import { APP_LAUNCH_VIDEO_PROMPT } from '../../services/taskPromptDefaults/appLaunchVideo.js';
@@ -39,18 +39,44 @@ const missingReferenceSource = err => {
if (err.code === 'ENOENT') throw new ServerError('Style reference file is missing', { status: 404 });
throw err;
};
+// A generous cap for a single reference file — well above a real frame or a
+// short reference clip, but bounded so a stray multi-GB file in the uploads
+// scratch dir (unrelated to this feature; not size-capped at every write
+// path that lands there) can't be copied into a run and driven through
+// ffmpeg. Independent of MAX_BASE64_UPLOAD_BYTES, which bounds the JSON body
+// wire size for the upload itself, not a file a `source: 'gallery'` pick
+// resolves straight off disk.
+const MAX_STYLE_REFERENCE_BYTES = 200 * 1024 * 1024;
// A style reference names a filename, never a path (#8961) — resolve it
// against exactly the bucket its `source`/`kind` claims (Media History's own
// images/videos folders, or the generic uploads scratch dir) and refuse
// anything that escapes it via a symlink or a directory it does not sit in
// directly, the same containment shape `publish` uses for its selected take.
+// A direct API call (bypassing the form) could otherwise point `kind:
+// 'image'` at an arbitrary non-image file already sitting in one of these
+// buckets — sniff the leading bytes to make sure it actually is one. There is
+// no equivalent narrow video signature that wouldn't also reject the mov/webm
+// containers the gallery genuinely stores (unlike PNG/JPEG/WEBP/GIF, "is this
+// really a video" has no single magic-byte test that fits every container
+// PortOS accepts) — `encodeReferenceContactSheet`'s ffmpeg duration probe
+// already fails closed on non-video bytes, so that check stays the video gate.
async function resolveStyleReferenceSource(dataRoot, { source, kind, filename }) {
const bucket = source === 'gallery' ? (kind === 'video' ? 'videos' : 'images') : 'uploads';
const root = await realpath(join(dataRoot, bucket)).catch(missingReferenceSource);
const sourcePath = await realpath(join(root, filename)).catch(missingReferenceSource);
- if (dirname(sourcePath) !== root || !(await stat(sourcePath).catch(missingReferenceSource)).isFile()) {
+ const info = await stat(sourcePath).catch(missingReferenceSource);
+ if (dirname(sourcePath) !== root || !info.isFile()) {
throw new ServerError('Style reference source is invalid', { status: 400 });
}
+ if (info.size > MAX_STYLE_REFERENCE_BYTES) {
+ throw new ServerError('Style reference file is too large', { status: 400 });
+ }
+ if (kind === 'image') {
+ const handle = await open(sourcePath, 'r');
+ const head = Buffer.alloc(16);
+ await handle.read(head, 0, 16, 0).finally(() => handle.close());
+ if (!detectImageFormat(head)) throw new ServerError('Style reference is not a recognized image', { status: 400 });
+ }
return sourcePath;
}
diff --git a/server/routes/apps/launchVideos.test.js b/server/routes/apps/launchVideos.test.js
index 08190a4423..5c2f7117fe 100644
--- a/server/routes/apps/launchVideos.test.js
+++ b/server/routes/apps/launchVideos.test.js
@@ -1,5 +1,5 @@
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
-import { mkdir, readFile, readdir, writeFile, symlink, rm } from 'node:fs/promises';
+import { mkdir, readFile, readdir, writeFile, symlink, rm, truncate } from 'node:fs/promises';
import { execFileSync } from 'node:child_process';
import { join } from 'node:path';
import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../../lib/mockPathsDataRoot.js';
@@ -197,15 +197,21 @@ describe('user-triggered launch videos', () => {
});
});
+// Minimal real magic-byte headers — the route sniffs the leading bytes of an
+// `image` reference (#8961) so an arbitrary non-image file already sitting in
+// a bucket can't silently become one via a direct API call.
+const PNG_HEADER = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, ...Buffer.from('fake png bytes')]);
+const JPEG_HEADER = Buffer.from([0xff, 0xd8, 0xff, ...Buffer.from('uploaded bytes')]);
+
describe('style reference (#8961)', () => {
it('copies a gallery image reference beside composition/ and names its path in the prompt', async () => {
await mkdir(PATHS.images, { recursive: true });
- await writeFile(join(PATHS.images, 'ref.png'), 'fake png bytes');
+ await writeFile(join(PATHS.images, 'ref.png'), PNG_HEADER);
const response = await submit({ styleReference: { kind: 'image', source: 'gallery', filename: 'ref.png' } });
expect(response.status).toBe(202);
const runRoot = join(PATHS.data, 'launch-videos', 'example', response.body.runId);
expect(await readdir(join(runRoot, 'reference'))).toEqual(['style-reference.png']);
- expect(await readFile(join(runRoot, 'reference', 'style-reference.png'), 'utf8')).toBe('fake png bytes');
+ expect(await readFile(join(runRoot, 'reference', 'style-reference.png'))).toEqual(PNG_HEADER);
// composition/ never receives reference bytes — the launch asset gate
// refuses raster files there, and a later revision only snapshots it.
expect(await readdir(join(runRoot, 'composition'))).toEqual(['portos-motion.js']);
@@ -220,11 +226,24 @@ describe('style reference (#8961)', () => {
it('resolves an uploaded reference from the generic uploads store, not the gallery', async () => {
await mkdir(PATHS.uploads, { recursive: true });
- await writeFile(join(PATHS.uploads, 'abc12345-mine.jpg'), 'uploaded bytes');
+ await writeFile(join(PATHS.uploads, 'abc12345-mine.jpg'), JPEG_HEADER);
const response = await submit({ styleReference: { kind: 'image', source: 'upload', filename: 'abc12345-mine.jpg' } });
expect(response.status).toBe(202);
const runRoot = join(PATHS.data, 'launch-videos', 'example', response.body.runId);
- expect(await readFile(join(runRoot, 'reference', 'style-reference.jpg'), 'utf8')).toBe('uploaded bytes');
+ expect(await readFile(join(runRoot, 'reference', 'style-reference.jpg'))).toEqual(JPEG_HEADER);
+ });
+
+ it('rejects an oversized reference and a file claiming to be an image that is not one', async () => {
+ await mkdir(PATHS.images, { recursive: true });
+ await writeFile(join(PATHS.images, 'not-an-image.png'), 'plain text, no magic bytes');
+ expect((await submit({ styleReference: { kind: 'image', source: 'gallery', filename: 'not-an-image.png' } })).status).toBe(400);
+ const hugePath = join(PATHS.images, 'huge.png');
+ await writeFile(hugePath, PNG_HEADER);
+ // A sparse extend, not a 200MB in-memory buffer — the route only reads
+ // this file's `stat().size` and its leading bytes, never the whole thing.
+ await truncate(hugePath, 201 * 1024 * 1024);
+ expect((await submit({ styleReference: { kind: 'image', source: 'gallery', filename: 'huge.png' } })).status).toBe(400);
+ expect(addTask).not.toHaveBeenCalled();
});
it.skipIf(!ffmpeg)('samples a video reference into a contact sheet every 0.5s and names both paths', async () => {