Support OpenSSH Include expansion for SSH config imports

Closes #2647
This commit is contained in:
Phil Denhoff
2026-05-23 00:27:20 -07:00
committed by GitHub
parent 59ac13dc53
commit 2dcaa78960
6 changed files with 961 additions and 34 deletions
+89 -28
View File
@@ -1,8 +1,9 @@
import { readFileSync, existsSync } from 'fs'
import { existsSync } from 'fs'
import { execFile } from 'child_process'
import { join } from 'path'
import { homedir } from 'os'
import type { SshTarget } from '../../shared/ssh-types'
import { expandSshConfigIncludes } from './ssh-config-include-expander'
export type SshConfigHost = {
host: string
@@ -22,7 +23,7 @@ export type SshConfigHost = {
*/
export function parseSshConfig(content: string): SshConfigHost[] {
const hosts: SshConfigHost[] = []
let current: SshConfigHost | null = null
let current: SshConfigHost[] = []
for (const rawLine of content.split('\n')) {
const line = rawLine.trim()
@@ -40,66 +41,126 @@ export function parseSshConfig(content: string): SshConfigHost[] {
const value = rawValue.trim()
if (key === 'host') {
if (current) {
hosts.push(current)
if (current.length > 0) {
hosts.push(...current)
}
// Skip wildcard-only entries (e.g. "Host *" or "Host *.*")
const patterns = value.split(/\s+/)
const hasConcretePattern = patterns.some((p) => !p.includes('*') && !p.includes('?'))
if (!hasConcretePattern) {
current = null
const patterns = splitHostPatterns(value)
const concretePatterns = patterns.filter(
(pattern) => !pattern.startsWith('!') && !pattern.includes('*') && !pattern.includes('?')
)
if (concretePatterns.length === 0) {
current = []
continue
}
current = { host: patterns[0] }
current = concretePatterns.map((pattern) => ({ host: pattern }))
continue
}
if (key === 'match') {
// Match blocks are complex conditionals — push current and skip
if (current) {
hosts.push(current)
if (current.length > 0) {
hosts.push(...current)
}
current = null
current = []
continue
}
if (!current) {
if (current.length === 0) {
continue
}
switch (key) {
case 'hostname':
current.hostname = value
for (const host of current) {
host.hostname = value
}
break
case 'port':
current.port = parseInt(value, 10) || 22
for (const host of current) {
host.port = parseInt(value, 10) || 22
}
break
case 'user':
current.user = value
for (const host of current) {
host.user = value
}
break
case 'identityfile':
current.identityFile = resolveHomePath(value)
for (const host of current) {
host.identityFile = resolveHomePath(value)
}
break
case 'proxycommand':
current.proxyCommand = value
for (const host of current) {
host.proxyCommand = value
}
break
case 'proxyusefdpass':
current.proxyUseFdpass = value.toLowerCase() === 'yes'
for (const host of current) {
host.proxyUseFdpass = value.toLowerCase() === 'yes'
}
break
case 'proxyjump':
current.proxyJump = value
for (const host of current) {
host.proxyJump = value
}
break
}
}
if (current) {
hosts.push(current)
if (current.length > 0) {
hosts.push(...current)
}
return hosts
}
function splitHostPatterns(input: string): string[] {
const patterns: string[] = []
let current = ''
let inQuotes = false
let escaped = false
for (const char of input) {
if (escaped) {
current += char
escaped = false
continue
}
if (inQuotes && char === '\\') {
escaped = true
continue
}
if (char === '"') {
inQuotes = !inQuotes
continue
}
// Why: multi-alias import must not turn OpenSSH inline comments into targets.
if (!inQuotes && char === '#') {
break
}
if (!inQuotes && /\s/.test(char)) {
if (current) {
patterns.push(current)
current = ''
}
continue
}
current += char
}
if (current) {
patterns.push(current)
}
return patterns
}
function resolveHomePath(filepath: string): string {
if (filepath.startsWith('~/') || filepath === '~') {
return join(homedir(), filepath.slice(1))
@@ -115,7 +176,7 @@ export function loadUserSshConfig(): SshConfigHost[] {
}
try {
const content = readFileSync(configPath, 'utf-8')
const content = expandSshConfigIncludes(configPath)
return parseSshConfig(content)
} catch {
console.warn(`[ssh] Failed to read SSH config at ${configPath}`)
@@ -129,15 +190,16 @@ export function sshConfigHostsToTargets(
existingTargetHosts: Set<string>
): SshTarget[] {
const targets: SshTarget[] = []
const seenLabels = new Set(existingTargetHosts)
for (const entry of hosts) {
const effectiveHost = entry.hostname || entry.host
const label = entry.host
// Skip if already imported (match on label, which is the Host alias)
if (existingTargetHosts.has(label)) {
if (seenLabels.has(label)) {
continue
}
seenLabels.add(label)
targets.push({
id: `ssh-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
@@ -154,7 +216,6 @@ export function sshConfigHostsToTargets(
return targets
}
// ── ssh -G config resolution ──────────────────────────────────────────
export type SshResolvedConfig = {