Skip to content

Commit 203aa09

Browse files
committed
fix(filesystem): preserve UNC paths in access validation
On Windows, path.resolve() converts UNC paths like \server\share to C:\server\share, corrupting the path and breaking access checks for network shares. Added normalizePathSafe() to detect and preserve UNC paths before normalization, ensuring subdirectories under UNC shares are correctly validated as being within allowed directories. Fixes modelcontextprotocol#3527
1 parent f424458 commit 203aa09

2 files changed

Lines changed: 81 additions & 4 deletions

File tree

‎src/filesystem/__tests__/path-validation.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,40 @@ describe('Path Validation', () => {
439439
expect(isPathWithinAllowedDirectories('\\\\other\\share\\project', allowed)).toBe(false);
440440
}
441441
});
442+
443+
it('allows UNC path subdirectories (GitHub issue #3527)', () => {
444+
// This test verifies that UNC paths like \\server\share\subdir are correctly
445+
// recognized as being within allowed directories like \\server\share.
446+
// The bug was that path.resolve() on Windows corrupts UNC paths by prepending
447+
// the current drive letter (e.g., \\server\share becomes C:\server\share).
448+
449+
// Test exact match of UNC share root
450+
const allowedRoot = ['\\\\192.168.4.96\\Mega'];
451+
expect(isPathWithinAllowedDirectories('\\\\192.168.4.96\\Mega', allowedRoot)).toBe(true);
452+
453+
// Test subdirectories under UNC share - this was failing before the fix
454+
expect(isPathWithinAllowedDirectories('\\\\192.168.4.96\\Mega\\Drops', allowedRoot)).toBe(true);
455+
expect(isPathWithinAllowedDirectories('\\\\192.168.4.96\\Mega\\00R RPGs', allowedRoot)).toBe(true);
456+
expect(isPathWithinAllowedDirectories('\\\\192.168.4.96\\Mega\\deeply\\nested\\path', allowedRoot)).toBe(true);
457+
expect(isPathWithinAllowedDirectories('\\\\192.168.4.96\\Mega\\file.txt', allowedRoot)).toBe(true);
458+
459+
// Test that different shares on same server are blocked
460+
expect(isPathWithinAllowedDirectories('\\\\192.168.4.96\\Plex', allowedRoot)).toBe(false);
461+
expect(isPathWithinAllowedDirectories('\\\\192.168.4.96\\Plex\\Movies', allowedRoot)).toBe(false);
462+
463+
// Test that different servers are blocked
464+
expect(isPathWithinAllowedDirectories('\\\\otherserver\\share\\path', allowedRoot)).toBe(false);
465+
466+
// Test with multiple UNC allowed directories
467+
const allowedMultiple = ['\\\\192.168.4.96\\Mega', '\\\\192.168.4.96\\Plex'];
468+
expect(isPathWithinAllowedDirectories('\\\\192.168.4.96\\Mega\\Drops', allowedMultiple)).toBe(true);
469+
expect(isPathWithinAllowedDirectories('\\\\192.168.4.96\\Plex\\Movies', allowedMultiple)).toBe(true);
470+
expect(isPathWithinAllowedDirectories('\\\\192.168.4.96\\OtherShare\\file', allowedMultiple)).toBe(false);
471+
472+
// Test UNC path prefix attacks are blocked
473+
expect(isPathWithinAllowedDirectories('\\\\192.168.4.96\\MegaBackup', allowedRoot)).toBe(false);
474+
expect(isPathWithinAllowedDirectories('\\\\192.168.4.96\\MegaExtra\\file', allowedRoot)).toBe(false);
475+
});
442476
});
443477

444478
describe('Symlink Tests', () => {

‎src/filesystem/path-validation.ts‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
import path from 'path';
22

3+
/**
4+
* Normalizes a path without corrupting UNC paths.
5+
* On Windows, path.resolve() converts UNC paths like \\server\share to C:\server\share,
6+
* which breaks UNC path handling. This function preserves UNC paths.
7+
*
8+
* @param p - The path to normalize
9+
* @returns Normalized path
10+
*/
11+
function normalizePathSafe(p: string): string {
12+
// Check if it's a UNC path (starts with \\ or //) BEFORE normalizing
13+
// We must check this first because path.normalize on macOS corrupts UNC paths
14+
if (p.startsWith('\\\\') || p.startsWith('//')) {
15+
// UNC paths: normalize slashes to backslashes and remove redundant slashes
16+
// but preserve the leading \\
17+
let normalized = p.replace(/\//g, '\\');
18+
// Ensure exactly two leading backslashes (not more, not less)
19+
normalized = normalized.replace(/^\\+/, '\\\\');
20+
// Normalize any double backslashes in the rest of the path to single
21+
// but be careful not to break the leading \\
22+
const rest = normalized.substring(2).replace(/\\+/g, '\\');
23+
return '\\\\' + rest;
24+
}
25+
26+
// For non-UNC paths, use path.normalize then path.resolve
27+
const normalized = path.normalize(p);
28+
return path.resolve(normalized);
29+
}
30+
331
/**
432
* Checks if an absolute path is within any of the allowed directories.
533
*
@@ -27,13 +55,15 @@ export function isPathWithinAllowedDirectories(absolutePath: string, allowedDire
2755
// Normalize the input path
2856
let normalizedPath: string;
2957
try {
30-
normalizedPath = path.resolve(path.normalize(absolutePath));
58+
normalizedPath = normalizePathSafe(absolutePath);
3159
} catch {
3260
return false;
3361
}
3462

3563
// Verify it's absolute after normalization
36-
if (!path.isAbsolute(normalizedPath)) {
64+
// UNC paths are always absolute, as are resolved paths
65+
const isUncPath = normalizedPath.startsWith('\\\\');
66+
if (!isUncPath && !path.isAbsolute(normalizedPath)) {
3767
throw new Error('Path must be absolute after normalization');
3868
}
3969

@@ -51,13 +81,14 @@ export function isPathWithinAllowedDirectories(absolutePath: string, allowedDire
5181
// Normalize the allowed directory
5282
let normalizedDir: string;
5383
try {
54-
normalizedDir = path.resolve(path.normalize(dir));
84+
normalizedDir = normalizePathSafe(dir);
5585
} catch {
5686
return false;
5787
}
5888

5989
// Verify allowed directory is absolute after normalization
60-
if (!path.isAbsolute(normalizedDir)) {
90+
const isUncDir = normalizedDir.startsWith('\\\\');
91+
if (!isUncDir && !path.isAbsolute(normalizedDir)) {
6192
throw new Error('Allowed directories must be absolute paths after normalization');
6293
}
6394

@@ -81,6 +112,18 @@ export function isPathWithinAllowedDirectories(absolutePath: string, allowedDire
81112
return pathDrive === dirDrive && normalizedPath.startsWith(normalizedDir.replace(/\\?$/, '\\'));
82113
}
83114

115+
// Special handling for UNC paths
116+
// Both paths must be UNC paths for a valid match
117+
if (isUncPath && isUncDir) {
118+
// For UNC paths, use backslash as separator
119+
return normalizedPath.startsWith(normalizedDir + '\\');
120+
}
121+
122+
// If one is UNC and the other isn't, they can't match
123+
if (isUncPath !== isUncDir) {
124+
return false;
125+
}
126+
84127
return normalizedPath.startsWith(normalizedDir + path.sep);
85128
});
86129
}

0 commit comments

Comments
 (0)