Skip to content

Commit 70aa6e8

Browse files
Reject control characters in file utility messages
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 8e01054 commit 70aa6e8

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

hyperbrowser/client/file_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,22 @@ def ensure_existing_file_path(
1515
raise HyperbrowserError("missing_file_message must be a string")
1616
if not missing_file_message.strip():
1717
raise HyperbrowserError("missing_file_message must not be empty")
18+
if any(
19+
ord(character) < 32 or ord(character) == 127
20+
for character in missing_file_message
21+
):
22+
raise HyperbrowserError(
23+
"missing_file_message must not contain control characters"
24+
)
1825
if not isinstance(not_file_message, str):
1926
raise HyperbrowserError("not_file_message must be a string")
2027
if not not_file_message.strip():
2128
raise HyperbrowserError("not_file_message must not be empty")
29+
if any(
30+
ord(character) < 32 or ord(character) == 127
31+
for character in not_file_message
32+
):
33+
raise HyperbrowserError("not_file_message must not contain control characters")
2234
try:
2335
normalized_path = os.fspath(file_path)
2436
except HyperbrowserError:

tests/test_file_utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ def test_ensure_existing_file_path_rejects_blank_missing_message(tmp_path: Path)
4646
)
4747

4848

49+
def test_ensure_existing_file_path_rejects_control_chars_in_missing_message(
50+
tmp_path: Path,
51+
):
52+
file_path = tmp_path / "file.txt"
53+
file_path.write_text("content")
54+
55+
with pytest.raises(
56+
HyperbrowserError,
57+
match="missing_file_message must not contain control characters",
58+
):
59+
ensure_existing_file_path(
60+
str(file_path),
61+
missing_file_message="missing\tmessage",
62+
not_file_message="not-file",
63+
)
64+
65+
4966
def test_ensure_existing_file_path_rejects_non_string_not_file_message(tmp_path: Path):
5067
file_path = tmp_path / "file.txt"
5168
file_path.write_text("content")
@@ -70,6 +87,23 @@ def test_ensure_existing_file_path_rejects_blank_not_file_message(tmp_path: Path
7087
)
7188

7289

90+
def test_ensure_existing_file_path_rejects_control_chars_in_not_file_message(
91+
tmp_path: Path,
92+
):
93+
file_path = tmp_path / "file.txt"
94+
file_path.write_text("content")
95+
96+
with pytest.raises(
97+
HyperbrowserError,
98+
match="not_file_message must not contain control characters",
99+
):
100+
ensure_existing_file_path(
101+
str(file_path),
102+
missing_file_message="missing",
103+
not_file_message="not-file\nmessage",
104+
)
105+
106+
73107
def test_ensure_existing_file_path_accepts_pathlike_inputs(tmp_path: Path):
74108
file_path = tmp_path / "pathlike-file.txt"
75109
file_path.write_text("content")

0 commit comments

Comments
 (0)