Skip to content

Allow subtree traversal from filesystem roots - #141

Open
Str0k wants to merge 1 commit into
cpburnz:masterfrom
Str0k:fix/subdir-filesystem-root
Open

Allow subtree traversal from filesystem roots#141
Str0k wants to merge 1 commit into
cpburnz:masterfrom
Str0k:fix/subdir-filesystem-root

Conversation

@Str0k

@Str0k Str0k commented Sep 12, 2026

Copy link
Copy Markdown

The new subdir traversal support rejects valid subtrees when the root is a filesystem root. _get_sub_path_safe() appends another separator to the root, so / requires a // prefix and C:\ requires a doubled backslash. Equivalent Windows paths with different casing are also rejected.

Use os.path.commonpath() for the descendant check and normcase() for root equality. This preserves rejection of parent paths and sibling paths that happen to share the root's string prefix. It does not change symlink-following behavior.

Related to #126. A minimal reproduction that does not scan the filesystem is:

import os
from pathspec.util import _get_sub_path_safe

root = os.path.abspath(os.sep)
print(_get_sub_path_safe(root, 'child'))  # expected: 'child'; currently ValueError

Tests cover actual traversal through both iter_tree_files() and iter_tree_entries() with absolute and relative subdirectories, equivalent Windows root casing, the root itself, and rejected parent/sibling paths. The filesystem-root tests traverse only their temporary directory.

Validation on Windows / Python 3.12:

  • Before the fix, the new filesystem-root and Windows-case tests produce three failures including subtests.
  • With simple, RE2 and Hyperscan available: python -m unittest discover -t . -s tests/ succeeds (219 tests, 14 skipped because symlink creation is unavailable).
  • Pytest reports 205 passed, 14 skipped and 662 subtests passed.
  • Mypy reports 2 errors and Pyright reports 8 errors already present on an independent checkout of base f9a833e; the diagnostics are unchanged by this patch.
  • Includes a changelog entry in the source and generated change history.

AI assistance: Codex helped implement and test this patch. This is a traversal correctness fix; no security vulnerability is claimed.

Co-authored-by: Codex <noreply@openai.com>
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.

1 participant