Skip to content

Commit

Permalink
fix(isFQDN): numeric domain names (#1546)
Browse files Browse the repository at this point in the history
  • Loading branch information
tux-tn authored Dec 8, 2020
1 parent dc05855 commit 3358f45
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/lib/isFQDN.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,52 @@ export default function isFQDN(str, options) {
str = str.substring(0, str.length - 1);
}
const parts = str.split('.');
for (let i = 0; i < parts.length; i++) {
if (parts[i].length > 63) {
const tld = parts[parts.length - 1];

if (options.require_tld) {
// disallow fqdns without tld
if (parts.length < 2) {
return false;
}
}
if (options.require_tld) {
const tld = parts.pop();
if (!parts.length || !/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) {

if (!/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) {
return false;
}

// disallow spaces && special characers
if (/[\s\u2002-\u200B\u202F\u205F\u3000\uFEFF\uDB40\uDC20\u00A9\uFFFD]/.test(tld)) {
return false;
}
}
for (let part, i = 0; i < parts.length; i++) {
part = parts[i];
if (!options.allow_numeric_tld && i === parts.length - 1 && /^\d+$/.test(part)) {
return false; // reject numeric TLDs

// reject numeric TLDs
if (!options.allow_numeric_tld && /^\d+$/.test(tld)) {
return false;
}

return parts.every((part) => {
if (part.length > 63) {
return false;
}

if (!/^[a-z_\u00a1-\uffff0-9-]+$/i.test(part)) {
return false;
}

// disallow full-width chars
if (/[\uff01-\uff5e]/.test(part)) {
return false;
}
if (part[0] === '-' || part[part.length - 1] === '-') {

// disallow parts starting or ending with hyphen
if (/^-|-$/.test(part)) {
return false;
}

if (!options.allow_underscores && /_/.test(part)) {
return false;
}
}
return true;

return true;
});
}
3 changes: 3 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('Validators', () => {
`${repeat('a', 31)}@gmail.com`,
'test@gmail.com',
'test.1@gmail.com',
'test@1337.com',
],
invalid: [
'invalidemail@',
Expand Down Expand Up @@ -374,6 +375,7 @@ describe('Validators', () => {
'http://[2010:836B:4179::836B:4179]',
'http://example.com/example.json#/foo/bar',
'http://user:@www.foobar.com',
'http://1337.com',
],
invalid: [
'http://localhost:3000/',
Expand Down Expand Up @@ -894,6 +896,7 @@ describe('Validators', () => {
'foo--bar.com',
'xn--froschgrn-x9a.com',
'rebecca.blackfriday',
'1337.com',
],
invalid: [
'abc',
Expand Down

0 comments on commit 3358f45

Please sign in to comment.