Skip to content

Commit 1cc5c26

Browse files
Validate parsed credential and port component types
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 7206416 commit 1cc5c26

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

hyperbrowser/config.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,18 @@ def normalize_base_url(base_url: str) -> str:
146146
"Failed to parse base_url credentials",
147147
original_error=exc,
148148
) from exc
149+
if parsed_base_url_username is not None and not isinstance(
150+
parsed_base_url_username, str
151+
):
152+
raise HyperbrowserError("base_url parser returned invalid URL components")
153+
if parsed_base_url_password is not None and not isinstance(
154+
parsed_base_url_password, str
155+
):
156+
raise HyperbrowserError("base_url parser returned invalid URL components")
149157
if parsed_base_url_username is not None or parsed_base_url_password is not None:
150158
raise HyperbrowserError("base_url must not include user credentials")
151159
try:
152-
parsed_base_url.port
160+
parsed_base_url_port = parsed_base_url.port
153161
except HyperbrowserError:
154162
raise
155163
except ValueError as exc:
@@ -162,6 +170,11 @@ def normalize_base_url(base_url: str) -> str:
162170
"base_url must contain a valid port number",
163171
original_error=exc,
164172
) from exc
173+
if parsed_base_url_port is not None and (
174+
isinstance(parsed_base_url_port, bool)
175+
or not isinstance(parsed_base_url_port, int)
176+
):
177+
raise HyperbrowserError("base_url parser returned invalid URL components")
165178

166179
decoded_base_path = ClientConfig._decode_url_component_with_limit(
167180
parsed_base_url.path, component_label="base_url path"

tests/test_config.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,81 @@ def port(self) -> int:
509509
ClientConfig.normalize_base_url("https://example.local")
510510

511511

512+
def test_client_config_normalize_base_url_rejects_invalid_username_types(
513+
monkeypatch: pytest.MonkeyPatch,
514+
):
515+
class _ParsedURL:
516+
scheme = "https"
517+
netloc = "example.local"
518+
hostname = "example.local"
519+
query = ""
520+
fragment = ""
521+
username = object()
522+
password = None
523+
path = "/api"
524+
525+
@property
526+
def port(self) -> int:
527+
return 443
528+
529+
monkeypatch.setattr(config_module, "urlparse", lambda _value: _ParsedURL())
530+
531+
with pytest.raises(
532+
HyperbrowserError, match="base_url parser returned invalid URL components"
533+
):
534+
ClientConfig.normalize_base_url("https://example.local")
535+
536+
537+
def test_client_config_normalize_base_url_rejects_invalid_password_types(
538+
monkeypatch: pytest.MonkeyPatch,
539+
):
540+
class _ParsedURL:
541+
scheme = "https"
542+
netloc = "example.local"
543+
hostname = "example.local"
544+
query = ""
545+
fragment = ""
546+
username = None
547+
password = object()
548+
path = "/api"
549+
550+
@property
551+
def port(self) -> int:
552+
return 443
553+
554+
monkeypatch.setattr(config_module, "urlparse", lambda _value: _ParsedURL())
555+
556+
with pytest.raises(
557+
HyperbrowserError, match="base_url parser returned invalid URL components"
558+
):
559+
ClientConfig.normalize_base_url("https://example.local")
560+
561+
562+
def test_client_config_normalize_base_url_rejects_invalid_port_types(
563+
monkeypatch: pytest.MonkeyPatch,
564+
):
565+
class _ParsedURL:
566+
scheme = "https"
567+
netloc = "example.local"
568+
hostname = "example.local"
569+
query = ""
570+
fragment = ""
571+
username = None
572+
password = None
573+
path = "/api"
574+
575+
@property
576+
def port(self):
577+
return "443"
578+
579+
monkeypatch.setattr(config_module, "urlparse", lambda _value: _ParsedURL())
580+
581+
with pytest.raises(
582+
HyperbrowserError, match="base_url parser returned invalid URL components"
583+
):
584+
ClientConfig.normalize_base_url("https://example.local")
585+
586+
512587
def test_client_config_normalize_base_url_wraps_hostname_access_errors(
513588
monkeypatch: pytest.MonkeyPatch,
514589
):

0 commit comments

Comments
 (0)