The `hsts` package provides HTTP Strict Transport Security (HSTS) middleware for Node.js applications, primarily for use with Express or Connect. It adds the `Strict-Transport-Security` header to HTTP responses, instructing browsers to interact with the site exclusively over HTTPS for a specified duration. Key features include configurable `maxAge` (in seconds), `includeSubDomains`, and `preload` directives for HSTS preloading services. This package is part of the Helmet.js suite, a collection of security middleware. The current stable version is 2.2.0, which has been stable for some time, indicating a mature, maintenance-oriented release cadence rather than frequent updates unless security vulnerabilities or major breaking changes in web standards require it. Its primary differentiator is its simplicity and integration within the widely adopted Helmet.js ecosystem, ensuring robust and standard-compliant HSTS implementation.
npm install hstsVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic and conditional HSTS middleware setup for an Express application, including options for preloading.
Always provide the `maxAge` value in seconds. For example, `15552000` for 180 days.
Consider applying the HSTS middleware conditionally, only for requests that are already secure (`req.secure`). You might also need a separate mechanism like `express-enforces-ssl` to redirect HTTP traffic to HTTPS.
When using `preload: true`, ensure `maxAge` is `31536000` or higher and `includeSubDomains` is `true`.
Implement a separate redirect mechanism (e.g., using `express-enforces-ssl` or custom middleware) to redirect all incoming HTTP traffic to HTTPS before the HSTS middleware is applied.
Ensure your application is served over HTTPS. Use a tool like `ngrok` or a local proxy for development, or deploy to an environment with SSL/TLS enabled for testing.
Double-check your `maxAge` calculation. For example, 180 days is 180 * 24 * 60 * 60 = 15,552,000 seconds. A common long-term value is 31,536,000 for one year.
Ensure your HSTS middleware is configured with `maxAge: 31536000` (at least 1 year), `includeSubDomains: true`, and `preload: true`. Verify the header is correctly served via HTTPS.
No dependency data recorded yet.