Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/filesystem/path-validation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
import path from 'path';

/**
* Checks if a path is a UNC path (e.g. \\server\share).
*/
function isUNCPath(p: string): boolean {
return p.startsWith('\\\\');
}

/**
* Normalizes a path that may be a UNC path on Windows.
*
* On Windows, path.normalize can strip one leading backslash from UNC paths
* (e.g. \\server\share becomes \server\share), and then path.resolve
* interprets it as a drive-relative path (e.g. C:\server\share). This
* function preserves the UNC prefix through normalization.
*/
function normalizePossiblyUNCPath(p: string): string {
if (isUNCPath(p)) {
let normalized = path.normalize(p);
// path.normalize may strip a leading backslash from UNC paths
if (!normalized.startsWith('\\\\')) {
normalized = '\\' + normalized;
}
return normalized;
}
return path.resolve(path.normalize(p));
}

/**
* Checks if an absolute path is within any of the allowed directories.
*
*
* @param absolutePath - The absolute path to check (will be normalized)
* @param allowedDirectories - Array of absolute allowed directory paths (will be normalized)
* @returns true if the path is within an allowed directory, false otherwise
Expand All @@ -27,7 +54,7 @@ export function isPathWithinAllowedDirectories(absolutePath: string, allowedDire
// Normalize the input path
let normalizedPath: string;
try {
normalizedPath = path.resolve(path.normalize(absolutePath));
normalizedPath = normalizePossiblyUNCPath(absolutePath);
} catch {
return false;
}
Expand All @@ -51,7 +78,7 @@ export function isPathWithinAllowedDirectories(absolutePath: string, allowedDire
// Normalize the allowed directory
let normalizedDir: string;
try {
normalizedDir = path.resolve(path.normalize(dir));
normalizedDir = normalizePossiblyUNCPath(dir);
} catch {
return false;
}
Expand Down
Loading