feat(filesystem): add --follow-symlinks and --symlink-depth options#3678
feat(filesystem): add --follow-symlinks and --symlink-depth options#3678CodeForgeNet wants to merge 3 commits intomodelcontextprotocol:mainfrom
Conversation
Closes modelcontextprotocol#3457 - Add followSymlinks and symlinkMaxDepth policy to lib.ts - Modify validatePath() to use lstat + hop-by-hop resolution when enabled - Circular symlink detection via visited Set - Filter -- flags from dirArgs before building allowedDirectories - 13 new test cases covering all symlink scenarios - Default behavior unchanged (followSymlinks: false)
|
This implements the feature requested in #3457. Happy to make any changes based on feedback. |
|
@claude review |
|
Claude finished @cliffhall's task in 2m 52s —— View job PR Review: feat(filesystem): add --follow-symlinks and --symlink-depth options
Overall the implementation is well-structured and the circular symlink detection is correct. The default behavior is genuinely unchanged. I have a few findings ranging from a write-access security implication that needs clearer documentation, to a correctness issue in the depth-counting logic, to a handful of code-quality items. Security: Write access through symlinks is not called out in the README
The README only says: "Allow symlinks to point to targets outside allowed directories" — nothing about writes. A user enabling this flag for NFS read access probably doesn't realize they've also enabled writes to arbitrary outside paths via a planted symlink. Recommendation: Add a prominent note in
Correctness: Double
|
| Severity | Finding |
|---|---|
| 🔴 Should address | Document write-access implications of --follow-symlinks in README |
| 🟡 Should fix | Remove redundant double fs.realpath call in non-symlink path |
| 🟡 Should fix | Correct test hop-count comments (says 2 hops, actually 3) |
| 🟡 Should fix | Replace test early-returns with vitest skip so CI shows accurate skip counts |
| 🟠 Consider | Remove optional chaining on stats?.isSymbolicLink() — fix the mock instead |
| 🔵 Nice to have | Warn at startup if --symlink-depth is set without --follow-symlinks |
The core logic (hop-by-hop resolution, depth enforcement, circular detection) is correct. The security model is sound given the flag is opt-in. The write-access documentation gap is the most important thing to address before merging.
Closes #3457
The filesystem server currently blocks any symlink whose resolved target
falls outside the allowed directories -- no exceptions. This works well
as a default but is too rigid for common Unix workflows where symlinks
point to external storage, shared mounts, or locations managed outside
the server's root.
This PR adds two optional CLI flags:
--follow-symlinksto opt in tocross-boundary symlink resolution, and
--symlink-depth=Nto cap howmany hops outside allowed directories are permitted (default: 1).
The default behavior is unchanged. If you're not using these flags,
nothing about path validation is different.
Server Details
Motivation and Context
Users with symlinks into mounted drives, shared NFS paths, or external
directories kept having to add those paths to their allowed list manually.
The request was for an opt-in way to follow symlinks outside the boundary
without giving up all security enforcement.
The depth limit keeps it bounded: you choose how many hops outside the
allowed directories are acceptable before the server blocks access.
How Has This Been Tested?
13 new test cases in
__tests__/symlinks.test.tscovering:All existing tests (53 in
path-validation.test.ts) still pass.Breaking Changes
None. The flags are opt-in. Existing configs work exactly as before.
Types of changes
Checklist
Additional context
Circular symlinks are detected via a visited Set and throw a clear error
rather than looping. The depth counter only increments for hops that land
outside allowed directories -- hops within allowed dirs don't count against
the limit.