diff --git a/package.json b/package.json index 0b20bf4..9e38136 100644 --- a/package.json +++ b/package.json @@ -35,34 +35,33 @@ "express-validator": "^6.14.2", "graphql": "^15.8.0", "graphql-tag": "^2.12.6", - "helmet": "^5.1.0", - "ip-cidr": "^3.0.10", + "helmet": "^5.1.1", "jsonwebtoken": "^8.5.1", "moment": "^2.29.4", "mongodb": "^4.7.0", "mongoose": "^6.4.0", - "pino": "^8.3.1", + "pino": "^8.4.0", "pino-pretty": "^8.1.0", - "swagger-ui-express": "^4.4.0", + "swagger-ui-express": "^4.5.0", "switcher-client": "^3.1.1", "validator": "^13.7.0" }, "devDependencies": { - "@babel/cli": "^7.17.10", - "@babel/core": "^7.7.10", - "@babel/node": "^7.7.10", - "@babel/preset-env": "^7.17.10", - "@babel/register": "^7.17.7", + "@babel/cli": "^7.18.10", + "@babel/core": "^7.18.10", + "@babel/node": "^7.18.10", + "@babel/preset-env": "^7.18.10", + "@babel/register": "^7.18.9", "babel-jest": "^28.1.1", "babel-polyfill": "^6.26.0", "env-cmd": "^10.1.0", "eslint": "^8.18.0", - "jest": "^28.1.1", + "jest": "^28.1.3", "jest-sonar-reporter": "^2.0.0", "node-notifier": "^10.0.1", - "nodemon": "^2.0.18", + "nodemon": "^2.0.19", "sinon": "^14.0.0", - "supertest": "^6.2.3" + "supertest": "^6.2.4" }, "repository": { "type": "git", diff --git a/src/helpers/ipcidr.js b/src/helpers/ipcidr.js new file mode 100644 index 0000000..94453d0 --- /dev/null +++ b/src/helpers/ipcidr.js @@ -0,0 +1,17 @@ +class IPCIDR { + constructor(cidr) { + this.cidr = cidr; + } + + ip4ToInt(ip) { + return ip.split('.').reduce((int, oct) => (int << 8) + parseInt(oct, 10), 0) >>> 0; + } + + isIp4InCidr(ip) { + const [range, bits = 32] = this.cidr.split('/'); + const mask = ~(2 ** (32 - Number(bits)) - 1); + return (this.ip4ToInt(ip) & mask) === (this.ip4ToInt(range) & mask); + } +} + +module.exports = IPCIDR; \ No newline at end of file diff --git a/src/models/config-strategy.js b/src/models/config-strategy.js index 4f5c883..6a1daa4 100644 --- a/src/models/config-strategy.js +++ b/src/models/config-strategy.js @@ -2,9 +2,9 @@ import mongoose from 'mongoose'; import History from './history'; import { recordHistory } from './common/index'; import moment from 'moment'; -import IPCIDR from 'ip-cidr'; import { NotFoundError } from '../exceptions'; import { parseJSON, payloadReader } from '../helpers'; +import IPCIDR from '../helpers/ipcidr'; export const StrategiesType = Object.freeze({ NETWORK: 'NETWORK_VALIDATION', @@ -181,7 +181,7 @@ function processNETWORK_Exist(input, values, cidrRegex) { for (const value of values) { if (value.match(cidrRegex)) { const cidr = new IPCIDR(value); - if (cidr.contains(input)) { + if (cidr.isIp4InCidr(input)) { return true; } } else { @@ -192,10 +192,10 @@ function processNETWORK_Exist(input, values, cidrRegex) { } function processNETWORK_NotExist(input, values, cidrRegex) { - const result = values.filter(element => { + const result = values.filter((element) => { if (element.match(cidrRegex)) { const cidr = new IPCIDR(element); - if (cidr.contains(input)) { + if (cidr.isIp4InCidr(input)) { return true; } } else {