From b2e96d369ccd45d307dd70e4e1df3a63d9fd4e38 Mon Sep 17 00:00:00 2001 From: zsviczian Date: Thu, 21 May 2026 23:01:07 +0200 Subject: [PATCH] hardend data:url embeddables --- src/shared/Dialogs/Messages.ts | 1 + src/view/components/CustomEmbeddable.tsx | 55 +++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/shared/Dialogs/Messages.ts b/src/shared/Dialogs/Messages.ts index 76d51d8d..520966af 100644 --- a/src/shared/Dialogs/Messages.ts +++ b/src/shared/Dialogs/Messages.ts @@ -14,6 +14,7 @@ I build this plugin as a labor of love. Curious about the philosophy behind it? - ExcaliAI now shows an **"AI Usage: input/output"** button next to the Run button. Clicking it opens the same usage modal. - Added an explicit opt-in for executing cmd:// links from drawings. Command links are now blocked by default, with a security warning prompt on first use and a dedicated setting under Excalidraw Automate. - Replaced the dropped-link title resolver from Iframely with an HTTPS oEmbed endpoint, and restricted auto title resolution to HTTPS links. +- Hardened data URL embeddables: HTML loaded through data:text/html now renders in a sandboxed iframe with a defensive CSP to keep interactive content contained inside the embeddable. ## New in ExcalidrawAutomate - Added \`getPathForImageFileId(fileId: FileId): string | null\` — returns the vault path for an image element identified by its Excalidraw fileId. Note: Excalidraw does not maintain a persistent index of fileIds to paths; the path is only available for images that have appeared in an open drawing during the current Obsidian session. diff --git a/src/view/components/CustomEmbeddable.tsx b/src/view/components/CustomEmbeddable.tsx index 692cbc93..5ba79c85 100644 --- a/src/view/components/CustomEmbeddable.tsx +++ b/src/view/components/CustomEmbeddable.tsx @@ -48,6 +48,57 @@ const CANVAS_VIEWTYPES = new Set([ "pdf", ]); +const DATA_URL_IFRAME_SANDBOX = "allow-scripts allow-forms allow-popups"; +const DATA_URL_IFRAME_CSP = + "default-src 'none'; script-src https: data: 'unsafe-inline'; style-src https: 'unsafe-inline'; img-src https: data: blob:; font-src https: data:; connect-src https:; media-src https: data: blob:; frame-src https: data:; form-action https:; base-uri 'none'"; + +function decodeDataUrlToHtml(dataUrl: string): string | null { + const commaIndex = dataUrl.indexOf(","); + if (commaIndex === -1) { + return null; + } + + const header = dataUrl.slice(0, commaIndex).toLowerCase(); + const payload = dataUrl.slice(commaIndex + 1); + + try { + return header.includes(";base64") + ? atob(payload) + : decodeURIComponent(payload); + } catch { + return null; + } +} + +function injectDefensiveCsp(html: string): string { + if (!html || /http-equiv\s*=\s*["']content-security-policy["']/i.test(html)) { + return html; + } + + const cspTag = ``; + + if (/]*>/i.test(html)) { + return html.replace(/]*>/i, (head) => `${head}${cspTag}`); + } + + if (/]*>/i.test(html)) { + return html.replace( + /]*>/i, + (htmlOpen) => `${htmlOpen}${cspTag}`, + ); + } + + return `${cspTag}${html}`; +} + +function getSandboxedDataUrlSrcDoc(src: string): string | null { + const decodedHtml = decodeDataUrlToHtml(src); + if (!decodedHtml) { + return null; + } + return injectDefensiveCsp(decodedHtml); +} + let noticeTimer: number; function showNoticeOnce(message: string) { if (noticeTimer) { @@ -405,6 +456,7 @@ export function renderWebView( _: UIAppState, ): JSX.Element { const isDataURL = src.startsWith("data:"); + const srcDoc = isDataURL ? getSandboxedDataUrlSrcDoc(src) : null; if (DEVICE.isDesktop && !isDataURL) { return ( ); }