fix(filesystem): normalize UNC paths in access check#3601
fix(filesystem): normalize UNC paths in access check#3601nielskaspers wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
Use normalizePath() from path-utils instead of raw path.resolve(path.normalize()) in isPathWithinAllowedDirectories(). The normalizePath function has explicit UNC path handling that preserves the \\ prefix, whereas path.resolve can mangle UNC paths on Windows, causing subdirectory access checks to fail even when the parent UNC share is in the allowed directories list. Fixes modelcontextprotocol#3527
|
@claude review |
|
Claude finished @cliffhall's task in 1m 45s —— View job PR Review
Summary: The fix is correct and well-targeted. It resolves a real bug where UNC paths like What Changed
Before: normalizedPath = path.resolve(path.normalize(absolutePath));
// ...
normalizedDir = path.resolve(path.normalize(dir));After: normalizedPath = normalizePath(path.resolve(normalizePath(absolutePath)));
// ...
normalizedDir = normalizePath(path.resolve(normalizePath(dir)));The double-wrap (
Issues FoundMissing test for the exact bug scenario (minor, but worth adding) The existing UNC test in // Missing coverage for issue #3527 specifically
const allowed = ['\\\\server\\share'];
expect(isPathWithinAllowedDirectories('\\\\server\\share\\subdir', allowed)).toBe(true);
expect(isPathWithinAllowedDirectories('\\\\server\\share\\subdir\\file.txt', allowed)).toBe(true);Windows-only test coverage (expected limitation, but worth noting) All UNC path tests are guarded by Security ReviewThe security properties are fully preserved:
VerdictThis is a clean, focused fix. The double-wrap normalization is correct and the root cause analysis is accurate. The only gap is a test targeting the specific bug scenario from #3527 (share root as allowed dir). Everything else looks good. |
Summary
UNC/network share paths (
\\server\share\subdir) fail the access check inisPathWithinAllowedDirectories()despite the parent share being in the allowed directories list. The root listing works but any subdirectory access fails.Root cause:
path-validation.tsusedpath.resolve(path.normalize(...))to normalize paths, which can mangle the\\prefix on UNC paths on Windows. Meanwhile,path-utils.tsalready has anormalizePath()function with explicit UNC path handling that preserves the prefix correctly.Fix: Use
normalizePath()frompath-utils.tsinisPathWithinAllowedDirectories()instead of rawpath.resolve(path.normalize(...)), ensuring both the requested path and allowed directories are normalized consistently for UNC paths.All 145 existing tests pass.
Fixes #3527