In an Express 5 app, what does app.use(helmet()) do by default, and what does it not protect you from?
answer
- response headers, not request checks
- about a dozen set, one removed
- strict self-only CSP by default
- X-Powered-By: Express disappears
basics
~10 sHelmet is middleware that sets security-related HTTP response headers, such as a strict Content-Security-Policy, Strict-Transport-Security and X-Content-Type-Options: nosniff, and removes X-Powered-By. It only instructs browsers; it does not validate, authenticate or configure CORS.
solid answer
~30 s`helmet()` returns one middleware that, with no options, sets twelve response headers, among them `Content-Security-Policy` (a `default-src 'self'` policy), `Strict-Transport-Security: max-age=31536000; includeSubDomains`, `X-Content-Type-Options: nosniff`, `X-Frame-Options: SAMEORIGIN`, `Referrer-Policy: no-referrer` and `Cross-Origin-Resource-Policy: same-origin`, and it removes the `X-Powered-By: Express` header that Express adds by default. Each header is a top-level option you can configure or set to `false`. Every one of them is an instruction the browser enforces, so Helmet does nothing about injection, broken authorization, rate limiting or CORS. The default CSP is the part most likely to break a page that uses inline scripts or assets from another host.
code
javascript · 17 linesimport express from 'express';
import helmet from 'helmet';
const app = express();
app.disable('x-powered-by');
app.use(
helmet({
contentSecurityPolicy: {
directives: { 'script-src': ["'self'", 'cdn.example.com'] },
reportOnly: true,
},
}),
);
app.get('/api/status', (req, res) => res.json({ ok: true }));
export default app;go deeper
Recall that Helmet sets response headers browsers enforce, name four or five of them, and know it removes X-Powered-By. Say clearly that it does not validate or block requests.
Explain how options work per header: an object configures one, false disables it, and CSP directives merge into the defaults unless useDefaults is false.
Show you have rolled it out: the default CSP breaking inline scripts, report-only trials, HSTS remembered by browsers for a year, and responses produced before Helmet runs.
Frame Helmet as browser-side policy only, and say which headers a team owns per app, mainly CSP, versus the defaults it can ship everywhere.
## What Helmet is **Helmet** is a small middleware package for Node and Express. Calling `helmet()` returns one Express middleware function; mounted with `app.use(helmet())`, it sets a group of **security-related HTTP response headers** on each response that passes through it. Express itself adds none of them to the responses your routes send (only its built-in 404 and error pages carry a minimal `default-src 'none'` policy), and it actually *adds* an `X-Powered-By: Express` header to every response. The point an interviewer is listening for is the mechanism. Each of these headers is an **instruction to the browser**. Helmet never inspects the request, never rejects anything and never changes what your handlers compute. The protection exists only when a browser receives the header and enforces it; a script calling your API from a terminal ignores all of it. ## The default headers With no options, Helmet 8 sets these twelve headers (values as the package documents them) and removes one: | Header | Default value | What the browser does with it | |---|---|---| | `Content-Security-Policy` | `default-src 'self'`, `script-src 'self'`, `object-src 'none'`, `frame-ancestors 'self'`, `upgrade-insecure-requests` and more | Loads scripts, styles, images and frames only from the allowed sources | | `Strict-Transport-Security` | `max-age=31536000; includeSubDomains` | Uses HTTPS for the host and its subdomains for 365 days | | `X-Content-Type-Options` | `nosniff` | Stops MIME sniffing of responses | | `X-Frame-Options` | `SAMEORIGIN` | Legacy clickjacking guard | | `Referrer-Policy` | `no-referrer` | Sends no `Referer` header from your pages | | `Cross-Origin-Opener-Policy` | `same-origin` | Isolates the page from cross-origin windows it opens | | `Cross-Origin-Resource-Policy` | `same-origin` | Blocks other origins from loading your resources in some cases | | `Origin-Agent-Cluster` | `?1` | Origin-based process isolation | | `X-DNS-Prefetch-Control`, `X-Download-Options`, `X-Permitted-Cross-Domain-Policies` | `off`, `noopen`, `none` | Narrow legacy controls | | `X-XSS-Protection` | `0` | Turns the old browser XSS filter **off**; Helmet's docs call that filter buggy | | `X-Powered-By` | removed | No framework banner | `Cross-Origin-Embedder-Policy` is a header Helmet supports but does **not** set by default. ## Configuring one header Each header is a top-level option, named in camel case: - pass an object to configure it, for example `strictTransportSecurity: { maxAge: 63072000, preload: true }`; - pass `false` to turn it off, for example `contentSecurityPolicy: false`; - CSP `directives` you pass are **merged into the default policy**; set `useDefaults: false` to start from an empty policy, or `reportOnly: true` to send `Content-Security-Policy-Report-Only` while you trial a policy; - every piece is also exported as standalone middleware, such as `helmet.contentSecurityPolicy()`. ```js app.use( helmet({ contentSecurityPolicy: { directives: { 'script-src': ["'self'", 'cdn.example.com'] }, }, }), ); ``` ## Where it bites 1. **The CSP default breaks pages.** `script-src 'self'` and `script-src-attr 'none'` block inline `<script>` blocks, inline event handlers and scripts from another host. A JSON-only API rarely notices; an app that serves its own HTML usually needs directives or a nonce function. 2. **HSTS outlives your config.** Helmet sends it on every response, and a browser that receives it over HTTPS remembers it for the full `max-age`. Turning it off for local development avoids browsers forcing `localhost` onto HTTPS. 3. **Coverage follows the stack.** Helmet can only change responses produced after it runs, so it is mounted near the top; where each middleware sits is the subject of the middleware-pipeline lesson. ## X-Powered-By and app.disable Express enables its `x-powered-by` setting by default and writes `X-Powered-By: Express` as soon as it starts handling a request. Helmet removes that header from responses it sees; `app.disable('x-powered-by')` stops Express from writing it at all, and Helmet's own README points Express users to that built-in setting as an alternative. Both only reduce casual **fingerprinting**: the framework can still be recognised by its default 404 page and other response quirks. ## What Helmet does not do - It does not validate input, escape output or prevent injection. - It does not configure CORS; the `cors` package sets those headers. - It does not authenticate, authorize or rate-limit anyone. - It does not provide TLS; HTTPS is still terminated by the server or the proxy in front of it. A fair one-line summary for an interview: Helmet is cheap, worth mounting everywhere, and only as good as the browser enforcement behind each header, so it complements validation, authorization and CORS rather than replacing any of them.
- Why can helmet's default Content-Security-Policy break pages an Express app serves, and how do you adjust it?The default `script-src 'self'` and `script-src-attr 'none'` block inline `<script>` blocks, inline event handlers and scripts from other hosts. Pass `contentSecurityPolicy: { directives: { 'script-src': ["'self'", 'cdn.example.com'] } }`, which is merged into the defaults, use a function entry that returns a per-request nonce, or set `reportOnly: true` to watch violations before enforcing.
- If helmet() is mounted, is app.disable('x-powered-by') still worth calling?Yes, cheaply. Helmet strips the header only from responses that pass through it, while `app.disable('x-powered-by')` stops Express writing it for every response, including any produced before Helmet runs or in an app that later drops Helmet. Helmet's README points Express users to it. Either way it only reduces fingerprinting; it is not a real defence.
- Helmet blocks malicious requests before they reach the routes.
- Helmet also sets the CORS headers an API needs.
- Removing X-Powered-By hides that the app runs Express.
- Helmet's default CSP allows inline scripts, so it never breaks a page.
- Helmet only sends Strict-Transport-Security when the request arrived over HTTPS.