diff --git a/src/lib/isEmail.js b/src/lib/isEmail.js index 1aceca3cf..308f522d4 100644 --- a/src/lib/isEmail.js +++ b/src/lib/isEmail.js @@ -10,6 +10,7 @@ const default_email_options = { allow_underscores: false, require_display_name: false, allow_utf8_local_part: true, + allow_idn: true, require_tld: true, blacklisted_chars: '', ignore_max_length: false, @@ -144,6 +145,7 @@ export default function isEmail(str, options) { require_tld: options.require_tld, ignore_max_length: options.ignore_max_length, allow_underscores: options.allow_underscores, + allow_idn: options.allow_idn, })) { if (!options.allow_ip_domain) { return false; diff --git a/src/lib/isFQDN.js b/src/lib/isFQDN.js index eb6928fda..394b76387 100644 --- a/src/lib/isFQDN.js +++ b/src/lib/isFQDN.js @@ -7,6 +7,7 @@ const default_fqdn_options = { allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false, + allow_idn: true, ignore_max_length: false, }; @@ -71,6 +72,11 @@ export default function isFQDN(str, options) { return false; } + // verify if domain is IDN + if (!options.allow_idn && !/^[a-z0-9-_]+$/i.test(part)) { + return false; + } + return true; }); } diff --git a/test/validators.test.js b/test/validators.test.js index 31a36d029..70ca0c545 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -140,6 +140,29 @@ describe('Validators', () => { }); }); + it('should not validate email addresses with non-ASCII domain names if allow_idn is set to false', () => { + test({ + validator: 'isEmail', + args: [{ allow_idn: false }], + valid: [], + invalid: [ + 'ka25maj@gˇmail.com', + 'i18n@exampلe.com', + 'i18n@EXАМПЛЕ.com', + 'i18n@exамple.com', + 'i18n@éxample.com', + 'i18n@eßample.com', + 'i18n@EXΑΜΠΛΕ.com', + 'i18n@exαmple.com', + 'i18n@exampłe.com', + 'i18n@eซample.com', + 'i18n@例題.com', + 'i18n@例え.com', + 'i18n@사례.com', + ], + }); + }); + it('should validate email addresses with display names', () => { test({ validator: 'isEmail', @@ -828,6 +851,29 @@ describe('Validators', () => { }); }); + it('should not validate URLs with non-ASCII domain names if allow_idn is set to false', () => { + test({ + validator: 'isURL', + args: [{ allow_idn: false }], + valid: [], + invalid: [ + 'gˇmail.com', + 'exampلe.com', + 'EXАМПЛЕ.com', + 'exамple.com', + 'éxample.com', + 'eßample.com', + 'EXΑΜΠΛΕ.com', + 'exαmple.com', + 'exampłe.com', + 'eซample.com', + '例題.com', + '例え.com', + '사례.com', + ], + }); + }); + it('should validate MAC addresses', () => { test({ validator: 'isMACAddress',