Skip to content

fix(py/middleware)!: contain skills and filesystem tools to configured roots - #52

Merged
huangjeff5 merged 7 commits into
mainfrom
fix/py-1.0-pr6-middleware-sandbox
Nov 11, 2026
Merged

huangjeff5 merged 7 commits into
mainfrom
fix/py-1.0-pr6-middleware-sandbox

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Filesystem and Skills middleware let model-chosen paths and skill files reach outside their configured roots. Filesystem rewrote absolute paths into the root, followed symlinks out of it, and leaked the host root path in error messages. Skills let a SKILL.md frontmatter name: shadow another skill, and read any SKILL.md, including symlinks and huge files. Both now check the real path first and keep everything inside the root.

# 1. Bound filesystem tools to a kitchen workspace
fs = Filesystem(root_dir='./kitchen')
response = await ai.generate(prompt='Read the supplier invoice.', use=[fs])

# model calls read_file(file_path='/etc/hosts')
# before
# => leading '/' stripped, silently reads ./kitchen/etc/hosts
# after
# => Tool "read_file" failed: Absolute path '/etc/hosts' escapes the root directory; use a path relative to the root.

# model calls list_files() where specials -> ../outside
# before
# => lists 'specials' with the outside target's type and size
# after
# => 'specials' is left out

Changes

  • Absolute paths are checked, not rewritten: /etc/hosts used to become {root}/etc/hosts, and {root}/menu.md became {root}/{root}/menu.md. Absolute paths now resolve as-is: inside the root they work, outside they return an error the model can correct.
  • Symlinks are resolved before any check or I/O: Every operation checks the real path against the root. list_files leaves out symlinks that point outside the root and special files (FIFOs, sockets, devices). write_file refuses existing non-regular files instead of hanging on a FIFO, and edit_file enforces the 10 MiB cap before reading and before writing.
  • Tool errors stay in the tool turn, without host paths: A failed call used to add a user message with the raw error, including the absolute host root, between the tool response and the next model turn. The error is now the tool response itself, with the root path stripped.
  • A skill's name is its directory name: skills/allergy-check/SKILL.md is always allergy-check; frontmatter name: is ignored, so one skill can't claim another's name. SKILL.md must be an exact-case regular file under 1 MiB and not a symlink. Symlinked skill directories are still followed. Skill descriptions are escaped so they can't close the <skills> block in the system prompt, and skill paths are scanned once per generate() call instead of on every hook and tool call.

Breaking changes

  • A skill whose frontmatter name: differs from its directory name is now listed and loaded under the directory name. Prompts or code that refer to the frontmatter name get Unknown skill:
    # skills/allergy_check/SKILL.md has `name: allergy-check`
    # before
    use_skill(skill_name='allergy-check')
    # after: rename the directory, or use the directory name
    use_skill(skill_name='allergy_check')
  • A skill is skipped if its SKILL.md is a symlink or over 1 MiB (logged as a warning), or not spelled exactly SKILL.md (a skill.md used to load on case-insensitive filesystems such as macOS):
    # before: loaded
    skills/plating/SKILL.md -> ~/shared/plating.md
    # after: link the directory instead
    skills/plating -> ~/shared/plating/

Cross-SDK

Conversation history on a failed Filesystem call changes shape. Python now puts the error in the tool turn, as JS and Go already do. JS, Go, and Dart owners need no changes.

// before
[{"role":"tool","content":[{"toolResponse":{"name":"read_file","output":"Tool call failed; see user message below for details."}}]},
 {"role":"user","content":[{"text":"Tool \"read_file\" failed: Path '../outside' escapes the root directory."}]}]
// after
[{"role":"tool","content":[{"toolResponse":{"name":"read_file","output":"Tool \"read_file\" failed: Path '../outside' escapes the root directory."}}]}]

@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from e4face7 to 3f06c40 Compare November 9, 2026 19:08
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from 3f06c40 to c23d2da Compare November 9, 2026 19:57
@huangjeff5
huangjeff5 changed the base branch from main to fix/py-1.0-pr5-web-serving November 9, 2026 19:57
@huangjeff5
huangjeff5 added this pull request to stack #59 November 9, 2026 19:58
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from c23d2da to 6843c66 Compare November 9, 2026 20:02
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from 6843c66 to 3eccbe6 Compare November 9, 2026 20:21
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from 3eccbe6 to 597b801 Compare November 9, 2026 21:23
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from 597b801 to ef6c441 Compare November 9, 2026 21:28
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from ef6c441 to 2b13e02 Compare November 9, 2026 21:53
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from 2b13e02 to 3c47f6b Compare November 9, 2026 22:03
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from 3c47f6b to a211f0f Compare November 9, 2026 23:19
@huangjeff5 huangjeff5 changed the title fix(py): contain Skills and Filesystem middleware to configured roots fix(py/middleware): contain skills and filesystem tools to configured roots Nov 10, 2026
@huangjeff5
huangjeff5 requested a review from hilariie November 10, 2026 04:07
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from a211f0f to 048d352 Compare November 10, 2026 07:44
@huangjeff5
huangjeff5 removed this pull request from stack #59 November 10, 2026 18:16
@huangjeff5
huangjeff5 added this pull request to stack #75 November 10, 2026 18:16
@huangjeff5
huangjeff5 removed this pull request from stack #75 November 10, 2026 18:16
@huangjeff5
huangjeff5 added this pull request to stack #76 November 10, 2026 18:17
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from 048d352 to e7b93f7 Compare November 10, 2026 18:37
@huangjeff5
huangjeff5 removed this pull request from stack #76 November 10, 2026 18:38
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from e3e8d6e to 65ba206 Compare November 11, 2026 02:35
@huangjeff5
huangjeff5 removed this pull request from stack #82 November 11, 2026 02:36
@huangjeff5
huangjeff5 added this pull request to stack #88 November 11, 2026 02:37
@huangjeff5 huangjeff5 changed the title fix(py/middleware): contain skills and filesystem tools to configured roots fix(py/middleware)!: contain skills and filesystem tools to configured roots Nov 11, 2026
@huangjeff5
huangjeff5 removed this pull request from stack #88 November 11, 2026 06:22
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from 65ba206 to aba2bcd Compare November 11, 2026 06:22
@huangjeff5
huangjeff5 added this pull request to stack #89 November 11, 2026 06:23

@pavelgj pavelgj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from aba2bcd to d7b803f Compare November 11, 2026 16:21
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from d7b803f to a3d8c7b Compare November 11, 2026 17:31
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.
Skills are keyed by directory name since b159ca983, so the 64-char
frontmatter name cap and its fallback test no longer do anything.
README showed frontmatter name: as if it named the skill. Skills are
keyed by directory now, out-of-root skill dirs and symlinked SKILL.md
are skipped, and absolute filesystem paths outside the root are refused.
…ll path win

Restores pre-PR Python behavior and matches Go (TestSkillsFollowsSymlinkedSkillDirectory,
TestSkillsCollisionLaterPathWins). A SKILL.md that is itself a symlink is
still never read, and use_skill names stay catalog keys, never paths.
@huangjeff5
huangjeff5 removed this pull request from stack #89 November 11, 2026 18:06
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr6-middleware-sandbox branch from a3d8c7b to 8f7c172 Compare November 11, 2026 18:07
@huangjeff5
huangjeff5 changed the base branch from fix/py-1.0-pr5-web-serving to main November 11, 2026 18:07
@huangjeff5
huangjeff5 added this pull request to stack #103 November 11, 2026 18:40
@huangjeff5
huangjeff5 merged commit afa4f4c into main Nov 11, 2026
17 of 18 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-1.0-pr6-middleware-sandbox branch November 11, 2026 18:40
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.

2 participants