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}`) } }