Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/ssh-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import os from 'node:os'
const RE_SPACE = /\s/
const RE_LINE_BREAK = /\r|\n/
const RE_SECTION_DIRECTIVE = /^(Host|Match)$/i
const RE_MULTI_VALUE_DIRECTIVE = /^(GlobalKnownHostsFile|Host|IPQoS|SendEnv|UserKnownHostsFile|ProxyCommand|Match|CanonicalDomains)$/i
const RE_MULTI_VALUE_DIRECTIVE = /^(GlobalKnownHostsFile|Host|IPQoS|SendEnv|UserKnownHostsFile|ProxyCommand|KnownHostsCommand|Match|CanonicalDomains)$/i
const RE_COMMAND_DIRECTIVE = /^(ProxyCommand|KnownHostsCommand)$/i
const RE_QUOTE_DIRECTIVE = /^(?:CertificateFile|IdentityFile|IdentityAgent|User)$/i
const RE_SINGLE_LINE_DIRECTIVE = /^(Include|IdentityFile)$/i

Expand Down Expand Up @@ -303,7 +304,7 @@ export default class SSHConfig extends Array<Line> {
const key = computeOpts?.ignoreCase ? name.toLowerCase() : name
let val: string | string[]
if (Array.isArray(value)) {
if (/ProxyCommand/i.test(key)) {
if (RE_COMMAND_DIRECTIVE.test(key)) {
val = value.map(({ val, separator, quoted }) => {
return `${separator}${quoted ? `"${val.replace(/"/g, '\\"')}"` : val}`
}).join('').trim()
Expand Down
10 changes: 10 additions & 0 deletions test/legacy/compute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,14 @@ describe('compute', function() {
assert.equal(result.ProxyCommand, '"/foo/bar - baz/proxylauncher.sh" "/some/param with space"')
})

it('.compute should preserve KnownHostsCommand arguments', async () => {
const config = SSHConfig.parse(`
Host *.sbx
KnownHostsCommand "/opt/homebrew/bin/sbx" ssh known-hosts %H
`)

const result = config.compute({ Host: 'example.sbx' })
assert.equal(result.KnownHostsCommand, '"/opt/homebrew/bin/sbx" ssh known-hosts %H')
})

})
12 changes: 12 additions & 0 deletions test/legacy/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ describe('stringify', function() {
`)
})

it('.stringify KnownHostsCommand with a quoted executable', function() {
const config = parse(`
Host *.sbx
KnownHostsCommand "/opt/homebrew/bin/sbx" ssh known-hosts %H
`)

assert.equal(stringify(config), `
Host *.sbx
KnownHostsCommand "/opt/homebrew/bin/sbx" ssh known-hosts %H
`)
})

it('.stringify Match with criteria', function() {
const config = parse(`
Match host foo final exec "return 0"
Expand Down
10 changes: 10 additions & 0 deletions test/unit/compute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,16 @@ describe('compute', function() {
assert.equal(result.ProxyCommand, '"/foo/bar - baz/proxylauncher.sh" "/some/param with space"')
})

it('.compute should preserve KnownHostsCommand arguments', async () => {
const config = SSHConfig.parse(`
Host *.sbx
KnownHostsCommand "/opt/homebrew/bin/sbx" ssh known-hosts %H
`)

const result = config.compute({ Host: 'example.sbx' })
assert.equal(result.KnownHostsCommand, '"/opt/homebrew/bin/sbx" ssh known-hosts %H')
})

describe('compute with ignoreCase', function() {
it('.compute with ignoreCase: true should normalize directive names to lowercase', async function() {
const config = SSHConfig.parse(`
Expand Down
12 changes: 12 additions & 0 deletions test/unit/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ describe('stringify', function() {
`)
})

it('.stringify KnownHostsCommand with a quoted executable', function() {
const config = parse(`
Host *.sbx
KnownHostsCommand "/opt/homebrew/bin/sbx" ssh known-hosts %H
`)

assert.equal(stringify(config), `
Host *.sbx
KnownHostsCommand "/opt/homebrew/bin/sbx" ssh known-hosts %H
`)
})

it('.stringify Match with criteria', function() {
const config = parse(`
Match host foo final exec "return 0"
Expand Down