Helmet.js: Secure Express Apps with HTTP Headers
Helmet.js adds a security layer to Express apps by setting crucial HTTP headers. Use it in any public-facing Node app to prevent common attacks like XSS. The footgun: its default Content-Security-Policy is strict and requires app-specific configuration.
WHY IT EXISTS: Web applications are vulnerable to many client-side attacks like cross-site scripting (XSS), clickjacking, and protocol downgrades. Browsers have security features to mitigate these, but they must be explicitly enabled via specific HTTP response headers. Manually setting and maintaining these headers is tedious and error-prone.
THE MENTAL MODEL: Think of Helmet.js as a bouncer for your web application's responses. Before sending a response to the client's browser, Helmet adds a list of rules (HTTP headers) telling the browser what it is and isn't allowed to do with the content. This helps prevent malicious actors from exploiting your users by injecting scripts or framing your site.
HOW IT WORKS: Helmet is an Express middleware function. When you add app.use(helmet()) to your application, it bundles 13 smaller middleware functions, each responsible for setting one specific HTTP security header. The most significant of these is Content-Security-Policy (CSP), which provides a highly configurable whitelist of sources for scripts, styles, images, and other assets. You can disable individual headers or provide specific configuration for them, such as defining the allowed sources in your CSP.
WHEN TO USE IT: Use Helmet in virtually every production Node.js application that uses Express or a similar framework. It provides a strong security baseline with minimal initial setup. It's especially critical for applications that handle user data or are exposed to the public internet.
WHEN NOT TO USE IT: You might disable specific headers in certain contexts. For example, the upgrade-insecure-requests CSP directive, enabled by default, can cause issues in local development environments that don't use HTTPS. In such cases, you can conditionally disable that specific directive for development builds rather than removing Helmet entirely. Also, Helmet performs very little validation on your CSP; you must use an external tool like CSP Evaluator to check your policy's correctness.
ONE CANONICAL EXAMPLE: The most common task is customizing the Content-Security-Policy. By default, it only allows resources from the same origin ('self'). If your app uses Google Fonts and a script from an analytics provider, it will break. You must explicitly allow these sources: app.use(helmet({ contentSecurityPolicy: { directives: { "script-src": ["'self'", "analytics.example.com"], "style-src": ["'self'", "fonts.googleapis.com"], "font-src": ["'self'", "fonts.gstatic.com"] } } })); This tells the browser it's okay to load scripts from your domain and the analytics provider, and styles/fonts from Google's domains, while blocking all others.
Read the original → helmet.js.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.