Repository navigation
Reject paths containing an embedded null byte - #1376
rahul05ranjan wants to merge 5 commits into
Conversation
The real OS raises ValueError ("embedded null byte/character in path")
for any path operation on a name containing \x00, but the fake
filesystem silently accepted such names: open(), makedirs(), rename(),
link() and symlink() happily created filesystem entries whose names
contain NUL bytes, so code passing under pyfakefs explodes with ValueError
on the real filesystem - the exact divergence pyfakefs exists to prevent.
Validate for embedded NUL bytes in resolve_path()/lresolve() (covers
stat-family, listdir, chmod, open and other resolution-based entry
points), in rename() for both paths, in create_link()/create_symlink()
for the link path, and in add_object() on the entry name (covers mkdir
and any other creation path). exists()/isdir()/islink() keep returning
False for such paths, matching the real os.path predicates which swallow
the ValueError.
Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
for more information, see https://pre-commit.ci
pre-commit's mypy flagged add_object's null check: obj_name comes from make_string_path(file_object.name), whose AnyStr is unsolved there, so the matching_string overload resolved to bytes and the containment check failed to type. Give obj_name the concrete str | bytes union and narrow with isinstance before the membership test, which keeps the exact same runtime behavior for both string kinds. Verified with mypy 2.4.0 (clean on fake_filesystem.py), ruff check/format clean, and fake_os_test.py 565 passed 562 skipped locally. Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
mrbean-bremen
left a comment
There was a problem hiding this comment.
This also needs a release notes entry.
| if matching_string(new_path_str, "\x00") in new_path_str: | ||
| # the real OS refuses NUL bytes inside path components | ||
| raise ValueError(f"embedded null character in path: {new_path_str!r}") |
There was a problem hiding this comment.
Maybe split this out into a helper function (something like check_for_null_bytes).
There was a problem hiding this comment.
Done — extracted the check into a check_for_null_bytes helper in pyfakefs/helpers.py and replaced all six duplicated inline checks with calls to it. Also added the release notes entry you asked for in the same commit (bd50264).
Split the repeated embedded null-byte path checks out into a check_for_null_bytes helper in pyfakefs/helpers.py, as suggested in the review. Also adds the missing release notes entry. Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
|
Added the release notes entry (in the short style you prefer) together with the |
The real os.chmod on PyPy raises TypeError instead of ValueError for a path containing an embedded null byte, which made the new test fail in real-fs mode on the PyPy CI runners. Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
|
The PyPy CI failures came from the new test itself: real |
Summary
The real OS raises
ValueError: embedded null byte/character in pathfor any operation on a path containing\x00, but the fake filesystem silently accepted such names. Code that creates NUL-named entries passes under pyfakefs and then explodes withValueErroron the real filesystem — exactly the divergence pyfakefs exists to prevent.On the real OS both raise
ValueErrorimmediately.Differential harness (real fs vs fake, same op sequences) found
os.statalready mismatched (fake raisedFileNotFoundErrorinstead ofValueError), and the mutation ops (open,makedirs,rename,link,symlink) did not fail at all —rename()even moved an existing file to a NUL-named target.Fix
resolve_path()/lresolve()raiseValueErrorfor embedded NUL bytes — covers the stat-family,listdir,chmod,openand other resolution-based entry points.rename()validates both paths before any mutation.create_link()/create_symlink()validate the link path up front (before the target object is touched).add_object()validates the entry name — coversmkdirand any other creation path.exists(),isdir(),islink()(via_is_of_type/existsexception handling) keep returningFalsefor such paths, matching the realos.pathpredicates, which swallow theValueError.Verification
Python 3.13.5, system pytest, no extra dependencies.
pyfakefs/tests/fake_os_test.py(test_*_raises_on_embedded_null_byte): all 6 fail on un-fixed HEAD (fake silently accepted the paths), all 6 pass with the fix in FAKE mode, and all 6 pass in REAL-fs mode (TEST_REAL_FS=1,RealOsModuleTest) — i.e. the tests assert exactly what the real OS does.ValueErrorcase now matches real behavior, including bytes paths and theos.path.exists→Falsesemantics.fake_filesystem_unittest_test.py::TestClassSetupdeselected — it stalls in this environment on clean HEAD too (known pre-existing subprocess stall).TEST_REAL_FS=1fake_os_test.py: 903 passed, 0 failures.is_case_sensitivemodes) — behavior unchanged and still matching the real filesystem.This is a different surface from open PR #1375 (ENAMETOOLONG, component length); the only shared file is
pyfakefs/fake_filesystem.pybut the hunks do not overlap.Co-authored-by: rahul05ranjan 11793455+rahul05ranjan@users.noreply.github.com