| Server IP : 68.183.124.220 / Your IP : 216.73.217.137 Web Server : Apache/2.4.18 (Ubuntu) System : Linux Sandbox-A 4.4.0-210-generic #242-Ubuntu SMP Fri Apr 16 09:57:56 UTC 2021 x86_64 User : gavin ( 1000) PHP Version : 7.0.33-0ubuntu0.16.04.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/gavin/workspace/happymandarin/node_modules/hsts/ |
Upload File : |
HTTP Strict Transport Security middlware
========================================
[](https://travis-ci.org/helmetjs/hsts)
[](http://standardjs.com/)
This middleware adds the `Strict-Transport-Security` header to the response. This tells browsers, "hey, only use HTTPS for the next period of time". ([See the spec](http://tools.ietf.org/html/draft-ietf-websec-strict-transport-sec-04) for more.)
This will set the Strict Transport Security header, telling browsers to visit by HTTPS for the next ninety days:
```javascript
var hsts = require('hsts')
var ninetyDaysInMilliseconds = 7776000000
app.use(hsts({ maxAge: ninetyDaysInMilliseconds }))
```
You can also include subdomains. If this is set on *example.com*, supported browsers will also use HTTPS on *my-subdomain.example.com*. Here's how you do that:
```javascript
app.use(hsts({
maxAge: 123000,
includeSubDomains: true
}))
```
Chrome lets you submit your site for baked-into-Chrome HSTS by adding `preload` to the header. You can add that with the following code, and then submit your site to the Chrome team at [hstspreload.appspot.com](https://hstspreload.appspot.com/).
```javascript
app.use(hsts({
maxAge: 10886400000, // Must be at least 18 weeks to be approved by Google
includeSubDomains: true, // Must be enabled to be approved by Google
preload: true
}))
```
This'll be set if `req.secure` is true, a boolean auto-populated by Express. If you're not using Express, that value won't necessarily be set, so you have two options:
```javascript
// Set the header based on conditions
app.use(hsts({
maxAge: 1234000,
setIf: function(req, res) {
return Math.random() < 0.5;
}
}))
// ALWAYS set the header
app.use(hsts({
maxAge: 1234000,
force: true
}))
```
Note that the max age is in milliseconds, even though the spec uses seconds. This will round to the nearest full second.
This only works if your site actually has HTTPS. It won't tell users on HTTP to *switch* to HTTPS, it will just tell HTTPS users to stick around. You can enforce this with the [express-enforces-ssl](https://github.com/aredo/express-enforces-ssl) module. This header is [somewhat well-supported by browsers](http://caniuse.com/#feat=stricttransportsecurity).