Skip to content

fix(security): validate sourceDir in MCP handlers and resolve symlinks before WriteFiles#1144

Merged
sqrrrl merged 1 commit intogoogle:masterfrom
g0w6y:fix-mcp-sourceDir-traversal
Apr 9, 2026
Merged

fix(security): validate sourceDir in MCP handlers and resolve symlinks before WriteFiles#1144
sqrrrl merged 1 commit intogoogle:masterfrom
g0w6y:fix-mcp-sourceDir-traversal

Conversation

@g0w6y
Copy link
Copy Markdown
Contributor

@g0w6y g0w6y commented Apr 9, 2026

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

…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
@sqrrrl sqrrrl merged commit 4eb9dca into google:master Apr 9, 2026
1 check passed
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.

2 participants