fix(filesystem): do not follow symlinks when reporting sizes in list_directory_with_sizes - #4732
Open
annatchijova wants to merge 2 commits into
Conversation
…directory_with_sizes list_directory_with_sizes stat'd each entry with fs.stat, which follows symlinks. When an allowed directory contains a symlink that points outside the sandbox, the tool reported the size and mtime of the symlink target -- disclosing metadata about files outside the allowed directories, which the tool's own description says it must not touch. The sibling tools (list_directory, directory_tree) classify entries from the non-following Dirent, and read_file rejects out-of-sandbox targets via the realpath check in validatePath; only this tool followed symlinks. Use fs.lstat so a symlink is described by its own metadata, matching the other listing tools. Content was never exposed (reads go through validatePath); this closes the metadata leak. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VWmkpCnuhCPKFPD2fexY1a
…mlinks out of sandbox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
list_directory_with_sizesstats each entry withfs.stat, which follows symlinks. When an allowed directory contains a symlink that points outside the sandbox, the tool reports the target's size and mtime — disclosing metadata about files outside the allowed directories, which the tool's own description says it "only works within." The sibling tools are consistent the other way:list_directoryanddirectory_treeclassify entries from the non-followingDirent, andread_filerejects out-of-sandbox targets via therealpathcheck invalidatePath. This tool was the only one that followed symlinks out of the sandbox.Fix
Use
fs.lstatso a symlink is described by its own metadata, matching the other listing tools. One line (plus a comment). Content was never exposed (reads go throughvalidatePath); this closes the metadata leak.Repro (before the fix)
An allowed dir containing
leak_link -> /etc/hostname:list_directory_with_sizesreportsleak_link's size as 9 (the size of/etc/hostname). Withfs.lstatit reports the link's own size (13) instead. A regression test asserting this is included.Scope / severity
Low. Metadata only (size/mtime/existence), and it requires a pre-existing symlink in an allowed directory — the server exposes no symlink-creating tool (
writeuseswx+atomic-rename,moveusesrename), so a client cannot fabricate the symlink through the server. Still a genuine confinement-boundary break worth closing, and it aligns this tool withlist_directory/directory_tree.Alternative fix (if a maintainer prefers)
Keep
fs.statfor in-sandbox symlinks but re-validate first:await validatePath(entryPath)and skip entries that resolve outside the allowed set (the patternsearchFilesWithValidationuses).lstatwas chosen as the minimal change consistent withlist_directory/directory_tree.