Skip to content

Commit 888c8a0

Browse files
Harden default-prefix sanitization for file path errors
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent c5c9eeb commit 888c8a0

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

hyperbrowser/client/file_utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ def _normalize_error_prefix(prefix: object, *, default_prefix: str) -> str:
1616
normalized_default_prefix = _DEFAULT_OPEN_ERROR_MESSAGE_PREFIX
1717
else:
1818
try:
19-
stripped_default_prefix = normalized_default_prefix.strip()
19+
sanitized_default_prefix = "".join(
20+
"?" if ord(character) < 32 or ord(character) == 127 else character
21+
for character in normalized_default_prefix
22+
)
23+
stripped_default_prefix = sanitized_default_prefix.strip()
2024
except Exception:
2125
stripped_default_prefix = _DEFAULT_OPEN_ERROR_MESSAGE_PREFIX
2226
if not is_plain_string(stripped_default_prefix) or not stripped_default_prefix:

tests/test_file_path_display_helper_usage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def test_file_path_error_messages_use_shared_display_helper():
1515
for module_path in FILE_PATH_DISPLAY_MODULES:
1616
module_text = Path(module_path).read_text(encoding="utf-8")
1717
assert "build_file_path_error_message(" in module_text
18+
assert "default_prefix=" in module_text
1819
assert "format_file_path_for_error(" not in module_text
1920
assert 'missing_file_message=f"' not in module_text
2021
assert 'not_file_message=f"' not in module_text

tests/test_file_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,16 @@ def test_build_file_path_error_message_uses_open_default_when_default_prefix_inv
371371
assert message == "Failed to open file at path: /tmp/path.txt"
372372

373373

374+
def test_build_file_path_error_message_sanitizes_default_prefix_when_prefix_invalid():
375+
message = build_file_path_error_message(
376+
"/tmp/path.txt",
377+
prefix=123, # type: ignore[arg-type]
378+
default_prefix="Custom\tdefault",
379+
)
380+
381+
assert message == "Custom?default: /tmp/path.txt"
382+
383+
374384
def test_build_open_file_error_message_uses_default_prefix_for_non_string():
375385
message = build_open_file_error_message(
376386
"/tmp/path.txt",

0 commit comments

Comments
 (0)