From d62d2948a166abb31a543cfdd967eb14b68d4ed9 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Tue, 6 Oct 2020 16:17:35 -0400 Subject: [PATCH] fix(isFQDN): allow all-underscore labels with allow_underscores (#1253) --- src/lib/isFQDN.js | 8 ++++---- test/validators.js | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib/isFQDN.js b/src/lib/isFQDN.js index cb1b724f7..2a439dbbe 100644 --- a/src/lib/isFQDN.js +++ b/src/lib/isFQDN.js @@ -33,10 +33,7 @@ export default function isFQDN(str, options) { } for (let part, i = 0; i < parts.length; i++) { part = parts[i]; - if (options.allow_underscores) { - part = part.replace(/_/g, ''); - } - if (!/^[a-z\u00a1-\uffff0-9-]+$/i.test(part)) { + if (!/^[a-z_\u00a1-\uffff0-9-]+$/i.test(part)) { return false; } // disallow full-width chars @@ -46,6 +43,9 @@ export default function isFQDN(str, options) { if (part[0] === '-' || part[part.length - 1] === '-') { return false; } + if (!options.allow_underscores && /_/.test(part)) { + return false; + } } return true; } diff --git a/test/validators.js b/test/validators.js index 853e1068c..93ab9a612 100644 --- a/test/validators.js +++ b/test/validators.js @@ -467,6 +467,7 @@ describe('Validators', () => { 'http://foo_bar.com', 'http://pr.example_com.294.example.com/', 'http://foo__bar.com', + 'http://_.example.com', ], invalid: [], });