Why Links Inside a Sandboxed iframe Cannot Open Their Intended Destination

Troubleshoot links in sandboxed iframes with a local example, and understand popup, top-navigation, and sandbox escape permissions.

Published
Reading time
7 min read
Why Links Inside a Sandboxed iframe Cannot Open Their Intended Destination
On this page

A sandboxed iframe link not working may be hitting a navigation restriction rather than a broken URL. An iframe can usually navigate itself, while opening a new tab or navigating the top-level page requires specific permissions. This guide explains the distinction and provides a local example for comparing both cases.

Key Takeaways

  • A link with no target normally navigates within its iframe; target="_blank" requests a new browsing context, and target="_top" requests navigation of the top-level page.
  • allow-popups permits the sandboxed page to request new tabs or windows. Without allow-popups-to-escape-sandbox, those new contexts inherit sandbox restrictions.
  • allow-top-navigation-by-user-activation permits top-level navigation when there is user activation. allow-top-navigation is broader.
  • Grant only the permissions the embedded content needs; removing the sandbox altogether is not a general fix.

An iframe with the sandbox attribute applies restrictions to its embedded document. Tokens in the attribute selectively lift restrictions:

Link or behaviorRelevant permissionPractical effect
Navigate the iframe itself, such as a normal link or target="_self"None of the navigation tokens belowThe destination replaces the document inside the iframe. The destination may independently refuse to be framed.
Open a new tab or window, such as target="_blank"allow-popupsAllows the request for a new browsing context, subject to browser and other applicable policies.
Open a new context without inheriting sandbox restrictionsallow-popups and allow-popups-to-escape-sandboxThe new context is allowed to escape the originating iframe's sandbox restrictions.
Navigate the top-level page after a user actionallow-top-navigation-by-user-activationAllows top-level navigation when the browser recognizes user activation.
Navigate the top-level page without requiring user activationallow-top-navigationGrants a broader top-navigation permission.

A named target can refer to an existing browsing context or request a new one; the result depends on the target and applicable navigation rules. Likewise, _parent targets the immediate parent, which is not necessarily the top-level page in a nested iframe. Do not assume that a permission for _top makes every named or parent-target navigation permissible.

Advertisement

Reproduce the problem locally

The following example uses three files in one directory. It compares a fully sandboxed iframe with one that permits popups and user-activated top navigation. It does not use JavaScript or third-party dependencies.

Verification note: No documentation retrieval or code execution was available for this article. The example has not been run here, and the current HTML Living Standard and MDN iframe documentation could not be consulted. Check those sources and your supported browsers before relying on version-sensitive behavior.

Create index.html:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Sandbox navigation comparison</title>
</head>
<body>
  <h1>Sandbox navigation comparison</h1>

  <h2>No additional permissions</h2>
  <iframe
    title="Restricted example"
    src="child.html"
    sandbox
    width="600"
    height="200"></iframe>

  <h2>Popups and user-activated top navigation allowed</h2>
  <iframe
    title="Permitted example"
    src="child.html"
    sandbox="allow-popups allow-top-navigation-by-user-activation"
    width="600"
    height="200"></iframe>
</body>
</html>

Create child.html in the same directory:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Embedded links</title>
</head>
<body>
  <p><a href="destination.html" target="_self">Open in this iframe</a></p>
  <p><a href="destination.html" target="_blank" rel="noopener">Open in a new tab</a></p>
  <p><a href="destination.html" target="_top">Open in the top-level page</a></p>
</body>
</html>

Create destination.html:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Destination</title>
</head>
<body>
  <h1>Destination</h1>
  <p>The link reached this page.</p>
</body>
</html>

From that directory, start a local server with Python installed:

python3 -m http.server 8000

Open http://localhost:8000/ in a browser. Stop the server with Ctrl+C when finished. This server is for a local demonstration, not a production deployment.

  1. In either iframe, select Open in this iframe. The destination is expected to replace that iframe's document. Reload the page before testing the other links.
  2. In the first iframe, try Open in a new tab and Open in the top-level page. The sandbox is expected to block these requests.
  3. In the second iframe, try Open in a new tab. A new context is permitted, but it inherits sandbox restrictions because the example does not include allow-popups-to-escape-sandbox.
  4. Return to the example and select Open in the top-level page in the second iframe. The direct click is intended to provide the user activation needed for the navigation. Use the browser's Back button to return.

Browser settings and other policies can still affect the result. If a test fails, inspect the browser console and verify that the intended iframe and link were clicked.

Isolate a permission

To see which token affects which link, edit the second iframe's sandbox value and reload:

  • With only allow-popups, the new-tab link is permitted, but the top-navigation restriction remains.
  • With only allow-top-navigation-by-user-activation, the direct _top link is permitted when user activation is recognized, but the new-tab restriction remains.
  • Add allow-popups-to-escape-sandbox alongside allow-popups only if the new page must not inherit the iframe's sandbox restrictions. The escape token is not a substitute for permission to open the new context.

Advertisement

  1. Inspect the actual iframe element. Find its sandbox attribute and tokens in developer tools. If the iframe is nested, inspect its ancestors too: additional restrictions can still apply.
  2. Inspect the link's target. Determine whether it navigates the iframe, an existing named context, the parent, the top-level page, or a new context. A JavaScript-initiated navigation may also depend on whether scripts are allowed and whether user activation is available.
  3. Check the console and destination. A destination that refuses framing can make an otherwise permitted _self navigation fail. Browser popup controls, content security policies, redirects, and restrictions imposed by another embedding layer can also affect behavior.
  4. Make the smallest permission change and retest. For a direct click that must replace the host page, consider allow-top-navigation-by-user-activation. For a new tab, consider allow-popups. Test the destination's subsequent behavior as well as whether the first navigation occurs.
  5. Check custom protocols separately. Links to non-HTTP application protocols can be subject to additional browser and sandbox rules. The allow-top-navigation-to-custom-protocols token addresses a different capability from ordinary web navigation; confirm current behavior and browser support before depending on it.

Security trade-offs

A third-party embed with top-navigation permission can send visitors away from your site. User-activation-gated permission narrows that opportunity, but a deceptive click inside the widget can still initiate navigation. The broader allow-top-navigation permission allows navigation without that activation requirement and deserves particularly careful review.

Popup permission allows embedded content to open new contexts. Those contexts inherit sandbox restrictions unless escape is permitted. Adding allow-popups-to-escape-sandbox can be necessary for a destination that needs capabilities the sandbox would otherwise restrict, but it also means the destination no longer has that sandbox boundary. Review the destination and redirect chain before granting it.

The example uses rel="noopener" for the new-tab link to avoid giving the destination an opener relationship through that link. This is separate from sandbox inheritance: noopener does not grant popup permission or remove inherited sandbox restrictions.

Be especially cautious with allow-scripts and allow-same-origin together for content served from the embedding page's origin: that combination can allow the embedded page to undermine its sandbox. The local example intentionally grants neither token.

Common Mistakes

  • Adding allow-popups for a _top link. New-context creation and top-level navigation are different capabilities.
  • Adding the escape token alone. A new context must first be allowed to open.
  • Assuming a permitted navigation guarantees a successful destination. Framing rules, redirects, browser settings, or other policies may still interfere.
  • Treating a direct click and an asynchronous navigation as equivalent. User activation may no longer be available when delayed work attempts navigation.
  • Removing all sandbox restrictions without reviewing the embedded content. This gives up protections unrelated to the broken link.

Best Practices

Document which destination and target the widget needs, then grant the narrowest applicable token. Prefer a direct user action when top-level navigation is required, and test in each browser your application supports. If the destination must open outside the sandbox, evaluate the security consequences separately from the decision to allow popups.

Conclusion

Start by identifying where the link is trying to navigate. Permit new contexts with allow-popups, or consider user-activated top navigation for a direct _top link. Keep the remaining sandbox restrictions unless the embedded workflow has a specific, reviewed need for more access.

What would your domain sell for?

A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.