File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ module . exports = function ( options ) {
2
+ options = options ? options : { } ;
3
+ const maxAge = options . maxAge ? options . maxAge : 86400 ;
4
+ const includeSubDomains =
5
+ options . includeSubDomains === undefined ? true : options . includeSubdomains ;
6
+
7
+ return function ( req , res , next ) {
8
+ let ignoreRequest = process . env . NODE_ENV !== 'production' ;
9
+ const secure =
10
+ req . connection . encrypted || req . get ( 'X-Forwarded-Proto' ) === 'https' ;
11
+
12
+ if ( options . ignoreFilter ) {
13
+ ignoreRequest = ignoreRequest || options . ignoreFilter ( req ) ;
14
+ }
15
+
16
+ if ( ignoreRequest ) {
17
+ next ( ) ;
18
+ return ;
19
+ }
20
+
21
+ if ( secure ) {
22
+ let header = 'max-age=' + maxAge ;
23
+ if ( includeSubDomains ) {
24
+ header += '; includeSubDomains' ;
25
+ }
26
+
27
+ if ( options . preload ) {
28
+ header += '; preload' ;
29
+ }
30
+
31
+ res . setHeader ( 'Strict-Transport-Security' , header ) ;
32
+ next ( ) ;
33
+ } else {
34
+ res . writeHead ( 301 , {
35
+ Location : 'https://' + req . get ( 'host' ) + req . url ,
36
+ } ) ;
37
+ res . end ( ) ;
38
+ }
39
+ } ;
40
+ } ;
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import cookieParser from 'cookie-parser';
10
10
import passport from 'passport' ;
11
11
import setupAuth from './api/auth' ;
12
12
import setupApi from './api' ;
13
+ import enforceSSL from './common/enforce-ssl' ;
13
14
14
15
import queries from './api/controllers/queries' ;
15
16
import { User , Comment , Post } from './api/models' ;
@@ -26,6 +27,7 @@ app.prepare().then(() => {
26
27
server . use ( compression ( ) ) ;
27
28
}
28
29
30
+ server . use ( enforceSSL ( ) ) ;
29
31
server . use ( '/static' , express . static ( __dirname + '/static' ) ) ;
30
32
server . use ( cookieParser ( ) ) ;
31
33
server . use ( morgan ( 'dev' ) ) ;
You can’t perform that action at this time.
0 commit comments