-
Notifications
You must be signed in to change notification settings - Fork 6
Adding express-ipfilter for ip whitelist support with environment variable IP_WHITELIST #13
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ const SESSION_TIMEOUT_HOURS = process.env.SESSION_TIMEOUT_HOURS || 2; | |
|
|
||
| const express = require('express'), | ||
| enforce = require('express-sslify'), | ||
| ipfilter = require('express-ipfilter').IpFilter, | ||
| path = require('path'), | ||
| bodyParser = require('body-parser'), | ||
| cookieSession = require('cookie-session'), | ||
|
|
@@ -47,6 +48,21 @@ if (process.env.FORCE_HTTPS === "true") { | |
| app.use(enforce.HTTPS({trustProtoHeader: true})); | ||
| } | ||
|
|
||
| if (process.env.IP_WHITELIST) { | ||
| let clientIp = function(req, res) { | ||
| return req.headers['x-forwarded-for'] ? (req.headers['x-forwarded-for']).split(',').pop() : "" | ||
| } | ||
| let whitelist_ips = ['::1', '127.0.0.1'].concat(process.env.IP_WHITELIST.split(',')) | ||
| app.use( | ||
| ipfilter(whitelist_ips, { | ||
| detectIp: clientIp, | ||
| forbidden: 'You are not authorized to access this page.', | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error handling is the only tricky thing with this, and express in general. Right now this message is not displayed, but instead an internal error message is shown alone on a blank page. May need special handling for the IpDeniedError in our normal error handler. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I the Opts object you are passing here does not match the interface given by express-ipfilter: |
||
| filter: whitelist_ips, | ||
| mode: 'allow', | ||
| }) | ||
| ) | ||
| } | ||
|
|
||
| app.set('port', process.env.PORT || 5000); | ||
| app.use(bodyParser.urlencoded({limit: '50mb', extended: false})); | ||
| app.use(bodyParser.json({limit: '50mb'})); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should indeed work for heroku deployment, but can't validate locally well because there is no x-forwarded-for header set. I think this should change to look at the host header if x-forwarded-for is not set. And include 'localhost' in the whitelist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I had assumed this was Heroku only.
I'll update it to be:
if(x-forwarded-for header) return last entry in x-forwarded-for header
else use tcp/ip remote_ip