| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * Unit tests for server/config.js |
| 5 | * Tests path de-duplication and child-path suppression. |
| 6 | */ |
| 7 | |
| 8 | const path = require('path'); |
| 9 | const { getEffectivePaths, getAllowedRoot } = require('../server/config'); |
| 10 | |
| 11 | // Normalise separator so tests pass on both Windows and Linux |
| 12 | const sep = path.sep; |
| 13 | const join = (...parts) => parts.join(sep); |
| 14 | |
| 15 | describe('getEffectivePaths', () => |
| 16 | { |
| 17 | test('returns empty array for empty input', () => |
| 18 | { |
| 19 | expect(getEffectivePaths([])).toEqual([]); |
| 20 | }); |
| 21 | |
| 22 | test('returns empty array for non-array input', () => |
| 23 | { |
| 24 | expect(getEffectivePaths(null)).toEqual([]); |
| 25 | expect(getEffectivePaths(undefined)).toEqual([]); |
| 26 | }); |
| 27 | |
| 28 | test('de-duplicates identical paths', () => |
| 29 | { |
| 30 | const dir = path.resolve('/mnt/data'); |
| 31 | const result = getEffectivePaths(['/mnt/data', '/mnt/data']); |
| 32 | expect(result).toHaveLength(1); |
| 33 | expect(result[0]).toBe(dir); |
| 34 | }); |
| 35 | |
| 36 | test('drops child paths when parent is also configured', () => |
| 37 | { |
| 38 | const parent = '/mnt/data'; |
| 39 | const child = '/mnt/data/photos'; |
| 40 | const result = getEffectivePaths([parent, child]); |
| 41 | // child should be dropped |
| 42 | expect(result).toHaveLength(1); |
| 43 | expect(result[0]).toBe(path.resolve(parent)); |
| 44 | }); |
| 45 | |
| 46 | test('keeps sibling paths that share a common prefix but are not children', () => |
| 47 | { |
| 48 | const a = '/mnt/data'; |
| 49 | const b = '/mnt/documents'; |
| 50 | const result = getEffectivePaths([a, b]); |
| 51 | expect(result).toHaveLength(2); |
| 52 | }); |
| 53 | |
| 54 | test('handles deeply nested child suppression', () => |
| 55 | { |
| 56 | const root = '/mnt/storage'; |
| 57 | const deep = '/mnt/storage/a/b/c/d'; |
| 58 | const result = getEffectivePaths([root, deep]); |
| 59 | expect(result).toHaveLength(1); |
| 60 | expect(result[0]).toBe(path.resolve(root)); |
| 61 | }); |
| 62 | |
| 63 | test('keeps multiple independent roots', () => |
| 64 | { |
| 65 | const paths = ['/mnt/ssd', '/home/user/shared', '/tmp/uploads']; |
| 66 | const result = getEffectivePaths(paths); |
| 67 | expect(result).toHaveLength(3); |
| 68 | }); |
| 69 | }); |
| 70 | |
| 71 | describe('getAllowedRoot', () => |
| 72 | { |
| 73 | const roots = ['/mnt/data', '/mnt/docs']; |
| 74 | |
| 75 | test('returns the root when file is directly inside it', () => |
| 76 | { |
| 77 | const file = path.join('/mnt/data', 'file.txt'); |
| 78 | const result = getAllowedRoot(file, roots); |
| 79 | expect(result).toBe(path.resolve('/mnt/data')); |
| 80 | }); |
| 81 | |
| 82 | test('returns null when file is not inside any root', () => |
| 83 | { |
| 84 | const result = getAllowedRoot('/etc/passwd', roots); |
| 85 | expect(result).toBeNull(); |
| 86 | }); |
| 87 | |
| 88 | test('path traversal attempt returns null', () => |
| 89 | { |
| 90 | const traversal = path.join('/mnt/data', '..', '..', 'etc', 'passwd'); |
| 91 | const result = getAllowedRoot(traversal, roots); |
| 92 | expect(result).toBeNull(); |
| 93 | }); |
| 94 | |
| 95 | test('returns root when path equals root exactly', () => |
| 96 | { |
| 97 | const result = getAllowedRoot('/mnt/docs', roots); |
| 98 | expect(result).toBe(path.resolve('/mnt/docs')); |
| 99 | }); |
| 100 | |
| 101 | test('returns correct root for nested file', () => |
| 102 | { |
| 103 | const file = path.join('/mnt/docs', 'sub', 'report.pdf'); |
| 104 | const result = getAllowedRoot(file, roots); |
| 105 | expect(result).toBe(path.resolve('/mnt/docs')); |
| 106 | }); |
| 107 | }); |