Skip to content
Closed
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions src/filesystem/__tests__/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,22 @@ describe('Lib Functions', () => {
expect(result).toBe(path.resolve(newFilePath));
});

it('rejects when parent directory does not exist', async () => {
it('walks up to the nearest existing ancestor for nested paths', async () => {
const newFilePath = process.platform === 'win32' ? 'C:\\Users\\test\\nonexistent\\newfile.txt' : '/home/user/nonexistent/newfile.txt';
// Create errors with the ENOENT code

// The target and its immediate parent are absent; the allowed root exists.
const enoentError1 = new Error('ENOENT') as NodeJS.ErrnoException;
enoentError1.code = 'ENOENT';
const enoentError2 = new Error('ENOENT') as NodeJS.ErrnoException;
enoentError2.code = 'ENOENT';

const existingRoot = process.platform === 'win32' ? 'C:\\Users\\test' : '/home/user';

mockFs.realpath
.mockRejectedValueOnce(enoentError1)
.mockRejectedValueOnce(enoentError2);

await expect(validatePath(newFilePath))
.rejects.toThrow('Parent directory does not exist');
.mockRejectedValueOnce(enoentError2)
.mockResolvedValueOnce(existingRoot);

await expect(validatePath(newFilePath)).resolves.toBe(path.resolve(newFilePath));
});

it('resolves relative paths against allowed directories instead of process.cwd()', async () => {
Expand Down
32 changes: 21 additions & 11 deletions src/filesystem/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,29 @@ export async function validatePath(requestedPath: string): Promise<string> {
}
return realPath;
} catch (error) {
// Security: For new files that don't exist yet, verify parent directory
// This ensures we can't create files in unauthorized locations
// Security: For new files/directories, resolve the nearest existing
// ancestor. This permits mkdir({ recursive: true }) while still checking
// the real path of an existing directory for symlink escapes.
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
const parentDir = path.dirname(absolute);
try {
const realParentPath = await fs.realpath(parentDir);
const normalizedParent = normalizePath(realParentPath);
if (!isPathWithinAllowedDirectories(normalizedParent, allowedDirectories)) {
throw new Error(`Access denied - parent directory outside allowed directories: ${realParentPath} not in ${allowedDirectories.join(', ')}`);
let existingAncestor = path.dirname(absolute);
while (true) {
try {
const realAncestorPath = await fs.realpath(existingAncestor);
const normalizedAncestor = normalizePath(realAncestorPath);
if (!isPathWithinAllowedDirectories(normalizedAncestor, allowedDirectories)) {
throw new Error(`Access denied - parent directory outside allowed directories: ${realAncestorPath} not in ${allowedDirectories.join(', ')}`);
}
return absolute;
} catch (ancestorError) {
if ((ancestorError as NodeJS.ErrnoException).code !== 'ENOENT') {
throw ancestorError;
}
}
return absolute;
} catch {
throw new Error(`Parent directory does not exist: ${parentDir}`);
const parent = path.dirname(existingAncestor);
if (parent === existingAncestor) {
throw new Error(`Parent directory does not exist: ${existingAncestor}`);
}
existingAncestor = parent;
}
}
throw error;
Expand Down