Skip to content

Commit

Permalink
Add support for exact matching of long and short ipv6 notations, add …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
ArtemBaskal committed May 28, 2020
1 parent e72e86c commit dab9fa9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 67 deletions.
91 changes: 44 additions & 47 deletions client/src/__tests__/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,123 +4,120 @@ import { IP_MATCH_LIST_STATUS } from '../helpers/constants';
describe('getIpMatchListStatus', () => {
describe('IPv4', () => {
test('should return EXACT on find the exact ip match', () => {
const list = `
127.0.0.2
const list = `127.0.0.2
2001:db8:11a3:9d7:0:0:0:0
192.168.0.1/8
127.0.0.1
127.0.0.3
`;
127.0.0.3`;
expect(getIpMatchListStatus('127.0.0.1', list))
.toEqual(IP_MATCH_LIST_STATUS.EXACT);
});

test('should return CIDR on find the cidr match', () => {
const list = `
127.0.0.2
const list = `127.0.0.2
2001:db8:11a3:9d7:0:0:0:0
192.168.0.1/8
127.0.0.0/24
127.0.0.3
`;
127.0.0.3`;
expect(getIpMatchListStatus('127.0.0.1', list))
.toEqual(IP_MATCH_LIST_STATUS.CIDR);
});

test('should return NOT_FOUND if the ip is not in the list', () => {
const list = `
127.0.0.1
const list = `127.0.0.1
2001:db8:11a3:9d7:0:0:0:0
192.168.0.1/8
127.0.0.2
127.0.0.3
`;
127.0.0.3`;
expect(getIpMatchListStatus('127.0.0.4', list))
.toEqual(IP_MATCH_LIST_STATUS.NOT_FOUND);
});

test('should return the first EXACT or CIDR match in the list', () => {
const list1 = `
2001:db8:11a3:9d7:0:0:0:0
const list1 = `2001:db8:11a3:9d7:0:0:0:0
127.0.0.1
127.0.0.0/24
127.0.0.3
`;
127.0.0.8/24
127.0.0.3`;
expect(getIpMatchListStatus('127.0.0.1', list1))
.toEqual(IP_MATCH_LIST_STATUS.EXACT);

const list2 = `
2001:db8:11a3:9d7:ffff:ffff:ffff:ffff
2001:0db8:11a3:09d7:0000:0000:0000:0000/64
const list2 = `2001:db8:11a3:9d7:ffff:ffff:ffff:ffff
2001:0db8:11a3:09d7:0000:0000:0000:0000/64
127.0.0.0/24
127.0.0.1
127.0.0.3
`;
127.0.0.8/24
127.0.0.3`;
expect(getIpMatchListStatus('127.0.0.1', list2))
.toEqual(IP_MATCH_LIST_STATUS.CIDR);
});
});

describe('IPv6', () => {
test('should return EXACT on find the exact ip match', () => {
const list = `
127.0.0.0
const list = `127.0.0.0
2001:db8:11a3:9d7:0:0:0:0
2001:db8:11a3:9d7:ffff:ffff:ffff:ffff
127.0.0.1
`;
127.0.0.1`;
expect(getIpMatchListStatus('2001:db8:11a3:9d7:0:0:0:0', list))
.toEqual(IP_MATCH_LIST_STATUS.EXACT);
});

test('should return EXACT on find the exact ip match of short and long notation', () => {
const list = `127.0.0.0
192.168.0.1/8
2001:db8::
127.0.0.2`;
expect(getIpMatchListStatus('2001:db8:0:0:0:0:0:0', list))
.toEqual(IP_MATCH_LIST_STATUS.EXACT);
});

test('should return CIDR on find the cidr match', () => {
const list = `
2001:0db8:11a3:09d7:0000:0000:0000:0000/64
const list1 = `2001:0db8:11a3:09d7:0000:0000:0000:0000/64
127.0.0.1
127.0.0.2
`;
expect(getIpMatchListStatus('2001:db8:11a3:9d7:0:0:0:0', list))
127.0.0.2`;
expect(getIpMatchListStatus('2001:db8:11a3:9d7:0:0:0:0', list1))
.toEqual(IP_MATCH_LIST_STATUS.CIDR);

const list2 = `2001:0db8::/16
127.0.0.0
2001:db8:11a3:9d7:0:0:0:0
2001:db8::
2001:db8:11a3:9d7:ffff:ffff:ffff:ffff
127.0.0.1`;
expect(getIpMatchListStatus('2001:db1::', list2))
.toEqual(IP_MATCH_LIST_STATUS.CIDR);
});

test('should return NOT_FOUND if the ip is not in the list', () => {
const list = `
2001:db8:11a3:9d7:0:0:0:0
const list = `2001:db8:11a3:9d7:0:0:0:0
2001:0db8:11a3:09d7:0000:0000:0000:0000/64
127.0.0.1
127.0.0.2
`;
127.0.0.2`;
expect(getIpMatchListStatus('::', list))
.toEqual(IP_MATCH_LIST_STATUS.NOT_FOUND);
});

test('should return the first EXACT or CIDR match in the list', () => {
const list1 = `
2001:db8:11a3:9d7:0:0:0:0
const list1 = `2001:db8:11a3:9d7:0:0:0:0
2001:0db8:11a3:09d7:0000:0000:0000:0000/64
127.0.0.3
`;
127.0.0.3`;
expect(getIpMatchListStatus('2001:db8:11a3:9d7:0:0:0:0', list1))
.toEqual(IP_MATCH_LIST_STATUS.EXACT);

const list2 = `
2001:0db8:11a3:09d7:0000:0000:0000:0000/64
const list2 = `2001:0db8:11a3:09d7:0000:0000:0000:0000/64
2001:db8:11a3:9d7:0:0:0:0
127.0.0.3
`;
127.0.0.3`;
expect(getIpMatchListStatus('2001:db8:11a3:9d7:0:0:0:0', list2))
.toEqual(IP_MATCH_LIST_STATUS.CIDR);
});
});

describe('Empty list or IP', () => {
test('should return NOT_FOUND on empty ip', () => {
const list = `
127.0.0.0
const list = `127.0.0.0
2001:db8:11a3:9d7:0:0:0:0
2001:db8:11a3:9d7:ffff:ffff:ffff:ffff
127.0.0.1
`;
127.0.0.1`;
expect(getIpMatchListStatus('', list))
.toEqual(IP_MATCH_LIST_STATUS.NOT_FOUND);
});
Expand Down
44 changes: 24 additions & 20 deletions client/src/helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,21 +497,15 @@ export const normalizeMultiline = (multiline) => `${normalizeTextarea(multiline)


/**
* @param ip {string}
* @param cidr {string}
* @param parsedIp {object} ipaddr.js IPv4 or IPv6 object
* @param cidr {array} ipaddr.js CIDR array
* @returns {boolean}
*/
export const isIpMatchCidr = (ip, cidr) => {
export const isIpMatchCidr = (parsedIp, parsedCidr) => {
try {
const [cidrIp] = cidr.split('/');
const cidrIpVersion = ipaddr.parse(cidrIp)
.kind();

const parsedIp = ipaddr.parse(ip);
const cidrIpVersion = parsedCidr[0].kind();
const ipVersion = parsedIp.kind();

const parsedCidr = ipaddr.parseCIDR(cidr);

return ipVersion === cidrIpVersion && parsedIp.match(parsedCidr);
} catch (e) {
return false;
Expand All @@ -528,19 +522,29 @@ export const getIpMatchListStatus = (ip, list) => {
return IP_MATCH_LIST_STATUS.NOT_FOUND;
}

const listArr = list.split('\n');
const listArr = list.trim()
.split('\n');

for (let i = 0; i < listArr.length; i += 1) {
const listItem = listArr[i];
try {
for (let i = 0; i < listArr.length; i += 1) {
const listItem = listArr[i];

if (listItem === ip) {
return IP_MATCH_LIST_STATUS.EXACT;
}
const parsedIp = ipaddr.parse(ip);
const isItemAnIp = ipaddr.isValid(listItem);
const parsedItem = isItemAnIp ? ipaddr.parse(listItem) : ipaddr.parseCIDR(listItem);

if (isItemAnIp && parsedIp.toString() === parsedItem.toString()) {
return IP_MATCH_LIST_STATUS.EXACT;
}

if (listItem.includes('/') && isIpMatchCidr(ip, listItem)) {
return IP_MATCH_LIST_STATUS.CIDR;
if (!isItemAnIp && isIpMatchCidr(parsedIp, parsedItem)) {
return IP_MATCH_LIST_STATUS.CIDR;
}
}
return IP_MATCH_LIST_STATUS.NOT_FOUND;
} catch (e) {
return IP_MATCH_LIST_STATUS.NOT_FOUND;
}

return IP_MATCH_LIST_STATUS.NOT_FOUND;
};

window.ipaddr = ipaddr;

0 comments on commit dab9fa9

Please sign in to comment.