fix: allow dot-dot-prefixed sftp upload paths (#3706)

* fix: allow dot-dot-prefixed sftp upload paths

* test: cover sftp upload root sibling rejection

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Jinwoo-H <jinwoo0825@gmail.com>
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Neil
2026-05-30 12:04:29 -07:00
committed by GitHub
co-authored by Orca Jinwoo-H
parent 18eb3f2d20
commit f470168cba
2 changed files with 38 additions and 2 deletions
+33
View File
@@ -56,6 +56,39 @@ describe('sftp-upload', () => {
expect(writeStream.listenerCount('error')).toBe(0)
})
it('uploads files from valid dot-dot-prefixed local directories', async () => {
const localDir = await mkdtemp(join(tmpdir(), 'orca-sftp-upload-'))
await mkdir(join(localDir, '..fixtures'))
await writeFile(join(localDir, '..fixtures', 'asset.txt'), 'asset')
const sftp = createSftpMock()
await uploadDirectory(sftp, localDir, '/remote/assets', await realpath(localDir), {
exclusive: true
})
expect(sftp.mkdir).toHaveBeenCalledWith('/remote/assets/..fixtures', expect.any(Function))
expect(sftp.createWriteStream).toHaveBeenCalledWith('/remote/assets/..fixtures/asset.txt', {
flags: 'wx'
})
})
it('rejects sibling directories outside the upload root', async () => {
const localDir = await mkdtemp(join(tmpdir(), 'orca-sftp-upload-'))
const escapedDir = `${localDir}-sibling`
await mkdir(escapedDir)
await writeFile(join(escapedDir, 'asset.txt'), 'asset')
const sftp = createSftpMock()
await expect(
uploadDirectory(sftp, escapedDir, '/remote/assets', await realpath(localDir), {
exclusive: true
})
).rejects.toThrow('Path escaped upload root')
expect(sftp.mkdir).not.toHaveBeenCalled()
expect(sftp.createWriteStream).not.toHaveBeenCalled()
})
it('does not create the remote file when the local source is a symlink', async () => {
const localDir = await mkdtemp(join(tmpdir(), 'orca-sftp-upload-'))
await writeFile(join(localDir, 'target.txt'), 'secret')
+5 -2
View File
@@ -1,7 +1,7 @@
import { constants } from 'fs'
import type { ReadStream } from 'fs'
import { lstat, open, readdir, realpath } from 'fs/promises'
import { isAbsolute, join as pathJoin, relative } from 'path'
import { isAbsolute, join as pathJoin, relative, sep } from 'path'
import type { SFTPWrapper } from 'ssh2'
export function mkdirSftp(
@@ -163,7 +163,10 @@ async function assertLocalUploadPathInsideRoot(
): Promise<void> {
const candidateRealPath = await realpath(candidatePath)
const relativeToRoot = relative(rootRealPath, candidateRealPath)
if (relativeToRoot !== '' && (relativeToRoot.startsWith('..') || isAbsolute(relativeToRoot))) {
if (
relativeToRoot !== '' &&
(relativeToRoot === '..' || relativeToRoot.startsWith(`..${sep}`) || isAbsolute(relativeToRoot))
) {
throw new Error(`Path escaped upload root: ${candidatePath}`)
}
}