From 32796244d3c1af9f5a77e45ae271379caaff13a2 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Thu, 9 Apr 2026 12:21:06 -0600 Subject: [PATCH 1/7] Remove sandbox "allow-same-origin" attribute, use srcDoc instead --- packages/components/src/sandbox/index.tsx | 89 ++++--------------- .../components/src/sandbox/test/index.tsx | 88 +++++++++++++----- 2 files changed, 79 insertions(+), 98 deletions(-) diff --git a/packages/components/src/sandbox/index.tsx b/packages/components/src/sandbox/index.tsx index 1cc4652a70a6c6..47b549beb56e8a 100644 --- a/packages/components/src/sandbox/index.tsx +++ b/packages/components/src/sandbox/index.tsx @@ -6,6 +6,7 @@ import { useRef, useState, useEffect, + useMemo, } from '@wordpress/element'; import { useFocusableIframe, useMergeRefs } from '@wordpress/compose'; @@ -137,44 +138,13 @@ function SandBox( { const ref = useRef< HTMLIFrameElement >( null ); const [ width, setWidth ] = useState( 0 ); const [ height, setHeight ] = useState( 0 ); - - function isFrameAccessible() { - try { - return !! ref.current?.contentDocument?.body; - } catch { - return false; - } - } - - function trySandBox( forceRerender = false ) { - if ( ! isFrameAccessible() ) { - return; - } - - const { contentDocument, ownerDocument } = - ref.current as HTMLIFrameElement & { - contentDocument: Document; - }; - - if ( - ! forceRerender && - null !== - contentDocument?.body.getAttribute( - 'data-resizable-iframe-connected' - ) - ) { - return; - } - - // Put the html snippet into a html document, and then write it to the iframe's document - // we can use this in the future to inject custom styles or scripts. - // Scripts go into the body rather than the head, to support embedded content such as Instagram - // that expect the scripts to be part of the body. + // Build the full HTML document as a string for the srcdoc attribute. + // This replaces the previous approach of writing to contentDocument, + // which required allow-same-origin on the sandbox iframe. + const srcDoc = useMemo( () => { + const lang = document.documentElement.lang; const htmlDoc = ( - + { title }