Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-ipfilter

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-ipfilter - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

lib/deniedError.js

20

example/app.js

@@ -12,3 +12,4 @@ 'use strict';

var users = require('./routes/users');
var ipfilter = require('express-ipfilter');
var ipfilter = require('express-ipfilter').IpFilter;
var IpDeniedError = require('express-ipfilter').IpDeniedError;

@@ -30,3 +31,3 @@ var app = express();

var ips = ['::ffff:127.0.0.1'];
app.use(ipfilter(ips, { errorMessage: { message: 'You are not allowed' }}));
app.use(ipfilter(ips, {}));

@@ -48,6 +49,12 @@ app.use('/', routes);

if (app.get('env') === 'development') {
app.use(function(err, req, res) {
res.status(err.status || 500);
app.use(function(err, req, res, _next) {
console.log('Error handler', err);
if(err instanceof IpDeniedError){
res.status(401);
}else{
res.status(err.status || 500);
}
res.render('error', {
message: err.message,
message: 'You shall not pass',
error: err

@@ -60,3 +67,4 @@ });

// no stacktraces leaked to user
app.use(function(err, req, res) {
app.use(function(err, req, res, _next) {
console.log('Error handler', err);
res.status(err.status || 500);

@@ -63,0 +71,0 @@ res.render('error', {

@@ -34,3 +34,4 @@ module.exports = function(grunt){

files: {
'lib/ipfilter.js': 'src/ipfilter.js'
'lib/ipfilter.js': 'src/ipfilter.js',
'lib/deniedError.js': 'src/deniedError.js'
}

@@ -37,0 +38,0 @@ }

@@ -1,1 +0,4 @@

module.exports = require('./lib/ipfilter');
module.exports = {
IpFilter: require('./lib/ipfilter'),
IpDeniedError: require('./lib/deniedError')
};

@@ -18,2 +18,3 @@ /*!

var rangeCheck = require('range_check');
var IpDeniedError = require('./deniedError');

@@ -36,4 +37,6 @@ /**

* - `log` console log actions. Defaults to true.
* - `errorCode` the HTTP status code to use when denying access. Defaults to 401.
* - `errorMessage` the error message to use when denying access. Defaults to 'Unauthorized'.
- `allowPrivateIPs` whether to allow private IPs.
- `allowForward` whether to allow forwarded IPs.
- `allowCloudflare` whether to allow CloudFlare forwarded headers.
- 'allowCodio' whether to allow Codio forwarded headers.
* - 'excluding' routes that should be excluded from ip filtering

@@ -55,8 +58,4 @@ *

logF: logger,
errorCode: 401,
errorMessage: 'Unauthorized',
allowedHeaders: [],
allowPrivateIPs: false,
allowForward: false,
allowCloudflare: false,
allowCodio: false,
excluding: []

@@ -68,25 +67,16 @@ });

var forwardedIpsStr = '';
var headerIp = _.reduce(settings.allowedHeaders, function (acc, header) {
var testIp = req.headers[header];
if (testIp != '') {
acc = testIp;
}
if (settings.allowForward) {
forwardedIpsStr = req.headers['x-forwarded-for'];
}
return acc;
}, '');
//Allow getting cloudflare connecting client IP
var cloudFlareConnectingIp = '';
if (settings.allowCloudflare) {
cloudFlareConnectingIp = req.headers['cf-connecting-ip'];
if (headerIp) {
var splitHeaderIp = headerIp.split(',');
ipAddress = splitHeaderIp[0];
}
//Allow getting codio connecting client IP
var codioConnectingIp = '';
if (settings.allowCodio) {
codioConnectingIp = req.headers['x-real-ip'];
}
if (forwardedIpsStr) {
var forwardedIps = forwardedIpsStr.split(',');
ipAddress = forwardedIps[0];
}
if (!ipAddress) {

@@ -96,10 +86,2 @@ ipAddress = req.connection.remoteAddress;

if (cloudFlareConnectingIp) {
ipAddress = cloudFlareConnectingIp;
}
if (codioConnectingIp) {
ipAddress = codioConnectingIp;
}
if (!ipAddress) {

@@ -216,6 +198,6 @@ return '';

res.statusCode = settings.errorCode;
return res.send(settings.errorMessage);
var err = new IpDeniedError('Access denied to IP address: ' + ip);
return next(err);
};
};
//# sourceMappingURL=ipfilter.js.map
{
"name": "express-ipfilter",
"description": "A light-weight IP address based filtering system",
"version": "0.1.1",
"version": "0.2.0",
"author": "BaM Interactive",

@@ -6,0 +6,0 @@ "dependencies": {

@@ -9,3 +9,3 @@ express-ipfilter: A light-weight IP address based filtering system

## Version
0.1.0
0.2.0

@@ -20,3 +20,3 @@ ## Installation

> NOTE: Starting with version 0.1.0, allow forwarded IP addresses through headers (forward, Cloudflare, Codio) are disabled by **default**. You must explicitly enable these features with the new settings.
> NOTE: Starting with version 0.1.0, allow forwarded IP addresses through headers (forward, Cloudflare, Codio) are disabled by **default**. You must explicitly enable them by adding them to the `allowedHeaders` list.

@@ -28,3 +28,3 @@ Blacklisting certain IP addresses, while allowing all other IPs:

var express = require('express')
, ipfilter = require('express-ipfilter')
, ipfilter = require('express-ipfilter').IpFilter
, app = express.createServer()

@@ -46,3 +46,3 @@ ;

var express = require('express')
, ipfilter = require('express-ipfilter')
, ipfilter = require('express-ipfilter').IpFilter
, app = express.createServer()

@@ -79,2 +79,4 @@ ;

> See the example app for an example of how to handle errors.
## Options

@@ -86,7 +88,3 @@

| log | console log actions | boolean|true|
| errorCode | the HTTP status code to use when denying access | number|401|
| errorMessage | the error message to use when denying access | string|Unauthorized|
| allowForward | Enable or disable forwarded ip address through the request heards | boolean | false |
| allowCloudflare | Enable or disable CloudFlare forwarded ip address through the request heards | boolean | false |
| allowCodio | Enable or disable Codio forwarded ip address through the request heards | boolean | false |
| allowedHeaders | an array of strings for header names that are acceptable for retrieving an IP address | array | [] |
| excluding | routes that should be excluded from ip filtering | array|[]|

@@ -112,2 +110,8 @@

0.2.0
* Changed how error handling works
* Removed settings for specific vendor ip addresses and added `allowedHeaders` to support those header-based IP addresses.
* You must now specifically require `IpFilter`, i.e. `var ipfilter = require('express-ipfilter').IpFilter;`
* If you want to handle errors you must require the error type as well `var IpDeniedError = require('express-ipfilter').IpDeniedError;`
0.1.1

@@ -114,0 +118,0 @@ * Added a favicon to the example to supress the 404 error looking for it.

@@ -15,2 +15,3 @@ /*!

var rangeCheck = require('range_check');
var IpDeniedError = require('./deniedError');

@@ -33,4 +34,6 @@ /**

* - `log` console log actions. Defaults to true.
* - `errorCode` the HTTP status code to use when denying access. Defaults to 401.
* - `errorMessage` the error message to use when denying access. Defaults to 'Unauthorized'.
- `allowPrivateIPs` whether to allow private IPs.
- `allowForward` whether to allow forwarded IPs.
- `allowCloudflare` whether to allow CloudFlare forwarded headers.
- 'allowCodio' whether to allow Codio forwarded headers.
* - 'excluding' routes that should be excluded from ip filtering

@@ -50,8 +53,4 @@ *

logF: logger,
errorCode: 401,
errorMessage: 'Unauthorized',
allowedHeaders: [],
allowPrivateIPs: false,
allowForward: false,
allowCloudflare: false,
allowCodio: false,
excluding: []

@@ -63,37 +62,20 @@ });

var forwardedIpsStr = '';
var headerIp = _.reduce(settings.allowedHeaders, function(acc, header){
var testIp = req.headers[header];
if(testIp!= ''){
acc = testIp;
}
if(settings.allowForward){
forwardedIpsStr = req.headers['x-forwarded-for'];
}
return acc;
},'');
//Allow getting cloudflare connecting client IP
var cloudFlareConnectingIp = '';
if(settings.allowCloudflare){
cloudFlareConnectingIp = req.headers['cf-connecting-ip'];
if(headerIp) {
var splitHeaderIp = headerIp.split(',');
ipAddress = splitHeaderIp[0];
}
//Allow getting codio connecting client IP
var codioConnectingIp = '';
if(settings.allowCodio){
codioConnectingIp = req.headers['x-real-ip'];
}
if (forwardedIpsStr) {
var forwardedIps = forwardedIpsStr.split(',');
ipAddress = forwardedIps[0];
}
if (!ipAddress) {
if(!ipAddress) {
ipAddress = req.connection.remoteAddress;
}
if(cloudFlareConnectingIp){
ipAddress=cloudFlareConnectingIp;
}
if(codioConnectingIp){
ipAddress=codioConnectingIp;
}
if(!ipAddress){

@@ -208,5 +190,5 @@ return '';

res.statusCode = settings.errorCode;
return res.send(settings.errorMessage);
var err = new IpDeniedError('Access denied to IP address: ' + ip);
return next(err);
};
};

@@ -5,3 +5,4 @@ /* global describe, it, beforeEach */

var ipfilter = require('../index');
var ipfilter = require('../index').IpFilter;
var IpDeniedError = require('../index').IpDeniedError;
var assert = require('assert');

@@ -12,3 +13,3 @@

beforeEach(function () {
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, allowForward: true });
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, allowedHeaders: ['x-forwarded-for'] });
this.req = {

@@ -46,22 +47,9 @@ session: {},

this.req.connection.remoteAddress = '127.0.0.1';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});
it('should deny all blacklisted forwarded ips', function (done) {
this.req.headers['x-forwarded-for'] = '127.0.0.1';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -73,3 +61,3 @@ });

beforeEach(function () {
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, mode: 'allow', allowForward: true });
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, mode: 'allow', allowedHeaders: ['x-forwarded-for'] });
this.req = {

@@ -107,10 +95,3 @@ session: {},

this.req.connection.remoteAddress = '127.0.0.2';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -120,10 +101,3 @@

this.req.headers['x-forwarded-for'] = '127.0.0.2';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -136,3 +110,3 @@ });

// Ip range: 127.0.0.1 - 127.0.0.14
this.ipfilter = ipfilter(['127.0.0.1/28'], { log: false, mode: 'allow', allowForward: true });
this.ipfilter = ipfilter(['127.0.0.1/28'], { log: false, mode: 'allow', allowedHeaders: ['x-forwarded-for'] });
this.req = {

@@ -163,10 +137,3 @@ session: {},

this.req.connection.remoteAddress = '127.0.0.17';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -176,10 +143,3 @@

this.req.headers['x-forwarded-for'] = '127.0.0.17';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -191,3 +151,3 @@ });

beforeEach(function () {
this.ipfilter = ipfilter(['127.0.0.1/28'], { log: false, allowForward: true });
this.ipfilter = ipfilter(['127.0.0.1/28'], { log: false, allowedHeaders: ['x-forwarded-for'] });
this.req = {

@@ -218,10 +178,3 @@ session: {},

this.req.connection.remoteAddress = '127.0.0.1';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -231,10 +184,3 @@

this.req.headers['x-forwarded-for'] = '127.0.0.1';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -268,3 +214,3 @@ });

// Ip range: 127.0.0.1 - 127.0.0.14
this.ipfilter = ipfilter([['127.0.0.1', '127.0.0.3']], { log: false, mode: 'allow', allowForward: true });
this.ipfilter = ipfilter([['127.0.0.1', '127.0.0.3']], { log: false, mode: 'allow', allowedHeaders: ['x-forwarded-for'] });
this.req = {

@@ -302,10 +248,3 @@ session: {},

this.req.connection.remoteAddress = '127.0.0.17';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -315,10 +254,3 @@

this.req.headers['x-forwarded-for'] = '127.0.0.17';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -349,10 +281,3 @@ });

this.req.connection.remoteAddress = '127.0.0.17';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -364,3 +289,3 @@ });

beforeEach(function () {
this.ipfilter = ipfilter([['127.0.0.1', '127.0.0.3']], { log: false, allowForward: true });
this.ipfilter = ipfilter([['127.0.0.1', '127.0.0.3']], { log: false, allowedHeaders: ['x-forwarded-for'] });
this.req = {

@@ -391,10 +316,3 @@ session: {},

this.req.connection.remoteAddress = '127.0.0.1';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -404,10 +322,3 @@

this.req.headers['x-forwarded-for'] = '127.0.0.1';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -440,3 +351,3 @@ });

beforeEach(function () {
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, allowForward: false });
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, allowedHeaders: [] });
this.req = {

@@ -454,10 +365,3 @@ session: {},

this.req.headers['x-forwarded-for'] = '127.0.0.2';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -469,3 +373,3 @@ });

beforeEach(function () {
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, allowCloudflare: true });
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, allowedHeaders: ['cf-connecting-ip'] });
this.req = {

@@ -492,3 +396,3 @@ session: {},

beforeEach(function () {
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, allowCloudflare: false });
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, allowedHeaders: [] });
this.req = {

@@ -506,10 +410,3 @@ session: {},

this.req.headers['cf-connecting-ip'] = '127.0.0.2';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -541,10 +438,3 @@ });

this.req.connection.remoteAddress = '190.0.0.0';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -568,10 +458,3 @@ });

this.req.connection.remoteAddress = '';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -592,15 +475,12 @@ });

connection: {
remoteAddress: ''
remoteAddress: '127.0.0.1'
}
};
this.req.connection.remoteAddress = '127.0.0.1';
var res = {
send: function send() {
assert.equal(1, messages.length);
done();
}
var next = function(){
assert.equal(1, messages.length);
done();
};
this.ipfilter(this.req, res, function () {});
this.ipfilter(this.req, function(){}, next);
});

@@ -623,10 +503,10 @@

this.req.connection.remoteAddress = '127.0.0.1';
var res = {
send: function send() {
assert.equal('Access denied to IP address: 127.0.0.1', messages[0]);
done();
}
var next = function(){
assert.equal('Access denied to IP address: 127.0.0.1', messages[0]);
done();
};
this.ipfilter(this.req, res, function () {});
this.ipfilter(this.req, function(){}, next);
});

@@ -650,10 +530,3 @@ });

this.req.connection.remoteAddress = '127.0.0.1';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -732,10 +605,3 @@ });

this.req.connection.remoteAddress = '127.0.0.1';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -745,10 +611,3 @@

this.req.connection.remoteAddress = '192.168.1.15';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -758,10 +617,3 @@

this.req.connection.remoteAddress = '127.0.0.15';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -775,3 +627,3 @@ });

beforeEach(function () {
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, allowCodio: true });
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, allowedHeaders: ['x-real-ip'] });
this.req = {

@@ -795,10 +647,3 @@ session: {},

this.req.headers['x-real-ip'] = '127.0.0.1';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -809,3 +654,3 @@ });

beforeEach(function () {
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, mode: 'allow', allowCodio: true });
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, mode: 'allow', allowedHeaders: ['x-real-ip'] });
this.req = {

@@ -828,10 +673,3 @@ session: {},

this.req.headers['x-real-ip'] = '127.0.0.2';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -842,3 +680,3 @@ });

beforeEach(function () {
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, mode: 'allow', allowCodio: false });
this.ipfilter = ipfilter(['127.0.0.1'], { log: false, mode: 'allow', allowedHeaders: ['x-real-ip'] });
this.req = {

@@ -930,10 +768,3 @@ session: {},

this.req.connection.remoteAddress = '127.0.0.1';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -943,10 +774,3 @@

this.req.connection.remoteAddress = '192.168.1.15';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -956,10 +780,3 @@

this.req.connection.remoteAddress = '2001:4860:8006::62';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -969,10 +786,3 @@

this.req.connection.remoteAddress = '2001:4860:8007:0::62';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});

@@ -982,13 +792,15 @@

this.req.connection.remoteAddress = '127.0.0.15';
var res = {
send: function send() {
assert.equal(401, res.statusCode);
done();
}
};
this.ipfilter(this.req, res, function () {});
checkError(this.ipfilter, this.req, done);
});
});
});
function checkError(ipfilter, req, done){
var next = function next(err) {
assert (err instanceof IpDeniedError);
done();
};
ipfilter(req, function(){}, next);
}
//# sourceMappingURL=ipfilter.spec.js.map

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc