Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
User.findById(id, function (err, user) {
if (!err) {
if (user) {
if (user.allowAccess('Admin')) {
// check for IP Range
if (!range_check.in_range(req.connection.remoteAddress, config.adminIPRange)) {
// console.log("IP Address " + req.connection.remoteAddress + " is not within the allowed range(s).")
return next(new restify.NotAuthorizedError("Access restricted."));
}
}
return next({});
} else {
return next(new restify.NotAuthorizedError("Access restricted."));
}
} else {
return next(new restify.NotAuthorizedError("Access restricted."));
}
});
} else {
this.zone = this.zones.have(this.question.name);
if (!this.zone) {
winston.debug('Not our zone', [this.question.name]);
address = this.req._socket._remote.address;
for (aclname in this.cfg.acl) {
acl = this.cfg.acl[aclname];
if (!(acl.ip instanceof Array))
acl.ip = [acl.ip];
for (i in acl.ip) {
ip = acl.ip[i];
if (ip == address || check.in_range(address, ip)) {
found = true;
break;
}
}
if (found)
break;
}
winston.debug('Recursion', [found, acl]);
if (found && acl.recursion) {
next = this.recurse;
} else {
this.forbidden();
return;
}
return function(req,res,next){
var remoteIP = {
ip: req.ip.replace("::ffff:",""), //app.set trust proxy could potentially modify this and cause issues
v: "ip"+range_check.ver(req.ip.replace("::ffff:",""))
};
req.cf_ip = remoteIP.ip;//override this if cloudflare present
if (req.headers['cf-connecting-ip'] == undefined){
return next(); //no cloudflare IP, continue on like this never happened. Shhhh!
}
if (range_check.in_range(remoteIP.ip, ipRanges[remoteIP.v])){
req.cf_ip = req.headers['cf-connecting-ip'];
}
next();
};
};
{
return false;
}
else
{
var range_check = require('range_check');
if (range_check.vaild_ip(ip_address))
{
var ip_ver = range_check.ver(ip_address);
if (ip_ver === 4)
{
return range_check.in_range(ip_address, ranges.v4);
}
else if (ip_ver === 6)
{
return range_check.in_range(ip_address, ranges.v6);
}
else
{
return false;
}
}
else
{
return false;
}
}
}
function check(respond, route, unpm, done) {
var ip = respond.req.connection.remoteAddress
var allowed = cidrCheck.in_range(ip, unpm.config.cidr)
if(allowed) {
return done()
}
unpm.log.info({ip: ip, message: 'IP out of CIDR range'})
respond.res.writeHead(403, {
'Content-Type': 'application/json'
})
respond.res.end(JSON.stringify({
error: 'forbidden',
reason: 'invalid ip'
}))
}
function check(req)
{
var ip_address = (req.connection.remoteAddress ? req.connection.remoteAddress : req.remoteAddress);
if (typeof req.headers['cf-connecting-ip'] === 'undefined')
{
return false;
}
else
{
var range_check = require('range_check');
if (range_check.vaild_ip(ip_address))
{
var ip_ver = range_check.ver(ip_address);
if (ip_ver === 4)
{
return range_check.in_range(ip_address, ranges.v4);
}
else if (ip_ver === 6)
{
return range_check.in_range(ip_address, ranges.v6);
}
else
{
return false;
}
}
else
{
return false;
}
}
}