Skip to content

Commit 7947228

Browse files
Enforce exact string types for parsed URL components
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 7951831 commit 7947228

2 files changed

Lines changed: 42 additions & 11 deletions

File tree

hyperbrowser/config.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ def normalize_base_url(base_url: str) -> str:
114114
original_error=exc,
115115
) from exc
116116
if (
117-
not isinstance(parsed_base_url_scheme, str)
118-
or not isinstance(parsed_base_url_netloc, str)
119-
or not isinstance(parsed_base_url_path, str)
120-
or not isinstance(parsed_base_url_query, str)
121-
or not isinstance(parsed_base_url_fragment, str)
117+
type(parsed_base_url_scheme) is not str
118+
or type(parsed_base_url_netloc) is not str
119+
or type(parsed_base_url_path) is not str
120+
or type(parsed_base_url_query) is not str
121+
or type(parsed_base_url_fragment) is not str
122122
):
123123
raise HyperbrowserError("base_url parser returned invalid URL components")
124124
try:
@@ -130,8 +130,9 @@ def normalize_base_url(base_url: str) -> str:
130130
"Failed to parse base_url host",
131131
original_error=exc,
132132
) from exc
133-
if parsed_base_url_hostname is not None and not isinstance(
134-
parsed_base_url_hostname, str
133+
if (
134+
parsed_base_url_hostname is not None
135+
and type(parsed_base_url_hostname) is not str
135136
):
136137
raise HyperbrowserError("base_url parser returned invalid URL components")
137138
if (
@@ -159,12 +160,14 @@ def normalize_base_url(base_url: str) -> str:
159160
"Failed to parse base_url credentials",
160161
original_error=exc,
161162
) from exc
162-
if parsed_base_url_username is not None and not isinstance(
163-
parsed_base_url_username, str
163+
if (
164+
parsed_base_url_username is not None
165+
and type(parsed_base_url_username) is not str
164166
):
165167
raise HyperbrowserError("base_url parser returned invalid URL components")
166-
if parsed_base_url_password is not None and not isinstance(
167-
parsed_base_url_password, str
168+
if (
169+
parsed_base_url_password is not None
170+
and type(parsed_base_url_password) is not str
168171
):
169172
raise HyperbrowserError("base_url parser returned invalid URL components")
170173
if parsed_base_url_username is not None or parsed_base_url_password is not None:

tests/test_config.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,34 @@ def port(self) -> int:
544544
ClientConfig.normalize_base_url("https://example.local")
545545

546546

547+
def test_client_config_normalize_base_url_rejects_string_subclass_components(
548+
monkeypatch: pytest.MonkeyPatch,
549+
):
550+
class _WeirdString(str):
551+
pass
552+
553+
class _ParsedURL:
554+
scheme = _WeirdString("https")
555+
netloc = _WeirdString("example.local")
556+
hostname = "example.local"
557+
query = _WeirdString("")
558+
fragment = _WeirdString("")
559+
username = None
560+
password = None
561+
path = _WeirdString("/api")
562+
563+
@property
564+
def port(self) -> int:
565+
return 443
566+
567+
monkeypatch.setattr(config_module, "urlparse", lambda _value: _ParsedURL())
568+
569+
with pytest.raises(
570+
HyperbrowserError, match="base_url parser returned invalid URL components"
571+
):
572+
ClientConfig.normalize_base_url("https://example.local")
573+
574+
547575
def test_client_config_normalize_base_url_rejects_invalid_hostname_types(
548576
monkeypatch: pytest.MonkeyPatch,
549577
):

0 commit comments

Comments
 (0)