From: Steven Black Date: Tue, 7 Jul 2026 20:37:02 +0000 (-0700) Subject: Merge pull request #3188 from XhmikosR/xmr/codeql X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=c189d5d3a77325d05bf5844c46e288a5a63e5a8e;p=stevenblack-hosts.git Merge pull request #3188 from XhmikosR/xmr/codeql A couple of CodeQL fixes --- c189d5d3a77325d05bf5844c46e288a5a63e5a8e diff --cc updateHostsFile.py index a29b2a21b,2376bf89a..846d8f5d6 --- a/updateHostsFile.py +++ b/updateHostsFile.py @@@ -1018,37 -1009,7 +1009,34 @@@ def remove_dups_and_excl(mergefile, exc settings["numberofrules"] = numberofrules mergefile.close() - if outputfile is None: - return finalfile - +# Dot-separated labels of [a-z0-9_-], hyphens not at label ends, at least two +# labels. Expects a lowercased hostname. +VALID_DOMAIN_REGEX = re.compile( + r"(?:[a-z0-9_]|[a-z0-9_][a-z0-9_-]*[a-z0-9_])" + r"(?:\.(?:[a-z0-9_]|[a-z0-9_][a-z0-9_-]*[a-z0-9_]))+" +) + + +def is_valid_domain(hostname): + """ + Check whether a lowercased hostname looks like a valid domain name. + + Parameters + ---------- + hostname : str + The hostname to validate. + + Returns + ------- + valid : bool + Whether the hostname only contains characters allowed in a domain + name and has a valid label structure. + """ + + return bool(VALID_DOMAIN_REGEX.fullmatch(hostname)) + + def normalize_rule(rule, targetip, keep_domain_comments): """ Standardize and format the rule string provided.