Skip to content

Commit 694d8a9

Browse files
Validate _build_url path inputs for type and emptiness
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 3d93250 commit 694d8a9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

hyperbrowser/client/base.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ def __init__(
5555
self.transport = transport(config.api_key, headers=config.headers)
5656

5757
def _build_url(self, path: str) -> str:
58-
normalized_path = path if path.startswith("/") else f"/{path}"
58+
if not isinstance(path, str):
59+
raise HyperbrowserError("path must be a string")
60+
stripped_path = path.strip()
61+
if not stripped_path:
62+
raise HyperbrowserError("path must not be empty")
63+
normalized_path = (
64+
stripped_path if stripped_path.startswith("/") else f"/{stripped_path}"
65+
)
5966
if normalized_path == "/api" or normalized_path.startswith("/api/"):
6067
return f"{self.config.base_url}{normalized_path}"
6168
return f"{self.config.base_url}/api{normalized_path}"

tests/test_url_building.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import pytest
2+
13
from hyperbrowser import Hyperbrowser
24
from hyperbrowser.config import ClientConfig
5+
from hyperbrowser.exceptions import HyperbrowserError
36

47

58
def test_client_build_url_normalizes_leading_slash():
@@ -29,3 +32,14 @@ def test_client_build_url_uses_normalized_base_url():
2932
assert client._build_url("/session") == "https://example.local/api/session"
3033
finally:
3134
client.close()
35+
36+
37+
def test_client_build_url_rejects_empty_or_non_string_paths():
38+
client = Hyperbrowser(config=ClientConfig(api_key="test-key"))
39+
try:
40+
with pytest.raises(HyperbrowserError, match="path must not be empty"):
41+
client._build_url(" ")
42+
with pytest.raises(HyperbrowserError, match="path must be a string"):
43+
client._build_url(123) # type: ignore[arg-type]
44+
finally:
45+
client.close()

0 commit comments

Comments
 (0)