From 5c68841d9da9fce9de48c0bf88d619ef7a10aa41 Mon Sep 17 00:00:00 2001 From: Chen Yangjian <252317+cyjake@users.noreply.github.com> Date: Wed, 8 May 2024 12:26:52 +0800 Subject: [PATCH] fix: canonicalized hostname should not contain glob pattern (#83) --- src/ssh-config.ts | 8 ++++---- test/unit/ssh-config.test.ts | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/ssh-config.ts b/src/ssh-config.ts index 403de69..e98bfb3 100644 --- a/src/ssh-config.ts +++ b/src/ssh-config.ts @@ -223,11 +223,11 @@ export default class SSHConfig extends Array { } } - if (canonicalDomains.length > 0 && canonicalizeHostName) { + if (canonicalDomains.length > 0 && canonicalizeHostName && context.params.Host === context.params.OriginalHost) { for (const domain of canonicalDomains) { - const host = `${line.value}.${domain}` - const { status } = spawnSync('nslookup', [host]) - if (status === 0) { + const host = `${context.params.OriginalHost}.${domain}` + const { status, stderr } = spawnSync('nslookup', [host]) + if (status === 0 && !/can't find/.test(stderr.toString())) { context.params.Host = host setProperty('Host', host) doPass() diff --git a/test/unit/ssh-config.test.ts b/test/unit/ssh-config.test.ts index c117970..73e8bc8 100644 --- a/test/unit/ssh-config.test.ts +++ b/test/unit/ssh-config.test.ts @@ -96,8 +96,8 @@ describe('SSHConfig', function() { it('.compute By Host with canonical domains', async () => { const config = SSHConfig.parse(` Host www.cyj.me - ServerAliveInterval 60 - ServerAliveCountMax 2 + ServerAliveInterval 60 + ServerAliveCountMax 2 Host www User matthew @@ -110,6 +110,23 @@ describe('SSHConfig', function() { assert.equal(result.ServerAliveCountMax, '2') }) + it('.compute By Host with canonical domains', async () => { + const config = SSHConfig.parse(` + Host www.example.net + ServerAliveInterval 60 + ServerAliveCountMax 2 + + Host * + User matthew + CanonicalizeHostName yes + CanonicalDomains example.net example.com + `) + + const result = config.compute('www') + assert.ok(result) + assert.equal(result.ServerAliveCountMax, '2') + }) + it('.compute by Match host', async function() { const config = SSHConfig.parse(` Match host tahoe1