Skip to content

Commit 8deb73a

Browse files
Validate HYPERBROWSER_HEADERS value types before config init
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 1de269f commit 8deb73a

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

hyperbrowser/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,12 @@ def from_env(cls) -> "ClientConfig":
6363
raise HyperbrowserError(
6464
"HYPERBROWSER_HEADERS must be a JSON object of string pairs"
6565
)
66+
if any(
67+
not isinstance(key, str) or not isinstance(value, str)
68+
for key, value in parsed_headers.items()
69+
):
70+
raise HyperbrowserError(
71+
"HYPERBROWSER_HEADERS must be a JSON object of string pairs"
72+
)
6673
headers = parsed_headers
6774
return cls(api_key=api_key, base_url=base_url, headers=headers)

tests/test_config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ def test_client_config_from_env_rejects_non_object_headers_json(monkeypatch):
6262
ClientConfig.from_env()
6363

6464

65+
def test_client_config_from_env_rejects_non_string_header_values(monkeypatch):
66+
monkeypatch.setenv("HYPERBROWSER_API_KEY", "test-key")
67+
monkeypatch.setenv("HYPERBROWSER_HEADERS", '{"X-Request-Id":123}')
68+
69+
with pytest.raises(
70+
HyperbrowserError,
71+
match="HYPERBROWSER_HEADERS must be a JSON object of string pairs",
72+
):
73+
ClientConfig.from_env()
74+
75+
6576
def test_client_config_from_env_ignores_blank_headers(monkeypatch):
6677
monkeypatch.setenv("HYPERBROWSER_API_KEY", "test-key")
6778
monkeypatch.setenv("HYPERBROWSER_HEADERS", " ")

0 commit comments

Comments
 (0)