Repository navigation
refactor(rules): share the AGENTS.md-family rule adapter and guard writes against symlink escapes - #3123
Merged
Conversation
…ites against symlink escapes Pool, Vibe and DeepSeek Harness read instructions exactly the way the AGENTS.md standard describes them, and their rule adapters had grown into three verbatim copies. They now extend a shared NestedAgentsmdRule that takes the global directory and tool target from a getFamily() override. FeatureProcessor.writeAiFiles judged the output path only as spelled. It now resolves the nearest existing ancestor and skips, with a warning, a target that really sits outside the output root through a symbolic link, so a link a checked-out repository carries cannot redirect a write. Links that stay inside the root still pass, which keeps dotfiles checkouts linked from the home directory working under --global. Refs #2984 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_019yLN6CVqeZ8HCrjRb9o7mK
Review of the write-side link guard found two gaps: - A dangling link slipped through: `lstat` accepts it, `realpath` cannot resolve it and fell back to the link's own in-root path, so a link at `AGENTS.md` pointing at a `~/.zshenv` that does not exist yet was written through. `writablePathEscapesRoot` now follows a dangling link to the target it would create (relative targets from the link's directory, chains capped at 40 hops) and judges that path instead. - Only `FeatureProcessor.writeAiFiles` asked. `DirFeatureProcessor.writeAiDirs` (skills, subagent directories) and the Hermes / Goose config rewrites in the commands processor now ask the same shared `refusesWriteOutsideRoot` helper, so one warning describes the one behavior. The Symlinks section of the file-formats reference documents the write-side guard beside the discovery-side rule. Refs #2984 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_019yLN6CVqeZ8HCrjRb9o7mK
…ery file of a directory unit The write guard resolved a dangling link's target lexically, so a target spelled `sub/../victim` folded the `..` away before `sub` — itself a link out of the root — was looked at, and the write landed outside the root. The guard now walks the path segment by segment the way the OS does, splicing a dangling link's target into the walk and replacing a resolvable link by what it resolves to, and it answers rather than throws on a link cycle or a path spelled below a file. `writeAiDirs` guarded only the directory, so a `SKILL.md` or supporting file that is itself a link out of the root was still written through; every path the directory unit writes is checked first, and the Hermes user-config rewrite in the project-plugin activation asks the same guard the commands processor already does. Refs #2984 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_019yLN6CVqeZ8HCrjRb9o7mK
A dangling link whose absolute target spells `sub/../victim` was passed through `resolve`, which folds the `..` away before `sub` is looked up, so a link `sub` leading out of the root let the write land outside it. Split the target (and the entry path) into raw segments so the walk sees every `..` where it is spelled. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_019yLN6CVqeZ8HCrjRb9o7mK
Owner
Author
|
@dyoshikawa Thank you! |
This was referenced Oct 2, 2026
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
Resolves the two remaining items of #2984 (items 2 and 4 were closed out earlier by #3091 and the upstream Windows-path statement).
Item 1 — shared AGENTS.md-family rule adapter
pool-rule.ts,vibe-rule.tsanddsh-rule.tswere three verbatim copies of the same adapter: anAGENTS.mdroot, nesting driven byagentsmd.subprojectPath, and folding of every other non-root rule into the root file. They now extend a newNestedAgentsmdRulebase (src/features/rules/nested-agentsmd-rule.ts) that carries all of that logic; each target only supplies its global directory and tool target through agetFamily()override, and keeps the vendor-specific JSDoc describing its discovery walk. The three adapters shrink from ~200 lines each to ~20. The existing per-target tests are unchanged and still pass; a small base-class test covers the family hook.Item 3 — symlink guard on every writer
Decision recorded here: the strict
assertWritablePathInsideRootused by the sweep paths was not adopted for ordinary writes, because it refuses every symbolic link and would break the common dotfiles setup (~/.claude -> ~/dotfiles/.claude) under--global, as the triage comment on the issue anticipated.Instead,
writeAiFilesnow calls a newwritablePathEscapesRoothelper (src/utils/file.ts) before reading or writing a target. It resolves the nearest existing ancestor of the output path and skips the file, with a warning, only when that real path sits outside the output root. A checked-out repository carryingAGENTS.md -> ~/.bashrcor.cursor -> /etccan therefore no longer redirect a write, while links that stay inside the root keep working. Paths whose root or intermediate directories do not exist yet pass, since no link can be on the way.Two gaps found in review are closed in the follow-up commit:
realpathcannot resolve them.writablePathEscapesRootnow follows a dangling link to the target it would create (relative targets resolved from the link's directory, chains capped at 40 hops) and judges that path, soAGENTS.md -> ~/.zshenvis refused even before~/.zshenvexists.FeatureProcessor.writeAiFiles. It is now a sharedrefusesWriteOutsideRoothelper thatDirFeatureProcessor.writeAiDirs(skills, subagent directories) and the Hermes / Goose config rewrites in the commands processor also call, so.claude/skills -> /etcis refused too.docs/reference/file-formats.mdnow documents the write-side guard.Refs #2984
Test plan
pnpm cicheck(12441 tests)nested-agentsmd-rule.test.ts;writablePathEscapesRootcases infile.test.ts; refusal and in-root-link cases infeature-processor.test.ts🤖 Generated with Claude Code
https://claude.ai/code/session_019yLN6CVqeZ8HCrjRb9o7mK