|
1 | 1 | import { describe, expect, it } from 'vitest' |
2 | | -import { dirname, joinPaths } from './utils' |
| 2 | +import { dirname, ensureWithinOutDir, joinPaths } from './utils' |
3 | 3 |
|
4 | 4 | describe('joinPath', () => { |
5 | 5 | it('Should joined path is valid.', () => { |
@@ -27,9 +27,34 @@ describe('joinPath', () => { |
27 | 27 | expect(joinPaths('a\\b\\c', 'd\\e')).toBe('a/b/c/d/e') |
28 | 28 | }) |
29 | 29 | }) |
| 30 | + |
30 | 31 | describe('dirname', () => { |
31 | 32 | it('Should dirname is valid.', () => { |
32 | 33 | expect(dirname('parent/child')).toBe('parent') |
33 | 34 | expect(dirname('windows\\test.txt')).toBe('windows') |
34 | 35 | }) |
35 | 36 | }) |
| 37 | + |
| 38 | +describe('ensureWithinOutDir', () => { |
| 39 | + it('Should not throw for paths within outDir', () => { |
| 40 | + expect(() => ensureWithinOutDir('./static', 'static/index.html')).not.toThrow() |
| 41 | + expect(() => ensureWithinOutDir('./static', 'static/sub/page.html')).not.toThrow() |
| 42 | + expect(() => ensureWithinOutDir('/out', '/out/index.html')).not.toThrow() |
| 43 | + expect(() => ensureWithinOutDir('./static', 'static/a/../b.html')).not.toThrow() |
| 44 | + }) |
| 45 | + |
| 46 | + it('Should throw for paths outside outDir via traversal', () => { |
| 47 | + expect(() => ensureWithinOutDir('./static', 'pwned.txt')).toThrow('Path traversal detected') |
| 48 | + expect(() => ensureWithinOutDir('./static', '../pwned.txt')).toThrow('Path traversal detected') |
| 49 | + expect(() => ensureWithinOutDir('./out', 'pwned.txt')).toThrow('Path traversal detected') |
| 50 | + expect(() => ensureWithinOutDir('./static', 'static/../../pwned.txt')).toThrow( |
| 51 | + 'Path traversal detected' |
| 52 | + ) |
| 53 | + }) |
| 54 | + |
| 55 | + it('Should throw for paths that partially match outDir name', () => { |
| 56 | + expect(() => ensureWithinOutDir('./static', 'static-evil/pwned.html')).toThrow( |
| 57 | + 'Path traversal detected' |
| 58 | + ) |
| 59 | + }) |
| 60 | +}) |
0 commit comments