diff --git a/src/main/ipc/ssh-browse.test.ts b/src/main/ipc/ssh-browse.test.ts new file mode 100644 index 000000000..4d288be23 --- /dev/null +++ b/src/main/ipc/ssh-browse.test.ts @@ -0,0 +1,84 @@ +import { EventEmitter } from 'events' +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { registerSshBrowseHandler } from './ssh-browse' + +const { handleMock, removeHandlerMock } = vi.hoisted(() => ({ + handleMock: vi.fn(), + removeHandlerMock: vi.fn() +})) + +vi.mock('electron', () => ({ + ipcMain: { + handle: handleMock, + removeHandler: removeHandlerMock + } +})) + +type BrowseHandler = ( + event: unknown, + args: { targetId: string; dirPath: string } +) => Promise + +function createMockChannel(): EventEmitter & { stderr: EventEmitter } { + return Object.assign(new EventEmitter(), { + stderr: new EventEmitter() + }) +} + +describe('registerSshBrowseHandler', () => { + let handler: BrowseHandler + + beforeEach(() => { + handleMock.mockReset() + removeHandlerMock.mockReset() + handleMock.mockImplementation((_channel: string, registeredHandler: BrowseHandler) => { + handler = registeredHandler + }) + }) + + it('bypasses remote ls aliases when listing a directory', async () => { + const channel = createMockChannel() + const exec = vi.fn().mockResolvedValue(channel) + const getConnectionManager = () => ({ + getConnection: () => ({ exec }) + }) + registerSshBrowseHandler(getConnectionManager as never) + + const resultPromise = handler(null, { targetId: 'ssh-1', dirPath: '~' }) + await Promise.resolve() + channel.emit('data', Buffer.from('/home/user\nsrc/\nREADME.md\nnotes file.txt\n')) + channel.emit('exit', 0) + channel.emit('close') + + await expect(resultPromise).resolves.toEqual({ + resolvedPath: '/home/user', + entries: [ + { name: 'src', isDirectory: true }, + { name: 'notes file.txt', isDirectory: false }, + { name: 'README.md', isDirectory: false } + ] + }) + expect(exec).toHaveBeenCalledWith('cd "$HOME" && pwd && command ls -1Ap') + }) + + it('escapes remote browse paths before invoking command ls', async () => { + const channel = createMockChannel() + const exec = vi.fn().mockResolvedValue(channel) + const getConnectionManager = () => ({ + getConnection: () => ({ exec }) + }) + registerSshBrowseHandler(getConnectionManager as never) + + const resultPromise = handler(null, { targetId: 'ssh-1', dirPath: "/tmp/it's here" }) + await Promise.resolve() + channel.emit('data', Buffer.from("/tmp/it's here\n")) + channel.emit('exit', 0) + channel.emit('close') + + await expect(resultPromise).resolves.toEqual({ + resolvedPath: "/tmp/it's here", + entries: [] + }) + expect(exec).toHaveBeenCalledWith("cd '/tmp/it'\\''s here' && pwd && command ls -1Ap") + }) +}) diff --git a/src/main/ipc/ssh-browse.ts b/src/main/ipc/ssh-browse.ts index 874e677db..78a7eb1ea 100644 --- a/src/main/ipc/ssh-browse.ts +++ b/src/main/ipc/ssh-browse.ts @@ -30,14 +30,14 @@ export function registerSshBrowseHandler( throw new Error(`SSH connection "${args.targetId}" not found`) } - // Why: using printf with a delimiter instead of ls avoids issues with - // filenames containing spaces or special characters. The -1 flag outputs - // one entry per line. The -p flag appends / to directories. + // Why: using one line per entry preserves filenames containing spaces. + // `command ls` bypasses user aliases/functions like `ls='eza ...'`. + // The -1 flag outputs one entry per line. The -p flag appends / to directories. // We resolve ~ and get the absolute path via `cd && pwd`. // `cd` and `ls` are chained with `&&` so a failing `ls` (e.g. permission // denied after a readable `cd ... && pwd`) propagates as a non-zero exit // code rather than being indistinguishable from an empty directory. - const command = `cd ${shellEscape(args.dirPath)} && pwd && ls -1ap` + const command = `cd ${shellEscape(args.dirPath)} && pwd && command ls -1Ap` const channel = await conn.exec(command) return new Promise((resolve, reject) => {