Repository: validatorjs/validator.js
Pull request #1469 (2020-10-06 20:21:31)

fix(isFQDN): allow all-underscore labels with allow_underscores (#1253)
Update `isFQDN` to allow domain names with all-underscore labels when `allow_underscores` is true. Underscore is not allowed in LDH "preferred name syntax" host names, but may appear anywhere in generic domain name labels (cf. [RFC 8499](https://tools.ietf.org/html/rfc8499#page-8)).

## Checklist

- [x] PR contains only changes related; no stray files, etc.
- [ ] ~~README updated (where applicable)~~
- [x] Tests written (where applicable)


codecov[bot]: # [Codecov](https://codecov.io/gh/validatorjs/validator.js/pull/1469?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`master@279e771`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/validatorjs/validator.js/pull/1469/graphs/tree.svg?width=650&height=150&src=pr&token=YzR3kbN1m2)](https://codecov.io/gh/validatorjs/validator.js/pull/1469?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##             master    #1469   +/-   ##
=========================================
  Coverage          ?   99.92%           
=========================================
  Files             ?       96           
  Lines             ?     1275           
  Branches          ?        0           
=========================================
  Hits              ?     1274           
  Misses            ?        1           
  Partials          ?        0           
```


| [Impacted Files](https://codecov.io/gh/validatorjs/validator.js/pull/1469?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/lib/isFQDN.js](https://codecov.io/gh/validatorjs/validator.js/pull/1469/diff?src=pr&el=tree#diff-c3JjL2xpYi9pc0ZRRE4uanM=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/validatorjs/validator.js/pull/1469?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/validatorjs/validator.js/pull/1469?src=pr&el=footer). Last update [279e771...d62d294](https://codecov.io/gh/validatorjs/validator.js/pull/1469?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

gibson042: Done.

Patch:
diff --git a/src/lib/isFQDN.js b/src/lib/isFQDN.js
index c731bed..e58c750 100644
--- a/src/lib/isFQDN.js
+++ b/src/lib/isFQDN.js
@@ -37,10 +37,7 @@ export default function isFQDN(str, options) {
     if (!options.allow_numeric_tld && i === parts.length - 1 && /^\d+$/.test(part)) {
       return false; // reject numeric TLDs
     }
-    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
@@ -50,6 +47,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;
 }


Test patch:
diff --git a/test/validators.js b/test/validators.js
index 2c89d8d..01bed06 100644
--- a/test/validators.js
+++ b/test/validators.js
@@ -481,6 +481,7 @@ describe('Validators', () => {
         'http://foo_bar.com',
         'http://pr.example_com.294.example.com/',
         'http://foo__bar.com',
+        'http://_.example.com',
       ],
       invalid: [],
     });