Skip to content

Reject paths containing an embedded null byte - #1376

Open
rahul05ranjan wants to merge 5 commits into
pytest-dev:mainfrom
rahul05ranjan:fix/null-byte-path-validation
Open

rahul05ranjan wants to merge 5 commits into
pytest-dev:mainfrom
rahul05ranjan:fix/null-byte-path-validation

Conversation

@rahul05ranjan

Copy link
Copy Markdown
Contributor

Summary

The real OS raises ValueError: embedded null byte/character in path for 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 with ValueError on the real filesystem — exactly the divergence pyfakefs exists to prevent.

>>> with Patcher():
...     open('/base/a\x00b', 'w')     # silently creates a NUL-named file
...     os.makedirs('/base/d\x00e')    # silently creates a NUL-named directory

On the real OS both raise ValueError immediately.

Differential harness (real fs vs fake, same op sequences) found os.stat already mismatched (fake raised FileNotFoundError instead of ValueError), 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() raise ValueError for embedded NUL bytes — covers the stat-family, listdir, chmod, open and 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 — covers mkdir and any other creation path.
  • exists(), isdir(), islink() (via _is_of_type/exists exception handling) keep returning False for such paths, matching the real os.path predicates, which swallow the ValueError.

Verification

Python 3.13.5, system pytest, no extra dependencies.

  • 6 new regression tests in 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.
  • Differential re-run of the finding battery: every ValueError case now matches real behavior, including bytes paths and the os.path.exists → False semantics.
  • Full suite: 1465 passed / 1056 skipped / 2 xfailed (was 1459 passed before; +6 new tests, no new failures). fake_filesystem_unittest_test.py::TestClassSetup deselected — it stalls in this environment on clean HEAD too (known pre-existing subprocess stall).
  • TEST_REAL_FS=1 fake_os_test.py: 903 passed, 0 failures.
  • Unrelated surfaces re-checked against the real OS after the change: unicode filename battery (NFC/NFD/RTL/emoji/control chars) and case-sensitivity battery (both is_case_sensitive modes) — 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.py but the hunks do not overlap.

Co-authored-by: rahul05ranjan 11793455+rahul05ranjan@users.noreply.github.com

rahul05ranjan and others added 3 commits November 13, 2026 03:29
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>
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 mrbean-bremen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs a release notes entry.

Comment thread pyfakefs/fake_filesystem.py Outdated
Comment on lines +2866 to +2868
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}")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe split this out into a helper function (something like check_for_null_bytes).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@rahul05ranjan

Copy link
Copy Markdown
Contributor Author

Added the release notes entry (in the short style you prefer) together with the check_for_null_bytes helper from the inline comment — see commit bd50264.

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>
@rahul05ranjan

Copy link
Copy Markdown
Contributor Author

The PyPy CI failures came from the new test itself: real os.chmod on PyPy raises TypeError instead of ValueError for NUL paths. Adapted the assertion in 347adcb.

@mrbean-bremen mrbean-bremen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants