Skip to content

Commit dba402d

Browse files
Nickster28drew-gross
authored andcommitted
Added support for trusting proxies w/ HTTPS (#535)
* Added https check for Heroku * Made Heroku HTTPS check more robust - added client switch for trusting proxies, and used app.enable('trust proxy') to let Express parse the headers * Added support for setting trust proxy when using as middleware * Updated README * README edits * Pull request feedback to combine trustProxy into dashboard config object and de-emphasize allowInsecureHTTP in favor of trustProxy
1 parent 6a385ca commit dba402d

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

Parse-Dashboard/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ module.exports = function(config, allowInsecureHTTP) {
5353
// Serve public files.
5454
app.use(express.static(path.join(__dirname,'public')));
5555

56+
// Allow setting via middleware
57+
if (config.trustProxy && app.disabled('trust proxy')) {
58+
app.enable('trust proxy');
59+
}
60+
5661
// Serve the configuration.
5762
app.get('/parse-dashboard-config.json', function(req, res) {
5863
let response = {

Parse-Dashboard/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,20 @@ program.option('--mountPath [mountPath]', 'the mount path to run parse-dashboard
2424
program.option('--allowInsecureHTTP [allowInsecureHTTP]', 'set this flag when you are running the dashboard behind an HTTPS load balancer or proxy with early SSL termination.');
2525
program.option('--sslKey [sslKey]', 'the path to the SSL private key.');
2626
program.option('--sslCert [sslCert]', 'the path to the SSL certificate.');
27+
program.option('--trustProxy [trustProxy]', 'set this flag when you are behind a front-facing proxy, such as when hosting on Heroku. Uses X-Forwarded-* headers to determine the client\'s connection and IP address.');
2728

2829
program.parse(process.argv);
2930

3031
const host = program.host || process.env.HOST || '0.0.0.0';
3132
const port = program.port || process.env.PORT || 4040;
3233
const mountPath = program.mountPath || process.env.MOUNT_PATH || '/';
3334
const allowInsecureHTTP = program.allowInsecureHTTP || process.env.PARSE_DASHBOARD_ALLOW_INSECURE_HTTP;
35+
const trustProxy = program.trustProxy || process.env.PARSE_DASHBOARD_TRUST_PROXY;
36+
37+
if (trustProxy && allowInsecureHTTP) {
38+
console.log("Set only trustProxy *or* allowInsecureHTTP, not both. Only one is needed to handle being behind a proxy.");
39+
process.exit(-1);
40+
}
3441

3542
let explicitConfigFileProvided = !!program.config;
3643
let configFile = null;
@@ -105,7 +112,9 @@ p.then(config => {
105112

106113
const app = express();
107114

108-
if (allowInsecureHTTP) app.enable('trust proxy');
115+
if (allowInsecureHTTP || trustProxy) app.enable('trust proxy');
116+
117+
config.data.trustProxy = trustProxy;
109118
app.use(mountPath, parseDashboard(config.data, allowInsecureHTTP));
110119
if(!configSSLKey || !configSSLCert){
111120
// Start the server.

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ You can also define each configuration option individually.
8282
HOST: "0.0.0.0"
8383
PORT: "4040"
8484
MOUNT_PATH: "/"
85-
PARSE_DASHBOARD_ALLOW_INSECURE_HTTP: undefined // Or "1" to allow http
85+
PARSE_DASHBOARD_TRUST_PROXY: undefined // Or "1" to trust connection info from a proxy's X-Forwarded-* headers
8686
PARSE_DASHBOARD_SERVER_URL: "http://localhost:1337/parse"
8787
PARSE_DASHBOARD_MASTER_KEY: "myMasterKey"
8888
PARSE_DASHBOARD_APP_ID: "myAppId"
@@ -213,7 +213,24 @@ Make sure the server URLs for your apps can be accessed by your browser. If you
213213
## Security Considerations
214214
In order to securely deploy the dashboard without leaking your apps master key, you will need to use HTTPS and Basic Authentication.
215215

216-
The deployed dashboard detects if you are using a secure connection. If you are deploying the dashboard behind a load balancer or proxy that does early SSL termination, then the app won't be able to detect that the connection is secure. In this case, you can start the dashboard with the `--allowInsecureHTTP=1` option. You will then be responsible for ensureing that your proxy or load balancer only allows HTTPS.
216+
The deployed dashboard detects if you are using a secure connection. If you are deploying the dashboard behind a load balancer or front-facing proxy, then the app won't be able to detect that the connection is secure. In this case, you can start the dashboard with the `--trustProxy=1` option (or set the PARSE_DASHBOARD_TRUST_PROXY config var to 1) to rely on the X-Forwarded-* headers for the client's connection security. This is useful for hosting on services like Heroku, where you can trust the provided proxy headers to correctly determine whether you're using HTTP or HTTPS. You can also turn on this setting when using the dashboard as [express](https://github.com/expressjs/express) middleware:
217+
218+
```
219+
var trustProxy = true;
220+
var dashboard = new ParseDashboard({
221+
"apps": [
222+
{
223+
"serverURL": "http://localhost:1337/parse",
224+
"appId": "myAppId",
225+
"masterKey": "myMasterKey",
226+
"appName": "MyApp"
227+
}
228+
],
229+
"trustProxy": 1
230+
});
231+
```
232+
233+
217234

218235
### Configuring Basic Authentication
219236
You can configure your dashboard for Basic Authentication by adding usernames and passwords your `parse-dashboard-config.json` configuration file:

0 commit comments

Comments
 (0)