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(security): discard peer-synced media whose bytes are a playlist or manifest
Adam Eivyb8598c02a0e9
diff --git a/server/services/sharing/peerSyncAssets.js b/server/services/sharing/peerSyncAssets.jsindex 595758c04c..01d86b5c5b 100644--- a/server/services/sharing/peerSyncAssets.js+++ b/server/services/sharing/peerSyncAssets.js@@ -37,6 +37,7 @@ import { getWorkForSync } from '../writersRoom/sync.js';import { createKeyCachedQueue } from '../../lib/createKeyCachedQueue.js';import { peerSyncEvents, findPeerById } from './peerSyncShared.js';import { isStr } from '../../lib/textUtils.js';+import { detectMediaManifest } from '../../lib/mimeTypes.js';import { mapWithConcurrency } from '../../lib/mapWithConcurrency.js';import { withBackupAssetPublication } from '../../lib/backupSnapshotBoundary.js';@@ -1135,6 +1136,13 @@ async function doPullOneAsset(peer, base, entry, urlPrefix, localDir, safeName,console.warn('⚠️ peerSync: discarded asset with mismatched sha256');return;}+ // Every pulled kind is media that ffmpeg may probe, thumbnail or render, and+ // ffmpeg opens a playlist or manifest by its bytes whatever the name says.+ const manifest = detectMediaManifest(buffer);+ if (manifest) {+ console.warn(`⚠️ peerSync: discarded ${entry.kind} asset whose bytes are a ${manifest} manifest`);+ return;+ }// The download stays outside the backup lease. Replacing the file, its sidecar// and poster, and the media_assets row that indexes them hold it together, so// a cut never captures a row for bytes it did not copy, or the reverse.diff --git a/server/services/sharing/peerSyncAssets.manifest.test.js b/server/services/sharing/peerSyncAssets.manifest.test.jsnew file mode 100644index 0000000000..f70d2e9401--- /dev/null+++ b/server/services/sharing/peerSyncAssets.manifest.test.js@@ -0,0 +1,73 @@+/**+ * A pulled media asset whose bytes are a playlist or manifest is discarded:+ * ffmpeg 4.4/5.1 open such a file by content whatever its extension and read+ * the local media it names into a later probe, thumbnail or render.+ */+import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { existsSync, mkdtempSync, rmSync } from 'fs';+import { tmpdir } from 'os';+import { join } from 'path';+import { createHash } from 'crypto';+import { makePathsProxy } from '../../lib/mockPathsDataRoot.js';++let tempRoot = mkdtempSync(join(tmpdir(), 'portos-peer-manifest-boot-'));+const bootRoot = tempRoot;++vi.mock('../../lib/fileUtils.js', async () => {+ const actual = await vi.importActual('../../lib/fileUtils.js');+ return makePathsProxy(actual, { dataRoot: () => tempRoot });+});+vi.mock('../../lib/databaseMaintenanceJournal.js', async (importOriginal) => ({+ ...(await importOriginal()),+ assertDatabaseAdmission: () => {},+}));+vi.mock('../instances.js', () => ({+ getPeers: vi.fn(async () => [{ instanceId: 'peer-a', name: 'peer-a', address: '192.0.2.10', port: 5555 }]),+}));+vi.mock('../../lib/peerHttpClient.js', () => ({ peerFetch: vi.fn() }));+vi.mock('../tracks/index.js', () => ({ getTrack: vi.fn(), trackAudioFilename: vi.fn() }));++const { peerFetch } = await import('../../lib/peerHttpClient.js');+const { assetWriteQueue, inflightPulls, pullMissingAssetsFromPeer } = await import('./peerSyncAssets.js');++const served = (bytes) => ({+ ok: true,+ headers: { has: (name) => name === 'content-length', get: () => String(bytes.length) },+ arrayBuffer: async () => bytes,+});+const pull = (filename, bytes) => pullMissingAssetsFromPeer('peer-a', [+ { filename, kind: 'music', sha256: createHash('sha256').update(bytes).digest('hex') },+]);++describe('peer asset pull content check', () => {+ beforeEach(() => {+ tempRoot = mkdtempSync(join(tmpdir(), 'portos-peer-manifest-'));+ assetWriteQueue.clear();+ inflightPulls.clear();+ vi.spyOn(console, 'log').mockImplementation(() => {});+ vi.spyOn(console, 'warn').mockImplementation(() => {});+ });+ afterEach(() => {+ rmSync(tempRoot, { recursive: true, force: true });+ });+ afterAll(() => rmSync(bootRoot, { recursive: true, force: true }));++ it('discards a music asset whose bytes are an HLS playlist', async () => {+ const playlist = Buffer.from('#EXTM3U\n#EXTINF:2.0,\n../private/song.m4a\n#EXT-X-ENDLIST\n');+ vi.mocked(peerFetch).mockReset().mockResolvedValue(served(playlist));++ await pull('pulled.mp3', playlist);++ expect(peerFetch).toHaveBeenCalled();+ expect(existsSync(join(tempRoot, 'music', 'pulled.mp3'))).toBe(false);+ });++ it('still lands ordinary audio bytes', async () => {+ const audio = Buffer.from('example-peer-audio');+ vi.mocked(peerFetch).mockReset().mockResolvedValue(served(audio));++ await pull('pulled.mp3', audio);++ expect(existsSync(join(tempRoot, 'music', 'pulled.mp3'))).toBe(true);+ });+});