|
1 | | -import { validateMimeTypeAndExtensionMatch, validateVectorStorePath } from '../src/validator' |
| 1 | +import { isPathTraversal, isUnsafeFilePath, validateMimeTypeAndExtensionMatch, validateVectorStorePath } from '../src/validator' |
2 | 2 | import path from 'path' |
3 | 3 | import { getUserHome } from '../src/utils' |
4 | 4 |
|
| 5 | +describe('isPathTraversal', () => { |
| 6 | + describe('returns true for dangerous patterns', () => { |
| 7 | + it.each([ |
| 8 | + ['directory traversal (..)', '../etc/passwd'], |
| 9 | + ['multiple levels of traversal', '../../sensitive'], |
| 10 | + ['bare double-dot', '..'], |
| 11 | + ['Windows absolute path', 'C:\\windows'], |
| 12 | + ['Windows absolute path with forward slash', 'C:/windows'], |
| 13 | + ['Windows absolute path with leading whitespace', ' C:\\windows'], |
| 14 | + ['UNC path', '\\\\server\\share'], |
| 15 | + ['URL encoded dot (%2e)', '%2e%2e/etc'], |
| 16 | + ['URL encoded dot uppercase (%2E)', '%2E%2E'], |
| 17 | + ['mixed encoding (.%2e)', '.%2e/etc'], |
| 18 | + ['mixed encoding (%2e.)', '%2e./etc'], |
| 19 | + ['URL encoded forward slash (%2f)', '%2f'], |
| 20 | + ['URL encoded forward slash uppercase (%2F)', '%2F'], |
| 21 | + ['URL encoded backslash (%5c)', '%5c'], |
| 22 | + ['URL encoded backslash uppercase (%5C)', '%5C'], |
| 23 | + ['null byte', 'path\0name'], |
| 24 | + ['URL encoded null byte (%00)', 'path%00name'], |
| 25 | + ['absolute Unix path', '/etc/passwd'], |
| 26 | + ['absolute Unix root', '/'] |
| 27 | + ])('should detect %s: %s', (_description, input) => { |
| 28 | + expect(isPathTraversal(input)).toBe(true) |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + describe('returns false for safe inputs', () => { |
| 33 | + it.each([ |
| 34 | + ['simple filename with extension', 'filename.txt'], |
| 35 | + ['plain name without extension', 'myfile'], |
| 36 | + ['empty string', ''], |
| 37 | + ['name with underscores', 'hello_world'], |
| 38 | + ['relative path with slash', 'uploads/file.txt'] |
| 39 | + ])('should not flag %s: %s', (_description, input) => { |
| 40 | + expect(isPathTraversal(input)).toBe(false) |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + describe('PATH_TRAVERSAL_SAFETY=false bypasses all checks', () => { |
| 45 | + beforeEach(() => { |
| 46 | + process.env.PATH_TRAVERSAL_SAFETY = 'false' |
| 47 | + }) |
| 48 | + afterEach(() => { |
| 49 | + delete process.env.PATH_TRAVERSAL_SAFETY |
| 50 | + }) |
| 51 | + |
| 52 | + it.each([ |
| 53 | + ['absolute Unix path', '/data/uploads'], |
| 54 | + ['mixed encoding', '.%2e/etc'], |
| 55 | + ['directory traversal', '../etc/passwd'], |
| 56 | + ['Windows absolute path', 'C:\\windows'] |
| 57 | + ])('should return false for %s when safety disabled', (_desc, input) => { |
| 58 | + expect(isPathTraversal(input)).toBe(false) |
| 59 | + }) |
| 60 | + }) |
| 61 | +}) |
| 62 | + |
| 63 | +describe('isUnsafeFilePath', () => { |
| 64 | + describe('PATH_TRAVERSAL_SAFETY=false bypasses all checks', () => { |
| 65 | + beforeEach(() => { |
| 66 | + process.env.PATH_TRAVERSAL_SAFETY = 'false' |
| 67 | + }) |
| 68 | + afterEach(() => { |
| 69 | + delete process.env.PATH_TRAVERSAL_SAFETY |
| 70 | + }) |
| 71 | + |
| 72 | + it.each([ |
| 73 | + ['absolute Unix path', '/data/uploads'], |
| 74 | + ['directory traversal', '../etc/passwd'], |
| 75 | + ['Windows absolute path', 'C:\\windows'], |
| 76 | + ['null byte', 'path\0name'], |
| 77 | + ['control character', 'path\x01name'] |
| 78 | + ])('should return false for %s when safety disabled', (_desc, input) => { |
| 79 | + expect(isUnsafeFilePath(input)).toBe(false) |
| 80 | + }) |
| 81 | + }) |
| 82 | +}) |
| 83 | + |
5 | 84 | describe('validateMimeTypeAndExtensionMatch', () => { |
6 | 85 | describe('valid cases', () => { |
7 | 86 | it.each([ |
@@ -359,4 +438,31 @@ describe('validateVectorStorePath', () => { |
359 | 438 | expect(result).toBe(path.normalize(mixedPath)) |
360 | 439 | }) |
361 | 440 | }) |
| 441 | + |
| 442 | + describe('PATH_TRAVERSAL_SAFETY=false bypasses all checks', () => { |
| 443 | + beforeEach(() => { |
| 444 | + process.env.PATH_TRAVERSAL_SAFETY = 'false' |
| 445 | + }) |
| 446 | + afterEach(() => { |
| 447 | + delete process.env.PATH_TRAVERSAL_SAFETY |
| 448 | + }) |
| 449 | + |
| 450 | + it('should allow arbitrary absolute Unix path', () => { |
| 451 | + expect(validateVectorStorePath('/data/faiss-store')).toBe('/data/faiss-store') |
| 452 | + }) |
| 453 | + |
| 454 | + it('should allow path outside allowed directories (/tmp)', () => { |
| 455 | + expect(validateVectorStorePath('/tmp/mystore')).toBe('/tmp/mystore') |
| 456 | + }) |
| 457 | + |
| 458 | + it('should allow path containing .. without throwing', () => { |
| 459 | + const result = validateVectorStorePath('../mystore') |
| 460 | + expect(typeof result).toBe('string') |
| 461 | + }) |
| 462 | + |
| 463 | + it('should return default path when undefined', () => { |
| 464 | + const userHome = getUserHome() |
| 465 | + expect(validateVectorStorePath(undefined)).toBe(path.join(userHome, '.flowise', 'vectorstore')) |
| 466 | + }) |
| 467 | + }) |
362 | 468 | }) |
0 commit comments