GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(ssh): cover changed host keys and Windows quoting in recovery hint · pingdotgg/t3code@0081f87

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/ssh/src
    • command.test.ts
    • command.ts
  • scripts
    • ssh-host-key-evidence-fixture.mjs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/ssh/src/command.test.ts‎

Copy file name to clipboardExpand all lines: packages/ssh/src/command.test.ts
+44-8Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { assert, describe, it } from "@effect/vitest";
22
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
33
import * as NodeServices from "@effect/platform-node/NodeServices";
4+
import * as HostProcess from "@t3tools/shared/HostProcess";
45
import * as Effect from "effect/Effect";
56
import * as Layer from "effect/Layer";
67
import * as Result from "effect/Result";
8+
import * as Schema from "effect/Schema";
79
import * as Sink from "effect/Sink";
810
import * as Stream from "effect/Stream";
911
import { ChildProcess, ChildProcessSpawner } from "effect/process";
@@ -12,6 +14,7 @@ import { parseSshResolveOutput, remoteStateKey, runSshCommand } from "./command.
1214
import { SshCommandError } from "./errors.ts";
1315

1416
const encoder = new TextEncoder();
17+
const isSshCommandError = Schema.is(SshCommandError);
1518

1619
const makeFailedProcess = (input: { readonly stdout: string; readonly stderr?: string }) => {
1720
const stdoutStream = Stream.make(encoder.encode(input.stdout));
@@ -185,24 +188,57 @@ describe("ssh command", () => {
185188
}).pipe(Effect.provide(layerProcess));
186189
});
187190

191+
const changedKeyStderr =
192+
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n" +
193+
"@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @\r\n" +
194+
" ssh-keygen -f '/home/me/.ssh/known_hosts' -R 'devbox'\r\n" +
195+
"Host key verification failed.\r\n";
196+
188197
it.effect.each([
189-
[{ alias: "my host", username: "o'brien" }, "ssh 'o'\\''brien@my host'"],
190-
[{ alias: "-oProxyCommand=false", username: null }, "ssh -- -oProxyCommand=false"],
191-
] as const)("quotes the terminal command for a failed host key check", ([target, command]) => {
192-
const stderr = "Host key verification failed.\r\n";
198+
{
199+
name: "quotes the terminal command for a POSIX shell",
200+
target: { alias: "my host", username: "o'brien" },
201+
platform: "linux",
202+
stderr: "Host key verification failed.\r\n",
203+
expected: ["`ssh 'o'\\''brien@my host'`"],
204+
},
205+
{
206+
name: "quotes the terminal command for a Windows shell",
207+
target: { alias: "my host", username: "me" },
208+
platform: "win32",
209+
stderr: "Host key verification failed.\r\n",
210+
expected: ['`ssh "me@my host"`'],
211+
},
212+
{
213+
name: "keeps an option-shaped alias out of the option list",
214+
target: { alias: "-oProxyCommand=false", username: null },
215+
platform: "linux",
216+
stderr: "Host key verification failed.\r\n",
217+
expected: ["`ssh -- -oProxyCommand=false`"],
218+
},
219+
{
220+
name: "tells the user to remove a changed host key first",
221+
target: { alias: "devbox", username: null },
222+
platform: "linux",
223+
stderr: changedKeyStderr,
224+
expected: ["has changed", "`ssh-keygen -R`", "`ssh devbox`", "-R 'devbox'"],
225+
},
226+
] as const)("$name", ({ target, platform, stderr, expected }) => {
193227
const spawner = ChildProcessSpawner.make(() =>
194228
Effect.succeed(makeFailedProcess({ stdout: "", stderr })),
195229
);
196230
return runSshCommand({ ...target, hostname: "devbox", port: null }).pipe(
197231
Effect.flip,
198232
Effect.map((error) => {
199-
assert.instanceOf(error, SshCommandError);
200-
assert.include(error.message, `\`${command}\``);
201-
if (error instanceof SshCommandError) assert.equal(error.stderr, stderr);
233+
assert.isTrue(isSshCommandError(error));
234+
for (const text of expected) assert.include(error.message, text);
235+
if (isSshCommandError(error)) assert.equal(error.stderr, stderr);
202236
}),
237+
Effect.provideService(HostProcess.Platform, platform),
203238
Effect.provide(
204-
Layer.merge(
239+
Layer.mergeAll(
205240
NodeServices.layer,
241+
NodeCrypto.layer,
206242
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
207243
),
208244
),
Collapse file

‎packages/ssh/src/command.ts‎

Copy file name to clipboardExpand all lines: packages/ssh/src/command.ts
+26-13Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,28 +151,36 @@ export function shellSingleQuote(value: string): string {
151151
return `'${value.replaceAll("'", "'\\''")}'`;
152152
}
153153

154-
function shellArg(value: string): string {
155-
return /^[\w@%+=:,./-]+$/u.test(value) ? value : shellSingleQuote(value);
154+
const SAFE_SHELL_ARG = /^[\w@%+=:,./-]+$/u;
155+
156+
// cmd.exe and PowerShell both read double quotes, but not POSIX single quotes.
157+
function terminalArg(value: string, platform: NodeJS.Platform): string {
158+
if (SAFE_SHELL_ARG.test(value)) return value;
159+
return platform === "win32" ? `"${value}"` : shellSingleQuote(value);
156160
}
157161

158-
// ssh prints this for both unknown and changed host keys when it cannot ask.
159-
// The app never answers host key prompts, so the user has to check the
160-
// fingerprint themselves in a terminal before the key is trusted.
161-
function hostKeyVerificationMessage(
162-
target: DesktopSshEnvironmentTarget,
163-
hostSpec: string,
164-
stderr: string,
165-
): string | null {
162+
// ssh prints this for unknown and changed host keys when it cannot ask. The app
163+
// never answers host key prompts, so the user checks the fingerprint in a terminal.
164+
function hostKeyVerificationMessage(input: {
165+
readonly target: DesktopSshEnvironmentTarget;
166+
readonly hostSpec: string;
167+
readonly stderr: string;
168+
readonly platform: NodeJS.Platform;
169+
}): string | null {
170+
const { target, hostSpec, stderr, platform } = input;
166171
if (!stderr.includes("Host key verification failed")) return null;
167172
const command = [
168173
"ssh",
169174
...(target.port !== null ? ["-p", String(target.port)] : []),
170175
...(hostSpec.startsWith("-") ? ["--"] : []),
171176
hostSpec,
172177
]
173-
.map(shellArg)
178+
.map((arg) => terminalArg(arg, platform))
174179
.join(" ");
175-
return `SSH could not verify the host key for ${hostSpec}. Run \`${command}\` in a terminal, check that the host key fingerprint is the one you expect, then try again.\n\n${stderr.trim()}`;
180+
const guidance = stderr.includes("REMOTE HOST IDENTIFICATION HAS CHANGED")
181+
? `The host key for ${hostSpec} has changed since it was trusted. Only if you expect this change, remove the old key with the \`ssh-keygen -R\` command in the output below, run \`${command}\` in a terminal, check that the new host key fingerprint is the one you expect, then try again.`
182+
: `SSH could not verify the host key for ${hostSpec}. Run \`${command}\` in a terminal, check that the host key fingerprint is the one you expect, then try again.`;
183+
return `${guidance}\n\n${stderr.trim()}`;
176184
}
177185

178186
function normalizeSshErrorMessage(input: {
@@ -313,7 +321,12 @@ const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(func
313321
stdout: diagnosticStdout,
314322
stderr,
315323
message:
316-
hostKeyVerificationMessage(target, hostSpec, stderr) ??
324+
hostKeyVerificationMessage({
325+
target,
326+
hostSpec,
327+
stderr,
328+
platform: yield* HostProcess.Platform,
329+
}) ??
317330
normalizeSshErrorMessage({
318331
stdout: diagnosticStdout,
319332
stderr,
Collapse file

‎scripts/ssh-host-key-evidence-fixture.mjs‎

Copy file name to clipboardExpand all lines: scripts/ssh-host-key-evidence-fixture.mjs
-59Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.