Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
fix(outputs): render inline HTML fences inert until the reader runs their scripts (#942)
Ruben Lütke4ddd6ab4a827
diff --git a/dodaios/ui/web/frontend/src/components/outputs/RenderedFence.test.tsx b/dodaios/ui/web/frontend/src/components/outputs/RenderedFence.test.tsxindex b46a4d7a02..6003285a79 100644--- a/dodaios/ui/web/frontend/src/components/outputs/RenderedFence.test.tsx+++ b/dodaios/ui/web/frontend/src/components/outputs/RenderedFence.test.tsx@@ -44,6 +44,12 @@ describe("wrapInlineHtml", () => {expect(doc).toContain("</script></body>");});+ it("injects no script into the inert preview", () => {+ const doc = wrapInlineHtml("<div>hi</div>", "dark", "a", { scripts: false });+ expect(doc).not.toContain("<script");+ expect(doc).toContain(`content="${INLINE_HTML_CSP}"`);+ });+it("keeps a page's own theme stamp and adds a head when it has none", () => {const doc = wrapInlineHtml('<html data-theme="dark"><body>x</body></html>', "light", "a");expect(doc).toContain('<html data-theme="dark"><head><meta charset="utf-8">');@@ -74,11 +80,36 @@ describe("MarkdownProse fences", () => {);const frame = within(fences[1]).getByTestId("inline-html-frame") as HTMLIFrameElement;- expect(frame.getAttribute("sandbox")).toBe("allow-scripts");+ // Inert by default (#346): an empty sandbox runs no script at all.+ expect(frame.getAttribute("sandbox")).toBe("");+ expect(frame.getAttribute("data-scripts")).toBe("off");expect(frame.getAttribute("srcdoc")).toContain('<div id="card">hi</div>');expect(frame.getAttribute("srcdoc")).toContain("Content-Security-Policy");});+ it("runs no script until the reader clicks Run scripts, and never joins the app origin", () => {+ const evil = '<html><head></head><body><script>location.href="http://blocked.invalid/leak"</script></body></html>';+ render(<MarkdownProse slug="r" path="r.md" files={[]} text={"```html\n" + evil + "\n```\n"} />);+ const fence = screen.getByTestId("rendered-fence");++ const inert = within(fence).getByTestId("inline-html-frame") as HTMLIFrameElement;+ expect(inert.getAttribute("sandbox")).toBe("");+ expect(inert.getAttribute("srcdoc")).not.toContain(INLINE_HTML_SIZE_MESSAGE);++ fireEvent.click(within(fence).getByTestId("inline-html-run-scripts"));+ const live = within(fence).getByTestId("inline-html-frame") as HTMLIFrameElement;+ expect(live.getAttribute("sandbox")).toBe("allow-scripts");+ expect(live.getAttribute("sandbox")).not.toContain("allow-same-origin");+ expect(live.getAttribute("data-scripts")).toBe("on");+ expect(live.getAttribute("srcdoc")).toContain(INLINE_HTML_SIZE_MESSAGE);+ expect(within(fence).queryByTestId("inline-html-run-scripts")).toBeNull();+ });++ it("offers no Run scripts switch on an svg fence", () => {+ render(<MarkdownProse slug="r" path="r.md" files={[]} text={"```svg\n" + SVG + "\n```\n"} />);+ expect(screen.queryByTestId("inline-html-run-scripts")).toBeNull();+ });+it("shows the markup behind the Source switch and comes back to Rendered", () => {render(<MarkdownProse slug="run-1" path="r.md" files={[]} text={"```html\n<b>bold</b>\n```\n"} />,diff --git a/dodaios/ui/web/frontend/src/components/outputs/RenderedFence.tsx b/dodaios/ui/web/frontend/src/components/outputs/RenderedFence.tsxindex f177531a45..0b4fcb59ac 100644--- a/dodaios/ui/web/frontend/src/components/outputs/RenderedFence.tsx+++ b/dodaios/ui/web/frontend/src/components/outputs/RenderedFence.tsx@@ -11,12 +11,20 @@* Safety is the artifact page's model, mirrored for inline markup:* - SVG is drawn through an `<img>` from a data: URL — a browser never runs* script or fetches anything for an SVG-as-image.- * - HTML runs in an `<iframe srcdoc>` with `sandbox="allow-scripts"` and NO- * `allow-same-origin`, so it lives in an opaque origin that cannot reach the- * app's cookies, storage or API; a Content-Security-Policy stamped into the- * document (`INLINE_HTML_CSP`, the verbatim twin of `ARTIFACT_PAGE_CSP` in- * `dodaios/ui/web/artifact_view.py`) shuts every way out — no network, no- * forms, no navigation. The frame grows to its content through a+ * - HTML renders INERT by default: an `<iframe srcdoc>` with an empty+ * `sandbox` (no scripts, no same-origin, no forms, no popups) plus a+ * Content-Security-Policy stamped into the document (`INLINE_HTML_CSP`, the+ * verbatim twin of `ARTIFACT_PAGE_CSP` in `dodaios/ui/web/artifact_view.py`)+ * that blocks every subresource fetch. A fence shows up in a chat on its+ * own, so nothing in it may run or reach the network before the reader+ * asks for it.+ * - Scripts run only after an explicit "Run scripts" click. The frame then+ * gets `sandbox="allow-scripts"` (still NO `allow-same-origin`, so it lives+ * in an opaque origin that cannot reach the app's cookies, storage or API).+ * CSP cannot stop a script from navigating its own frame+ * (`location.href = "https://..."`), so a running snippet CAN reach a web+ * address; that is why running is the reader's choice, never the default+ * (issue #346). The scripted frame grows to its content through a* `postMessage` the injected reporter sends; the parent trusts a message* only from that frame's own window, and only up to a ceiling.*/@@ -42,6 +50,8 @@ export const INLINE_HTML_SIZE_MESSAGE = "jarvis-inline-html-size";const MIN_FRAME_PX = 96;const MAX_FRAME_PX = 720;+/** Height of an inert frame: without a script nothing can report the content's size. */+const INERT_FRAME_PX = 320;/** The app's card surface in each palette, for a fragment that brings no page of its own. */const FRAGMENT_SKIN: Record<Theme, { bg: string; ink: string }> = {@@ -71,10 +81,16 @@ function reporterScript(token: string): string {* The document the frame loads: the snippet as-is when it is a whole page* (CSP and theme stamp added to its head), otherwise wrapped in a minimal* page on the app's card surface so a bare `<div>` or `<svg>` reads in both- * palettes. Exported for the tests.+ * palettes. With `scripts: false` (the inert preview) no size reporter is+ * injected, since the frame would refuse to run it anyway. Exported for the tests.*/-export function wrapInlineHtml(html: string, theme: Theme, token: string): string {- const script = reporterScript(token);+export function wrapInlineHtml(+ html: string,+ theme: Theme,+ token: string,+ { scripts = true }: { scripts?: boolean } = {},+): string {+ const script = scripts ? reporterScript(token) : "";if (/<html[\s>]/i.test(html)) {let doc = html;if (!/<html[^>]*\sdata-theme=/i.test(doc)) {@@ -111,14 +127,27 @@ export function svgDataUrl(svg: string): string {return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(document)}`;}-/** The sandboxed frame an inline HTML snippet runs in, sized to its content. */-export function InlineHtmlFrame({ html, title }: { html: string; title: string }) {+/**+ * The sandboxed frame an inline HTML snippet renders in. Inert unless+ * `scripts` is set (the reader clicked "Run scripts"); only a scripted frame+ * can report its content height, so an inert one keeps a fixed height.+ */+export function InlineHtmlFrame({+ html,+ title,+ scripts = false,+}: {+ html: string;+ title: string;+ scripts?: boolean;+}) {const theme = useThemeValue();const token = useId();const ref = useRef<HTMLIFrameElement>(null);const [height, setHeight] = useState(MIN_FRAME_PX * 2);useEffect(() => {+ if (!scripts) return;const onMessage = (event: MessageEvent) => {const frame = ref.current;if (!frame || event.source !== frame.contentWindow) return;@@ -137,19 +166,23 @@ export function InlineHtmlFrame({ html, title }: { html: string; title: string }};window.addEventListener("message", onMessage);return () => window.removeEventListener("message", onMessage);- }, [token]);+ }, [token, scripts]);- const doc = useMemo(() => wrapInlineHtml(html, theme, token), [html, theme, token]);+ const doc = useMemo(+ () => wrapInlineHtml(html, theme, token, { scripts }),+ [html, theme, token, scripts],+ );return (<iframeref={ref}- key={theme}+ key={`${theme}-${scripts ? "scripts" : "inert"}`}title={title}srcDoc={doc}- sandbox="allow-scripts"+ sandbox={scripts ? "allow-scripts" : ""}referrerPolicy="no-referrer"- style={{ height }}+ style={{ height: scripts ? height : INERT_FRAME_PX }}+ data-scripts={scripts ? "on" : "off"}className="block w-full border-0 bg-transparent"data-testid="inline-html-frame"/>@@ -160,6 +193,7 @@ export function RenderedFence({ language, code }: { language: FenceLanguage; codconst t = useT();const [mode, setMode] = useState<"rendered" | "source">("rendered");const [copied, setCopied] = useState(false);+ const [scripts, setScripts] = useState(false);const copy = () => {void robustCopy(code).then((ok) => {@@ -195,6 +229,17 @@ export function RenderedFence({ language, code }: { language: FenceLanguage; cod<div className="flex items-center justify-between gap-2 border-b border-border/40 bg-muted/20 px-3 py-1"><span className="text-micro uppercase tracking-wider text-muted-foreground">{language}</span><div className="flex items-center gap-1.5">+ {language === "html" && !scripts && mode === "rendered" ? (+ <button+ type="button"+ onClick={() => setScripts(true)}+ className="rounded px-2 py-0.5 text-micro font-medium text-muted-foreground transition hover:bg-muted hover:text-foreground"+ title={t("outputs_view.fence_run_scripts_hint")}+ data-testid="inline-html-run-scripts"+ >+ {t("outputs_view.fence_run_scripts")}+ </button>+ ) : null}<div role="group" className="inline-flex rounded border border-border/60 bg-muted/40 p-0.5">{segment("rendered", t("outputs_view.fence_rendered"))}{segment("source", t("outputs_view.fence_source"))}@@ -221,7 +266,7 @@ export function RenderedFence({ language, code }: { language: FenceLanguage; cod<img src={svgDataUrl(code)} alt="SVG" className="max-w-full" data-testid="inline-svg" /></div>) : (- <InlineHtmlFrame html={code} title="HTML" />+ <InlineHtmlFrame html={code} title="HTML" scripts={scripts} />)}</div>);diff --git a/dodaios/ui/web/frontend/src/i18n/locales/de.json b/dodaios/ui/web/frontend/src/i18n/locales/de.jsonindex 2be2fcf555..cb5e39daac 100644--- a/dodaios/ui/web/frontend/src/i18n/locales/de.json+++ b/dodaios/ui/web/frontend/src/i18n/locales/de.json@@ -3704,6 +3704,8 @@"file_truncated": "Datei gekürzt auf 1 MiB — Rest im Datei-Manager anzeigen.","fence_rendered": "Gerendert","fence_source": "Quelltext",+ "fence_run_scripts": "Skripte ausführen",+ "fence_run_scripts_hint": "Das JavaScript dieses Ausschnitts ausführen. Ein laufendes Skript kann eine Webadresse aufrufen.","open_action": "Öffnen","open_with_title": "Öffnen mit…","open_with_remember": "Als Standard merken",diff --git a/dodaios/ui/web/frontend/src/i18n/locales/en.json b/dodaios/ui/web/frontend/src/i18n/locales/en.jsonindex 096d821187..4423c7d16b 100644--- a/dodaios/ui/web/frontend/src/i18n/locales/en.json+++ b/dodaios/ui/web/frontend/src/i18n/locales/en.json@@ -3704,6 +3704,8 @@"file_truncated": "File truncated to 1 MiB — view the rest in the file manager.","fence_rendered": "Rendered","fence_source": "Source",+ "fence_run_scripts": "Run scripts",+ "fence_run_scripts_hint": "Run this snippet's JavaScript. A running script can contact a web address.","open_action": "Open","open_with_title": "Open with…","open_with_remember": "Remember as default",diff --git a/dodaios/ui/web/frontend/src/i18n/locales/es.json b/dodaios/ui/web/frontend/src/i18n/locales/es.jsonindex 8b05e329cc..9a0cbea8e6 100644--- a/dodaios/ui/web/frontend/src/i18n/locales/es.json+++ b/dodaios/ui/web/frontend/src/i18n/locales/es.json@@ -3704,6 +3704,8 @@"file_truncated": "Archivo truncado a 1 MiB: ver el resto en el gestor de archivos.","fence_rendered": "Renderizado","fence_source": "Código",+ "fence_run_scripts": "Ejecutar scripts",+ "fence_run_scripts_hint": "Ejecuta el JavaScript de este fragmento. Un script en ejecución puede contactar una dirección web.","open_action": "Abrir","open_with_title": "Abrir con…","open_with_remember": "Recordar como predeterminado",diff --git a/dodaios/ui/web/frontend/src/i18n/locales/pt.json b/dodaios/ui/web/frontend/src/i18n/locales/pt.jsonindex f5a868a047..47527b78c9 100644--- a/dodaios/ui/web/frontend/src/i18n/locales/pt.json+++ b/dodaios/ui/web/frontend/src/i18n/locales/pt.json@@ -3704,6 +3704,8 @@"file_truncated": "Ficheiro truncado a 1 MiB — vê o resto no gestor de ficheiros.","fence_rendered": "Renderizado","fence_source": "Código-fonte",+ "fence_run_scripts": "Executar scripts",+ "fence_run_scripts_hint": "Executa o JavaScript deste trecho. Um script em execução pode contactar um endereço web.","open_action": "Abrir","open_with_title": "Abrir com…","open_with_remember": "Lembrar como predefinição",diff --git a/dodaios/ui/web/frontend/src/i18n/locales/zh.json b/dodaios/ui/web/frontend/src/i18n/locales/zh.jsonindex 55f7959c1b..65b06a0d21 100644--- a/dodaios/ui/web/frontend/src/i18n/locales/zh.json+++ b/dodaios/ui/web/frontend/src/i18n/locales/zh.json@@ -3704,6 +3704,8 @@"file_truncated": "文件已截断为 1 MiB,请在文件管理器中查看其余内容。","fence_rendered": "渲染","fence_source": "源码",+ "fence_run_scripts": "运行脚本",+ "fence_run_scripts_hint": "运行此片段的 JavaScript。运行中的脚本可以访问网址。","open_action": "打开","open_with_title": "打开方式…","open_with_remember": "记住为默认",