GeezerVison is showing github.com — fix(ssh): cover changed host keys and Windows quoting in recovery hint · pingdotgg/t3code@0081f87
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/ssh/src/command.test.ts +44-8Lines changed: 44 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { assert, describe, it } from "@effect/vitest"; | 2 | 2 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; | 3 | 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; | | 4 | +import * as HostProcess from "@t3tools/shared/HostProcess"; | 4 | 5 | import * as Effect from "effect/Effect"; | 5 | 6 | import * as Layer from "effect/Layer"; | 6 | 7 | import * as Result from "effect/Result"; | | 8 | +import * as Schema from "effect/Schema"; | 7 | 9 | import * as Sink from "effect/Sink"; | 8 | 10 | import * as Stream from "effect/Stream"; | 9 | 11 | import { ChildProcess, ChildProcessSpawner } from "effect/process"; | @@ -12,6 +14,7 @@ import { parseSshResolveOutput, remoteStateKey, runSshCommand } from "./command. | 12 | 14 | import { SshCommandError } from "./errors.ts"; | 13 | 15 |
| 14 | 16 | const encoder = new TextEncoder(); | | 17 | +const isSshCommandError = Schema.is(SshCommandError); | 15 | 18 |
| 16 | 19 | const makeFailedProcess = (input: { readonly stdout: string; readonly stderr?: string }) => { | 17 | 20 | const stdoutStream = Stream.make(encoder.encode(input.stdout)); | @@ -185,24 +188,57 @@ describe("ssh command", () => { | 185 | 188 | }).pipe(Effect.provide(layerProcess)); | 186 | 189 | }); | 187 | 190 |
| | 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 | + | 188 | 197 | 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 }) => { | 193 | 227 | const spawner = ChildProcessSpawner.make(() => | 194 | 228 | Effect.succeed(makeFailedProcess({ stdout: "", stderr })), | 195 | 229 | ); | 196 | 230 | return runSshCommand({ ...target, hostname: "devbox", port: null }).pipe( | 197 | 231 | Effect.flip, | 198 | 232 | 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); | 202 | 236 | }), | | 237 | + Effect.provideService(HostProcess.Platform, platform), | 203 | 238 | Effect.provide( | 204 | | - Layer.merge( | | 239 | + Layer.mergeAll( | 205 | 240 | NodeServices.layer, | | 241 | + NodeCrypto.layer, | 206 | 242 | Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner), | 207 | 243 | ), | 208 | 244 | ), | |
Collapse file Copy file name to clipboardExpand all lines: packages/ssh/src/command.ts +26-13Lines changed: 26 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -151,28 +151,36 @@ export function shellSingleQuote(value: string): string { | 151 | 151 | return `'${value.replaceAll("'", "'\\''")}'`; | 152 | 152 | } | 153 | 153 |
| 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); | 156 | 160 | } | 157 | 161 |
| 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; | 166 | 171 | if (!stderr.includes("Host key verification failed")) return null; | 167 | 172 | const command = [ | 168 | 173 | "ssh", | 169 | 174 | ...(target.port !== null ? ["-p", String(target.port)] : []), | 170 | 175 | ...(hostSpec.startsWith("-") ? ["--"] : []), | 171 | 176 | hostSpec, | 172 | 177 | ] | 173 | | - .map(shellArg) | | 178 | + .map((arg) => terminalArg(arg, platform)) | 174 | 179 | .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()}`; | 176 | 184 | } | 177 | 185 |
| 178 | 186 | function normalizeSshErrorMessage(input: { | @@ -313,7 +321,12 @@ const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(func | 313 | 321 | stdout: diagnosticStdout, | 314 | 322 | stderr, | 315 | 323 | message: | 316 | | - hostKeyVerificationMessage(target, hostSpec, stderr) ?? | | 324 | + hostKeyVerificationMessage({ | | 325 | + target, | | 326 | + hostSpec, | | 327 | + stderr, | | 328 | + platform: yield* HostProcess.Platform, | | 329 | + }) ?? | 317 | 330 | normalizeSshErrorMessage({ | 318 | 331 | stdout: diagnosticStdout, | 319 | 332 | stderr, | |
Collapse file Copy file name to clipboardExpand all lines: scripts/ssh-host-key-evidence-fixture.mjs -59Lines changed: 0 additions & 59 deletions
|