fix: Adding protocol_relative_pattern to reject another pattern for invalid URLs

This commit is contained in:
Diogo Castro 2026-06-22 10:10:02 -03:00
parent 0a152764fb
commit f38c2afe91
2 changed files with 4 additions and 1 deletions

View File

@ -101,13 +101,14 @@ class OffsiteMiddleware:
misconfigured domains.
"""
url_pattern = re.compile(r"^https?://.*$")
protocol_relative_pattern = re.compile(r"^//")
port_pattern = re.compile(r":\d+$")
valid_domains: list[str] = []
for domain in domains_list:
if domain is None:
raise ValueError(f"{domains_type} contains empty value.")
if url_pattern.match(domain):
if url_pattern.match(domain) or protocol_relative_pattern.match(domain):
raise ValueError(
f"{domains_type} accepts only domains, not URLs. "
f"Got URL entry {domain} in {domains_type}."

View File

@ -121,6 +121,7 @@ def test_process_request_no_allowed_domains(value):
[
["a.example", None],
["a.example", "http://b.example"],
["a.example", "//c.example"],
["a.example", "c.example:8080"],
],
)
@ -233,6 +234,7 @@ def test_request_scheduled_no_allowed_domains(value):
[
["a.example", None],
["a.example", "http://b.example"],
["a.example", "//c.example"],
["a.example", "c.example:8080"],
],
)