Skip to content

Commit 4101b3d

Browse files
Harden header env JSON parsing error wrapping
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent c9df765 commit 4101b3d

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

hyperbrowser/header_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def parse_headers_env_json(raw_headers: Optional[str]) -> Optional[Dict[str, str
112112
return None
113113
try:
114114
parsed_headers = json.loads(raw_headers)
115-
except json.JSONDecodeError as exc:
115+
except (json.JSONDecodeError, ValueError, RecursionError, TypeError) as exc:
116116
raise HyperbrowserError(
117117
"HYPERBROWSER_HEADERS must be valid JSON object"
118118
) from exc

tests/test_header_utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ def test_parse_headers_env_json_rejects_invalid_json():
101101
parse_headers_env_json("{invalid")
102102

103103

104+
def test_parse_headers_env_json_wraps_recursive_json_errors(
105+
monkeypatch: pytest.MonkeyPatch,
106+
):
107+
def _raise_recursion_error(_raw_headers: str):
108+
raise RecursionError("nested too deeply")
109+
110+
monkeypatch.setattr("hyperbrowser.header_utils.json.loads", _raise_recursion_error)
111+
112+
with pytest.raises(
113+
HyperbrowserError, match="HYPERBROWSER_HEADERS must be valid JSON object"
114+
):
115+
parse_headers_env_json('{"X-Trace-Id":"abc123"}')
116+
117+
104118
def test_parse_headers_env_json_rejects_non_mapping_payload():
105119
with pytest.raises(
106120
HyperbrowserError,

0 commit comments

Comments
 (0)