|
| 1 | +/** |
| 2 | + * Unit tests for file API path traversal security fixes. |
| 3 | + * |
| 4 | + * Run with: npx tsx src/__tests__/unit/files-security.test.ts |
| 5 | + * |
| 6 | + * Tests verify that: |
| 7 | + * 1. isPathSafe correctly prevents path traversal attacks |
| 8 | + * 2. Paths outside the base directory are rejected |
| 9 | + * 3. Symlink-based escapes are caught |
| 10 | + * 4. Edge cases (root, same path, trailing separators) are handled |
| 11 | + */ |
| 12 | + |
| 13 | +import { describe, it, after } from 'node:test'; |
| 14 | +import assert from 'node:assert/strict'; |
| 15 | +import path from 'path'; |
| 16 | +import os from 'os'; |
| 17 | +import fs from 'fs'; |
| 18 | + |
| 19 | +// Import the function under test |
| 20 | +import { isPathSafe } from '../../lib/files'; |
| 21 | + |
| 22 | +describe('isPathSafe', () => { |
| 23 | + it('should allow paths within the base directory', () => { |
| 24 | + assert.equal(isPathSafe('/home/user/project', '/home/user/project/src/index.ts'), true); |
| 25 | + assert.equal(isPathSafe('/home/user/project', '/home/user/project/package.json'), true); |
| 26 | + assert.equal(isPathSafe('/home/user/project', '/home/user/project/src/lib/utils.ts'), true); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should allow the base directory itself', () => { |
| 30 | + assert.equal(isPathSafe('/home/user/project', '/home/user/project'), true); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should reject paths outside the base directory', () => { |
| 34 | + assert.equal(isPathSafe('/home/user/project', '/home/user/other'), false); |
| 35 | + assert.equal(isPathSafe('/home/user/project', '/home/user'), false); |
| 36 | + assert.equal(isPathSafe('/home/user/project', '/etc/passwd'), false); |
| 37 | + assert.equal(isPathSafe('/home/user/project', '/tmp/malicious'), false); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should reject path traversal via ../', () => { |
| 41 | + // path.resolve will normalize these, but the resolved path should be outside base |
| 42 | + const base = '/home/user/project'; |
| 43 | + const traversal = path.resolve(base, '../../etc/passwd'); |
| 44 | + assert.equal(isPathSafe(base, traversal), false); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should reject directory names that are prefixes but not parents', () => { |
| 48 | + // /home/user/project-evil should NOT be allowed under /home/user/project |
| 49 | + assert.equal(isPathSafe('/home/user/project', '/home/user/project-evil/file.txt'), false); |
| 50 | + assert.equal(isPathSafe('/home/user/project', '/home/user/projectx'), false); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should handle Windows-style paths if on Windows', () => { |
| 54 | + if (process.platform === 'win32') { |
| 55 | + assert.equal(isPathSafe('C:\\Users\\user\\project', 'C:\\Users\\user\\project\\src\\index.ts'), true); |
| 56 | + assert.equal(isPathSafe('C:\\Users\\user\\project', 'D:\\other\\file.txt'), false); |
| 57 | + } |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe('File API path traversal scenarios', () => { |
| 62 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codepilot-test-')); |
| 63 | + const projectDir = path.join(tmpDir, 'myproject'); |
| 64 | + const secretFile = path.join(tmpDir, 'secret.txt'); |
| 65 | + |
| 66 | + // Setup test fixtures |
| 67 | + fs.mkdirSync(projectDir, { recursive: true }); |
| 68 | + fs.mkdirSync(path.join(projectDir, 'src'), { recursive: true }); |
| 69 | + fs.writeFileSync(path.join(projectDir, 'index.ts'), 'console.log("hello");\n'); |
| 70 | + fs.writeFileSync(path.join(projectDir, 'src', 'app.ts'), 'export default {};\n'); |
| 71 | + fs.writeFileSync(secretFile, 'TOP SECRET DATA\n'); |
| 72 | + |
| 73 | + it('should allow reading files inside the project', () => { |
| 74 | + const filePath = path.join(projectDir, 'index.ts'); |
| 75 | + assert.equal(isPathSafe(projectDir, filePath), true); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should allow reading files in subdirectories', () => { |
| 79 | + const filePath = path.join(projectDir, 'src', 'app.ts'); |
| 80 | + assert.equal(isPathSafe(projectDir, filePath), true); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should block reading files outside the project via relative path', () => { |
| 84 | + const maliciousPath = path.resolve(projectDir, '..', 'secret.txt'); |
| 85 | + assert.equal(isPathSafe(projectDir, maliciousPath), false); |
| 86 | + // Verify the secret file actually exists (test is meaningful) |
| 87 | + assert.equal(fs.existsSync(maliciousPath), true); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should block reading system files', () => { |
| 91 | + assert.equal(isPathSafe(projectDir, '/etc/passwd'), false); |
| 92 | + assert.equal(isPathSafe(projectDir, '/etc/shadow'), false); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should block reading via encoded traversal after resolution', () => { |
| 96 | + // Even if someone tries URL-encoded ../, path.resolve normalizes it |
| 97 | + const resolved = path.resolve(projectDir, '..', '..', 'etc', 'passwd'); |
| 98 | + assert.equal(isPathSafe(projectDir, resolved), false); |
| 99 | + }); |
| 100 | + |
| 101 | + // Symlink test (only on Unix-like systems) |
| 102 | + if (process.platform !== 'win32') { |
| 103 | + it('should block symlink escape from project directory', () => { |
| 104 | + const symlinkPath = path.join(projectDir, 'escape-link'); |
| 105 | + try { |
| 106 | + fs.symlinkSync('/etc', symlinkPath); |
| 107 | + const resolvedSymlink = fs.realpathSync(path.join(symlinkPath, 'passwd')); |
| 108 | + assert.equal(isPathSafe(projectDir, resolvedSymlink), false); |
| 109 | + } finally { |
| 110 | + try { fs.unlinkSync(symlinkPath); } catch { /* cleanup */ } |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + // Cleanup test fixtures |
| 116 | + after(() => { |
| 117 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('baseDir validation', () => { |
| 122 | + it('should reject baseDir set to root (bypass attempt)', () => { |
| 123 | + // If baseDir=/, every path would pass isPathSafe — must be blocked |
| 124 | + const homeDir = os.homedir(); |
| 125 | + assert.equal(isPathSafe(homeDir, '/'), false); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should reject baseDir outside home directory', () => { |
| 129 | + const homeDir = os.homedir(); |
| 130 | + assert.equal(isPathSafe(homeDir, '/tmp'), false); |
| 131 | + assert.equal(isPathSafe(homeDir, '/etc'), false); |
| 132 | + assert.equal(isPathSafe(homeDir, '/var/log'), false); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should allow baseDir inside home directory', () => { |
| 136 | + const homeDir = os.homedir(); |
| 137 | + const projectDir = path.join(homeDir, 'projects', 'myapp'); |
| 138 | + assert.equal(isPathSafe(homeDir, projectDir), true); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should allow baseDir equal to home directory', () => { |
| 142 | + const homeDir = os.homedir(); |
| 143 | + assert.equal(isPathSafe(homeDir, homeDir), true); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should block files outside home when no baseDir provided (fallback)', () => { |
| 147 | + const homeDir = os.homedir(); |
| 148 | + assert.equal(isPathSafe(homeDir, '/etc/passwd'), false); |
| 149 | + assert.equal(isPathSafe(homeDir, '/tmp/malicious'), false); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should allow files inside home when no baseDir provided (fallback)', () => { |
| 153 | + const homeDir = os.homedir(); |
| 154 | + const filePath = path.join(homeDir, 'documents', 'file.txt'); |
| 155 | + assert.equal(isPathSafe(homeDir, filePath), true); |
| 156 | + }); |
| 157 | +}); |
0 commit comments