Skip to content

Commit 571a247

Browse files
Sanitize open-error message prefixes
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 59302b4 commit 571a247

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎hyperbrowser/client/file_utils.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ def build_open_file_error_message(file_path: object, *, prefix: str) -> str:
5050
normalized_prefix = _DEFAULT_OPEN_ERROR_MESSAGE_PREFIX
5151
else:
5252
try:
53-
stripped_prefix = normalized_prefix.strip()
53+
sanitized_prefix = "".join(
54+
"?" if ord(character) < 32 or ord(character) == 127 else character
55+
for character in normalized_prefix
56+
)
57+
stripped_prefix = sanitized_prefix.strip()
5458
except Exception:
5559
stripped_prefix = _DEFAULT_OPEN_ERROR_MESSAGE_PREFIX
5660
if not is_plain_string(stripped_prefix) or not stripped_prefix:

‎tests/test_file_utils.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,15 @@ def test_build_open_file_error_message_uses_default_prefix_for_blank_string():
349349
assert message == "Failed to open file at path: /tmp/path.txt"
350350

351351

352+
def test_build_open_file_error_message_sanitizes_control_chars_in_prefix():
353+
message = build_open_file_error_message(
354+
"/tmp/path.txt",
355+
prefix="Failed\topen",
356+
)
357+
358+
assert message == "Failed?open: /tmp/path.txt"
359+
360+
352361
def test_ensure_existing_file_path_wraps_invalid_path_os_errors(monkeypatch):
353362
def raising_exists(path: str) -> bool:
354363
raise OSError("invalid path")

0 commit comments

Comments
 (0)