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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: quarantine previously stored media manifests (#12530)
[._.]/ Adam Eivy81def80b7aba
diff --git a/scripts/migrations/433-quarantine-media-manifests.js b/scripts/migrations/433-quarantine-media-manifests.jsnew file mode 100644index 0000000000..6b6eb8de81--- /dev/null+++ b/scripts/migrations/433-quarantine-media-manifests.js@@ -0,0 +1,66 @@+/** Quarantine persisted media that predates the ingest manifest guard (#12530).+ * No records are deleted and no output paths are seeded or migration-owned.+ */+import { lstat, open, readdir, rename } from 'node:fs/promises';+import { join } from 'node:path';+import { detectMediaManifest, id3v2TagLength, MEDIA_MANIFEST_PROBE_BYTES } from '../../server/lib/mimeTypes.js';++const MEDIA_DIRS = ['music', 'uploads', 'videos', 'audio', 'images', 'image-refs'];++async function manifestFormat(path) {+ const handle = await open(path, 'r');+ const readWindow = async (position) => {+ const buffer = Buffer.alloc(MEDIA_MANIFEST_PROBE_BYTES);+ const { bytesRead } = await handle.read(buffer, 0, buffer.length, position);+ return buffer.subarray(0, bytesRead);+ };+ return (async () => {+ const head = await readWindow(0);+ const format = detectMediaManifest(head);+ if (format) return format;+ // A tag can end just before the leading window's edge, leaving too little+ // playlist text for detection. Always inspect a full post-tag window.+ const tagLength = id3v2TagLength(head);+ return tagLength > 0 ? detectMediaManifest(await readWindow(tagLength)) : null;+ })().finally(() => handle.close());+}++async function quarantineDirectory(dir) {+ const entries = await readdir(dir, { withFileTypes: true }).catch((err) => {+ if (err.code === 'ENOENT') return [];+ throw err;+ });+ let quarantined = 0;+ for (const entry of entries) {+ const path = join(dir, entry.name);+ // Do not follow nested symlinks outside the media store.+ if (entry.isDirectory()) {+ quarantined += await quarantineDirectory(path);+ } else if (entry.isFile() && !entry.name.endsWith('.quarantined')) {+ const format = await manifestFormat(path);+ if (!format) continue;+ const destination = `${path}.quarantined`;+ // Upgrade migrations are serialized; refuse an existing destination+ // rather than letting rename overwrite any previously preserved bytes.+ const exists = await lstat(destination).then(() => true, (err) => {+ if (err.code === 'ENOENT') return false;+ throw err;+ });+ if (exists) throw new Error('Media quarantine destination already exists; preserve both files before retrying');+ await rename(path, destination);+ quarantined++;+ console.log(`📦 Quarantined ${format} media manifest`);+ }+ }+ return quarantined;+}++export default {+ async up({ rootDir }) {+ let quarantined = 0;+ for (const dir of MEDIA_DIRS) {+ quarantined += await quarantineDirectory(join(rootDir, 'data', dir));+ }+ return { quarantined };+ },+};diff --git a/scripts/migrations/433-quarantine-media-manifests.test.js b/scripts/migrations/433-quarantine-media-manifests.test.jsnew file mode 100644index 0000000000..669bc971f3--- /dev/null+++ b/scripts/migrations/433-quarantine-media-manifests.test.js@@ -0,0 +1,76 @@+import { afterEach, describe, expect, it, vi } from 'vitest';+import { mkdtemp, mkdir, readFile, readdir, rm, symlink, writeFile } from 'node:fs/promises';+import { join } from 'node:path';+import { tmpdir } from 'node:os';+import migration from './433-quarantine-media-manifests.js';++let rootDir;+afterEach(async () => {+ vi.restoreAllMocks();+ if (rootDir) await rm(rootDir, { recursive: true, force: true });+});++async function fixture(dir, name, bytes) {+ const path = join(rootDir, 'data', dir, name);+ await mkdir(join(path, '..'), { recursive: true });+ await writeFile(path, bytes);+ return path;+}++// A short, valid PCM WAV: media bytes must survive the scan unchanged.+const audio = Buffer.alloc(46);+audio.write('RIFF'); audio.writeUInt32LE(38, 4); audio.write('WAVEfmt ', 8);+audio.writeUInt32LE(16, 16); audio.writeUInt16LE(1, 20); audio.writeUInt16LE(1, 22);+audio.writeUInt32LE(8000, 24); audio.writeUInt32LE(16000, 28);+audio.writeUInt16LE(2, 32); audio.writeUInt16LE(16, 34);+audio.write('data', 36); audio.writeUInt32LE(2, 40);+const playlist = '#EXTM3U\n#EXT-X-TARGETDURATION:1\n#EXTINF:1,\nlocal.wav\n';++describe('migration 433 — persisted media manifests', () => {+ it('quarantines recursively across all stores, preserves audio and skips symlinks and reruns', async () => {+ rootDir = await mkdtemp(join(tmpdir(), 'migration-433-'));+ const log = vi.spyOn(console, 'log').mockImplementation(() => {});+ const paths = [];+ for (const dir of ['music', 'uploads', 'videos', 'audio', 'images', 'image-refs']) {+ paths.push(await fixture(dir, 'private-user/nested/disguised.mp3', playlist));+ }+ const valid = await fixture('music', 'valid.wav', audio);+ const outside = join(rootDir, 'outside.mp3');+ await writeFile(outside, playlist);+ await symlink(outside, join(rootDir, 'data', 'music', 'linked.mp3'));+ await expect(migration.up({ rootDir })).resolves.toEqual({ quarantined: 6 });+ for (const path of paths) {+ await expect(readFile(path)).rejects.toMatchObject({ code: 'ENOENT' });+ expect(await readFile(`${path}.quarantined`, 'utf8')).toBe(playlist);+ }+ expect(await readFile(valid)).toEqual(audio);+ expect(await readFile(outside, 'utf8')).toBe(playlist);+ expect(log.mock.calls).toEqual(Array.from({ length: 6 }, () => ['📦 Quarantined hls media manifest']));+ log.mockClear();+ await expect(migration.up({ rootDir })).resolves.toEqual({ quarantined: 0 });+ expect(log).not.toHaveBeenCalled();+ });++ it('checks a full post-ID3 probe when the tag ends at the leading window boundary', async () => {+ rootDir = await mkdtemp(join(tmpdir(), 'migration-433-'));+ const tagLength = (1 << 20) - 1;+ const tag = Buffer.alloc(tagLength);+ tag.write('ID3'); tag[3] = 4;+ const size = tagLength - 10;+ for (let i = 0; i < 4; i++) tag[9 - i] = (size >>> (i * 7)) & 0x7f;+ const path = await fixture('audio', 'tagged.mp3', Buffer.concat([tag, Buffer.from(playlist)]));+ await expect(migration.up({ rootDir })).resolves.toEqual({ quarantined: 1 });+ expect(await readFile(`${path}.quarantined`)).toEqual(Buffer.concat([tag, Buffer.from(playlist)]));+ });++ it('does not overwrite an existing quarantine and tolerates missing stores', async () => {+ rootDir = await mkdtemp(join(tmpdir(), 'migration-433-'));+ await expect(migration.up({ rootDir })).resolves.toEqual({ quarantined: 0 });+ const path = await fixture('uploads', 'disguised.mp4', playlist);+ await writeFile(`${path}.quarantined`, 'previous evidence');+ await expect(migration.up({ rootDir })).rejects.toThrow('destination already exists');+ expect(await readFile(path, 'utf8')).toBe(playlist);+ expect(await readFile(`${path}.quarantined`, 'utf8')).toBe('previous evidence');+ expect((await readdir(join(rootDir, 'data', 'uploads'))).sort()).toEqual(['disguised.mp4', 'disguised.mp4.quarantined']);+ });+});