| 1 | import { describe, expect, it } from 'vitest'; |
| 2 | |
| 3 | import { |
| 4 | isSafeTreePath, |
| 5 | MAX_TREE_PATH_DEPTH, |
| 6 | MAX_TREE_PATH_LENGTH, |
| 7 | normalizeTreePath, |
| 8 | UnsafeTreePathError, |
| 9 | } from './tree-path.js'; |
| 10 | |
| 11 | describe('normalizeTreePath', () |
| 12 | it('returns the root as an empty prefix', |
| 13 | expect(normalizeTreePath('')) |
| 14 | expect(normalizeTreePath('/ |
| 15 | expect(normalizeTreePath('/// |
| 16 | }); |
| 17 | |
| 18 | it('normalizes a directory to trailing-slash form', |
| 19 | expect(normalizeTreePath('src |
| 20 | expect(normalizeTreePath('src/ |
| 21 | expect(normalizeTreePath('/src/deep/ |
| 22 | }); |
| 23 | |
| 24 | it('rejects traversal', |
| 25 | for (const input |
| 26 | '..', |
| 27 | '../etc', |
| 28 | 'src/../../etc', |
| 29 | 'src/..', |
| 30 | '/../', |
| 31 | '.', |
| 32 | 'src/./deep', |
| 33 | ]) { |
| 34 | expect(() => |
| 35 | } |
| 36 | }); |
| 37 | |
| 38 | /** Express percent-decodes a query value exactly once, so these are the strings that actually arrive. Decoding again in the app would turn the doubly-encoded forms back into traversal. */ |
| 39 | it('sees decoded slashes as separators and encoded ones as literal text', |
| 40 | // "src%2Fdeep" arrives here already decoded |
| 41 | expect(normalizeTreePath('src/deep |
| 42 | // "src%252Fdeep" arrives with the escape intact and stays one segment |
| 43 | expect(normalizeTreePath('src%2Fdeep |
| 44 | // so a doubly-encoded traversal never becomes ".." |
| 45 | expect(normalizeTreePath('%2e%2e |
| 46 | }); |
| 47 | |
| 48 | it('rejects NUL and other control characters', |
| 49 | // a NUL would terminate the argument git actually receives |
| 50 | expect(() => |
| 51 | UnsafeTreePathError, |
| 52 | ); |
| 53 | expect(() => |
| 54 | expect(() => |
| 55 | }); |
| 56 | |
| 57 | it('rejects doubled separators', |
| 58 | expect(() => |
| 59 | }); |
| 60 | |
| 61 | it('caps length and depth', |
| 62 | expect(() => |
| 63 | normalizeTreePath('a'.repeat |
| 64 | ).toThrow(UnsafeTreePathError) |
| 65 | const deep = |
| 66 | { length: MAX_TREE_PATH_DEPTH |
| 67 | () => 'a' |
| 68 | ).join('/' |
| 69 | expect(() => |
| 70 | }); |
| 71 | |
| 72 | it('allows a path that only looks like pathspec magic', |
| 73 | // legal filenames; listTree neutralizes them with :(literal) |
| 74 | expect(normalizeTreePath(':(glob)src |
| 75 | expect(normalizeTreePath('* |
| 76 | }); |
| 77 | }); |
| 78 | |
| 79 | describe('isSafeTreePath', () |
| 80 | it('mirrors normalizeTreePath without throwing', |
| 81 | expect(isSafeTreePath('src/deep |
| 82 | expect(isSafeTreePath('')) |
| 83 | expect(isSafeTreePath('../etc |
| 84 | expect(isSafeTreePath(undefined)) |
| 85 | expect(isSafeTreePath(42)) |
| 86 | }); |
| 87 | }); |