Skip to content

Commit e75d424

Browse files
Sanitize extension missing-key display values
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent c60505b commit e75d424

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

hyperbrowser/client/managers/extension_utils.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
_MAX_DISPLAYED_MISSING_KEYS = 20
88
_MAX_DISPLAYED_MISSING_KEY_LENGTH = 120
9+
_TRUNCATED_KEY_DISPLAY_SUFFIX = "... (truncated)"
910

1011

1112
def _get_type_name(value: Any) -> str:
@@ -21,12 +22,20 @@ def _safe_stringify_key(value: object) -> str:
2122

2223
def _format_key_display(value: object) -> str:
2324
normalized_key = _safe_stringify_key(value)
25+
normalized_key = "".join(
26+
"?" if ord(character) < 32 or ord(character) == 127 else character
27+
for character in normalized_key
28+
).strip()
29+
if not normalized_key:
30+
return "<blank key>"
2431
if len(normalized_key) <= _MAX_DISPLAYED_MISSING_KEY_LENGTH:
2532
return normalized_key
26-
return (
27-
f"{normalized_key[:_MAX_DISPLAYED_MISSING_KEY_LENGTH]}"
28-
"... (truncated)"
33+
available_key_length = _MAX_DISPLAYED_MISSING_KEY_LENGTH - len(
34+
_TRUNCATED_KEY_DISPLAY_SUFFIX
2935
)
36+
if available_key_length <= 0:
37+
return _TRUNCATED_KEY_DISPLAY_SUFFIX
38+
return f"{normalized_key[:available_key_length]}{_TRUNCATED_KEY_DISPLAY_SUFFIX}"
3039

3140

3241
def _summarize_mapping_keys(mapping: Mapping[object, object]) -> str:

tests/test_extension_utils.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,28 @@ def test_parse_extension_list_response_data_missing_key_truncates_long_key_names
138138
HyperbrowserError,
139139
match=(
140140
"Expected 'extensions' key in response but got "
141-
r"\[k{120}\.\.\. \(truncated\)\] keys"
141+
r"\[k{105}\.\.\. \(truncated\)\] keys"
142142
),
143143
):
144144
parse_extension_list_response_data({long_key: "value"})
145145

146146

147+
def test_parse_extension_list_response_data_missing_key_normalizes_blank_key_names():
148+
with pytest.raises(
149+
HyperbrowserError,
150+
match="Expected 'extensions' key in response but got \\[<blank key>\\] keys",
151+
):
152+
parse_extension_list_response_data({" ": "value"})
153+
154+
155+
def test_parse_extension_list_response_data_missing_key_normalizes_control_characters():
156+
with pytest.raises(
157+
HyperbrowserError,
158+
match="Expected 'extensions' key in response but got \\[bad\\?key\\] keys",
159+
):
160+
parse_extension_list_response_data({"bad\tkey": "value"})
161+
162+
147163
def test_parse_extension_list_response_data_missing_key_handles_unprintable_keys():
148164
class _BrokenStringKey:
149165
def __str__(self) -> str:

0 commit comments

Comments
 (0)