Skip to content

Commit 5ce8f40

Browse files
authored
Merge pull request #585 from xxyy/feature/hsts-cfg
Make HSTS Behaviour Configurable (Fixes #584)
2 parents ec8936a + 6bdc90d commit 5ce8f40

File tree

6 files changed

+52
-6
lines changed

6 files changed

+52
-6
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ Environment variables (will overwrite other server configs)
154154
| HMD_S3_SECRET_ACCESS_KEY | no example | AWS secret key |
155155
| HMD_S3_REGION | `ap-northeast-1` | AWS S3 region |
156156
| HMD_S3_BUCKET | no example | AWS S3 bucket name |
157+
| HMD_HSTS_ENABLE | ` true` | set to enable [HSTS](https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security) if HTTPS is also enabled (default is ` true`) |
158+
| HMD_HSTS_INCLUDE_SUBDOMAINS | `true` | set to include subdomains in HSTS (default is `true`) |
159+
| HMD_HSTS_MAX_AGE | `31536000` | max duration in seconds to tell clients to keep HSTS status (default is a year) |
160+
| HMD_HSTS_PRELOAD | `true` | whether to allow preloading of the site's HSTS status (e.g. into browsers) |
157161

158162
Application settings `config.json`
159163
---
@@ -166,6 +170,7 @@ Application settings `config.json`
166170
| port | `80` | web app port |
167171
| alloworigin | `['localhost']` | domain name whitelist |
168172
| usessl | `true` or `false` | set to use ssl server (if true will auto turn on `protocolusessl`) |
173+
| hsts | `{"enable": "true", "maxAgeSeconds": "31536000", "includeSubdomains": "true", "preload": "true"}` | [HSTS](https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security) options to use with HTTPS (default is the example value, max age is a year) |
169174
| protocolusessl | `true` or `false` | set to use ssl protocol for resources path (only applied when domain is set) |
170175
| urladdport | `true` or `false` | set to add port on callback url (port 80 or 443 won't applied) (only applied when domain is set) |
171176
| usecdn | `true` or `false` | set to use CDN resources or not (default is `true`) |

app.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,16 @@ var sessionStore = new SequelizeStore({
9797
app.use(compression())
9898

9999
// use hsts to tell https users stick to this
100-
app.use(helmet.hsts({
101-
maxAge: 31536000 * 1000, // 365 days
102-
includeSubdomains: true,
103-
preload: true
104-
}))
100+
if (config.hsts.enable) {
101+
app.use(helmet.hsts({
102+
maxAge: config.hsts.maxAgeSeconds * 1000,
103+
includeSubdomains: config.hsts.includeSubdomains,
104+
preload: config.hsts.preload
105+
}))
106+
} else if (config.usessl) {
107+
logger.info('Consider enabling HSTS for extra security:')
108+
logger.info('https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security')
109+
}
105110

106111
i18n.configure({
107112
locales: ['en', 'zh', 'fr', 'de', 'ja', 'es', 'ca', 'el', 'pt', 'it', 'tr', 'ru', 'nl', 'hr', 'pl', 'uk', 'hi', 'sv', 'eo', 'da'],

app.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,22 @@
2323
"description": "Specify database type. See sequelize available databases. Default using postgres",
2424
"value": "postgres"
2525
},
26-
26+
"HMD_HSTS_ENABLE": {
27+
"description": "whether to also use HSTS if HTTPS is enabled",
28+
"required": false
29+
},
30+
"HMD_HSTS_MAX_AGE": {
31+
"description": "max duration, in seconds, to tell clients to keep HSTS status",
32+
"required": false
33+
},
34+
"HMD_HSTS_INCLUDE_SUBDOMAINS": {
35+
"description": "whether to tell clients to also regard subdomains as HSTS hosts",
36+
"required": false
37+
},
38+
"HMD_HSTS_PRELOAD": {
39+
"description": "whether to allow at all adding of the site to HSTS preloads (e.g. in browsers)",
40+
"required": false
41+
},
2742
"HMD_DOMAIN": {
2843
"description": "domain name",
2944
"required": false

config.json.example

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@
66
}
77
},
88
"development": {
9+
"hsts": {
10+
"enable": false
11+
},
912
"db": {
1013
"dialect": "sqlite",
1114
"storage": "./db.hackmd.sqlite"
1215
}
1316
},
1417
"production": {
1518
"domain": "localhost",
19+
"hsts": {
20+
"enable": "true",
21+
"maxAgeSeconds": "31536000",
22+
"includeSubdomains": "true",
23+
"preload": "true"
24+
},
1625
"db": {
1726
"username": "",
1827
"password": "",

lib/config/default.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ module.exports = {
77
urladdport: false,
88
alloworigin: ['localhost'],
99
usessl: false,
10+
hsts: {
11+
enable: true,
12+
maxAgeSeconds: 31536000,
13+
includeSubdomains: true,
14+
preload: true
15+
},
1016
protocolusessl: false,
1117
usecdn: true,
1218
allowanonymous: true,

lib/config/environment.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ module.exports = {
88
port: process.env.HMD_PORT,
99
urladdport: toBooleanConfig(process.env.HMD_URL_ADDPORT),
1010
usessl: toBooleanConfig(process.env.HMD_USESSL),
11+
hsts: {
12+
enable: toBooleanConfig(process.env.HMD_HSTS_ENABLE),
13+
maxAgeSeconds: process.env.HMD_HSTS_MAX_AGE,
14+
includeSubdomains: toBooleanConfig(process.env.HMD_HSTS_INCLUDE_SUBDOMAINS),
15+
preload: toBooleanConfig(process.env.HMD_HSTS_PRELOAD)
16+
},
1117
protocolusessl: toBooleanConfig(process.env.HMD_PROTOCOL_USESSL),
1218
alloworigin: process.env.HMD_ALLOW_ORIGIN ? process.env.HMD_ALLOW_ORIGIN.split(',') : undefined,
1319
usecdn: toBooleanConfig(process.env.HMD_USECDN),

0 commit comments

Comments
 (0)