Commit 2d8de8ea authored by peterhegman's avatar peterhegman

Move ip address integer check to main validator

It was confirmed that we also want this check for Geo settings
parent a93172ee
...@@ -2,9 +2,7 @@ import validateIpAddress from 'ee/validators/ip_address'; ...@@ -2,9 +2,7 @@ import validateIpAddress from 'ee/validators/ip_address';
import { __, sprintf } from '~/locale'; import { __, sprintf } from '~/locale';
export default address => { export default address => {
// Reject IP addresses that are only integers to match Ruby IPAddr if (!validateIpAddress(address)) {
// https://github.com/whitequark/ipaddr.js/issues/7#issuecomment-158545695
if (/^\d+$/.exec(address) || !validateIpAddress(address)) {
return sprintf(__('%{address} is an invalid IP address range'), { address }, false); return sprintf(__('%{address} is an invalid IP address range'), { address }, false);
} }
......
import ipaddr from 'ipaddr.js'; import ipaddr from 'ipaddr.js';
export default address => { export default address => {
// Reject IP addresses that are only integers to match Ruby IPAddr
// https://github.com/whitequark/ipaddr.js/issues/7#issuecomment-158545695
if (/^\d+$/.exec(address)) {
return false;
}
try { try {
// Checks if Valid IPv4/IPv6 (CIDR) - Throws if not // Checks if Valid IPv4/IPv6 (CIDR) - Throws if not
return Boolean(ipaddr.parseCIDR(address)); return Boolean(ipaddr.parseCIDR(address));
......
...@@ -2,19 +2,6 @@ import * as validateIpAddress from 'ee/validators/ip_address'; ...@@ -2,19 +2,6 @@ import * as validateIpAddress from 'ee/validators/ip_address';
import validateRestrictedIpAddress from 'ee/groups/settings/access_restriction_field/validate_ip_address'; import validateRestrictedIpAddress from 'ee/groups/settings/access_restriction_field/validate_ip_address';
describe('validateRestrictedIpAddress', () => { describe('validateRestrictedIpAddress', () => {
describe('when IP address is only integers', () => {
it.each`
address
${1}
${19}
${192}
`('$address - returns an error message', ({ address }) => {
expect(validateRestrictedIpAddress(address)).toBe(
`${address} is an invalid IP address range`,
);
});
});
describe('when `validateIpAddress` returns false', () => { describe('when `validateIpAddress` returns false', () => {
it('returns an error message', () => { it('returns an error message', () => {
validateIpAddress.default = jest.fn(() => false); validateIpAddress.default = jest.fn(() => false);
......
...@@ -2,6 +2,17 @@ import ipaddr from 'ipaddr.js'; ...@@ -2,6 +2,17 @@ import ipaddr from 'ipaddr.js';
import validateIpAddress from 'ee/validators/ip_address'; import validateIpAddress from 'ee/validators/ip_address';
describe('validateIpAddress', () => { describe('validateIpAddress', () => {
describe('when IP address is only integers', () => {
it.each`
address
${1}
${19}
${192}
`('$address - returns false', ({ address }) => {
expect(validateIpAddress(address)).toBe(false);
});
});
describe('when IP address is in valid CIDR format', () => { describe('when IP address is in valid CIDR format', () => {
it('returns true', () => { it('returns true', () => {
ipaddr.parseCIDR = jest.fn(() => [ ipaddr.parseCIDR = jest.fn(() => [
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment