-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c116cb
commit 4766c45
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/usr/bin/env python3 | ||
import re | ||
import json | ||
import sys | ||
from ipaddress import ip_network | ||
from pathlib import Path | ||
from typing import List, Iterator, Optional, Tuple | ||
|
||
HOSTNAME_RE = re.compile("(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE) | ||
|
||
|
||
class InvalidListValue: | ||
def __init__(self, file_path: Path, value: str, error: Optional[str] = None): | ||
self.file_path = file_path | ||
self.value = value | ||
self.error = error | ||
|
||
def __str__(self): | ||
if self.error: | ||
return "{}: {}".format(self.file_path, self.error) | ||
else: | ||
return "{}: Invalid value '{}'".format(self.file_path, self.value) | ||
|
||
|
||
def is_valid_hostname(hostname: str): | ||
if len(hostname) > 255: | ||
raise ValueError("Hostname {0!r} is too long (maximum is 255 chars)".format(hostname)) | ||
|
||
if hostname[-1] == ".": | ||
hostname = hostname[:-1] # strip exactly one dot from the right, if present | ||
|
||
if hostname[0] == ".": | ||
hostname = hostname[1:] # strip exactly one dot from the left, if present | ||
|
||
if not all(HOSTNAME_RE.match(x) for x in hostname.split(".")): | ||
raise ValueError("Hostname {0!r} contains invalid characters".format(hostname)) | ||
|
||
|
||
def is_valid_regexp(regexp: str): | ||
try: | ||
re.compile(regexp) | ||
except re.error: | ||
raise ValueError("Regexp {0!r} is not valid".format(regexp)) | ||
|
||
|
||
def validate(values: List[str], func) -> Iterator[Tuple[str, ValueError]]: | ||
for value in values: | ||
try: | ||
func(value) | ||
except ValueError as e: | ||
yield value, e | ||
|
||
|
||
def validate_file(p: Path) -> Iterator[InvalidListValue]: | ||
invalid_values = [] | ||
with p.open() as f: | ||
warninglist = json.load(f, encoding="utf-8") | ||
if warninglist["type"] == "cidr": | ||
invalid_values = validate(warninglist["list"], lambda value: ip_network(value, strict=True)) | ||
elif warninglist["type"] == "regexp": | ||
invalid_values = validate(warninglist["list"], lambda value: is_valid_regexp(value)) | ||
# Disabled, because current lists contains invalid domains | ||
# elif warninglist["type"] == "hostname": | ||
# invalid_values = validate_hostnames(warninglist["list"]) | ||
|
||
for (value, exception) in invalid_values: | ||
yield InvalidListValue(p, value, str(exception)) | ||
|
||
|
||
if __name__ == "__main__": | ||
invalid_values = [] | ||
for p in Path('../lists/').glob('*/*.json'): | ||
invalid_values.extend(validate_file(p)) | ||
|
||
if invalid_values: | ||
for invalid_value in invalid_values: | ||
print(invalid_value, file=sys.stderr) | ||
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,3 +42,7 @@ do | |
echo '' | ||
done | ||
|
||
pushd tools | ||
python3 validate_values.py | ||
popd | ||
|