Skip to content

refactor(rules): share the AGENTS.md-family rule adapter and guard writes against symlink escapes - #3123

Merged
dyoshikawa merged 4 commits into
mainfrom
resolve-issue-2984-nested-agentsmd-rule-base
Sep 18, 2026
Merged

dyoshikawa merged 4 commits into
mainfrom
resolve-issue-2984-nested-agentsmd-rule-base

Conversation

@dyoshikawa

@dyoshikawa dyoshikawa commented Sep 18, 2026 •

Copy link
Copy Markdown
Owner

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.ts and dsh-rule.ts were three verbatim copies of the same adapter: an AGENTS.md root, nesting driven by agentsmd.subprojectPath, and folding of every other non-root rule into the root file. They now extend a new NestedAgentsmdRule base (src/features/rules/nested-agentsmd-rule.ts) that carries all of that logic; each target only supplies its global directory and tool target through a getFamily() 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 assertWritablePathInsideRoot used 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, writeAiFiles now calls a new writablePathEscapesRoot helper (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 carrying AGENTS.md -> ~/.bashrc or .cursor -> /etc can 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:

  • Dangling links were judged by the link's own (in-root) path because realpath cannot resolve them. writablePathEscapesRoot now 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, so AGENTS.md -> ~/.zshenv is refused even before ~/.zshenv exists.
  • The guard lived only in FeatureProcessor.writeAiFiles. It is now a shared refusesWriteOutsideRoot helper that DirFeatureProcessor.writeAiDirs (skills, subagent directories) and the Hermes / Goose config rewrites in the commands processor also call, so .claude/skills -> /etc is refused too.
  • The Symlinks section of docs/reference/file-formats.md now documents the write-side guard.

Refs #2984

Test plan

  • pnpm cicheck (12441 tests)
  • New tests: nested-agentsmd-rule.test.ts; writablePathEscapesRoot cases in file.test.ts; refusal and in-root-link cases in feature-processor.test.ts

🤖 Generated with Claude Code

https://claude.ai/code/session_019yLN6CVqeZ8HCrjRb9o7mK

…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
dyoshikawa and others added 2 commits September 17, 2026 19:33
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
@dyoshikawa
dyoshikawa merged commit e21f0f0 into main Sep 18, 2026
9 checks passed
@dyoshikawa

Copy link
Copy Markdown
Owner Author

@dyoshikawa Thank you!

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