Skip to content

Commit

Permalink
Fix #13, do not test custom TLD
Browse files Browse the repository at this point in the history
  • Loading branch information
ameshkov committed Jan 30, 2024
1 parent 5dc37a7 commit ac4625f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"consola": "^3.2.3",
"glob": "^10.3.10",
"node-fetch": "^2.7.0",
"tldts": "^6.1.4",
"yargs": "^17.7.2"
},
"devDependencies": {
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 20 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const tldts = require('tldts');

/**
* Helper function that takes an array and returns a new one without any
* duplicate items.
Expand All @@ -17,25 +19,35 @@ function unique(arr) {
* @returns {boolean} Returns true if the domain is valid, false otherwise.
*/
function validDomain(domain) {
if (domain.endsWith('.onion')) {
// Note, that we don't count .onion domains valid as their existence
// cannot be verified with the urlfilter service.
if (domain.startsWith('/') && domain.endsWith('/')) {
// Ignore regular expressions.
return false;
}

const result = tldts.parse(`https://${domain}/`);

if (result === null || result.domain === null) {
return false;
}

if (result.isIp) {
// IP addresses cannot be verified too so just ignore them too.
return false;
}

if (/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(domain)) {
// IPv4 addresses cannot be verified too so just ignore them too.
// Don't check for IPv6 as they won't be matched by the domain regex.
if (result.isPrivate) {
// Do not check custom hostnames. It could be an .onion domain or
// a different custom TLD which we simply cannot test for existence.
return false;
}

if (!domain.includes('.')) {
if (result.domainWithoutSuffix === '') {
// Ignore top-level domains to avoid false positives on things like:
// https://github.com/AdguardTeam/DeadDomainsLinter/issues/6
return false;
}

return domain.match(/^[a-z0-9.-]+$/i) !== null;
return true;
}

module.exports = {
Expand Down

0 comments on commit ac4625f

Please sign in to comment.