fix: allow dotdot-prefixed upload children

This commit is contained in:
Neil
2026-05-31 02:06:20 -07:00
parent 115531f390
commit 48c34db7da
2 changed files with 87 additions and 2 deletions
+81
View File
@@ -524,6 +524,87 @@ describe('fs:importExternalPaths', () => {
expect(closeMock).toHaveBeenCalled()
})
it('stages runtime upload directories whose child names start with dotdot characters', async () => {
const sourcePath = '/tmp/dropped/project'
const resolvedPath = path.resolve(sourcePath)
const childDir = path.join(resolvedPath, '..assets')
const childFile = path.join(childDir, 'icon.txt')
lstatMock.mockImplementation(async (p: string) => {
if (p === resolvedPath || p === childDir) {
return {
size: 0,
ino: 1,
dev: 1,
isFile: () => false,
isDirectory: () => true,
isSymbolicLink: () => false
}
}
if (p === childFile) {
return {
size: 4,
ino: 2,
dev: 1,
isFile: () => true,
isDirectory: () => false,
isSymbolicLink: () => false
}
}
throw enoent()
})
readdirMock.mockImplementation(async (p: string) => {
if (p === resolvedPath) {
return [
{
name: '..assets',
isDirectory: () => true,
isSymbolicLink: () => false,
isFile: () => false
}
]
}
if (p === childDir) {
return [
{
name: 'icon.txt',
isDirectory: () => false,
isSymbolicLink: () => false,
isFile: () => true
}
]
}
return []
})
openMock.mockResolvedValue({
stat: vi.fn().mockResolvedValue({
size: 4,
ino: 2,
dev: 1,
isFile: () => true
}),
readFile: vi.fn().mockResolvedValue(Buffer.from('icon')),
close: vi.fn().mockResolvedValue(undefined)
})
const result = (await handlers.get('fs:stageExternalPathsForRuntimeUpload')!(null, {
sourcePaths: [sourcePath]
})) as { sources: unknown[] }
expect(result.sources).toEqual([
{
sourcePath,
status: 'staged',
name: 'project',
kind: 'directory',
entries: [
{ relativePath: '', kind: 'directory' },
{ relativePath: '..assets', kind: 'directory' },
{ relativePath: '..assets/icon.txt', kind: 'file', contentBase64: 'aWNvbg==' }
]
}
])
})
it('fails runtime upload staging when a file changes between lstat and open', async () => {
const sourcePath = '/tmp/dropped/logo.png'
const resolvedPath = path.resolve(sourcePath)
+6 -2
View File
@@ -14,7 +14,7 @@ import {
unlink,
writeFile
} from 'fs/promises'
import { basename, dirname, isAbsolute, join, relative, resolve } from 'path'
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'path'
import { pipeline } from 'stream/promises'
import type { Store } from '../persistence'
import { authorizeExternalPath, resolveAuthorizedPath, isENOENT } from './filesystem-auth'
@@ -554,7 +554,11 @@ async function assertRealPathInsideRoot(
): Promise<void> {
const candidateRealPath = await realpath(candidatePath)
const relativeToRoot = relative(rootRealPath, candidateRealPath)
if (relativeToRoot !== '' && (relativeToRoot.startsWith('..') || isAbsolute(relativeToRoot))) {
// Why: `..name` is a valid child path; only `..` and `../...` escape.
if (
relativeToRoot !== '' &&
(relativeToRoot === '..' || relativeToRoot.startsWith(`..${sep}`) || isAbsolute(relativeToRoot))
) {
throw new Error(`Path escaped upload root during staging: '${displayPath}'`)
}
}