File tree Expand file tree Collapse file tree 2 files changed +38
-2
lines changed
Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 22
33import pytest
44
5+ import hyperbrowser .client .file_utils as file_utils
56from hyperbrowser .client .file_utils import ensure_existing_file_path
67from 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\x00 path.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+ )
You can’t perform that action at this time.
0 commit comments