Frame Busting and Its Limitations
What you'll learn: How JavaScript-based frame busting attempts to prevent clickjacking—and why it's fundamentally unreliable.
What is Frame Busting?
Frame busting (or frame breaking) is a client-side JavaScript technique designed to prevent a page from being embedded in an <iframe>. The classic approach checks if the page is being framed and, if so, "breaks out" by forcing the top-level window to reload the legitimate page.
Classic Frame Busting Pattern
if (top !== self) {
top.location = self.location;
}
This code asks: "Am I inside a frame?" If yes, it redirects the parent window to display the page directly.
Why Frame Busting Fails
Frame busting sounds clever, but it's easily defeated by attackers:
-
JavaScript Can Be Disabled: Attackers can embed your page in a
<iframe sandbox>attribute that blocks scripts entirely. -
onBeforeUnload Blocking: Attackers can trap the redirect with event handlers that prevent navigation.
-
204 No Content Response: Setting
iframelocation to return HTTP 204 stops the bust. -
Double Framing: Nesting your page two frames deep can break the bust logic.
-
Browser Vulnerabilities: Implementation bugs may allow bypasses.
Analogy: Frame busting is like putting a "Do Not Frame" sign written in a language the attacker can erase, ignore, or mistranslate.
The Right Approach
Always use server-side headers like X-Frame-Options or CSP's frame-ancestors directive. These are enforced by the browser before your JavaScript runs, making them immune to client-side manipulation. Frame busting may add a thin extra layer, but never rely on it alone.
Key Takeaway:
Frame busting is a weak, JavaScript-based clickjacking defense easily bypassed by attackers; always enforce framing restrictions using server-side headers like X-Frame-Options or CSP frame-ancestors instead.