Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(ssh): parse config keywords case-insensitively

Lizi1191a34787be
diff --git a/packages/maker-remote-ssh/src/__tests__/sshConfig.test.ts b/packages/maker-remote-ssh/src/__tests__/sshConfig.test.ts
index 771ddeb0d3..7ab3b19846 100644
--- a/packages/maker-remote-ssh/src/__tests__/sshConfig.test.ts
+++ b/packages/maker-remote-ssh/src/__tests__/sshConfig.test.ts
@@ -59,6 +59,25 @@ describe('readSshConfig', () => {
expect(await readSshConfig(scratchFile)).toEqual([]);
});
+ it('recognizes Host directives case-insensitively', async () => {
+ await fs.writeFile(scratchFile, [
+ 'host lowercase',
+ ' hostname 10.0.0.1',
+ ' user alice',
+ '',
+ 'hOsT mixedcase',
+ ' HoStNaMe 10.0.0.2',
+ ' UsEr bob',
+ '',
+ ].join('\n'));
+
+ const hosts = await readSshConfig(scratchFile);
+ expect(hosts).toMatchObject([
+ { id: 'lowercase', hostname: '10.0.0.1', user: 'alice' },
+ { id: 'mixedcase', hostname: '10.0.0.2', user: 'bob' },
+ ]);
+ });
+
it('skips wildcard / pattern / negated host entries', async () => {
await fs.writeFile(scratchFile, [
'Host *',
@@ -83,6 +102,27 @@ describe('readSshConfig', () => {
// ── upsertHost + readSshConfig round-trip ────────────────────────────────────
describe('upsertHost round-trip', () => {
+ it('replaces an existing lowercase host block when alias collides', async () => {
+ await fs.writeFile(scratchFile, [
+ 'host foo',
+ ' hostname old',
+ ' user me',
+ ' ProxyJump bastion',
+ '',
+ ].join('\n'));
+
+ await upsertHost(host({ id: 'foo', hostname: 'new', user: 'me' }), scratchFile);
+
+ const raw = await fs.readFile(scratchFile, 'utf8');
+ expect(raw).not.toMatch(/hostname old/i);
+ expect(raw).toMatch(/Host foo/);
+ expect(raw).toMatch(/HostName new/);
+ expect(raw).not.toMatch(/ProxyJump bastion/);
+ expect(await readSshConfig(scratchFile)).toMatchObject([
+ { id: 'foo', hostname: 'new' },
+ ]);
+ });
+
it('round-trips an agent-only host (no IdentityFile)', async () => {
const h = host({ id: 'foo', hostname: '10.0.0.1', user: 'alice', authMethod: 'agent' });
await upsertHost(h, scratchFile);
@@ -228,6 +268,36 @@ describe('updateHostFields', () => {
expect(raw).toMatch(/Port\s+2222/);
});
+ it('updates a lowercase host block without replacing hand-written directives', async () => {
+ await fs.writeFile(scratchFile, [
+ 'host foo',
+ ' hostname 10.0.0.1',
+ ' user alice',
+ ' ProxyJump bastion',
+ '',
+ ].join('\n'));
+
+ await updateHostFields(host({
+ id: 'foo',
+ hostname: '10.0.0.2',
+ user: 'bob',
+ port: 2222,
+ authMethod: 'agent',
+ }), scratchFile);
+
+ const raw = await fs.readFile(scratchFile, 'utf8');
+ expect(raw).toMatch(/^host foo/m);
+ expect(raw).toMatch(/HostName\s+10\.0\.0\.2/i);
+ expect(raw).toMatch(/User\s+bob/i);
+ expect(raw).toMatch(/Port\s+2222/i);
+ expect(raw).toMatch(/ProxyJump\s+bastion/);
+
+ const back = await readSshConfig(scratchFile);
+ expect(back).toMatchObject([
+ { id: 'foo', hostname: '10.0.0.2', user: 'bob', port: 2222 },
+ ]);
+ });
+
it('upserts when the host block does not exist on disk', async () => {
// Empty file → updateHostFields should fall back to upsertHost rather than throw.
await fs.writeFile(scratchFile, '');
@@ -265,6 +335,25 @@ describe('updateHostFields', () => {
// ── removeHost ───────────────────────────────────────────────────────────────
describe('removeHost', () => {
+ it('removes a lowercase host block', async () => {
+ await fs.writeFile(scratchFile, [
+ 'hOsT foo',
+ ' HoStNaMe 10.0.0.1',
+ ' User alice',
+ '',
+ 'Host bar',
+ ' HostName 10.0.0.2',
+ ' User bob',
+ '',
+ ].join('\n'));
+
+ await removeHost('foo', scratchFile);
+
+ expect(await readSshConfig(scratchFile)).toMatchObject([
+ { id: 'bar', hostname: '10.0.0.2', user: 'bob' },
+ ]);
+ });
+
it('drops the named host block', async () => {
await upsertHost(host({ id: 'a' }), scratchFile);
await upsertHost(host({ id: 'b' }), scratchFile);
diff --git a/packages/maker-remote-ssh/src/sshConfig.ts b/packages/maker-remote-ssh/src/sshConfig.ts
index 4e391a329f..b0f470c966 100644
--- a/packages/maker-remote-ssh/src/sshConfig.ts
+++ b/packages/maker-remote-ssh/src/sshConfig.ts
@@ -73,6 +73,10 @@ function readAuthMarker(section: SshConfigSection): 'agent' | 'key' | null {
return null;
}
+function isHostDirective(section: SshConfigSection): boolean {
+ return section.param?.toLowerCase() === 'host';
+}
+
/**
* Read and parse ~/.ssh/config (or any path). Returns a list of concrete
* hosts — wildcards and `Match` blocks are skipped. Missing file → [].
@@ -90,7 +94,7 @@ export async function readSshConfig(filePath = defaultSshConfigPath()): Promise<
const hosts: HostConfig[] = [];
for (const section of parsed) {
- if (section.param !== 'Host') continue;
+ if (!isHostDirective(section)) continue;
const aliases = Array.isArray(section.value) ? section.value : [section.value];
for (const aliasRaw of aliases) {
if (typeof aliasRaw !== 'string') continue;
@@ -142,13 +146,14 @@ export async function upsertHost(
filePath = defaultSshConfigPath(),
): Promise<void> {
const existing = await readRawConfig(filePath);
- const parsed = SSHConfig.parse(existing) as unknown as { remove(query: { Host: string }): void };
+ const parsed = SSHConfig.parse(existing) as SshConfigSection[];
- // ssh-config's `remove({Host: alias})` is the documented way to drop a block.
- // We always drop then re-append so values stay in a known order.
- parsed.remove({ Host: host.id });
+ // Remove the complete block before appending the replacement. The ssh-config
+ // library's `remove({ Host: alias })` matcher is case-sensitive on the
+ // directive name, while OpenSSH treats keywords case-insensitively.
+ removeHostSections(parsed, host.id);
- const append = SSHConfig.parse(formatHostBlock(host)) as unknown as SshConfigSection[];
+ const append = SSHConfig.parse(formatHostBlock(host)) as SshConfigSection[];
for (const node of append) {
(parsed as unknown as { push(node: unknown): void }).push(node);
}
@@ -157,6 +162,16 @@ export async function upsertHost(
await writeAtomic(filePath, out);
}
+/** Remove all concrete Host blocks for an alias, regardless of keyword casing. */
+function removeHostSections(parsed: SshConfigSection[], alias: string): void {
+ for (let index = parsed.length - 1; index >= 0; index -= 1) {
+ const section = parsed[index];
+ if (!isHostDirective(section)) continue;
+ const values = Array.isArray(section.value) ? section.value : [section.value];
+ if (values.includes(alias)) parsed.splice(index, 1);
+ }
+}
+
/**
* Surgically update only the fields we own (HostName / User / Port /
* IdentityFile / IdentitiesOnly) inside an existing host block, leaving
@@ -231,7 +246,7 @@ function findHostSection(
alias: string,
): SshConfigSection | null {
for (const section of parsed) {
- if (section.param !== 'Host') continue;
+ if (!isHostDirective(section)) continue;
const values = Array.isArray(section.value) ? section.value : [section.value];
if (values.includes(alias)) return section;
}
@@ -334,9 +349,9 @@ export async function removeHost(
): Promise<void> {
const existing = await readRawConfig(filePath);
if (!existing) return;
- const parsed = SSHConfig.parse(existing) as unknown as { remove(q: { Host: string }): void; toString(): string };
- parsed.remove({ Host: alias });
- await writeAtomic(filePath, parsed.toString());
+ const parsed = SSHConfig.parse(existing) as SshConfigSection[];
+ removeHostSections(parsed, alias);
+ await writeAtomic(filePath, (parsed as unknown as { toString(): string }).toString());
}
// ── internals ──────────────────────────────────────────────────────────────