Skip to content

Commit

Permalink
Merge pull request #4044 from jweismann/dev
Browse files Browse the repository at this point in the history
redirect HTTP to HTTPS unless explicitly instructed not to do this
  • Loading branch information
PieterGit authored Nov 21, 2018
2 parents cf7b40e + 372efca commit 5787482
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ To learn more about the Nightscout API, visit https://YOUR-SITE.com/api-docs.htm
* `SHOW_RAWBG` (`never`) - possible values `always`, `never` or `noise`
* `CUSTOM_TITLE` (`Nightscout`) - Usually name of T1
* `THEME` (`default`) - possible values `default`, `colors`, or `colorblindfriendly`
* `INSECURE_USE_HTTP` (`false`) - possible values `false`, or `true`.
* `SECURE_HTTP_HEADERS` (`false`) - possible values `false`, or `true`.
* `ALARM_TIMEAGO_WARN` (`on`) - possible values `on` or `off`
* `ALARM_TIMEAGO_WARN_MINS` (`15`) - minutes since the last reading to trigger a warning
* `ALARM_TIMEAGO_URGENT` (`on`) - possible values `on` or `off`
Expand Down
20 changes: 19 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ function create(env, ctx) {
var appInfo = env.name + ' ' + env.version;
app.set('title', appInfo);
app.enable('trust proxy'); // Allows req.secure test on heroku https connections.
if (process.env.INSECURE_USE_HTTP !== 'true') {
app.use((req, res, next) => {
if (req.header('x-forwarded-proto') !== 'https')
res.redirect(`https://${req.header('host')}${req.url}`)
else
next()
})
if (process.env.SECURE_HTTP_HEADERS == 'true') {
const helmet = require('helmet')
app.use(helmet({
hsts: {
maxAge: 31536000,
includeSubDomains: true,
preload: true
}
}))
}
}

app.set('view engine', 'ejs');
// this allows you to render .html files as templates in addition to .ejs
Expand Down Expand Up @@ -208,4 +226,4 @@ function create(env, ctx) {
//}
return app;
}
module.exports = create;
module.exports = create;
145 changes: 138 additions & 7 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"express": "^4.16.4",
"express-minify": "^1.0.0",
"flot": "^0.8.0-alpha",
"helmet": "^3.14.0",
"jquery": "^3.3.1",
"jquery-ui-bundle": "^1.12.1-migrate",
"jquery.tooltips": "^1.0.0",
Expand Down

0 comments on commit 5787482

Please sign in to comment.