Skip to content

Commit b50565e

Browse files
Validate base URL port values during normalization
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent dcd4e08 commit b50565e

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

hyperbrowser/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ def normalize_base_url(base_url: str) -> str:
6060
)
6161
if parsed_base_url.username is not None or parsed_base_url.password is not None:
6262
raise HyperbrowserError("base_url must not include user credentials")
63+
try:
64+
parsed_base_url.port
65+
except ValueError as exc:
66+
raise HyperbrowserError(
67+
"base_url must contain a valid port number"
68+
) from exc
6369

6470
decoded_base_path = parsed_base_url.path
6571
for _ in range(10):

tests/test_config.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ def test_client_config_rejects_empty_or_invalid_base_url():
205205

206206
with pytest.raises(HyperbrowserError, match="include a host"):
207207
ClientConfig(api_key="test-key", base_url="http://")
208+
with pytest.raises(
209+
HyperbrowserError, match="base_url must contain a valid port number"
210+
):
211+
ClientConfig(api_key="test-key", base_url="https://example.local:99999")
212+
with pytest.raises(
213+
HyperbrowserError, match="base_url must contain a valid port number"
214+
):
215+
ClientConfig(api_key="test-key", base_url="https://example.local:bad")
208216

209217
with pytest.raises(HyperbrowserError, match="must not include query parameters"):
210218
ClientConfig(api_key="test-key", base_url="https://example.local#frag")
@@ -356,6 +364,14 @@ def test_client_config_normalize_base_url_validates_and_normalizes():
356364

357365
with pytest.raises(HyperbrowserError, match="base_url must start with"):
358366
ClientConfig.normalize_base_url("example.local")
367+
with pytest.raises(
368+
HyperbrowserError, match="base_url must contain a valid port number"
369+
):
370+
ClientConfig.normalize_base_url("https://example.local:99999")
371+
with pytest.raises(
372+
HyperbrowserError, match="base_url must contain a valid port number"
373+
):
374+
ClientConfig.normalize_base_url("https://example.local:bad")
359375

360376
with pytest.raises(HyperbrowserError, match="must not include query parameters"):
361377
ClientConfig.normalize_base_url("https://example.local?foo=bar")

tests/test_url_building.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ def test_client_build_url_rejects_runtime_invalid_base_url_changes():
9898
):
9999
client._build_url("/session")
100100

101+
client.config.base_url = "https://example.local:99999"
102+
with pytest.raises(
103+
HyperbrowserError, match="base_url must contain a valid port number"
104+
):
105+
client._build_url("/session")
106+
101107
client.config.base_url = "https://example.local/\napi"
102108
with pytest.raises(
103109
HyperbrowserError, match="base_url must not contain newline characters"

0 commit comments

Comments
 (0)