Skip to content

Commit 91f4104

Browse files
Harden mapping key display blank/control fallback
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 11cadb8 commit 91f4104

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

hyperbrowser/mapping_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ def safe_key_display_for_error(
1212
key_text = key_display(key)
1313
if not is_plain_string(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):
18+
raise ValueError("mapping key display must not contain control characters")
1519
return key_text
1620
except Exception:
1721
return "<unreadable key>"

tests/test_mapping_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,26 @@ class _DisplayString(str):
163163
)
164164

165165

166+
def test_safe_key_display_for_error_returns_unreadable_key_for_blank_display_values():
167+
assert (
168+
safe_key_display_for_error(
169+
"field",
170+
key_display=lambda key: f" {key[:0]} ",
171+
)
172+
== "<unreadable key>"
173+
)
174+
175+
176+
def test_safe_key_display_for_error_returns_unreadable_key_for_control_display_values():
177+
assert (
178+
safe_key_display_for_error(
179+
"field",
180+
key_display=lambda key: f"{key}\n\t",
181+
)
182+
== "<unreadable key>"
183+
)
184+
185+
166186
def test_read_string_mapping_keys_returns_string_keys():
167187
assert read_string_mapping_keys(
168188
{"a": 1, "b": 2},

0 commit comments

Comments
 (0)