Skip to content

Commit 5fe4d6a

Browse files
Reject control characters in base URL normalization
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 18c541b commit 5fe4d6a

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎hyperbrowser/config.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ def normalize_base_url(base_url: str) -> str:
4040
raise HyperbrowserError("base_url must not contain whitespace characters")
4141
if "\\" in normalized_base_url:
4242
raise HyperbrowserError("base_url must not contain backslashes")
43+
if any(
44+
ord(character) < 32 or ord(character) == 127
45+
for character in normalized_base_url
46+
):
47+
raise HyperbrowserError("base_url must not contain control characters")
4348

4449
parsed_base_url = urlparse(normalized_base_url)
4550
if (

‎tests/test_config.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ def test_client_config_rejects_empty_or_invalid_base_url():
222222
HyperbrowserError, match="base_url must not contain backslashes"
223223
):
224224
ClientConfig(api_key="test-key", base_url="https://example.local\\api")
225+
with pytest.raises(
226+
HyperbrowserError, match="base_url must not contain control characters"
227+
):
228+
ClientConfig(api_key="test-key", base_url="https://example.local\x00api")
225229

226230

227231
def test_client_config_normalizes_headers_to_internal_copy():
@@ -341,3 +345,7 @@ def test_client_config_normalize_base_url_validates_and_normalizes():
341345
HyperbrowserError, match="base_url must not contain backslashes"
342346
):
343347
ClientConfig.normalize_base_url("https://example.local\\api")
348+
with pytest.raises(
349+
HyperbrowserError, match="base_url must not contain control characters"
350+
):
351+
ClientConfig.normalize_base_url("https://example.local\x00api")

‎tests/test_url_building.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ def test_client_build_url_rejects_runtime_invalid_base_url_changes():
116116
):
117117
client._build_url("/session")
118118

119+
client.config.base_url = "https://example.local\x00api"
120+
with pytest.raises(
121+
HyperbrowserError, match="base_url must not contain control characters"
122+
):
123+
client._build_url("/session")
124+
119125
client.config.base_url = " "
120126
with pytest.raises(HyperbrowserError, match="base_url must not be empty"):
121127
client._build_url("/session")

0 commit comments

Comments
 (0)