Skip to content

Commit 5df8bd2

Browse files
Require concrete string header-name normalization outputs
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 02a3676 commit 5df8bd2

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

hyperbrowser/header_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def normalize_headers(
5353
raise HyperbrowserError(effective_pair_error_message)
5454
try:
5555
normalized_key = key.strip()
56-
if not isinstance(normalized_key, str):
56+
if type(normalized_key) is not str:
5757
raise TypeError("normalized header name must be a string")
5858
except HyperbrowserError:
5959
raise
@@ -104,7 +104,7 @@ def normalize_headers(
104104
raise HyperbrowserError("headers must not contain control characters")
105105
try:
106106
canonical_header_name = normalized_key.lower()
107-
if not isinstance(canonical_header_name, str):
107+
if type(canonical_header_name) is not str:
108108
raise TypeError("canonical header name must be a string")
109109
except HyperbrowserError:
110110
raise

tests/test_header_utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ def lower(self): # type: ignore[override]
6060
return object()
6161

6262

63+
class _StringSubclassStripResultHeaderName(str):
64+
class _NormalizedKey(str):
65+
pass
66+
67+
def strip(self, chars=None): # type: ignore[override]
68+
_ = chars
69+
return self._NormalizedKey("X-Trace-Id")
70+
71+
6372
class _BrokenHeadersEnvString(str):
6473
def strip(self, chars=None): # type: ignore[override]
6574
_ = chars
@@ -151,6 +160,18 @@ def test_normalize_headers_wraps_non_string_header_name_lower_results():
151160
assert exc_info.value.original_error is not None
152161

153162

163+
def test_normalize_headers_wraps_string_subclass_header_name_strip_results():
164+
with pytest.raises(
165+
HyperbrowserError, match="Failed to normalize header name"
166+
) as exc_info:
167+
normalize_headers(
168+
{_StringSubclassStripResultHeaderName("X-Trace-Id"): "trace-1"},
169+
mapping_error_message="headers must be a mapping of string pairs",
170+
)
171+
172+
assert isinstance(exc_info.value.original_error, TypeError)
173+
174+
154175
def test_normalize_headers_rejects_overly_long_header_names():
155176
long_header_name = "X-" + ("a" * 255)
156177
with pytest.raises(

0 commit comments

Comments
 (0)