Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions packages/cli-kit/src/public/node/path.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {relativizePath, normalizePath, cwd, sniffForPath, commonParentDirectory} from './path.js'
import {describe, test, expect} from 'vitest'
import {relativizePath, normalizePath, cwd, sniffForPath, commonParentDirectory, sanitizeRelativePath} from './path.js'
import {describe, test, expect, vi} from 'vitest'

describe('relativize', () => {
test('relativizes the path', () => {
Expand Down Expand Up @@ -93,3 +93,53 @@ describe('sniffForPath', () => {
expect(path).toStrictEqual('/path/to/project')
})
})

describe('sanitizeRelativePath', () => {
test('strips traversal segments', () => {
const warn = vi.fn()
expect(sanitizeRelativePath('a/../../b', warn)).toBe('b')
expect(warn).toHaveBeenCalled()
})

test('strips leading slashes (absolute Unix paths)', () => {
const warn = vi.fn()
expect(sanitizeRelativePath('/etc/passwd', warn)).toBe('etc/passwd')
expect(warn).toHaveBeenCalled()
})

test('strips multiple leading slashes', () => {
const warn = vi.fn()
expect(sanitizeRelativePath('//etc/passwd', warn)).toBe('etc/passwd')
expect(warn).toHaveBeenCalled()
})

test('strips Windows drive letters', () => {
const warn = vi.fn()
expect(sanitizeRelativePath('C:/Windows/System32', warn)).toBe('Windows/System32')
expect(warn).toHaveBeenCalled()
})

test('handles Windows backslashes', () => {
const warn = vi.fn()
expect(sanitizeRelativePath('C:\\Windows\\System32', warn)).toBe('Windows/System32')
expect(warn).toHaveBeenCalled()
})

test('collapses internal current directory markers', () => {
const warn = vi.fn()
expect(sanitizeRelativePath('a/./b', warn)).toBe('a/b')
expect(warn).not.toHaveBeenCalled()
})

test('returns empty string for empty result', () => {
const warn = vi.fn()
expect(sanitizeRelativePath('..', warn)).toBe('')
expect(warn).toHaveBeenCalled()
})

test('returns empty string for single slash', () => {
const warn = vi.fn()
expect(sanitizeRelativePath('/', warn)).toBe('')
expect(warn).toHaveBeenCalled()
})
})
11 changes: 9 additions & 2 deletions packages/cli-kit/src/public/node/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,20 @@ export function sanitizeRelativePath(input: string, warn: (msg: string) => void)
if (seg === '..') {
stripped = true
stack.pop()
} else if (seg !== '.') {
} else if (seg === '.' || seg === '') {
// Skip empty segments (leading/multiple slashes) and current directory markers.
// This prevents absolute paths like '/etc/passwd' from being treated as such.
if (seg === '') stripped = true
} else if (/^[a-zA-Z]:$/.test(seg)) {
// Skip Windows drive letters (e.g. 'C:') to prevent escaping to other drives.
stripped = true
} else {
stack.push(seg)
}
}
const result = stack.join('/')
if (stripped) {
warn(`Warning: path '${input}' contains '..' traversal — sanitized to '${result || '.'}'\n`)
warn(`Warning: path '${input}' contains traversal or absolute segments — sanitized to '${result || '.'}'\n`)
}
return result
}
Loading