Commit 900c90f0 authored by peterhegman's avatar peterhegman

Add frontend validation for group IP restriction field

Add frontend validation that matches the backend validation to improve
UX
parent f37a26db
import ipaddr from 'ipaddr.js'; import validateIpAddress from 'ee/validators/ip_address';
import { s__ } from '~/locale'; import { s__ } from '~/locale';
const validateAddress = address => {
try {
// Checks if Valid IPv4/IPv6 (CIDR) - Throws if not
return Boolean(ipaddr.parseCIDR(address));
} catch (e) {
// Checks if Valid IPv4/IPv6 (Non-CIDR) - Does not Throw
return ipaddr.isValid(address);
}
};
const validateIP = data => { const validateIP = data => {
let addresses = data.replace(/\s/g, '').split(','); let addresses = data.replace(/\s/g, '').split(',');
addresses = addresses.map(address => validateAddress(address)); addresses = addresses.map(address => validateIpAddress(address));
return !addresses.some(a => !a); return !addresses.some(a => !a);
}; };
......
import validateIpAddress from 'ee/validators/ip_address';
import { escape } from 'lodash';
import { __, sprintf } from '~/locale';
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) || !validateIpAddress(address)) {
return sprintf(
__('%{address} is an invalid IP address range'),
{ address: escape(address) },
false,
);
}
return '';
};
...@@ -30,6 +30,11 @@ export default { ...@@ -30,6 +30,11 @@ export default {
required: false, required: false,
default: () => [], default: () => [],
}, },
customValidator: {
type: Function,
required: false,
default: () => '',
},
errorMessage: { errorMessage: {
type: String, type: String,
required: false, required: false,
...@@ -61,7 +66,7 @@ export default { ...@@ -61,7 +66,7 @@ export default {
return this.disallowedValueErrorMessage; return this.disallowedValueErrorMessage;
} }
return ''; return this.customValidator(this.textInputValue);
}, },
}, },
watch: { watch: {
......
import '~/pages/groups/edit'; import '~/pages/groups/edit';
import initAccessRestrictionField from 'ee/groups/settings/access_restriction_field'; import initAccessRestrictionField from 'ee/groups/settings/access_restriction_field';
import validateRestrictedIpAddress from 'ee/groups/settings/access_restriction_field/validate_ip_address';
import { __ } from '~/locale'; import { __ } from '~/locale';
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
...@@ -10,7 +11,7 @@ document.addEventListener('DOMContentLoaded', () => { ...@@ -10,7 +11,7 @@ document.addEventListener('DOMContentLoaded', () => {
}); });
initAccessRestrictionField( initAccessRestrictionField(
'.js-ip-restriction', '.js-ip-restriction',
{ placeholder: __('Enter IP address range') }, { placeholder: __('Enter IP address range'), customValidator: validateRestrictedIpAddress },
'ip_restriction_field', 'ip_restriction_field',
); );
}); });
import ipaddr from 'ipaddr.js';
export default address => {
try {
// Checks if Valid IPv4/IPv6 (CIDR) - Throws if not
return Boolean(ipaddr.parseCIDR(address));
} catch (e) {
// Checks if Valid IPv4/IPv6 (Non-CIDR) - Does not Throw
return ipaddr.isValid(address);
}
};
---
title: Add frontend validation to "Restrict access by IP address" field
merge_request: 39061
author:
type: changed
import * as validateIpAddress from 'ee/validators/ip_address';
import validateRestrictedIpAddress from 'ee/groups/settings/access_restriction_field/validate_ip_address';
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', () => {
it('returns an error message', () => {
validateIpAddress.default = jest.fn(() => false);
expect(validateRestrictedIpAddress('foo bar')).toBe(`foo bar is an invalid IP address range`);
});
});
describe('when IP address is valid', () => {
it('returns an empty string', () => {
validateIpAddress.default = jest.fn(() => true);
expect(validateRestrictedIpAddress('192.168.0.0/24')).toBe('');
});
});
});
...@@ -225,29 +225,40 @@ describe('CommaSeparatedListTokenSelector', () => { ...@@ -225,29 +225,40 @@ describe('CommaSeparatedListTokenSelector', () => {
tokenSelectorTriggerEnter(); tokenSelectorTriggerEnter();
expect( expect(findTokenSelectorDropdown().text()).toBe('Add "foo"');
findTokenSelector()
.find('[role="menuitem"]')
.text(),
).toBe('Add "foo"');
}); });
}); });
describe('when `regexValidator` and `disallowedValues` props are not set', () => { describe('when `regexValidator` and `disallowedValues` props are not set', () => {
it('allows any value to be added', async () => { describe('when `customValidator` prop is not set', () => {
createComponent({ it('allows any value to be added', async () => {
scopedSlots: { createComponent({
'user-defined-token-content': '<span>Add "{{props.inputText}}"</span>', scopedSlots: {
}, 'user-defined-token-content': '<span>Add "{{props.inputText}}"</span>',
},
});
await setTokenSelectorInputValue('foo');
expect(findTokenSelectorDropdown().text()).toBe('Add "foo"');
}); });
});
await setTokenSelectorInputValue('foo'); describe('when `customValidator` prop is set', () => {
it('displays error message that is returned by `customValidator`', () => {
createComponent({
propsData: {
customValidator: () => 'Value is invalid',
},
scopedSlots: {
'user-defined-token-content': '<span>Add "{{props.inputText}}"</span>',
},
});
expect( tokenSelectorTriggerEnter();
findTokenSelector()
.find('[role="menuitem"]') expect(findErrorMessageText()).toBe('Value is invalid');
.text(), });
).toBe('Add "foo"');
}); });
}); });
......
import ipaddr from 'ipaddr.js';
import validateIpAddress from 'ee/validators/ip_address';
describe('validateIpAddress', () => {
describe('when IP address is in valid CIDR format', () => {
it('returns true', () => {
ipaddr.parseCIDR = jest.fn(() => [
{
octets: [192, 168, 0, 0],
},
24,
]);
expect(validateIpAddress('192.168.0.0/24')).toBe(true);
});
});
describe('when IP address is not in valid CIDR format', () => {
it('calls `ipaddr.isValid`', () => {
ipaddr.parseCIDR = jest.fn(() => {
throw new Error();
});
ipaddr.isValid = jest.fn();
validateIpAddress('192.168.0.0');
expect(ipaddr.isValid).toHaveBeenCalledWith('192.168.0.0');
});
});
});
...@@ -297,6 +297,9 @@ msgstr[1] "" ...@@ -297,6 +297,9 @@ msgstr[1] ""
msgid "%{actionText} & %{openOrClose} %{noteable}" msgid "%{actionText} & %{openOrClose} %{noteable}"
msgstr "" msgstr ""
msgid "%{address} is an invalid IP address range"
msgstr ""
msgid "%{author_link} wrote:" msgid "%{author_link} wrote:"
msgstr "" msgstr ""
......
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