Symlink-Safe Directory Walk

Walk a directory without following links that escape the root, recording skipped entries and enforcing a depth limit.

PythonSecurity Boundaries

Code

pythonsnippet
48 lines
import os
from pathlib import Path


def walk_confined(root: str, *, max_depth: int = 32) -> tuple[list[str], list[str]]:
    root_path = Path(root).resolve(strict=True)
    if not root_path.is_dir():
        raise NotADirectoryError(root)
    files: list[str] = []
    skipped: list[str] = []
    stack: list[tuple[Path, int]] = [(root_path, 0)]
    while stack:
        directory, depth = stack.pop()
        if depth >= max_depth:
            skipped.append(str(directory.relative_to(root_path)))
            continue
        with os.scandir(directory) as entries:
            ordered = sorted(entries, key=lambda entry: entry.name)
        for entry in ordered:
            entry_path = Path(entry.path)
            if entry.is_symlink():
                try:
                    target = entry_path.resolve(strict=True)
                except OSError:
                    skipped.append(str(entry_path.relative_to(root_path)))
                    continue
                if target.is_relative_to(root_path) and target.is_file():
                    files.append(str(entry_path.relative_to(root_path)))
                else:
                    skipped.append(str(entry_path.relative_to(root_path)))
            elif entry.is_dir(follow_symlinks=False):
                stack.append((entry_path, depth + 1))
            elif entry.is_file(follow_symlinks=False):
                files.append(str(entry_path.relative_to(root_path)))
    return sorted(files), sorted(skipped)


def main() -> None:
    root = Path(__file__).resolve().parent
    files, skipped = walk_confined(str(root), max_depth=2)
    print(f"files: {len(files)}")
    for name in files[:5]:
        print("  ", Path(name).as_posix())
    print(f"skipped: {len(skipped)}")


if __name__ == "__main__":
    main()

Output

Captured from a sandboxed run of this snippet in the build pipeline.

files: 1
   snippet.py
skipped: 0

What the code does

Under the hood: the functions walk_confined and main.

Iteration happens inside while stack — everything outside it runs once. Invalid states are rejected up front: it throws NotADirectoryError instead of continuing with bad data.

Points of care

  • The guard not root_path.is_dir() short-circuits the flow with an early raise when its condition holds.

Applying it

The primary signature is def walk_confined(root: str, *, max_depth: int = 32) -> tuple[list[str], list[str]]

The demo output is labeled (files:, skipped:), so every printed line maps back to its originating operation.

The sequence walk_confined lives in main; running the file triggers it.

Statements that matter

def main() -> None:
    root = Path(__file__).resolve().parent
    files, skipped = walk_confined(str(root), max_depth=2)
    print(f"files: {len(files)}")
    for name in files[:5]:
        print("  ", Path(name).as_posix())
    print(f"skipped: {len(skipped)}")
elif entry.is_file(follow_symlinks=False):
    files.append(str(entry_path.relative_to(root_path)))
elif entry.is_dir(follow_symlinks=False):
    stack.append((entry_path, depth + 1))

Interactive Tools for This Topic

FAQ

What does the snippet do with malformed data?
Validation comes first — `NotADirectoryError` marks the failure at `not root_path.is_dir()`, before any partial work happens.
What does Symlink-Safe Directory Walk print when run?
Invoke the file as-is — the `files:`, `skipped:` prefixes identify which step printed each line.