fix: parse large ssh host alias lists

This commit is contained in:
Neil
2026-05-30 05:01:02 -07:00
parent d6778abea0
commit e310f4f566
2 changed files with 23 additions and 3 deletions
+12
View File
@@ -172,6 +172,18 @@ Host staging stage *.example.com
])
})
it('parses generated configs with very large multi-alias Host lines', () => {
const aliases = Array.from({ length: 125_000 }, (_, index) => `host-${index}`)
const hosts = parseSshConfig(`
Host ${aliases.join(' ')}
HostName shared.example.com
`)
expect(hosts).toHaveLength(aliases.length)
expect(hosts[0]).toEqual({ host: 'host-0', hostname: 'shared.example.com' })
expect(hosts.at(-1)).toEqual({ host: 'host-124999', hostname: 'shared.example.com' })
})
it('applies identity agent settings to every concrete alias on a multi-pattern Host line', () => {
const config = `
Host staging stage
+11 -3
View File
@@ -44,7 +44,7 @@ export function parseSshConfig(content: string): SshConfigHost[] {
if (key === 'host') {
if (current.length > 0) {
hosts.push(...current)
appendSshConfigHosts(hosts, current)
}
const patterns = splitHostPatterns(value)
@@ -62,7 +62,7 @@ export function parseSshConfig(content: string): SshConfigHost[] {
if (key === 'match') {
if (current.length > 0) {
hosts.push(...current)
appendSshConfigHosts(hosts, current)
}
current = []
continue
@@ -122,11 +122,19 @@ export function parseSshConfig(content: string): SshConfigHost[] {
}
if (current.length > 0) {
hosts.push(...current)
appendSshConfigHosts(hosts, current)
}
return hosts
}
function appendSshConfigHosts(target: SshConfigHost[], entries: SshConfigHost[]): void {
// Why: generated SSH configs can contain very large Host alias lists; spreading
// those entries into push() can exceed V8's argument limit.
for (const entry of entries) {
target.push(entry)
}
}
function splitHostPatterns(input: string): string[] {
const patterns: string[] = []
let current = ''