Skip to content

Commit 412a50a

Browse files
Disallow case-insensitive duplicate header names
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 700d894 commit 412a50a

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

hyperbrowser/header_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def normalize_headers(
1717

1818
effective_pair_error_message = pair_error_message or mapping_error_message
1919
normalized_headers: Dict[str, str] = {}
20+
seen_header_names = set()
2021
for key, value in headers.items():
2122
if not isinstance(key, str) or not isinstance(value, str):
2223
raise HyperbrowserError(effective_pair_error_message)
@@ -30,10 +31,12 @@ def normalize_headers(
3031
or "\r" in value
3132
):
3233
raise HyperbrowserError("headers must not contain newline characters")
33-
if normalized_key in normalized_headers:
34+
canonical_header_name = normalized_key.lower()
35+
if canonical_header_name in seen_header_names:
3436
raise HyperbrowserError(
3537
"duplicate header names are not allowed after normalization"
3638
)
39+
seen_header_names.add(canonical_header_name)
3740
normalized_headers[normalized_key] = value
3841
return normalized_headers
3942

tests/test_config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,17 @@ def test_client_config_rejects_duplicate_header_names_after_normalization():
227227
)
228228

229229

230+
def test_client_config_rejects_case_insensitive_duplicate_header_names():
231+
with pytest.raises(
232+
HyperbrowserError,
233+
match="duplicate header names are not allowed after normalization",
234+
):
235+
ClientConfig(
236+
api_key="test-key",
237+
headers={"X-Correlation-Id": "one", "x-correlation-id": "two"},
238+
)
239+
240+
230241
def test_client_config_accepts_mapping_header_inputs():
231242
headers = MappingProxyType({"X-Correlation-Id": "abc123"})
232243
config = ClientConfig(api_key="test-key", headers=headers)

tests/test_header_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ def test_normalize_headers_rejects_duplicate_names_after_normalization():
3232
)
3333

3434

35+
def test_normalize_headers_rejects_case_insensitive_duplicate_names():
36+
with pytest.raises(
37+
HyperbrowserError,
38+
match="duplicate header names are not allowed after normalization",
39+
):
40+
normalize_headers(
41+
{"X-Trace-Id": "one", "x-trace-id": "two"},
42+
mapping_error_message="headers must be a mapping of string pairs",
43+
)
44+
45+
3546
def test_parse_headers_env_json_ignores_blank_values():
3647
assert parse_headers_env_json(" ") is None
3748

0 commit comments

Comments
 (0)