Repository navigation
fix(py): contain Skills and Filesystem middleware to configured roots - #7144
huangjeff5 wants to merge 2 commits into
Conversation
Skills (PR6-15):
- Skip skill dirs and SKILL.md files whose real path leaves the skill
root; never open a SKILL.md that is a symlink, FIFO, or over 1 MiB.
use_skill re-checks at read time (lstat, O_NOFOLLOW, fstat identity).
- Coerce YAML name/description to str (`name: 404` no longer fails
every turn), strip control/format chars, collapse whitespace.
- First skill to claim a name wins; later ones are logged and skipped.
- Escape tag openers and clip descriptions in the <skills> catalog.
- use_skill read failures no longer return the host path.
Filesystem (PR6-16):
- Drop the lstrip('/') rewrite. Absolute paths inside the root are used
as-is; absolute paths outside are an explicit error. NUL is rejected.
- read_file offset/limit are ge=0; negative values no longer slice.
- Tool errors are returned as the tool response with the host root
redacted instead of queueing a user turn after the tool turn.
- list_files uses lstat and omits symlinks that resolve outside root.
- Escape the read_file path attribute.
There was a problem hiding this comment.
Code Review
This pull request introduces significant security and robustness improvements to the filesystem sandbox and skills middleware. Key changes include stricter path resolution, host path redaction in tool errors, escaping of tag openers to prevent catalog injection, and the addition of comprehensive adversarial tests. The feedback suggests improving the _redact method in _filesystem.py to use regular expressions with lookaround assertions, preventing accidental partial redactions of sibling directories that share the root path as a prefix.
| def _redact(self, text: str) -> str: | ||
| """Rewrite host root paths in an error message relative to the root.""" | ||
| roots: set[str] = {self._root_abs, os.path.abspath(self.config.root_dir)} | ||
| for root in sorted(roots, key=lambda r: len(r), reverse=True): | ||
| if root == os.path.dirname(root): | ||
| continue # A filesystem root ('/') is no secret, and replacing it mangles every path. | ||
| text = text.replace(root + os.sep, '').replace(root, '.') | ||
| return text |
There was a problem hiding this comment.
The current implementation of _redact uses a simple text.replace(root, '.') which can lead to accidental partial redactions of sibling directories or files that share the root path as a prefix (e.g., /foo/bar_baz would be incorrectly mangled to ._baz if the root is /foo/bar).\n\nTo prevent this, we can use a regular expression with negative lookbehind and lookahead assertions to ensure that the root path is only replaced when it represents a complete path component.
def _redact(self, text: str) -> str:\n \"\"\"Rewrite host root paths in an error message relative to the root.\"\"\"\n import re\n roots: set[str] = {self._root_abs, os.path.abspath(self.config.root_dir)}\n for root in sorted(roots, key=lambda r: len(r), reverse=True):\n if root == os.path.dirname(root):\n continue # A filesystem root ('/') is no secret, and replacing it mangles every path.\n text = text.replace(root + os.sep, '')\n text = re.sub(r'(?<![\\w\\-.])' + re.escape(root) + r'(?![\\w\\-.])', '.', text)\n return text|
Closing: migrated to standalone repository genkit-ai/genkit-python (PR #52): genkit-ai/genkit-python#52 |
Security hardening for Skills and Filesystem middleware against directory traversal and symlink escapes.
Main Changes
name:.lstrip("/")rewrite; absolute paths outside root are explicit errors.