Skip to content

Commit 5fd7798

Browse files
Reject API paths with surrounding whitespace
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 2485dd7 commit 5fd7798

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

hyperbrowser/client/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ def _build_url(self, path: str) -> str:
7373
if not isinstance(path, str):
7474
raise HyperbrowserError("path must be a string")
7575
stripped_path = path.strip()
76+
if stripped_path != path:
77+
raise HyperbrowserError(
78+
"path must not contain leading or trailing whitespace"
79+
)
7680
if not stripped_path:
7781
raise HyperbrowserError("path must not be empty")
7882
if "\\" in stripped_path:

tests/test_url_building.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ def test_client_build_url_uses_normalized_base_url():
4242
)
4343
try:
4444
assert client._build_url("/session") == "https://example.local/api/session"
45-
assert client._build_url(" session ") == "https://example.local/api/session"
45+
with pytest.raises(
46+
HyperbrowserError,
47+
match="path must not contain leading or trailing whitespace",
48+
):
49+
client._build_url(" session ")
4650
finally:
4751
client.close()
4852

@@ -219,7 +223,12 @@ def test_client_build_url_rejects_empty_or_non_string_paths():
219223
client = Hyperbrowser(config=ClientConfig(api_key="test-key"))
220224
try:
221225
with pytest.raises(HyperbrowserError, match="path must not be empty"):
222-
client._build_url(" ")
226+
client._build_url("")
227+
with pytest.raises(
228+
HyperbrowserError,
229+
match="path must not contain leading or trailing whitespace",
230+
):
231+
client._build_url(" /session")
223232
with pytest.raises(HyperbrowserError, match="path must be a string"):
224233
client._build_url(123) # type: ignore[arg-type]
225234
with pytest.raises(

0 commit comments

Comments
 (0)