Spaces:
Sleeping
Sleeping
File size: 1,627 Bytes
eb47743 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import { describe, it, expect } from 'vitest'
import { resolveWithin } from '../paths'
import path from 'node:path'
describe('resolveWithin', () => {
const base = '/tmp/sandbox'
it('resolves a simple relative path within base', () => {
const result = resolveWithin(base, 'file.txt')
expect(result).toBe('/tmp/sandbox/file.txt')
})
it('resolves nested relative path', () => {
const result = resolveWithin(base, 'subdir/file.txt')
expect(result).toBe('/tmp/sandbox/subdir/file.txt')
})
it('throws when path escapes base with ..', () => {
expect(() => resolveWithin(base, '../escape.txt')).toThrow('Path escapes base directory')
})
it('throws when path tries deep escape', () => {
expect(() => resolveWithin(base, '../../etc/passwd')).toThrow('Path escapes base directory')
})
it('throws for absolute path outside base', () => {
expect(() => resolveWithin(base, '/etc/passwd')).toThrow('Path escapes base directory')
})
it('allows an absolute path within the base', () => {
const result = resolveWithin(base, '/tmp/sandbox/file.txt')
expect(result).toBe('/tmp/sandbox/file.txt')
})
it('handles double slashes and normalizes', () => {
const result = resolveWithin(base, 'subdir//file.txt')
expect(result).toBe('/tmp/sandbox/subdir/file.txt')
})
it('does not allow sibling directory access', () => {
expect(() => resolveWithin(base, '../other/file.txt')).toThrow()
})
it('handles base dir with trailing slash', () => {
const result = resolveWithin('/tmp/sandbox/', 'file.txt')
expect(result).toBe('/tmp/sandbox/file.txt')
})
})
|