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: require consent before sending audio to generated previews

[._.]/ Adam Eivy7e62214dc608
diff --git a/client/src/components/codeAnimation/CodeAnimationPreview.jsx b/client/src/components/codeAnimation/CodeAnimationPreview.jsx
index 75f37e2bff..ffef463e63 100644
--- a/client/src/components/codeAnimation/CodeAnimationPreview.jsx
+++ b/client/src/components/codeAnimation/CodeAnimationPreview.jsx
@@ -19,13 +19,32 @@ const slug = (text) => (text || 'code-animation').toLowerCase().replace(/[^a-z0-
// the very top when the document has none. `<` is escaped so no value can
// close the script element early.
const scriptLiteral = (value) => JSON.stringify(value).replace(/</g, '\\u003c');
-export function injectAudioGlobal(html, globalName, audioDataUrl) {
- if (!audioDataUrl) return html;
- const tag = `<script>window[${scriptLiteral(globalName)}] = ${scriptLiteral(audioDataUrl)};</script>`;
+const ANIMATION_CSP = [
+ "default-src 'none'",
+ "script-src 'unsafe-inline'",
+ "style-src 'unsafe-inline'",
+ 'img-src data: blob:',
+ 'media-src data: blob:',
+ 'font-src data:',
+ "connect-src 'none'",
+ "form-action 'none'",
+ "base-uri 'none'",
+ "object-src 'none'",
+ "frame-src 'none'",
+ "worker-src 'none'",
+ "manifest-src 'none'",
+].join('; ');
+
+export function prepareAnimationHtml(html, globalName, audioDataUrl) {
+ if (!html) return html;
+ const policy = `<meta http-equiv="Content-Security-Policy" content="${ANIMATION_CSP}">`;
+ const audio = audioDataUrl
+ ? `<script>window[${scriptLiteral(globalName)}] = ${scriptLiteral(audioDataUrl)};</script>`
+ : '';
const head = html.match(/<head[^>]*>/i);
- if (!head) return `${tag}${html}`;
+ if (!head) return `${policy}${audio}${html}`;
const at = head.index + head[0].length;
- return `${html.slice(0, at)}${tag}${html.slice(at)}`;
+ return `${html.slice(0, at)}${policy}${audio}${html.slice(at)}`;
}
const blobToDataUrl = (blob) => new Promise((resolve, reject) => {
@@ -36,8 +55,11 @@ const blobToDataUrl = (blob) => new Promise((resolve, reject) => {
});
/**
- * Runs a generated code animation in a sandboxed iframe (scripts only — no
- * same-origin, so the page cannot reach PortOS's API or storage) and drives
+ * Runs a generated code animation in an opaque-origin sandboxed iframe. Its
+ * CSP permits the inline code and local media the film needs, while blocking
+ * network-backed subresources, fetch/websocket, and form submissions. Browser
+ * sandboxing still permits the frame to navigate itself, so this is not a
+ * complete network isolation boundary for arbitrary hostile HTML. It also drives
* the prompt's recording handshake: Record posts `messages.record`, the page
* answers with the WebM Blob, which can be downloaded or saved to the shared
* video gallery.
@@ -47,8 +69,17 @@ const blobToDataUrl = (blob) => new Promise((resolve, reject) => {
*/
export default function CodeAnimationPreview({ html, audioUrl, messages, audioGlobal, frame, title }) {
const iframeRef = useRef(null);
- // status: none | loading | ready | failed; dataUrl is set only when ready.
- const [audio, setAudio] = useState({ status: audioUrl ? 'loading' : 'none', dataUrl: null });
+ // A generated page receives the selected track only after an explicit choice.
+ // Tie that choice to the URL so a changed track needs fresh consent.
+ const [audioDecision, setAudioDecision] = useState(() => ({
+ url: audioUrl,
+ choice: audioUrl ? null : 'none',
+ }));
+ const audioChoice = audioDecision.url === audioUrl
+ ? audioDecision.choice
+ : (audioUrl ? null : 'none');
+ // status: consent | none | loading | ready | failed; dataUrl is set only when ready.
+ const [audio, setAudio] = useState({ status: audioUrl ? 'consent' : 'none', dataUrl: null });
const [meta, setMeta] = useState(null);
const [recording, setRecording] = useState(false);
const [recordProgress, setRecordProgress] = useState(0);
@@ -58,8 +89,15 @@ export default function CodeAnimationPreview({ html, audioUrl, messages, audioGl
useEffect(() => {
let active = true;
- setAudio({ status: audioUrl ? 'loading' : 'none', dataUrl: null });
- if (!audioUrl) return () => { active = false; };
+ if (!audioUrl) {
+ setAudio({ status: 'none', dataUrl: null });
+ return () => { active = false; };
+ }
+ if (audioChoice !== 'with-audio') {
+ setAudio({ status: audioChoice === 'without-audio' ? 'none' : 'consent', dataUrl: null });
+ return () => { active = false; };
+ }
+ setAudio({ status: 'loading', dataUrl: null });
fetch(audioUrl, { credentials: 'same-origin' })
.then((res) => {
if (!res.ok) throw new Error(`Audio track unavailable (${res.status})`);
@@ -75,14 +113,14 @@ export default function CodeAnimationPreview({ html, audioUrl, messages, audioGl
toast.error(error.message);
});
return () => { active = false; };
- }, [audioUrl]);
+ }, [audioUrl, audioChoice]);
// Hold the frame back until the audio is in hand so the page boots once,
// with its audio global already set.
const srcDoc = useMemo(() => {
- if (!html || audio.status === 'loading') return null;
- return injectAudioGlobal(html, audioGlobal, audio.dataUrl);
- }, [html, audioGlobal, audio]);
+ if (!html || audioChoice == null || audio.status === 'loading') return null;
+ return prepareAnimationHtml(html, audioGlobal, audio.dataUrl);
+ }, [html, audioGlobal, audioChoice, audio]);
useEffect(() => {
clearTimeout(recordTimerRef.current);
@@ -167,13 +205,36 @@ export default function CodeAnimationPreview({ html, audioUrl, messages, audioGl
<iframe
ref={iframeRef}
title="Code animation preview"
- // No allow-same-origin: generated code runs in an opaque origin
- // and cannot touch PortOS's API, cookies, or storage.
+ // No allow-same-origin: generated code has an opaque origin. The
+ // injected CSP also blocks network resource and connection APIs.
sandbox="allow-scripts allow-downloads"
allow="autoplay"
srcDoc={srcDoc}
className="h-full w-full border-0"
/>
+ ) : audioChoice == null ? (
+ <div className="flex h-full flex-col items-center justify-center gap-3 p-5 text-center">
+ <p className="max-w-xl text-sm text-gray-200">
+ This generated animation can navigate itself. If you provide its selected audio track, its code could send that track outside PortOS.
+ Continue only if you trust this animation.
+ </p>
+ <div className="flex flex-wrap justify-center gap-2">
+ <button
+ type="button"
+ onClick={() => setAudioDecision({ url: audioUrl, choice: 'without-audio' })}
+ className={BUTTON_SECONDARY}
+ >
+ Preview without audio
+ </button>
+ <button
+ type="button"
+ onClick={() => setAudioDecision({ url: audioUrl, choice: 'with-audio' })}
+ className={BUTTON_PRIMARY}
+ >
+ Run with audio
+ </button>
+ </div>
+ </div>
) : (
<div className="flex h-full items-center justify-center gap-2 text-sm text-gray-400">
<LoaderCircle className="h-4 w-4 animate-spin" /> Loading audio track…
diff --git a/client/src/components/codeAnimation/CodeAnimationPreview.test.jsx b/client/src/components/codeAnimation/CodeAnimationPreview.test.jsx
index c5a15f7088..2c7e91f7d1 100644
--- a/client/src/components/codeAnimation/CodeAnimationPreview.test.jsx
+++ b/client/src/components/codeAnimation/CodeAnimationPreview.test.jsx
@@ -1,18 +1,18 @@
-import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { act, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { MemoryRouter } from 'react-router';
vi.mock('../../services/api', () => ({ uploadGalleryVideo: vi.fn() }));
-import CodeAnimationPreview, { injectAudioGlobal } from './CodeAnimationPreview';
+import CodeAnimationPreview, { prepareAnimationHtml } from './CodeAnimationPreview';
const MESSAGES = { ready: 'ca:ready', record: 'ca:record', recorded: 'ca:recorded', progress: 'ca:progress', error: 'ca:error' };
const HTML = '<!DOCTYPE html><html><head><title>x</title></head><body><canvas></canvas></body></html>';
-const renderPreview = () => render(
+const renderPreview = ({ audioUrl = null } = {}) => render(
<MemoryRouter>
- <CodeAnimationPreview html={HTML} audioUrl={null} messages={MESSAGES} audioGlobal="ANIMATION_AUDIO_URL" frame={{ width: 1920, height: 1080, durationSeconds: 5 }} title="Lantern" />
+ <CodeAnimationPreview html={HTML} audioUrl={audioUrl} messages={MESSAGES} audioGlobal="ANIMATION_AUDIO_URL" frame={{ width: 1920, height: 1080, durationSeconds: 5 }} title="Lantern" />
</MemoryRouter>,
);
@@ -20,16 +20,25 @@ const postFromFrame = (source, data) => act(async () => {
window.dispatchEvent(new MessageEvent('message', { data, source }));
});
-describe('injectAudioGlobal', () => {
- it('sets the audio global before any page script, with or without a <head>', () => {
- const withHead = injectAudioGlobal(HTML, 'ANIMATION_AUDIO_URL', 'data:audio/mpeg;base64,AA');
- expect(withHead).toContain('<head><script>window["ANIMATION_AUDIO_URL"] = "data:audio/mpeg;base64,AA";</script><title>');
- expect(injectAudioGlobal('<canvas></canvas>', 'G', 'data:x')).toBe('<script>window["G"] = "data:x";</script><canvas></canvas>');
- expect(injectAudioGlobal(HTML, 'G', null)).toBe(HTML);
+describe('prepareAnimationHtml', () => {
+ it('installs a restrictive policy before page scripts and the audio global', () => {
+ const output = prepareAnimationHtml(HTML, 'ANIMATION_AUDIO_URL', 'data:audio/mpeg;base64,AA');
+ expect(output).toContain('<head><meta http-equiv="Content-Security-Policy"');
+ expect(output).toContain("connect-src 'none'");
+ expect(output).toContain("form-action 'none'");
+ expect(output).toContain('img-src data: blob:');
+ expect(output).toContain('media-src data: blob:');
+ expect(output).toContain('<script>window["ANIMATION_AUDIO_URL"] = "data:audio/mpeg;base64,AA";</script><title>');
+ });
+
+ it('installs the policy without a <head> and when no audio is supplied', () => {
+ expect(prepareAnimationHtml('<canvas></canvas>', 'G', 'data:x'))
+ .toContain('<meta http-equiv="Content-Security-Policy"');
+ expect(prepareAnimationHtml(HTML, 'G', null)).toContain('<head><meta http-equiv="Content-Security-Policy"');
});
it('cannot break out of the script tag through the data URL', () => {
- const out = injectAudioGlobal(HTML, 'G', 'data:x"</script><script>alert(1)//');
+ const out = prepareAnimationHtml(HTML, 'G', 'data:x"</script><script>alert(1)//');
expect(out).not.toContain('</script><script>alert');
expect(out).toContain('"data:x\\"\\u003c/script>\\u003cscript>alert(1)//"');
});
@@ -41,11 +50,14 @@ describe('CodeAnimationPreview', () => {
globalThis.URL.revokeObjectURL = vi.fn();
});
+ afterEach(() => vi.unstubAllGlobals());
+
it('runs the page in a scripts-only sandbox and records through the postMessage handshake', async () => {
const user = userEvent.setup();
renderPreview();
const frame = screen.getByTitle('Code animation preview');
expect(frame.getAttribute('sandbox')).toBe('allow-scripts allow-downloads');
+ expect(frame.getAttribute('srcdoc')).toContain("connect-src 'none'");
const target = frame.contentWindow;
const postMessage = vi.spyOn(target, 'postMessage');
@@ -63,4 +75,32 @@ describe('CodeAnimationPreview', () => {
expect(screen.getByLabelText('Recorded animation')).toHaveAttribute('src', 'blob:recorded');
expect(screen.getByRole('button', { name: /save to media history/i })).toBeEnabled();
});
+
+ it('does not read or provide the selected audio until the user allows generated code to use it', async () => {
+ const user = userEvent.setup();
+ const fetchMock = vi.fn(async () => ({ ok: true, blob: async () => new Blob(['audio'], { type: 'audio/wav' }) }));
+ vi.stubGlobal('fetch', fetchMock);
+ renderPreview({ audioUrl: '/api/uploads/audio/example.wav' });
+
+ expect(screen.getByText(/could send that track outside PortOS/i)).toBeInTheDocument();
+ expect(fetchMock).not.toHaveBeenCalled();
+ expect(screen.queryByTitle('Code animation preview')).not.toBeInTheDocument();
+
+ await user.click(screen.getByRole('button', { name: 'Run with audio' }));
+ const frame = await screen.findByTitle('Code animation preview');
+ expect(fetchMock).toHaveBeenCalledWith('/api/uploads/audio/example.wav', { credentials: 'same-origin' });
+ expect(frame.getAttribute('srcdoc')).toContain('window["ANIMATION_AUDIO_URL"] = "data:audio/');
+ });
+
+ it('lets the user preview without audio and never fetches the selected track', async () => {
+ const user = userEvent.setup();
+ const fetchMock = vi.fn();
+ vi.stubGlobal('fetch', fetchMock);
+ renderPreview({ audioUrl: '/api/uploads/audio/example.wav' });
+
+ await user.click(screen.getByRole('button', { name: 'Preview without audio' }));
+ const frame = await screen.findByTitle('Code animation preview');
+ expect(fetchMock).not.toHaveBeenCalled();
+ expect(frame.getAttribute('srcdoc')).not.toContain('ANIMATION_AUDIO_URL');
+ });
});