Skip to content

Commit 7c3b1a3

Browse files
Harden mapping key-display whitespace normalization
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 82d5935 commit 7c3b1a3

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

hyperbrowser/mapping_utils.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ def safe_key_display_for_error(
99
key: object, *, key_display: Callable[[object], object]
1010
) -> str:
1111
try:
12-
key_text = key_display(key)
13-
if not is_plain_string(key_text):
12+
raw_key_text = key_display(key)
13+
if not is_plain_string(raw_key_text):
1414
raise TypeError("mapping key display must be a string")
15-
if not key_text.strip():
16-
raise ValueError("mapping key display must not be blank")
17-
if any(ord(character) < 32 or ord(character) == 127 for character in key_text):
15+
if any(
16+
ord(character) < 32 or ord(character) == 127
17+
for character in raw_key_text
18+
):
1819
raise ValueError("mapping key display must not contain control characters")
20+
key_text = raw_key_text.strip()
21+
if not is_plain_string(key_text):
22+
raise TypeError("normalized mapping key display must be a string")
23+
if not key_text:
24+
raise ValueError("mapping key display must not be blank")
1925
return key_text
2026
except Exception:
2127
return "<unreadable key>"

tests/test_mapping_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ def test_safe_key_display_for_error_returns_display_value():
140140
)
141141

142142

143+
def test_safe_key_display_for_error_strips_whitespace_from_display_value():
144+
assert (
145+
safe_key_display_for_error("field", key_display=lambda key: f" {key} ")
146+
== "field"
147+
)
148+
149+
143150
def test_safe_key_display_for_error_returns_unreadable_key_on_failures():
144151
assert (
145152
safe_key_display_for_error(

0 commit comments

Comments
 (0)