|
| 1 | +import {validatePath} from '../utils/fileUtils'; |
| 2 | +import fs from 'fs/promises'; |
| 3 | +import path from 'path'; |
| 4 | +import os from 'os'; |
| 5 | + |
| 6 | +describe('validatePath', () => { |
| 7 | + const testDir = path.join(process.cwd(), 'sample-validate-path'); |
| 8 | + const allowedDirectories = [testDir]; |
| 9 | + |
| 10 | + beforeAll(async () => { |
| 11 | + // Create test directory and ensure it's clean |
| 12 | + await fs.rm(testDir, {recursive: true, force: true}); |
| 13 | + await fs.mkdir(testDir, {recursive: true}); |
| 14 | + }); |
| 15 | + |
| 16 | + afterAll(async () => { |
| 17 | + // Clean up test directory |
| 18 | + await fs.rm(testDir, {recursive: true, force: true}); |
| 19 | + }); |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + // Clean up any leftover symlinks before each test |
| 23 | + try { |
| 24 | + await fs.unlink(path.join(testDir, 'symlink.txt')); |
| 25 | + } catch (error) { |
| 26 | + // Ignore errors if file doesn't exist |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + test('should validate path within allowed directories', async () => { |
| 31 | + const testFilePath = path.join(testDir, 'test.txt'); |
| 32 | + const validatedPath = await validatePath(testFilePath, allowedDirectories); |
| 33 | + expect(validatedPath).toBe(path.resolve(testFilePath)); |
| 34 | + }); |
| 35 | + |
| 36 | + test('should expand home directory path', async () => { |
| 37 | + const homePath = '~/test.txt'; |
| 38 | + const expandedPath = path.join(os.homedir(), 'test.txt'); |
| 39 | + const validatedPath = await validatePath(homePath, [os.homedir()]); |
| 40 | + expect(validatedPath).toBe(path.resolve(expandedPath)); |
| 41 | + }); |
| 42 | + |
| 43 | + test('should throw error for path outside allowed directories', async () => { |
| 44 | + const outsidePath = path.join(process.cwd(), 'outside.txt'); |
| 45 | + await expect(validatePath(outsidePath, allowedDirectories)).rejects.toThrow( |
| 46 | + 'Access denied - path outside allowed directories', |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + test('should handle symlinks within allowed directories pointing to allowed directories', async () => { |
| 51 | + const targetPath = path.join(testDir, 'target.txt'); |
| 52 | + const symlinkPath = path.join(testDir, 'symlink.txt'); |
| 53 | + |
| 54 | + // Create target file |
| 55 | + await fs.writeFile(targetPath, 'test content'); |
| 56 | + await fs.symlink(targetPath, symlinkPath); |
| 57 | + |
| 58 | + const validatedPath = await validatePath(symlinkPath, allowedDirectories); |
| 59 | + expect(validatedPath).toBe(path.resolve(targetPath)); |
| 60 | + |
| 61 | + // Clean up |
| 62 | + await fs.unlink(symlinkPath); |
| 63 | + await fs.unlink(targetPath); |
| 64 | + }); |
| 65 | + |
| 66 | + test('should allow symlinks within allowed directories pointing outside', async () => { |
| 67 | + const outsidePath = path.join(process.cwd(), 'outside.txt'); |
| 68 | + const symlinkPath = path.join(testDir, 'symlink.txt'); |
| 69 | + |
| 70 | + // Create target file outside allowed directories |
| 71 | + await fs.writeFile(outsidePath, 'test content'); |
| 72 | + await fs.symlink(outsidePath, symlinkPath); |
| 73 | + |
| 74 | + const validatedPath = await validatePath(symlinkPath, allowedDirectories); |
| 75 | + expect(validatedPath).toBe(path.resolve(outsidePath)); |
| 76 | + |
| 77 | + // Clean up |
| 78 | + await fs.unlink(symlinkPath); |
| 79 | + await fs.unlink(outsidePath); |
| 80 | + }); |
| 81 | + |
| 82 | + test('should handle non-existent files with valid parent directory', async () => { |
| 83 | + const newDir = path.join(testDir, 'new'); |
| 84 | + await fs.mkdir(newDir, {recursive: true}); |
| 85 | + const newFilePath = path.join(newDir, 'file.txt'); |
| 86 | + const validatedPath = await validatePath(newFilePath, allowedDirectories); |
| 87 | + expect(validatedPath).toBe(path.resolve(newFilePath)); |
| 88 | + }); |
| 89 | + |
| 90 | + test('should throw error for non-existent parent directory', async () => { |
| 91 | + const invalidPath = path.join(testDir, 'nonexistent', 'file.txt'); |
| 92 | + await expect(validatePath(invalidPath, allowedDirectories)).rejects.toThrow( |
| 93 | + 'Parent directory does not exist', |
| 94 | + ); |
| 95 | + }); |
| 96 | +}); |
0 commit comments