Skip to content

Commit 22a3a83

Browse files
Wrap invalid filesystem path errors in file utility
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 7ddd85e commit 22a3a83

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

hyperbrowser/client/file_utils.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,18 @@ def ensure_existing_file_path(
2222
raise HyperbrowserError("file_path must resolve to a string path")
2323
if not normalized_path:
2424
raise HyperbrowserError("file_path must not be empty")
25-
if not os.path.exists(normalized_path):
25+
if "\x00" in normalized_path:
26+
raise HyperbrowserError("file_path must not contain null bytes")
27+
try:
28+
path_exists = os.path.exists(normalized_path)
29+
except (OSError, ValueError) as exc:
30+
raise HyperbrowserError("file_path is invalid", original_error=exc) from exc
31+
if not path_exists:
2632
raise HyperbrowserError(missing_file_message)
27-
if not os.path.isfile(normalized_path):
33+
try:
34+
is_file = os.path.isfile(normalized_path)
35+
except (OSError, ValueError) as exc:
36+
raise HyperbrowserError("file_path is invalid", original_error=exc) from exc
37+
if not is_file:
2838
raise HyperbrowserError(not_file_message)
2939
return normalized_path

tests/test_file_utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44

5+
import hyperbrowser.client.file_utils as file_utils
56
from hyperbrowser.client.file_utils import ensure_existing_file_path
67
from hyperbrowser.exceptions import HyperbrowserError
78

@@ -79,3 +80,28 @@ def test_ensure_existing_file_path_rejects_empty_string_paths():
7980
missing_file_message="missing",
8081
not_file_message="not-file",
8182
)
83+
84+
85+
def test_ensure_existing_file_path_rejects_null_byte_paths():
86+
with pytest.raises(
87+
HyperbrowserError, match="file_path must not contain null bytes"
88+
):
89+
ensure_existing_file_path(
90+
"bad\x00path.txt",
91+
missing_file_message="missing",
92+
not_file_message="not-file",
93+
)
94+
95+
96+
def test_ensure_existing_file_path_wraps_invalid_path_os_errors(monkeypatch):
97+
def raising_exists(path: str) -> bool:
98+
raise OSError("invalid path")
99+
100+
monkeypatch.setattr(file_utils.os.path, "exists", raising_exists)
101+
102+
with pytest.raises(HyperbrowserError, match="file_path is invalid"):
103+
ensure_existing_file_path(
104+
"/tmp/maybe-invalid",
105+
missing_file_message="missing",
106+
not_file_message="not-file",
107+
)

0 commit comments

Comments
 (0)