filesystem: FS_SEARCH_EXCLUDE_PREFIXES is lexical; should excludes be canonical/symlink-aware?
Context. The proposed fix for #4162 (PR #4212) adds FS_SEARCH_EXCLUDE_PREFIXES as a fast lexical prefilter that refuses recursive search_files / directory_tree on configured slow roots, typically macOS CloudStorage / FileProvider paths.
Discussion on #4162 surfaced that the check is purely lexical: a symlink such as /allowed/sneaky whose target lives under an excluded prefix is not caught by the prefix check, and may still reach the slow provider path during canonicalisation / validation.
The case for keeping the #4162 fix scoped to the lexical prefilter, as discussed in that thread:
- The bypass is pre-existing behaviour — the original
searchFilesWithValidationexclude check is also lexical. validatePathcontinues to enforce theallowed_directoriesescape boundary via its ownrealpath, so the security boundary is unaffected. This question is about whether excludes should be symlink-aware.- The impact of the bypass is performance / hang risk from reaching a slow provider path, not an authorization bypass.
- An unbounded
realpath-before-compare exclude check risks re-introducing the kind of hang #4162 is trying to avoid.
Design question. Should excludes be hardened against symlink bypass?
One possible shape is two-stage:
- Fast lexical check first, preserving current cheap behaviour.
- If not excluded lexically, optionally perform bounded canonicalisation, e.g.
withFsTimeout(fs.realpath(...)), then re-check the canonical path against excluded prefixes, with an explicit fail-policy on timeout — deny, allow, or return a typed error.
This affects both search_files and directory_tree, so it seems like a design decision rather than a drop-in fix. Not gating #4162; filing this separately so the symlink-aware exclude question is not lost.
This looks worth pinning down as a protocol-level regression rather than only an implementation bug.
For filesystem search excludes should be canonical or symlink-aware, the acceptance test I would want is a minimal client/server fixture that records the exact JSON-RPC or HTTP exchange, then asserts both the wire result and the local lifecycle state after the failure path. In MCP deployments, the dangerous failures are often not just wrong return values; they are pending requests, dropped notifications, mismatched session state, or errors that get translated into a generic response and become impossible to diagnose.
A useful fix boundary would preserve three signals: the protocol error sent to the peer, the local exception or cancellation reason available to application code, and a trace/log field that ties the two together. That makes the behavior debuggable for agent hosts without requiring every host to special-case this SDK detail.
Read through main for how far the symlink case actually goes (reasoned from the code, not run: I have no Node toolchain here).
- In-tree symlinks are mostly inert. Both walkers recurse only on
entry.isDirectory()fromreaddir({ withFileTypes: true }), and aDirentfor a symlink reportsisDirectory() === false(it describes the link, not the target). So/allowed/sneaky -> /Users/x/Library/CloudStorage/...is never entered bysearchFilesWithValidation(lib.ts:676) orbuildTree(server.ts:652);directory_treelists it as a"file"entry. - The one touch is in
search_files:validatePath(fullPath, ...)runs on every entry before the exclude check (lib.ts:658), so each symlink costs onerealpathon its target. That is one call per link, not a traversal, but on a stalled FileProvider mount it can still block, which is why awithFsTimeoutaround it matters regardless of excludes. - The case the lexical prefilter does miss is the root argument itself:
search_files/directory_treewithpath: "/allowed/sneaky".validatePathresolves it to the excluded target and the recursion then starts there, sincerootPathis not aDirent. That is the one place I would add the bounded canonical re-check.
On the fail policy for the timeout: deny with a typed error seems right for this feature, since the point of the prefix list is to avoid the slow mount and an "allow on timeout" would just reproduce the hang with a delay.
Triage: Priority Low (total 3)
- Severity 2: A design follow-up on making search excludes symlink-aware, which only becomes actionable if the lexical exclude from #4212 lands; the allowed-directory boundary itself is enforced separately by validatePath.
- Urgency 1: no time pressure
- Bonuses: none
Board: Servers V2 (#43), Status Incoming, awaiting maintainer review (no milestone yet). Part of the backlog triage in #4875.
Superseded by #5326. The lexical FS_SEARCH_EXCLUDE_PREFIXES proposed in #4212 never landed: that PR was closed. #4162 was fixed instead by bounding every realpath/readdir with a timeout and capping recursive walks, each failing with a typed error.
That covers the symlink case raised here as well. A search_files or directory_tree root that is a symlink into a slow provider is resolved through the bounded realpath, and its walk is bounded too. In-tree symlinks are still never followed.
With no exclude-prefix list there is nothing to make symlink-aware, so closing as superseded.