Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate netloc with port number #104

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions courlan/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@
}

# https://github.com/python-validators/validators/blob/master/src/validators/domain.py
VALID_DOMAIN = re.compile(
VALID_DOMAIN_PORT = re.compile(
# First character of the domain
r"^(?:[a-zA-Z0-9]"
# Sub domain + hostname
+ r"(?:[a-zA-Z0-9-_]{0,61}[A-Za-z0-9])?\.)"
# First 61 characters of the gTLD
+ r"+[A-Za-z0-9][A-Za-z0-9-_]{0,61}"
# Last character of the gTLD
+ r"[A-Za-z]$",
+ r"[A-Za-z]"
# Port number
+ r"(\:(6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|"
+ r"6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{0,3}))?$",
re.IGNORECASE,
)

Expand Down Expand Up @@ -151,9 +154,9 @@ def domain_filter(domain: str) -> bool:
return True

# malformed domains
if not VALID_DOMAIN.match(domain):
if not VALID_DOMAIN_PORT.match(domain):
try:
if not VALID_DOMAIN.match(domain.encode("idna").decode("utf-8")):
if not VALID_DOMAIN_PORT.match(domain.encode("idna").decode("utf-8")):
return False
except UnicodeError:
return False
Expand Down
4 changes: 4 additions & 0 deletions tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,10 @@ def test_urlcheck():
assert check_url("http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]") is None
assert check_url("http://1:2:3:4:5:6:7:8:9") is None

# port
assert check_url("http://example.com:80") is not None
assert check_url("http://example.com:80:80") is None


def test_domain_filter():
"Test filters related to domain and hostnames."
Expand Down
Loading