Skip to content

Keep directory uploads inside their root - #1843

Open
sylvesterkaczmarek wants to merge 1 commit into
anthropics:mainfrom
sylvesterkaczmarek:fix/files-from-dir-symlink-boundary
Open

Keep directory uploads inside their root#1843
sylvesterkaczmarek wants to merge 1 commit into
anthropics:mainfrom
sylvesterkaczmarek:fix/files-from-dir-symlink-boundary

Conversation

@sylvesterkaczmarek

Copy link
Copy Markdown

Summary

Prevent files_from_dir() and async_files_from_dir() from following directory-tree entries outside the directory the caller asked to upload.

The current recursive walkers use Path.is_dir() / anyio.Path.is_dir(), which follow symlinks. As a result, a symlink inside the requested directory can:

  • point to a file outside the requested tree and have that file uploaded;
  • point to an external directory and recursively upload its contents;
  • point back to an ancestor directory and drive unbounded recursive traversal.

Fix

Resolve the requested root once and require every traversed entry to resolve underneath it.

For directory recursion, track the (st_dev, st_ino) identities currently on the recursion stack. Re-entering an active directory is treated as a symlink cycle and rejected explicitly.

Safe symlinks whose targets remain inside the requested root continue to work. External links and cycles are rejected. The same protection is applied to the sync and async helpers.

Regression coverage

Adds tests covering:

  • a file symlink escaping the requested root;
  • an internal file symlink that remains supported;
  • an ancestor directory-symlink cycle;
  • async external-directory traversal rejection;
  • async cycle detection.

The production change is confined to the hand-maintained directory upload helper.

@sylvesterkaczmarek
sylvesterkaczmarek requested a review from a team as a code owner August 17, 2026 07:44
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