X-Frame-Options Express Middleware
raw JSON →The `x-frame-options` package provides a simple Express middleware to set the `X-Frame-Options` HTTP response header, a security mechanism designed to prevent clickjacking attacks by controlling whether a page can be rendered within `<iframe>`, `<frame>`, `<embed>`, or `<object>` elements. Currently at version 1.0.0, the package was last published over a decade ago. While still functional, the `X-Frame-Options` header itself is considered a legacy solution in modern web development. For comprehensive and more granular protection against framing-based attacks, the `frame-ancestors` directive within Content Security Policy (CSP) is the recommended approach. This package is typically used for supporting older browsers that might not fully support CSP, often in conjunction with CSP `frame-ancestors` to ensure broad compatibility. The middleware defaults the `X-Frame-Options` header value to 'Deny', offering the strongest initial protection.
Common errors
error Refused to display 'https://example.com/sensitive-page' in a frame because it set 'X-Frame-Options' to 'DENY'. ↓
x-frame-options middleware to use SAMEORIGIN (if the embedding page is on the same origin) or, preferably, migrate to Content-Security-Policy: frame-ancestors 'self' or specify trusted origins for frame-ancestors. error Refused to display 'https://example.com/page' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'. ↓
Content-Security-Policy with a flexible frame-ancestors directive to whitelist allowed origins, as X-Frame-Options 'ALLOW-FROM' is deprecated and unreliable. Warnings
deprecated The `X-Frame-Options` header itself is considered a legacy security header. Modern web applications should primarily rely on the `frame-ancestors` directive within the Content Security Policy (CSP) for comprehensive clickjacking protection, as it offers more granular control and is the current best practice. ↓
breaking The `ALLOW-FROM` directive for `X-Frame-Options` is deprecated and has inconsistent browser support across modern browsers. Browsers that do not recognize `ALLOW-FROM` will silently ignore the entire `X-Frame-Options` header, leaving your site unprotected. ↓
gotcha The `X-Frame-Options` header cannot be set via `<meta>` tags in HTML; it must be delivered as an HTTP response header by the server. Attempts to set it via meta tags will be ignored by browsers. ↓
gotcha If both `X-Frame-Options` and `Content-Security-Policy` with `frame-ancestors` are present, modern browsers will prioritize the `frame-ancestors` directive. Ensure that the policies defined by both headers are consistent to avoid unexpected blocking or reduced protection. ↓
Install
npm install x-frame-options yarn add x-frame-options pnpm add x-frame-options Imports
- xFrameOptions wrong
const xFrameOptions = require('x-frame-options');correctimport xFrameOptions from 'x-frame-options';
Quickstart
const express = require('express');
const xFrameOptions = require('x-frame-options');
const app = express();
// Use the middleware to add X-Frame-Options header (defaults to 'DENY')
app.use(xFrameOptions());
// Or configure it with 'SAMEORIGIN'
// app.use(xFrameOptions('SAMEORIGIN'));
app.get('/', (req, res) => {
res.send('Hello World! This page has X-Frame-Options set.');
});
app.get('/test-header', (req, res) => {
const xfoHeader = res.get('X-Frame-Options');
res.send(`X-Frame-Options header value: ${xfoHeader || 'Not Set'}`);
});
const port = 3000;
app.listen(port, () => {
console.log(`Express app listening at http://localhost:${port}`);
});