* Fix ssh-relay install on hosts with split shell/SFTP namespaces On Synology DSM and similar hosts, the SSH shell and SFTP subsystem expose different absolute paths for the same directory (e.g., /var/services/homes/alice vs /homes/alice). The relay installer silently picked the wrong path and failed discovery. This fix implements SFTP namespace detection: each install creates an unguessable ownership marker and probes both namespaces to detect divergence. When paths differ, SFTP writes redirect to the candidate namespace while shell commands keep the canonical path. Markers are random tokens redacted from logs. * Fix ssh-relay install on hosts with split shell/SFTP namespaces Strengthen path validation to catch traversal and empty segments in absolute POSIX paths, preventing security issues. Improve split-namespace handling with comprehensive wire tests for uploads and file writes. Ensure system SSH connections bypass namespace mapping entirely rather than attempting incorrect retargeting.
181 lines
6.9 KiB
TypeScript
181 lines
6.9 KiB
TypeScript
// Cross-version isolation guard.
|
|
//
|
|
// Why: this test is the executable form of the "Pattern Note" in
|
|
// docs/ssh-relay-versioned-install-dirs.md — it asserts that a v2 deploy
|
|
// targeting a remote where a v1 daemon is already running NEVER touches
|
|
// v1's install dir or socket. Without this test a future refactor that
|
|
// collapses to a shared dir passes every other unit test and re-introduces
|
|
// the original "stale daemon serves new client" bug.
|
|
|
|
import { EventEmitter } from 'node:events'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
vi.mock('electron', () => ({
|
|
app: { getAppPath: () => '/mock/app' }
|
|
}))
|
|
|
|
vi.mock('fs', () => ({
|
|
existsSync: vi.fn().mockReturnValue(true),
|
|
readFileSync: vi.fn().mockReturnValue('0.1.0+v2hash')
|
|
}))
|
|
|
|
vi.mock('./relay-protocol', () => ({
|
|
RELAY_VERSION: '0.1.0',
|
|
RELAY_REMOTE_DIR: '.orca-remote',
|
|
parseUnameToRelayPlatform: vi.fn().mockReturnValue('linux-x64'),
|
|
RELAY_SENTINEL: 'ORCA-RELAY v0.1.0 READY\n',
|
|
RELAY_SENTINEL_TIMEOUT_MS: 10_000
|
|
}))
|
|
|
|
vi.mock('./ssh-relay-deploy-helpers', () => ({
|
|
uploadDirectory: vi.fn().mockResolvedValue(undefined),
|
|
waitForSentinel: vi.fn().mockResolvedValue({
|
|
write: vi.fn(),
|
|
onData: vi.fn(),
|
|
onClose: vi.fn()
|
|
}),
|
|
isUnconfirmedSshCommandTermination: (error: unknown) =>
|
|
error instanceof Error &&
|
|
(error as Error & { sshChannelCloseConfirmed?: boolean }).sshChannelCloseConfirmed === false,
|
|
execCommand: vi.fn()
|
|
}))
|
|
|
|
vi.mock('./ssh-remote-node-resolution', () => ({
|
|
resolveRemoteNodePath: vi.fn().mockResolvedValue('/usr/bin/node')
|
|
}))
|
|
|
|
vi.mock('./ssh-connection-utils', () => ({
|
|
shellEscape: (s: string) => `'${s}'`
|
|
}))
|
|
|
|
import { deployAndLaunchRelay } from './ssh-relay-deploy'
|
|
import { execCommand } from './ssh-relay-deploy-helpers'
|
|
import type { SshConnection } from './ssh-connection'
|
|
|
|
function makeMockConnection(): SshConnection {
|
|
return {
|
|
canRunConcurrentExecCommands: vi.fn().mockReturnValue(false),
|
|
exec: vi.fn().mockResolvedValue({
|
|
on: vi.fn(),
|
|
stderr: { on: vi.fn() },
|
|
stdin: {},
|
|
stdout: { on: vi.fn() },
|
|
close: vi.fn()
|
|
}),
|
|
// Why: production attaches and removes real SFTP/write-stream listeners, so the fake must be an emitter.
|
|
sftp: vi.fn().mockImplementation(() => {
|
|
const sftp = new EventEmitter()
|
|
return Promise.resolve(
|
|
Object.assign(sftp, {
|
|
mkdir: vi.fn((_p: string, cb: (err: Error | null) => void) => cb(null)),
|
|
// Shell home and SFTP start directory agree here, so no namespace redirect applies.
|
|
realpath: vi.fn((_p: string, cb: (err: Error | null, resolved: string) => void) =>
|
|
cb(null, '/home/u')
|
|
),
|
|
lstat: vi.fn((_p: string, cb: (err: Error | null) => void) =>
|
|
cb(Object.assign(new Error('No such file'), { code: 2 }))
|
|
),
|
|
createWriteStream: vi.fn().mockImplementation(() => {
|
|
const ws = new EventEmitter()
|
|
return Object.assign(ws, {
|
|
end: vi.fn(() => setTimeout(() => ws.emit('close'), 0))
|
|
})
|
|
}),
|
|
end: vi.fn(() => setTimeout(() => sftp.emit('close'), 0))
|
|
})
|
|
)
|
|
})
|
|
} as unknown as SshConnection
|
|
}
|
|
|
|
describe('cross-version isolation', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('a v2 deploy never references the v1 install dir or v1 socket path', async () => {
|
|
const conn = makeMockConnection()
|
|
const mockExec = vi.mocked(execCommand)
|
|
|
|
// Simulated remote where:
|
|
// v1 dir = ~/.orca-remote/relay-0.1.0+v1hash/ (live daemon, listening)
|
|
// v2 dir = ~/.orca-remote/relay-0.1.0+v2hash/ (does not yet exist)
|
|
// The v2 client has fullVersion='0.1.0+v2hash' (from the fs mock above).
|
|
//
|
|
// We feed enough exec results to walk through the deploy: platform,
|
|
// $HOME, isRelayAlreadyInstalled probe, lock acquire, upload (no exec),
|
|
// npm install, finalize, socket probe, socket poll, then GC scan.
|
|
const responses: string[] = [
|
|
'__ORCA_REMOTE_PLATFORM__ Linux x86_64', // tagged POSIX platform probe
|
|
'/home/u', // echo $HOME
|
|
'MISSING', // isRelayAlreadyInstalled (v2 dir doesn't exist)
|
|
'OPEN', // no sibling GC claim
|
|
'', // mkdir -p remoteRelayDir (v2)
|
|
'OK', // mkdir lock OK
|
|
'OPEN', // GC did not claim while the install lock was acquired
|
|
'MISSING', // re-probe after lock → still missing → proceed with install
|
|
'', // mkdir remoteDir (uploadRelay)
|
|
'', // chmod +x node
|
|
'', // npm install
|
|
'', // chmod prebuilds
|
|
'ORCA-NPTY-PROBE-OK\n', // node -e require() load-test (post-install verify)
|
|
'', // rm -f probe-stderr (best-effort cleanup after probe resolved)
|
|
'', // touch .install-complete (finalizeInstall)
|
|
'DEAD', // launch socket probe
|
|
'READY', // socket poll
|
|
'', // release .install-lock after relay liveness is observable
|
|
// GC scan begins here
|
|
'relay-0.1.0+v1hash\nrelay-0.1.0+v2hash\n', // ls listing
|
|
'OPEN', // v1 lock probe (siblings only — current dir is v2)
|
|
'COMPLETE', // v1 .install-complete probe
|
|
'ALIVE' // v1 socket probe → live → SKIP (don't GC v1)
|
|
]
|
|
for (const r of responses) {
|
|
mockExec.mockResolvedValueOnce(r)
|
|
}
|
|
|
|
await deployAndLaunchRelay(conn)
|
|
|
|
const allCmds = [
|
|
...mockExec.mock.calls.map(([, c]) => c),
|
|
...vi.mocked(conn.exec).mock.calls.map(([c]) => c as string)
|
|
]
|
|
|
|
// (a) the v2 deploy creates dirs/files under relay-0.1.0+v2hash
|
|
expect(allCmds.some((c) => c.includes('relay-0.1.0+v2hash'))).toBe(true)
|
|
|
|
// (b) the v2 launch and connect socket paths are rooted in v2 dir, never v1
|
|
const launchAndConnectCmds = vi
|
|
.mocked(conn.exec)
|
|
.mock.calls.map(([c]) => c as string)
|
|
.filter((c) => c.includes('--sock-path'))
|
|
expect(launchAndConnectCmds.length).toBeGreaterThan(0)
|
|
for (const cmd of launchAndConnectCmds) {
|
|
expect(cmd).toContain('relay-0.1.0+v2hash')
|
|
expect(cmd).not.toContain('relay-0.1.0+v1hash')
|
|
}
|
|
|
|
// (c) GC observes v1 has a live socket and never issues an rm -rf for it
|
|
const v1RemoveCmds = allCmds.filter(
|
|
(c) => c.includes('rm -rf') && c.includes('relay-0.1.0+v1hash')
|
|
)
|
|
expect(v1RemoveCmds).toHaveLength(0)
|
|
|
|
// (d) blanket isolation: every command that mentions v1hash MUST be a
|
|
// GC liveness probe (`ls`, `test -d`, `test -f`, or `for f in .../*.sock`)
|
|
// — never a write, mkdir, chmod, touch, rm, node launch, or socket poll.
|
|
// This prevents a future refactor that accidentally writes to the v1 dir
|
|
// (e.g. shared install-complete, upload over symlink) from passing.
|
|
const v1Refs = allCmds.filter((c) => c.includes('relay-0.1.0+v1hash'))
|
|
for (const cmd of v1Refs) {
|
|
const isReadOnlyProbe =
|
|
/^\s*ls\b/.test(cmd) ||
|
|
/\btest -d\b/.test(cmd) ||
|
|
/\btest -f\b/.test(cmd) ||
|
|
/\btest -S\b/.test(cmd) ||
|
|
/\bfor f in .*\.sock\b/.test(cmd)
|
|
expect(isReadOnlyProbe, `unexpected v1 reference: ${cmd}`).toBe(true)
|
|
}
|
|
})
|
|
})
|