Skip to content

Commit 11a6698

Browse files
committed
Merge pull request #3866 from dialoghq/feature/HSTS
feat(router): enable HSTS when enforceHTTPS is set
2 parents ddee95f + 08c84ed commit 11a6698

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

docs/customizing_deis/router_settings.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ setting description
5959
/deis/router/gzipVary nginx gzipVary setting (default: on)
6060
/deis/router/gzipDisable nginx gzipDisable setting (default: "msie6")
6161
/deis/router/gzipTypes nginx gzipTypes setting (default: "application/x-javascript application/xhtml+xml application/xml application/xml+rss application/json text/css text/javascript text/plain text/xml")
62+
/deis/router/hsts/enabled enable HTTP Strict Transport Security headers for HTTPS requests (default: false)
63+
/deis/router/hsts/maxAge maximum number of seconds user agents should observe HSTS rewrites (default: 10886400)
64+
/deis/router/hsts/includeSubDomains enforce HSTS for requests on all subdomains (default: false)
65+
/deis/router/hsts/preload allow the domain to be included in the HSTS preload list (default: false)
6266
/deis/router/maxWorkerConnections maximum number of simultaneous connections that can be opened by a worker process (default: 768)
6367
/deis/router/serverNameHashMaxSize nginx server_names_hash_max_size setting (default: 512)
6468
/deis/router/serverNameHashBucketSize nginx server_names_hash_bucket_size (default: 64)

router/boot.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func main() {
5959
mkdirEtcd(client, "/deis/builder")
6060
mkdirEtcd(client, "/deis/certs")
6161
mkdirEtcd(client, "/deis/router/hosts")
62+
mkdirEtcd(client, "/deis/router/hsts")
6263

6364
setDefaultEtcd(client, etcdPath+"/gzip", "on")
6465

router/image/templates/nginx.conf

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,17 @@ http {
6969
'' $scheme;
7070
}
7171

72-
{{ $enforceHTTPS := or (getv "/deis/router/enforceHTTPS") "false" }}
72+
## HSTS instructs the browser to replace all HTTP links with HTTPS links for this domain until maxAge seconds from now
73+
{{ $enableHSTS := or (getv "/deis/router/hsts/enabled") "false" }}
74+
{{ $maxAgeHSTS := or (getv "/deis/router/hsts/maxAge") "10886400" }}
75+
{{ $includeSubdomainsHSTS := or (getv "/deis/router/hsts/includeSubDomains") "false" }}
76+
{{ $preloadHSTS := or (getv "/deis/router/hsts/preload") "false" }}
77+
map $access_scheme $sts {
78+
'https' 'max-age={{ $maxAgeHSTS }}{{ if eq $includeSubdomainsHSTS "true" }}; includeSubDomains{{ end }}{{ if eq $preloadHSTS "true" }}; preload{{ end }}';
79+
}
80+
81+
## since HSTS headers are not permitted on HTTP requests, 301 redirects to HTTPS resources are also necessary
82+
{{ $enforceHTTPS := or (getv "/deis/router/enforceHTTPS") $enableHSTS "false" }}
7383

7484
## start deis-controller
7585
{{ if exists "/deis/controller/host" }}
@@ -115,6 +125,10 @@ http {
115125
return 301 https://$host$request_uri;
116126
}
117127
{{ end }}
128+
129+
{{ if eq $enableHSTS "true" }}
130+
add_header Strict-Transport-Security $sts always;
131+
{{ end }}
118132
}
119133
## end deis-controller
120134

@@ -230,6 +244,10 @@ http {
230244
}
231245
{{ end }}
232246

247+
{{ if eq $enableHSTS "true" }}
248+
add_header Strict-Transport-Security $sts always;
249+
{{ end }}
250+
233251
## workaround for nginx hashing empty string bug http://trac.nginx.org/nginx/ticket/765
234252
{{ if exists "/deis/router/affinityArg" }}
235253
set_random $prng 0 99;
@@ -288,6 +306,10 @@ http {
288306
}
289307
{{ end }}
290308

309+
{{ if eq $enableHSTS "true" }}
310+
add_header Strict-Transport-Security $sts always;
311+
{{ end }}
312+
291313
proxy_pass http://{{ $app }};
292314
}
293315
{{ else }}

0 commit comments

Comments
 (0)