mirror of
https://github.com/zsviczian/obsidian-excalidraw-plugin.git
synced 2026-11-27 06:32:42 +00:00
hardend data:url embeddables
This commit is contained in:
1 parent
8cddc6ae60
commit
b2e96d369c
2 files changed
+55
-1
No files matched your search
@@ -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 <code>cmd://</code> 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 <code>data:text/html</code> 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.
|
||||
|
||||
@@ -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 = `<meta http-equiv="Content-Security-Policy" content="${DATA_URL_IFRAME_CSP}">`;
|
||||
|
||||
if (/<head[^>]*>/i.test(html)) {
|
||||
return html.replace(/<head[^>]*>/i, (head) => `${head}${cspTag}`);
|
||||
}
|
||||
|
||||
if (/<html[^>]*>/i.test(html)) {
|
||||
return html.replace(
|
||||
/<html[^>]*>/i,
|
||||
(htmlOpen) => `${htmlOpen}<head>${cspTag}</head>`,
|
||||
);
|
||||
}
|
||||
|
||||
return `<!doctype html><html><head>${cspTag}</head><body>${html}</body></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 (
|
||||
<webview
|
||||
@@ -428,12 +480,13 @@ export function renderWebView(
|
||||
title="Excalidraw Embedded Content"
|
||||
allowFullScreen={true}
|
||||
allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
||||
sandbox={isDataURL ? DATA_URL_IFRAME_SANDBOX : undefined}
|
||||
src={isDataURL ? null : src}
|
||||
style={{
|
||||
overflow: "hidden",
|
||||
borderRadius: "var(--embeddable-radius)",
|
||||
}}
|
||||
srcDoc={isDataURL ? atob(src.split(",")[1]) : null}
|
||||
srcDoc={srcDoc}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user