utils.test.ts
This file
Path
src/helper/ssg/utils.test.ts
102 lines · 5.4 KiB · TypeScript
Last changed by
Merge commit from forkTaku Amanobefore adoption3a67f7f
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–24why, one step back1import { describe, expect, it } from 'vitest'2import { dirname, ensureWithinOutDir, joinPaths } from './utils'34describe('joinPath', () => {5 it('Should joined path is valid.', () => {6 expect(joinPaths('test')).toBe('test') //single7 expect(joinPaths('.test')).toBe('.test') //single with dot8 expect(joinPaths('/.test')).toBe('/.test') //single with dot with root9 expect(joinPaths('test', 'test2')).toBe('test/test2') // single and single10 expect(joinPaths('test', 'test2', '../test3')).toBe('test/test3') // single and single and single with parent11 expect(joinPaths('.', '../')).toBe('..') // dot and parent12 expect(joinPaths('test/', 'test2/')).toBe('test/test2') // trailing slashes13 expect(joinPaths('./test', './test2')).toBe('test/test2') // dot and slash14 expect(joinPaths('', 'test')).toBe('test') // empty path15 expect(joinPaths('/test', '/test2')).toBe('/test/test2') // root path16 expect(joinPaths('../', 'test')).toBe('../test') // parent and single17 expect(joinPaths('test', '..', 'test2')).toBe('test2') // single triple dot and single18 expect(joinPaths('test', '...', 'test2')).toBe('test/.../test2') // single triple dot and single19 expect(joinPaths('test', './test2', '.test3.')).toBe('test/test2/.test3.') // single and single with slash and single with dot20 expect(joinPaths('test', '../', '.test2')).toBe('.test2') // single and parent and single with dot21 expect(joinPaths('..', '..', 'test')).toBe('../../test') // parent and parent and single22 expect(joinPaths('..', '..')).toBe('../..') // parent and parent23 expect(joinPaths('.test../test2/../')).toBe('.test..') //shuffle24 expect(joinPaths('.test./.test2/../')).toBe('.test.') //shuffle2Merge commit from forkTaku Amanobefore adoption3a67f7f
lines 25–27why, one step back25 expect(joinPaths('static/a/b/../../../pwned')).toBe('pwned') // consecutive parents26 expect(joinPaths('///tmp/out', 'index.html')).toBe('/tmp/out/index.html')27 expect(joinPaths('//tmp', 'index.html')).toBe('/tmp/index.html')Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 28–30why, one step back28 })29 it('Should windows path is valid.', () => {30 expect(joinPaths('a\\b\\c', 'd\\e')).toBe('a/b/c/d/e')Merge commit from forkTaku Amanobefore adoption3a67f7f
line 31why, one step back31 expect(joinPaths('\\\\server\\share\\out', 'index.html')).toBe('//server/share/out/index.html')Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 32–47why, one step back32 })33})3435describe('dirname', () => {36 it('Should dirname is valid.', () => {37 expect(dirname('parent/child')).toBe('parent')38 expect(dirname('windows\\test.txt')).toBe('windows')39 })40})4142describe('ensureWithinOutDir', () => {43 it('Should not throw for paths within outDir', () => {44 expect(() => ensureWithinOutDir('./static', 'static/index.html')).not.toThrow()45 expect(() => ensureWithinOutDir('./static', 'static/sub/page.html')).not.toThrow()46 expect(() => ensureWithinOutDir('/out', '/out/index.html')).not.toThrow()47 expect(() => ensureWithinOutDir('./static', 'static/a/../b.html')).not.toThrow()Merge commit from forkTaku Amanobefore adoption3a67f7f
lines 48–53why, one step back48 expect(() => ensureWithinOutDir('./static', 'static/a/b/../../c.html')).not.toThrow()49 expect(() =>50 ensureWithinOutDir('\\\\server\\share\\out', '//server/share/out/index.html')51 ).not.toThrow()52 expect(() => ensureWithinOutDir('///tmp/out', '/tmp/out/index.html')).not.toThrow()53 expect(() => ensureWithinOutDir('//tmp', '/tmp/index.html')).not.toThrow()Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 54–62why, one step back54 })5556 it('Should throw for paths outside outDir via traversal', () => {57 expect(() => ensureWithinOutDir('./static', 'pwned.txt')).toThrow('Path traversal detected')58 expect(() => ensureWithinOutDir('./static', '../pwned.txt')).toThrow('Path traversal detected')59 expect(() => ensureWithinOutDir('./out', 'pwned.txt')).toThrow('Path traversal detected')60 expect(() => ensureWithinOutDir('./static', 'static/../../pwned.txt')).toThrow(61 'Path traversal detected'62 )Merge commit from forkTaku Amanobefore adoption3a67f7f
lines 63–65why, one step back63 expect(() => ensureWithinOutDir('./static', 'static/a/../../pwned.txt')).toThrow(64 'Path traversal detected'65 )Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 66–71why, one step back66 })6768 it('Should throw for paths that partially match outDir name', () => {69 expect(() => ensureWithinOutDir('./static', 'static-evil/pwned.html')).toThrow(70 'Path traversal detected'71 )Merge commit from forkTaku Amanobefore adoption3a67f7f
lines 72–100why, one step back72 })7374 it('Should accept the current directory as outDir', () => {75 expect(() => ensureWithinOutDir('.', 'index.html')).not.toThrow()76 expect(() => ensureWithinOutDir('./', 'index.html')).not.toThrow()77 expect(() => ensureWithinOutDir('', 'index.html')).not.toThrow()78 expect(() => ensureWithinOutDir('.', '../pwned.html')).toThrow('Path traversal detected')79 })8081 it('Should throw for paths that climb above a parent outDir', () => {82 expect(() => ensureWithinOutDir('..', '../out/index.html')).not.toThrow()83 expect(() => ensureWithinOutDir('..', '../../pwned.txt')).toThrow('Path traversal detected')84 expect(() => ensureWithinOutDir('../..', '../../../pwned.txt')).toThrow(85 'Path traversal detected'86 )87 })8889 it('Should throw when outDir and filePath differ in absoluteness', () => {90 expect(() => ensureWithinOutDir('/out', 'out/index.html')).toThrow('Path traversal detected')91 expect(() => ensureWithinOutDir('./out', '/out/index.html')).toThrow('Path traversal detected')92 expect(() => ensureWithinOutDir('.', '/etc/pwned.html')).toThrow('Path traversal detected')93 expect(() => ensureWithinOutDir('.', 'C:\\Windows\\pwned.html')).toThrow(94 'Path traversal detected'95 )96 expect(() => ensureWithinOutDir('.', 'C:pwned.html')).toThrow('Path traversal detected')97 expect(() => ensureWithinOutDir('C:', 'C:/pwned.html')).toThrow('Path traversal detected')98 expect(() =>99 ensureWithinOutDir('\\\\server\\share\\out', '/server/share/out/pwned.html')100 ).toThrow('Path traversal detected')Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 101–102why, one step back101 })102})