Repository navigation
fix: prevent path traversal in FileSystemKnowledge.get_file (#8624) - #8726
Merged
harshsinha03 merged 5 commits intoJul 9, 2026
Merged
harshsinha03 merged 5 commits into
harshsinha03 merged 5 commits into
Conversation
agno-agi#8624) FileSystemKnowledge._get_file accepted any absolute path or relative path with '..' segments without checking that the resolved target stayed under base_dir. An LLM-driven tool call to the exposed `get_file` tool could therefore read any file the process had access to (e.g. `../outside_secret.txt`, `/etc/passwd`). Resolve the candidate path with strict=True before any I/O and reject anything that does not satisfy is_relative_to(base_path). Symlinks inside base_dir pointing outside are also rejected because the containment check runs after canonical resolution. The exposed `get_file` tool inherits the fix transparently because it delegates to _get_file. Failures keep the existing return-empty-list contract so callers do not need to handle a new exception. Adds four regression tests covering relative traversal, absolute path escape, symlink escape, and the exposed tool entrypoint.
harshsinha03
approved these changes
Jul 9, 2026
harshsinha03
left a comment
Contributor
There was a problem hiding this comment.
LGTM! Thanks for contributing
9 of 11 tasks
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.
Summary
Fixes the path-traversal vulnerability in
FileSystemKnowledge._get_filefixes #8624.FileSystemKnowledge(base_dir=...)exposes aget_filetool that the agent can call. Before this change,_get_filebuilt the target path asPath(query) if isabs else base_path / querywith no containment check, so a model-driven tool call (or a relative path like../outside_secret.txt, or an absolute path such as/etc/passwd) could read any file the process had access to.The fix builds the path with the shared
safe_join_relative_pathhelper and rejects anything that escapesbase_dir, mirroring the pattern already used byAirflowToolsandagno/skills/agent_skills.py:safe_join_relative_pathrejects absolute paths,..traversal that escapes the base, control characters, and Windows drive/UNC prefixes, and it resolves the target before a containment check — so symlinks insidebase_dirthat point outside are blocked as well.Error handling is also tightened:
_get_fileno longer swallows read errors into an empty list. Genuine read failures (e.g. a file that exists but is unreadable) propagate to theget_filetool wrapper, which surfaces the real cause to the agent — the same shapeAirflowToolsuses:Agent-facing behaviour: files inside
base_dirreturn their content as before; missing / not-a-file / traversal / outside paths all return the safeFile not found: {path}(an escape is indistinguishable from a missing file, so nothing is disclosed); a present-but-unreadable file returns the real error.Type of change
Checklist
./scripts/format.shand./scripts/validate.sh) —ruff formatunchanged,ruff checkclean,mypyreports no errors inknowledge/filesystem.pytest_filesystem_knowledge.pyDuplicate and AI-Generated PR Check
Notes for the maintainer
_list_filesand_grepare not affected: they walkos.walk(self.base_path)and never join a user-supplied root, so no equivalent fix is required._get_filesignature, the tool contract, and all other class behaviour are unchanged. Files insidebase_dircontinue to work exactly as before...traversal, an absolute path outside the base, a symlink inside the base that points outside, and the exposedget_filetool. They follow the existing test file's structure (nestedbase_dirundertmp_path, no manual cleanup).