|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { validateRelativePath } from '../../../src/template/utils' |
| 3 | +import { TemplateError } from '../../../src/errors' |
| 4 | + |
| 5 | +const isWindows = process.platform === 'win32' |
| 6 | + |
| 7 | +describe('validateRelativePath', () => { |
| 8 | + describe('valid paths', () => { |
| 9 | + test('accepts simple relative path', () => { |
| 10 | + expect(() => validateRelativePath('foo', undefined)).not.toThrow() |
| 11 | + }) |
| 12 | + |
| 13 | + test('accepts nested relative path', () => { |
| 14 | + expect(() => validateRelativePath('foo/bar', undefined)).not.toThrow() |
| 15 | + }) |
| 16 | + |
| 17 | + test('accepts path with ./ prefix', () => { |
| 18 | + expect(() => validateRelativePath('./foo', undefined)).not.toThrow() |
| 19 | + }) |
| 20 | + |
| 21 | + test('accepts nested path with ./ prefix', () => { |
| 22 | + expect(() => validateRelativePath('./foo/bar', undefined)).not.toThrow() |
| 23 | + }) |
| 24 | + |
| 25 | + test('accepts path with internal parent ref that stays within context', () => { |
| 26 | + expect(() => validateRelativePath('foo/../bar', undefined)).not.toThrow() |
| 27 | + }) |
| 28 | + |
| 29 | + test('accepts current directory', () => { |
| 30 | + expect(() => validateRelativePath('.', undefined)).not.toThrow() |
| 31 | + }) |
| 32 | + |
| 33 | + test('accepts glob patterns', () => { |
| 34 | + expect(() => validateRelativePath('*.txt', undefined)).not.toThrow() |
| 35 | + expect(() => validateRelativePath('**/*.ts', undefined)).not.toThrow() |
| 36 | + expect(() => validateRelativePath('src/**/*', undefined)).not.toThrow() |
| 37 | + }) |
| 38 | + |
| 39 | + test('accepts hidden files and directories', () => { |
| 40 | + expect(() => validateRelativePath('.hidden', undefined)).not.toThrow() |
| 41 | + expect(() => |
| 42 | + validateRelativePath('.config/settings', undefined) |
| 43 | + ).not.toThrow() |
| 44 | + }) |
| 45 | + |
| 46 | + test('accepts filenames starting with double dots', () => { |
| 47 | + expect(() => validateRelativePath('..myconfig', undefined)).not.toThrow() |
| 48 | + expect(() => validateRelativePath('..cache', undefined)).not.toThrow() |
| 49 | + expect(() => |
| 50 | + validateRelativePath('...something', undefined) |
| 51 | + ).not.toThrow() |
| 52 | + expect(() => |
| 53 | + validateRelativePath('foo/..myconfig', undefined) |
| 54 | + ).not.toThrow() |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + describe('invalid paths - absolute', () => { |
| 59 | + test('rejects Unix absolute path', () => { |
| 60 | + expect(() => validateRelativePath('/absolute/path', undefined)).toThrow( |
| 61 | + TemplateError |
| 62 | + ) |
| 63 | + expect(() => validateRelativePath('/absolute/path', undefined)).toThrow( |
| 64 | + 'absolute paths are not allowed' |
| 65 | + ) |
| 66 | + }) |
| 67 | + |
| 68 | + test('rejects root path', () => { |
| 69 | + expect(() => validateRelativePath('/', undefined)).toThrow(TemplateError) |
| 70 | + }) |
| 71 | + |
| 72 | + // Windows path tests - only run on Windows where path.isAbsolute detects them |
| 73 | + test.skipIf(!isWindows)('rejects Windows drive letter path', () => { |
| 74 | + expect(() => |
| 75 | + validateRelativePath('C:\\Windows\\System32', undefined) |
| 76 | + ).toThrow(TemplateError) |
| 77 | + expect(() => |
| 78 | + validateRelativePath('C:\\Windows\\System32', undefined) |
| 79 | + ).toThrow('absolute paths are not allowed') |
| 80 | + }) |
| 81 | + |
| 82 | + test.skipIf(!isWindows)('rejects Windows UNC path', () => { |
| 83 | + expect(() => |
| 84 | + validateRelativePath('\\\\server\\share', undefined) |
| 85 | + ).toThrow(TemplateError) |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + describe('invalid paths - parent directory escape', () => { |
| 90 | + test('rejects simple parent directory escape', () => { |
| 91 | + expect(() => validateRelativePath('../foo', undefined)).toThrow( |
| 92 | + TemplateError |
| 93 | + ) |
| 94 | + expect(() => validateRelativePath('../foo', undefined)).toThrow( |
| 95 | + 'path escapes the context directory' |
| 96 | + ) |
| 97 | + }) |
| 98 | + |
| 99 | + test('rejects parent directory escape with forward slash', () => { |
| 100 | + expect(() => validateRelativePath('../file.txt', undefined)).toThrow( |
| 101 | + TemplateError |
| 102 | + ) |
| 103 | + }) |
| 104 | + |
| 105 | + test.skipIf(!isWindows)( |
| 106 | + 'rejects parent directory escape with backslash', |
| 107 | + () => { |
| 108 | + expect(() => validateRelativePath('..\\file.txt', undefined)).toThrow( |
| 109 | + TemplateError |
| 110 | + ) |
| 111 | + } |
| 112 | + ) |
| 113 | + |
| 114 | + test('rejects double parent directory escape', () => { |
| 115 | + expect(() => validateRelativePath('../../foo', undefined)).toThrow( |
| 116 | + TemplateError |
| 117 | + ) |
| 118 | + }) |
| 119 | + |
| 120 | + test('rejects path that escapes via nested parent refs', () => { |
| 121 | + expect(() => validateRelativePath('foo/../../bar', undefined)).toThrow( |
| 122 | + TemplateError |
| 123 | + ) |
| 124 | + }) |
| 125 | + |
| 126 | + test('rejects path with ./ prefix that escapes', () => { |
| 127 | + expect(() => |
| 128 | + validateRelativePath('./foo/../../../bar', undefined) |
| 129 | + ).toThrow(TemplateError) |
| 130 | + }) |
| 131 | + |
| 132 | + test('rejects just parent directory', () => { |
| 133 | + expect(() => validateRelativePath('..', undefined)).toThrow(TemplateError) |
| 134 | + }) |
| 135 | + |
| 136 | + test('rejects current directory followed by parent', () => { |
| 137 | + expect(() => validateRelativePath('./..', undefined)).toThrow( |
| 138 | + TemplateError |
| 139 | + ) |
| 140 | + }) |
| 141 | + |
| 142 | + test('rejects deeply nested escape', () => { |
| 143 | + expect(() => |
| 144 | + validateRelativePath('a/b/c/../../../../escape', undefined) |
| 145 | + ).toThrow(TemplateError) |
| 146 | + }) |
| 147 | + }) |
| 148 | + |
| 149 | + describe('error messages include path', () => { |
| 150 | + test('absolute path error includes the path', () => { |
| 151 | + try { |
| 152 | + validateRelativePath('/etc/passwd', undefined) |
| 153 | + expect.fail('Should have thrown') |
| 154 | + } catch (e) { |
| 155 | + expect(e.message).toContain('/etc/passwd') |
| 156 | + } |
| 157 | + }) |
| 158 | + |
| 159 | + test('escape path error includes the path', () => { |
| 160 | + try { |
| 161 | + validateRelativePath('../secret', undefined) |
| 162 | + expect.fail('Should have thrown') |
| 163 | + } catch (e) { |
| 164 | + expect(e.message).toContain('../secret') |
| 165 | + } |
| 166 | + }) |
| 167 | + }) |
| 168 | +}) |
0 commit comments