Files
orca/src/main/ssh/ssh-g-config-resolution.ts
T
0302ae86b8 feat(ssh): support Kerberos/GSSAPI hosts via the system OpenSSH transport (#7507)
* feat(ssh): support Kerberos/GSSAPI hosts via the system OpenSSH transport

ssh2 has no gssapi-with-mic support, and adding it would mean forking its
protocol layer plus packaging the kerberos native module for three
platforms. Instead, route GSSAPI hosts through the existing system-OpenSSH
transport, which delegates Kerberos (tickets, SSPI on Windows) to the
platform ssh binary.

Two tiers, because RHEL-family distros enable GSSAPIAuthentication
globally in /etc/ssh/ssh_config and ssh -G therefore reports it for every
host:

- Targets whose ~/.ssh/config Host block explicitly sets
  GSSAPIAuthentication yes (imported as target.gssapiAuthentication) try
  system ssh first, falling through to ssh2 so key auth and credential
  prompts still work when no ticket is available.
- When ssh2 exhausts key/agent auth and the ssh -G-resolved config
  enables GSSAPI, retry over system ssh before prompting for credentials,
  so Kerberos-only hosts on distro-default configs connect without a
  password prompt. Hosts where keys work never leave the ssh2 path.

Manual targets flagged for GSSAPI pass -o GSSAPIAuthentication=yes
explicitly since they bypass ssh_config. Both tiers work headless (no
credential callbacks required).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(ssh): harden GSSAPI transport selection (review fixes for PR #7507)

Review fixes on top of the Kerberos/GSSAPI feature branch (s546126/kerberos-ssh):

- HIGH: reset useSystemSshTransport on the ssh2 fall-through. doSystemSshProbe
  sets the flag before spawnSystemSshCommand, which throws synchronously when no
  system ssh binary is on PATH (outside the probe try/catch). The proactive
  fall-through previously reset only 2 of 3 transport fields, so exec/sftp kept
  routing through the failed transport - breaking GSSAPI on Windows-with-Git-ssh
  and headless Linux.
- MEDIUM: throw a cancellation error (not the stale ssh2 authError) when a
  disconnect supersedes the reactive probe mid-flight, and guard connect()'s
  catch on disposed, so a deliberate disconnect is not overwritten with
  auth-failed.
- MEDIUM: skip the encrypted-key passphrase prompt when the GSSAPI fallback
  applies, so a Kerberos ticket is tried before prompting; the general prompt
  still fires if the probe fails.

Adds 3 mutation-verified regression tests and hardens two existing tests to
assert the probe actually ran. Not connected to any PR remote.

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

* fix(ssh): isolate GSSAPI system transport

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

---------

Co-authored-by: s546126 <268420947+s546126@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
Co-authored-by: Jinwoo-H <jinwoo0825@gmail.com>
Co-authored-by: Orca <help@stably.ai>
2026-07-15 01:58:46 -07:00

120 lines
3.7 KiB
TypeScript

import { execFile } from 'node:child_process'
import { resolveSshConfigHomePath } from './ssh-config-path-expansion'
export type SshResolvedConfig = {
hostname: string
user?: string
port: number
identityFile: string[]
identityAgent?: string
identitiesOnly: boolean
forwardAgent: boolean
/** Effective GSSAPIAuthentication, including distro-wide /etc/ssh defaults. */
gssapiAuthentication?: boolean
proxyCommand?: string
proxyUseFdpass: boolean
proxyJump?: string
controlMaster: string
controlPath?: string
controlPersist: string
}
const SSH_G_TIMEOUT_MS = 5000
// Why: `ssh -G <host>` asks OpenSSH for the effective config, including
// Include/Match/wildcard inheritance, without reimplementing OpenSSH matching.
export function resolveWithSshG(host: string): Promise<SshResolvedConfig | null> {
return new Promise((resolve) => {
let settled = false
let child: ReturnType<typeof execFile> | undefined
const timer = setTimeout(() => {
if (settled) {
return
}
settled = true
child?.kill()
resolve(null)
}, SSH_G_TIMEOUT_MS)
const settle = (callback: () => void): void => {
if (settled) {
return
}
settled = true
clearTimeout(timer)
callback()
}
// Why: '--' prevents host labels starting with '-' from becoming SSH flags.
// execFile's timeout only signals ssh; keep the null fallback for stuck callbacks.
try {
child = execFile('ssh', ['-G', '--', host], { timeout: SSH_G_TIMEOUT_MS }, (err, stdout) => {
if (err) {
settle(() => resolve(null))
return
}
settle(() => resolve(parseSshGOutput(stdout)))
})
} catch {
settle(() => resolve(null))
}
})
}
export function parseSshGOutput(stdout: string): SshResolvedConfig {
const map = new Map<string, string>()
const identityFiles: string[] = []
for (const line of stdout.split('\n')) {
const spaceIdx = line.indexOf(' ')
if (spaceIdx === -1) {
continue
}
const key = line.substring(0, spaceIdx).toLowerCase()
const value = line.substring(spaceIdx + 1).trim()
if (key === 'identityfile') {
identityFiles.push(resolveSshConfigHomePath(value))
} else {
map.set(key, value)
}
}
return buildSshResolvedConfig(map, identityFiles)
}
function buildSshResolvedConfig(
map: Map<string, string>,
identityFiles: string[]
): SshResolvedConfig {
// Why: `ssh -G` outputs `proxycommand none` / `proxyjump none` when no
// proxy is configured. Treating "none" as real would spawn bad commands.
const rawProxy = map.get('proxycommand')
const proxyCommand = rawProxy && rawProxy !== 'none' ? rawProxy : undefined
const rawJump = map.get('proxyjump')
const proxyJump = rawJump && rawJump !== 'none' ? rawJump : undefined
const rawIdentityAgent = map.get('identityagent')
const identityAgent = rawIdentityAgent ? resolveSshConfigHomePath(rawIdentityAgent) : undefined
const rawControlPath = map.get('controlpath')
const controlPath =
rawControlPath && rawControlPath !== 'none'
? resolveSshConfigHomePath(rawControlPath)
: undefined
return {
hostname: map.get('hostname') ?? '',
user: map.get('user') || undefined,
port: Number.parseInt(map.get('port') ?? '22', 10),
identityFile: identityFiles,
identityAgent,
identitiesOnly: map.get('identitiesonly') === 'yes',
forwardAgent: map.get('forwardagent') === 'yes',
gssapiAuthentication: map.get('gssapiauthentication') === 'yes',
proxyCommand,
proxyUseFdpass: map.get('proxyusefdpass') === 'yes',
proxyJump,
controlMaster: map.get('controlmaster') ?? 'no',
controlPath,
controlPersist: map.get('controlpersist') ?? 'no'
}
}