Skip to content

Commit

Permalink
Merge pull request #480 from alphagov/force-ssl
Browse files Browse the repository at this point in the history
Serve GOV.UK elements over https
  • Loading branch information
gemmaleigh authored Jun 14, 2017
2 parents 377a7ab + 77b2265 commit 56808ac
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ dist/*
shots
shots_history
# These folders were previously generated by build tasks
lib/*
app/views/snippets/encoded/*
govuk_modules/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ GOV.UK elements ·

GOV.UK elements is three things:

1. [An online design guide](http://govuk-elements.herokuapp.com/), explaining how to make your service look consistent with the rest of GOV.UK.
1. [An online design guide](https://govuk-elements.herokuapp.com/), explaining how to make your service look consistent with the rest of GOV.UK.
2. An example of how to use the code in the [GOV.UK template](https://github.com/alphagov/govuk_template) and the [GOV.UK frontend toolkit](https://github.com/alphagov/govuk_frontend_toolkit).
3. An [npm package of the Sass files](https://www.npmjs.com/package/govuk-elements-sass).

Expand Down
3 changes: 3 additions & 0 deletions app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

module.exports = {

// Force HTTP to redirect to HTTPs on production
useHttps: 'true',

// Cookie warning
cookieText: 'GOV.UK uses cookies to make the site simpler. <a href="https://www.gov.uk/help/cookies">Find out more about cookies</a>'

Expand Down
8 changes: 8 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports.forceHttps = function (req, res, next) {
if (req.headers['x-forwarded-proto'] !== 'https') {
console.log('Redirecting request to https')
// 302 temporary - this is a feature that can be disabled
return res.redirect(302, 'https://' + req.get('Host') + req.url)
}
next()
}
13 changes: 13 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ var bodyParser = require('body-parser')
var config = require('./app/config.js')
var port = (process.env.PORT || 3000)
var IS_HEROKU = process.env.hasOwnProperty('IS_HEROKU')
var utils = path.join(__dirname, '/lib/utils.js')

// Grab environment variables specified in Procfile or as Heroku config vars
var env = process.env.NODE_ENV || 'development'
var useHttps = process.env.USE_HTTPS || config.useHttps

env = env.toLowerCase()
useHttps = useHttps.toLowerCase()

module.exports = app

Expand Down Expand Up @@ -62,3 +70,8 @@ app.listen(port, function () {
console.log('Listening on port ' + port + ' url: http://localhost:' + port)
}
})

// Force HTTPs on production connections
if (env === 'production' && useHttps === 'true') {
app.use(utils.forceHttps)
}

0 comments on commit 56808ac

Please sign in to comment.