Skip to content
TypeScript

fix(ssh): stop asking for a password when the SSH agent can't sign (#23437)

Keith Vertrees1c8c0d8845e4
diff --git a/packages/ssh/src/auth.test.ts b/packages/ssh/src/auth.test.ts
index 439350d6010..898f365e53e 100644
--- a/packages/ssh/src/auth.test.ts
+++ b/packages/ssh/src/auth.test.ts
@@ -11,7 +11,7 @@ import * as Path from "effect/Path";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
-import { buildSshChildEnvironment, isSshAuthFailure } from "./auth.ts";
+import { buildSshChildEnvironment, isSshAgentSigningFailure, isSshAuthFailure } from "./auth.ts";
describe("ssh auth", () => {
it.effect("detects ssh auth failures from common permission denied messages", () =>
@@ -30,6 +30,39 @@ describe("ssh auth", () => {
}),
);
+ it.effect("detects agent signing failures from current and pre-8.2 OpenSSH", () =>
+ Effect.sync(() => {
+ const denied = "julius@devbox: Permission denied (publickey,password).";
+ assert.isTrue(
+ isSshAgentSigningFailure(
+ new Error(
+ `sign_and_send_pubkey: signing failed for ED25519 "/home/julius/.ssh/id_ed25519" from agent: communication with agent failed\r\n${denied}`,
+ ),
+ ),
+ );
+ assert.isTrue(
+ isSshAgentSigningFailure(
+ new Error(`sign_and_send_pubkey: signing failed: agent refused operation\n${denied}`),
+ ),
+ );
+ assert.isTrue(
+ isSshAgentSigningFailure(
+ new Error(
+ `sign_and_send_pubkey: signing failed: communication with agent failed\n${denied}`,
+ ),
+ ),
+ );
+ assert.isFalse(
+ isSshAgentSigningFailure(
+ new Error(
+ `sign_and_send_pubkey: signing failed for ED25519-SK "/home/julius/.ssh/id_ed25519_sk": device not found\n${denied}`,
+ ),
+ ),
+ );
+ assert.isFalse(isSshAgentSigningFailure(new Error(denied)));
+ }),
+ );
+
it.effect("creates askpass env for cached password prompts", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
diff --git a/packages/ssh/src/auth.ts b/packages/ssh/src/auth.ts
index a759063e1df..df243823239 100644
--- a/packages/ssh/src/auth.ts
+++ b/packages/ssh/src/auth.ts
@@ -246,3 +246,11 @@ export function isSshAuthFailure(error: unknown): boolean {
/too many authentication failures/u.test(normalized)
);
}
+
+// A local agent signing failure cannot be remedied by the remote account password.
+export function isSshAgentSigningFailure(error: unknown): boolean {
+ const message = error instanceof Error ? error.message : String(error);
+ return /sign_and_send_pubkey: signing failed(?: for [^\n]* from agent:|: (?:agent refused operation|communication with agent failed))/u.test(
+ message,
+ );
+}
diff --git a/packages/ssh/src/tunnel.test.ts b/packages/ssh/src/tunnel.test.ts
index 360009a6063..6cea42d131a 100644
--- a/packages/ssh/src/tunnel.test.ts
+++ b/packages/ssh/src/tunnel.test.ts
@@ -15,7 +15,7 @@ import { HttpClient, HttpClientResponse } from "effect/http";
import { ChildProcess, ChildProcessSpawner } from "effect/process";
import * as SshAuth from "./auth.ts";
-import { SshCommandError } from "./errors.ts";
+import { SshCommandError, SshPasswordPromptError } from "./errors.ts";
import * as SshTunnel from "./tunnel.ts";
const makeSuccessfulProcess = (stdout: string) => {
@@ -260,6 +260,84 @@ describe("ssh tunnel scripts", () => {
}),
);
+ it.effect.each([
+ {
+ name: "does not ask for a password when the agent cannot sign an accepted key",
+ stderr:
+ 'sign_and_send_pubkey: signing failed for ED25519 "/home/julius/.ssh/id_ed25519" from agent: communication with agent failed\r\n' +
+ "julius@devbox: Permission denied (publickey,password).\r\n",
+ expectedPromptAttempts: [],
+ },
+ {
+ name: "asks for a password when signing failure has no agent provenance",
+ stderr:
+ 'sign_and_send_pubkey: signing failed for ED25519-CERT "key-cert.pub": agent refused operation\n' +
+ "julius@devbox: Permission denied (publickey,password).\n",
+ expectedPromptAttempts: [1],
+ },
+ {
+ name: "asks for a password when the server rejects every key",
+ stderr: "julius@devbox: Permission denied (publickey,password).\r\n",
+ expectedPromptAttempts: [1],
+ },
+ ])("$name", ({ stderr, expectedPromptAttempts }) =>
+ Effect.gen(function* () {
+ const promptAttempts: Array<number> = [];
+ const spawner = ChildProcessSpawner.make((command) =>
+ Effect.succeed(
+ commandArgs(command).includes("-G")
+ ? makeSuccessfulProcess("")
+ : {
+ ...makeSuccessfulProcess(""),
+ exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(255)),
+ stderr: Stream.make(new TextEncoder().encode(stderr)),
+ },
+ ),
+ );
+ const layer = Layer.mergeAll(
+ NodeServices.layer,
+ Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
+ Layer.succeed(HttpClient.HttpClient, testHttpClient),
+ Layer.succeed(NetService.NetService, testNetService),
+ Layer.succeed(
+ SshAuth.SshPasswordPrompt,
+ SshAuth.SshPasswordPrompt.of({
+ isAvailable: true,
+ request: (request) =>
+ Effect.sync(() => {
+ promptAttempts.push(request.attempt);
+ return null;
+ }),
+ }),
+ ),
+ SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
+ );
+ yield* Effect.gen(function* () {
+ const manager = yield* SshTunnel.SshEnvironmentManager;
+ const result = yield* Effect.result(
+ manager.ensureEnvironment({
+ alias: "devbox",
+ hostname: "devbox",
+ username: null,
+ port: null,
+ }),
+ );
+ assert.isTrue(Result.isFailure(result));
+ assert.deepEqual(promptAttempts, expectedPromptAttempts);
+ if (Result.isFailure(result)) {
+ if (expectedPromptAttempts.length === 0) {
+ assert.instanceOf(result.failure, SshCommandError);
+ if (result.failure instanceof SshCommandError) {
+ assert.equal(result.failure.stderr, stderr);
+ }
+ } else {
+ assert.instanceOf(result.failure, SshPasswordPromptError);
+ }
+ }
+ }).pipe(Effect.provide(layer), Effect.scoped);
+ }),
+ );
+
it.effect.each(["local tunnel", "remote server"] as const)(
"waits for %s shutdown before reconnecting the same target",
(stalledStep) =>
diff --git a/packages/ssh/src/tunnel.ts b/packages/ssh/src/tunnel.ts
index 1a98fa7d21a..d2ee6b39a9a 100644
--- a/packages/ssh/src/tunnel.ts
+++ b/packages/ssh/src/tunnel.ts
@@ -1454,7 +1454,7 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
if (input.authSecret !== null) {
authSecrets.delete(input.key);
}
- if (input.promptCount >= 2) {
+ if (input.promptCount >= 2 || SshAuth.isSshAgentSigningFailure(input.error)) {
return yield* input.error;
}