From 14d4d2c5bb4c52562dbd392bd28136a7a719ea99 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Fri, 3 Apr 2026 15:35:10 +0900 Subject: [PATCH 1/2] Fix and comment security concerns --- .../md-react-preview/app/src/preview-block.tsx | 16 +++++++++++++++- .../md-react-preview/src/preview-transform.ts | 11 ++++++++++- .../src/preview-plugin.ts | 5 ++++- .../src/PreviewBlock.vue | 13 ++++++++++++- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/packages/md-react-preview/app/src/preview-block.tsx b/packages/md-react-preview/app/src/preview-block.tsx index 872d693..dc31bae 100644 --- a/packages/md-react-preview/app/src/preview-block.tsx +++ b/packages/md-react-preview/app/src/preview-block.tsx @@ -113,11 +113,17 @@ export function PreviewBlock({ const initialColorScheme = useRef(colorScheme); useEffect(() => { if (colorScheme === initialColorScheme.current) return; - iframeRef.current?.contentWindow?.postMessage({ type: "mrp-theme", theme: colorScheme }, "*"); + // Security: specify origin instead of "*" to restrict postMessage recipients + iframeRef.current?.contentWindow?.postMessage( + { type: "mrp-theme", theme: colorScheme }, + window.location.origin, + ); }, [colorScheme]); useEffect(() => { function onMessage(e: MessageEvent) { + // Security: validate postMessage origin to prevent cross-origin message spoofing + if (e.origin !== window.location.origin) return; if (e.data?.type === "mrp-resize" && e.data?.blockId === blockId) { setIframeHeight(e.data.height); } @@ -198,6 +204,14 @@ export function PreviewBlock({ > + {/* + Security note: no sandbox attribute is set on this iframe. + Preview blocks are authored by trusted developers (markdown authors), + and adding sandbox="allow-scripts" alone would break ES module loading + (CORS) and postMessage origin checks. Adding both allow-scripts and + allow-same-origin together provides no real security benefit for + same-origin iframes. + */}