fix(security): validate sourceDir in MCP handlers and resolve symlinks before WriteFiles#1144
Merged
sqrrrl merged 1 commit intogoogle:masterfrom Apr 9, 2026
Merged
Conversation
…riteFiles - Add validateProjectDir() on sourceDir in create_project and clone_project MCP handlers before withContentDir(). Previously sourceDir was accepted with no boundary check, allowing any path on the filesystem to be set as the content directory via MCP. - Add fs.realpath() resolution in WriteFiles before fs.writeFile() to canonicalize symlinks at write time, and verify resolved path passes isInside() before writing. Previously localPath was written directly after path.relative() with no realpath call anywhere in files.ts. These two bugs chain into a single-call arbitrary file write primitive via the MCP interface. Neither is covered by CVE-2026-4092 or PR google#1140 which addressed a separate .clasp.json srcDir issue in the CLI path. CWE-22: Improper Limitation of a Pathname to a Restricted Directory CWE-59: Improper Link Resolution Before File Access
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
This PR fixes two related security vulnerabilities in the MCP server
layer introduced after CVE-2026-4092 and PR #1140. Both bugs are
independent of those fixes which addressed .clasp.json srcDir traversal
in the CLI path. These bugs exist in src/mcp/server.ts and
src/core/files.ts and are only reachable via the MCP tool interface.
Bug 1: sourceDir MCP parameter not validated
The create_project and clone_project MCP handlers accept a sourceDir
parameter that was passed directly to withContentDir() without going
through validateProjectDir(). The validation applied to projectDir on
lines 290 and 394 was never applied to sourceDir on lines 314 and 425.
An attacker with MCP access could set sourceDir to any location on the
filesystem, bypassing the os.homedir() and process.cwd() boundary check
entirely.
Fix: Apply validateProjectDir(sourceDir) in both handlers before calling
withContentDir(), mirroring the existing projectDir protection.
Bug 2: WriteFiles follows symlinks via unresolved localPath
WriteFiles constructed localPath using path.relative(process.cwd(),
resolvedPath) and passed it directly to fs.writeFile(). The function
path.resolve() does not canonicalize symlinks. realpath() was never
called anywhere in files.ts. Any symlink component in the path was
followed by the OS at write time, after all isInside() string checks
had already passed.
Fix: Resolve localPath through fs.realpath() inside WriteFiles before
mkdir and writeFile, then verify the resolved path passes isInside()
against absoluteContentDir before writing.
Chain
Both bugs together allow a single MCP tool call with a sourceDir
pointing to a symlink inside a permitted directory to achieve arbitrary
file write anywhere on the host filesystem with no user interaction.
Testing
All 199 existing tests pass with these changes.
References
Attack surface: MCP server tool parameters, src/mcp/server.ts and
src/core/files.ts, unpatched in master prior to this PR.
CWE-22: Improper Limitation of a Pathname to a Restricted Directory
CWE-59: Improper Link Resolution Before File Access