Skip to content

fix(py): contain Skills and Filesystem middleware to configured roots - #7144

Closed
huangjeff5 wants to merge 2 commits into
mainfrom
fix/py-1.0-pr6-middleware-sandbox
Closed

huangjeff5 wants to merge 2 commits into
mainfrom
fix/py-1.0-pr6-middleware-sandbox

Conversation

@huangjeff5

Copy link
Copy Markdown
Contributor

Security hardening for Skills and Filesystem middleware against directory traversal and symlink escapes.

Main Changes

  • Key skills by directory name: Aligned skill identification with JS and Go by using directory name rather than frontmatter name:.
  • Symlink containment: Rejected symlinks resolving outside configured skill roots with explicit warnings.
  • Filesystem root containment: Dropped lstrip("/") rewrite; absolute paths outside root are explicit errors.
  • Catalog injection protection: Escaped closing tags and sanitized control characters in prompt catalogs.

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +192 to +199
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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

@huangjeff5

Copy link
Copy Markdown
Contributor Author

Closing: migrated to standalone repository genkit-ai/genkit-python (PR #52): genkit-ai/genkit-python#52

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