Error: Permission denied to access property 'document'
You are encountering a common yet frustrating cross-origin restriction in web development. The error message Error: Permission denied to access property 'document' indicates that your parent page is attempting to read or modify the internal DOM of an <iframe> loaded from a different origin, which violates the browser's Same-Origin Policy (SOP).
Your current setup attempts to bypass this using the X-Frame-Options header set to ALLOW-FROM. While this header controls whether a page can be embedded in an iframe, it does not grant the embedded page permission to expose its internal API to the parent window. The specific line causing the crash is:
iFrameID.contentWindow.document.body.scrollHeight
Here, you are trying to access contentWindow.document. This property allows the parent script to read the document object of the iframe's context. Browsers block this access by default unless the two pages share the same origin (protocol, domain, and port). Even if your server sends X-Frame-Options: ALLOW-FROM http://mydomain.com, modern browsers often ignore this directive in favor of stricter CSP policies, or the header might be missing entirely on the target server.
The fundamental issue is that ALLOW-FROM is deprecated and not universally supported by all browsers. Furthermore, even if the browser allows the iframe to load, it does not automatically allow the parent page to inspect the iframe's content. To resize the iframe dynamically based on the scroll height of the inner content, you must change how the target server exposes its document.
The most robust solution is to configure the server hosting the iframe content (http://mydomain.com) to send a specific HTTP header: Cross-Origin-Opener-Policy set to same-origin or, more commonly for this use case, relying on Content-Security-Policy or ensuring the origins match. However, the standard approach for allowing cross-origin access to the document property is to ensure the iframe source and the parent page share the same domain, or to use a proxy.
If you must keep them separate, you need to adjust the security headers on the target server (http://mydomain.com). Specifically, you should add a Content-Security-Policy header that allows the parent origin to read the document context. A common configuration looks like this:
Content-Security-Policy: sandbox allow-scripts allow-same-origin
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin
Note that require-corp is a strict requirement for the COOP/COEP pair to work correctly in modern browsers, effectively allowing the parent page to access the frame's document if the policies align. However, this requires coordination between both domains.
Alternatively, if you have control over the server hosting the iframe content, you can use X-Frame-Options: SAMEORIGIN combined with Content-Security-Policy: sandbox allow-scripts allow-same-origin allow-forms allow-popups allow-presentation allow-pointer-lock allow-top-navigation. This ensures the page loads within a sandbox that permits specific interactions.
If modifying headers is not an option, or if you are using a framework like Laravel where header management can be tricky, consider using a proxy service. A proxy sits between your parent domain and the iframe content, effectively making them share the same origin in the eyes of the browser. This eliminates the SOP restriction entirely because the request path remains within your own domain hierarchy.
For developers working with Laravel applications specifically, managing these headers often involves modifying the App\Http\Middleware\TrustProxies or adding custom middleware to respond with the necessary Access-Control-Allow-Origin and CSP headers. However, be aware that simply setting ALLOW-FROM in PHP is insufficient for modern browser compatibility regarding document access.
The most reliable fix without a proxy is to ensure the iframe content explicitly sets Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp. This combination allows the parent window to retain the opener context and access the inner document, enabling your JavaScript to calculate scrollHeight accurately.
Without these specific headers on the target server, no amount of configuration on the parent side will grant access to contentWindow.document. The browser prioritizes security over functionality in this scenario unless the policies are explicitly aligned across both domains.