Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix security issue when running on Glitch #913

Merged
merged 5 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

## Fixes

- [Pull request #913: Fix security issue when running on Glitch](https://github.com/alphagov/govuk-prototype-kit/pull/913)

# 9.8.0 (Feature release)

## New features
Expand Down
3 changes: 2 additions & 1 deletion lib/middleware/authentication/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module.exports = function (req, res, next) {
const config = require('../../../app/config.js')

// Local Variables
const env = (process.env.NODE_ENV || 'development').toLowerCase()
const glitchEnv = (process.env.PROJECT_REMIX_CHAIN) ? 'production' : false // glitch.com
const env = (process.env.NODE_ENV || glitchEnv || 'development').toLowerCase()
const useAuth = (process.env.USE_AUTH || config.useAuth).toLowerCase()
const username = process.env.USERNAME
const password = process.env.PASSWORD
Expand Down
9 changes: 8 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,14 @@ exports.findAvailablePort = function (app, callback) {

// Redirect HTTP requests to HTTPS
exports.forceHttps = function (req, res, next) {
if (req.headers['x-forwarded-proto'] !== 'https') {
var protocol = req.headers['x-forwarded-proto']
// Glitch returns a comma separated list for x-forwarded-proto
// We need the first to determine if running on https
if (protocol) {
protocol = protocol.split(',').shift()
}

if (protocol !== '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)
Expand Down
3 changes: 2 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ documentationApp.use(utils.handleCookies(documentationApp))

// Set up configuration variables
var releaseVersion = packageJson.version
var env = (process.env.NODE_ENV || 'development').toLowerCase()
var glitchEnv = (process.env.PROJECT_REMIX_CHAIN) ? 'production' : false // glitch.com
var env = (process.env.NODE_ENV || glitchEnv || 'development').toLowerCase()
var useAutoStoreData = process.env.USE_AUTO_STORE_DATA || config.useAutoStoreData
var useCookieSessionStore = process.env.USE_COOKIE_SESSION_STORE || config.useCookieSessionStore
var useHttps = process.env.USE_HTTPS || config.useHttps
Expand Down