Skip to content

fix(filesystem): preserve UNC path prefix during normalization#3920

Closed
Yanhu007 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Yanhu007:fix/unc-path-validation
Closed

fix(filesystem): preserve UNC path prefix during normalization#3920
Yanhu007 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Yanhu007:fix/unc-path-validation

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #3756

On Windows, path.normalize can strip one leading backslash from UNC paths (\\server\share becomes \server\share). path.resolve then interprets this as drive-relative (e.g. C:\server\share), causing isPathWithinAllowedDirectories to always return false for network share paths.

Changes

  • Add safeNormalize() helper that detects UNC paths before normalization and restores the \\ prefix if it gets stripped
  • Replace both path.resolve(path.normalize(...)) calls with safeNormalize()
  • Add UNC path test cases (within/outside/exact match)

On Windows, path.normalize can strip one leading backslash from
UNC paths (\\server\share → \server\share). path.resolve then
interprets the single-backslash path as drive-relative
(e.g. C:\server\share), causing isPathWithinAllowedDirectories
to always return false for UNC network paths.

Add safeNormalize() helper that detects UNC paths and restores
the prefix if normalization strips it. Add tests for UNC paths.

Fixes modelcontextprotocol#3756
@olaservo
Copy link
Copy Markdown
Member

Thanks for working on this! We're going with #3921 which takes a slightly different approach (skipping path.resolve entirely for UNC paths rather than repairing after the fact), and its CI is passing.

That said, your test cases are great — we've asked the #3921 author to add UNC-specific tests based on yours. Appreciate the contribution!

Closing in favor of #3921.


This review was assisted by Claude Code.

@olaservo olaservo closed this Apr 12, 2026
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.

isPathWithinAllowedDirectories fails for UNC paths on Windows (network drives)

2 participants