How to Prevent Clickjacking with Frame Ancestors
Prevent clickjacking with a frame-ancestors CSP directive so the page cannot be framed by an attacker, and UI redress is impossible.
On this page
Introduction
The hardest part of prevent framing is not writing the code — it is deciding what the code is allowed to reject. A clickjacking setup that accepts everything is easy to ship and expensive to operate, because every malformed input becomes a production surprise. The discipline is to enumerate the shapes the system actually receives, encode them in clickjacking, and let the library reject the rest loudly. This keeps the happy path fast and the failure path visible.
Most teams adopt clickjacking for prevent framing after an incident teaches them that unvalidated input is load-bearing. The incident is the expensive way to learn the lesson; a quarterly review of the input shapes is the cheap way. Pin the shapes with clickjacking, refuse the deploy that breaks the pin, and treat the pin as a contract the rest of the system can rely on. A contract that is not enforced is a wish.
The sections below walk through the concrete decisions: where to put the clickjacking check, how to keep it fast under load, what to do when the contract changes, and how to test the failure paths so they degrade on purpose instead of by accident.
Set frame-ancestors
Set frame-ancestors is the first decision because it determines what the rest of the clickjacking pipeline can assume. A wrong choice here turns into prevent framing under load, and the invariant you thought clickjacking guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Set frame-ancestors is the one most teams skip: drive clickjacking to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
res.setHeader('Content-Security-Policy', "frame-ancestors 'none'");
// or for specific: frame-ancestors https://app.example.com
The javascript above is the shape to ship for prevent framing. It is small enough to audit and specific enough that the failure behavior is obvious. Adapt the constants to your traffic and latency budget, and resist the urge to handle cases the system has not seen yet.
Use none for strict
The mechanism behind Use none for strict is straightforward in clickjacking, but the consequences ripple. A wrong choice here turns into prevent framing under load, and the invariant you thought clickjacking guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Use none for strict is the one most teams skip: drive clickjacking to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
Or allow specific origins
Or allow specific origins is where most clickjacking implementations leak performance. A wrong choice here turns into prevent framing under load, and the invariant you thought clickjacking guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Or allow specific origins is the one most teams skip: drive clickjacking to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
test('sets frame-ancestors', () => {
expect(res.headers['content-security-policy']).toContain("frame-ancestors 'none'");
});
Pin the behavior with this test. A clickjacking setup that passes this test degrades on purpose instead of by accident, which is the difference between a managed failure and an incident.
Add X-Frame-Options
The hard part of Add X-Frame-Options is not the happy path — it is what happens when clickjacking fails halfway. A wrong choice here turns into prevent framing under load, and the invariant you thought clickjacking guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Add X-Frame-Options is the one most teams skip: drive clickjacking to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
Test framing
Test framing should be observable without a debugger. A wrong choice here turns into prevent framing under load, and the invariant you thought clickjacking guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Test framing is the one most teams skip: drive clickjacking to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
Audit headers
Audit headers is a trade, not a winner. A wrong choice here turns into prevent framing under load, and the invariant you thought clickjacking guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Audit headers is the one most teams skip: drive clickjacking to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
Common pitfalls
The common pitfalls in prevent framing all reduce to one mistake: treating clickjacking as a formality instead of a contract. The contract is the thing the rest of the system relies on, and a weak contract propagates weakness everywhere.
- Skipping the clickjacking boundary so prevent framing enters unvalidated.
- Assuming clickjacking is fast without measuring the tail latency under load.
- Letting prevent framing fail silently instead of failing loudly with a metric.
- Testing only the happy path so the clickjacking failure surfaces in production.
- Sharing mutable state across requests so one bad input poisons the next.
Conclusion
Do not conflate the conclusion with the implementation. They look similar from the outside but the failure modes differ: clickjacking fails fast and the caller knows, while an unchecked path fails slow and the caller guesses. Choose the one whose failure mode your system can absorb, and document the choice at the call site.
Self-check: write a test that drives clickjacking to the failing condition and assert the system degrades gracefully. Then prevent framing is a property you maintain, not a feature you hope for.
Sources: Clickjacking, CSP frame-ancestors
FAQ
- CSP or X-Frame-Options?
- CSP frame-ancestors is the modern way. X-Frame-Options is the legacy fallback.
- Need to frame?
- Use allowlist. Never *.
Report a problem with this articleOpens a pre-filled issue on GitHub.