Helmet
The Helmet plugin provides middleware for setting HTTP security headers, enhancing your application’s protection against common web vulnerabilities. It allows fine-grained control over headers to secure your application effectively.
Middlewares
plugins.helmet.middlewares.HelmetMiddleware
-
Purpose:
This middleware configures various HTTP headers to improve security, such as mitigating XSS attacks, preventing content sniffing, and enforcing secure policies for content, permissions, and cross-origin interactions. -
Usage:
When configured in a route’s middleware list, the HelmetMiddleware ensures the specified security headers are added to responses, improving security without modifying application logic. -
Configuration Integration:
The middleware is configured through the plugin and referenced by name in the route’s middleware list.
Middleware Configuration
The HelmetMiddleware accepts the following configuration options:
-
XSSProtection
Configures theX-XSS-Protection
header to prevent reflected XSS attacks.
Default:"0"
(disabled). -
ContentTypeNosniff
Sets theX-Content-Type-Options
header tonosniff
, preventing browsers from interpreting files as a different MIME type than declared. Default:"nosniff"
. -
XFrameOptions
Controls theX-Frame-Options
header to prevent clickjacking by disallowing the page from being embedded in an iframe. Default:"SAMEORIGIN"
. -
HSTSMaxAge
Configures theStrict-Transport-Security
(HSTS) header’smax-age
directive in seconds, enforcing HTTPS connections. -
HSTSExcludeSubdomains
Determines whether HSTS applies to subdomains. Iftrue
, excludes subdomains. Default:false
. -
ContentSecurityPolicy
Sets theContent-Security-Policy
(CSP) header to control sources of content. -
CSPReportOnly
EnablesContent-Security-Policy-Report-Only
, logging violations without blocking requests. Default:false
. -
HSTSPreloadEnabled
Adds thepreload
directive to HSTS, allowing the domain to be included in browser preload lists. Default:false
. -
ReferrerPolicy
Configures theReferrer-Policy
header to control the information sent in theReferer
header. Default:"ReferrerPolicy"
. -
PermissionPolicy
Sets thePermissions-Policy
header to control access to browser features. -
CrossOriginEmbedderPolicy
Sets theCross-Origin-Embedder-Policy
header. Default:"require-corp"
. -
CrossOriginOpenerPolicy
Sets theCross-Origin-Opener-Policy
header. Default:"same-origin"
. -
CrossOriginResourcePolicy
Sets theCross-Origin-Resource-Policy
header. Default:"same-origin"
. -
OriginAgentCluster
Adds theOrigin-Agent-Cluster
header. Default:"?1"
. -
XDNSPrefetchControl
Configures theX-DNS-Prefetch-Control
header to enable or disable DNS prefetching. Default:"off"
. -
XDownloadOptions
Sets theX-Download-Options
header to prevent Internet Explorer from executing downloads in the site’s context. Default:"noopen"
. -
XPermittedCrossDomain
Sets theX-Permitted-Cross-Domain-Policies
header to control permissible policies for cross-domain content. Default:"none"
.
Example Configuration
Below is an example of how to configure the Helmet plugin:
middlewares: HelmetMiddleware: use: plugins.helmet.middlewares.HelmetMiddleware config: xssProtection: "1; mode=block" contentTypeNosniff: "nosniff" xFrameOptions: "SAMEORIGIN" hstsMaxAge: 31536000 referrerPolicy: "no-referrer" crossOriginEmbedderPolicy: "require-corp" crossOriginOpenerPolicy: "same-origin" crossOriginResourcePolicy: "same-origin" xdnsPrefetchControl: "off" xDownloadOptions: "noopen" xPermittedCrossDomain: "none"
How It Works
-
Header Injection:
HelmetMiddleware adds security-related HTTP headers to outgoing responses based on the configuration. -
Policy Enforcement:
These headers instruct browsers to apply security restrictions, such as preventing MIME type sniffing, blocking insecure content, and enforcing strict origin policies. -
Response:
- For correctly configured routes, headers are injected into every response.
- If a policy is misconfigured, browser behavior may vary.
Using as Global Middleware
To use HelmetMiddleware as global middleware, configure the middleware file to include the middleware as global middleware:
plugins: # ... - path: /plugins/helmet.so
middlewares: # ... HelmetMiddleware: use: plugins.helmet.middlewares.HelmetMiddleware config: xssProtection: "1; mode=block" contentTypeNosniff: "nosniff" hstsMaxAge: 31536000 xFrameOptions: "SAMEORIGIN" # ...globalMiddlewares: - HelmetMiddleware
Using with Routes
To use HelmetMiddleware with a specific route, configure the route to include the middleware:
plugins: # ... - path: /plugins/helmet.so
middlewares: # ... HelmetMiddleware: use: plugins.helmet.middlewares.HelmetMiddleware config: xssProtection: "1; mode=block" contentTypeNosniff: "nosniff" hstsMaxAge: 31536000 xFrameOptions: "SAMEORIGIN"
routes: # ... - path: "/form/submit" method: "POST" middlewares: - HelmetMiddleware action: HttpForwardToFormService
Best Practices
- Enable HSTS: Use
hstsMaxAge
with a reasonable duration (e.g., 1 year) to enforce HTTPS connections. - Restrict CSP Sources: Specify strict policies for
contentSecurityPolicy
to control content loading. - Regularly Review Policies: Periodically audit your security headers to align with evolving best practices.
- Test in Staging: Validate the middleware in a staging environment to avoid unexpected behavior in production.