From f470168cba860af315a66fca18a5a5e7613dc0bd Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sat, 30 May 2026 12:04:29 -0700 Subject: [PATCH] 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 --------- Co-authored-by: Jinwoo-H Co-authored-by: Orca --- src/main/ssh/sftp-upload.test.ts | 33 ++++++++++++++++++++++++++++++++ src/main/ssh/sftp-upload.ts | 7 +++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/main/ssh/sftp-upload.test.ts b/src/main/ssh/sftp-upload.test.ts index 23c03180c..c172b8de3 100644 --- a/src/main/ssh/sftp-upload.test.ts +++ b/src/main/ssh/sftp-upload.test.ts @@ -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') diff --git a/src/main/ssh/sftp-upload.ts b/src/main/ssh/sftp-upload.ts index 4b7698494..c0c0c93ec 100644 --- a/src/main/ssh/sftp-upload.ts +++ b/src/main/ssh/sftp-upload.ts @@ -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 { 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}`) } }