(userId: string, path: string)
| 600 | * @param path - The relative path (user-provided) to check |
| 601 | */ |
| 602 | export async function ensureUserPathIsValidAndSecurelyAccessible(userId: string, path: string): Promise<void> { |
| 603 | const userRootPath = join(await AppConfig.getFilesRootPath(), userId, '/'); |
| 604 | |
| 605 | const fullPath = join(userRootPath, path); |
| 606 | |
| 607 | const resolvedFullPath = `${resolve(fullPath)}/`; |
| 608 | |
| 609 | // Normalize path separators for consistent comparison on Windows |
| 610 | const normalizedUserRootPath = userRootPath.replaceAll('\\', '/'); |
| 611 | const normalizedResolvedFullPath = resolvedFullPath.replaceAll('\\', '/'); |
| 612 | |
| 613 | if (!normalizedResolvedFullPath.startsWith(normalizedUserRootPath)) { |
| 614 | throw new Error('Invalid file path'); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * Ensures the file share path is valid and securely accessible (meaning it's not trying to access files outside of the file share's root directory). |
no test coverage detected