Skip to content

Commit 1c8c0d8

Browse files
fix(ssh): stop asking for a password when the SSH agent can't sign (#23437)
Co-authored-by: maria-rcks <[email protected]>
1 parent 2a4d128 commit 1c8c0d8

4 files changed

Lines changed: 122 additions & 3 deletions

File tree

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as Path from "effect/Path";
1111
import * as HostProcess from "@t3tools/shared/HostProcess";
1212
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
1313

14-
import { buildSshChildEnvironment, isSshAuthFailure } from "./auth.ts";
14+
import { buildSshChildEnvironment, isSshAgentSigningFailure, isSshAuthFailure } from "./auth.ts";
1515

1616
describe("ssh auth", () => {
1717
it.effect("detects ssh auth failures from common permission denied messages", () =>
@@ -30,6 +30,39 @@ describe("ssh auth", () => {
3030
}),
3131
);
3232

33+
it.effect("detects agent signing failures from current and pre-8.2 OpenSSH", () =>
34+
Effect.sync(() => {
35+
const denied = "julius@devbox: Permission denied (publickey,password).";
36+
assert.isTrue(
37+
isSshAgentSigningFailure(
38+
new Error(
39+
`sign_and_send_pubkey: signing failed for ED25519 "/home/julius/.ssh/id_ed25519" from agent: communication with agent failed\r\n${denied}`,
40+
),
41+
),
42+
);
43+
assert.isTrue(
44+
isSshAgentSigningFailure(
45+
new Error(`sign_and_send_pubkey: signing failed: agent refused operation\n${denied}`),
46+
),
47+
);
48+
assert.isTrue(
49+
isSshAgentSigningFailure(
50+
new Error(
51+
`sign_and_send_pubkey: signing failed: communication with agent failed\n${denied}`,
52+
),
53+
),
54+
);
55+
assert.isFalse(
56+
isSshAgentSigningFailure(
57+
new Error(
58+
`sign_and_send_pubkey: signing failed for ED25519-SK "/home/julius/.ssh/id_ed25519_sk": device not found\n${denied}`,
59+
),
60+
),
61+
);
62+
assert.isFalse(isSshAgentSigningFailure(new Error(denied)));
63+
}),
64+
);
65+
3366
it.effect("creates askpass env for cached password prompts", () =>
3467
Effect.gen(function* () {
3568
const fs = yield* FileSystem.FileSystem;

‎packages/ssh/src/auth.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,11 @@ export function isSshAuthFailure(error: unknown): boolean {
246246
/too many authentication failures/u.test(normalized)
247247
);
248248
}
249+
250+
// A local agent signing failure cannot be remedied by the remote account password.
251+
export function isSshAgentSigningFailure(error: unknown): boolean {
252+
const message = error instanceof Error ? error.message : String(error);
253+
return /sign_and_send_pubkey: signing failed(?: for [^\n]* from agent:|: (?:agent refused operation|communication with agent failed))/u.test(
254+
message,
255+
);
256+
}

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

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { HttpClient, HttpClientResponse } from "effect/http";
1515
import { ChildProcess, ChildProcessSpawner } from "effect/process";
1616

1717
import * as SshAuth from "./auth.ts";
18-
import { SshCommandError } from "./errors.ts";
18+
import { SshCommandError, SshPasswordPromptError } from "./errors.ts";
1919
import * as SshTunnel from "./tunnel.ts";
2020

2121
const makeSuccessfulProcess = (stdout: string) => {
@@ -260,6 +260,84 @@ describe("ssh tunnel scripts", () => {
260260
}),
261261
);
262262

263+
it.effect.each([
264+
{
265+
name: "does not ask for a password when the agent cannot sign an accepted key",
266+
stderr:
267+
'sign_and_send_pubkey: signing failed for ED25519 "/home/julius/.ssh/id_ed25519" from agent: communication with agent failed\r\n' +
268+
"julius@devbox: Permission denied (publickey,password).\r\n",
269+
expectedPromptAttempts: [],
270+
},
271+
{
272+
name: "asks for a password when signing failure has no agent provenance",
273+
stderr:
274+
'sign_and_send_pubkey: signing failed for ED25519-CERT "key-cert.pub": agent refused operation\n' +
275+
"julius@devbox: Permission denied (publickey,password).\n",
276+
expectedPromptAttempts: [1],
277+
},
278+
{
279+
name: "asks for a password when the server rejects every key",
280+
stderr: "julius@devbox: Permission denied (publickey,password).\r\n",
281+
expectedPromptAttempts: [1],
282+
},
283+
])("$name", ({ stderr, expectedPromptAttempts }) =>
284+
Effect.gen(function* () {
285+
const promptAttempts: Array<number> = [];
286+
const spawner = ChildProcessSpawner.make((command) =>
287+
Effect.succeed(
288+
commandArgs(command).includes("-G")
289+
? makeSuccessfulProcess("")
290+
: {
291+
...makeSuccessfulProcess(""),
292+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(255)),
293+
stderr: Stream.make(new TextEncoder().encode(stderr)),
294+
},
295+
),
296+
);
297+
const layer = Layer.mergeAll(
298+
NodeServices.layer,
299+
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
300+
Layer.succeed(HttpClient.HttpClient, testHttpClient),
301+
Layer.succeed(NetService.NetService, testNetService),
302+
Layer.succeed(
303+
SshAuth.SshPasswordPrompt,
304+
SshAuth.SshPasswordPrompt.of({
305+
isAvailable: true,
306+
request: (request) =>
307+
Effect.sync(() => {
308+
promptAttempts.push(request.attempt);
309+
return null;
310+
}),
311+
}),
312+
),
313+
SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
314+
);
315+
yield* Effect.gen(function* () {
316+
const manager = yield* SshTunnel.SshEnvironmentManager;
317+
const result = yield* Effect.result(
318+
manager.ensureEnvironment({
319+
alias: "devbox",
320+
hostname: "devbox",
321+
username: null,
322+
port: null,
323+
}),
324+
);
325+
assert.isTrue(Result.isFailure(result));
326+
assert.deepEqual(promptAttempts, expectedPromptAttempts);
327+
if (Result.isFailure(result)) {
328+
if (expectedPromptAttempts.length === 0) {
329+
assert.instanceOf(result.failure, SshCommandError);
330+
if (result.failure instanceof SshCommandError) {
331+
assert.equal(result.failure.stderr, stderr);
332+
}
333+
} else {
334+
assert.instanceOf(result.failure, SshPasswordPromptError);
335+
}
336+
}
337+
}).pipe(Effect.provide(layer), Effect.scoped);
338+
}),
339+
);
340+
263341
it.effect.each(["local tunnel", "remote server"] as const)(
264342
"waits for %s shutdown before reconnecting the same target",
265343
(stalledStep) =>

‎packages/ssh/src/tunnel.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,7 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
14541454
if (input.authSecret !== null) {
14551455
authSecrets.delete(input.key);
14561456
}
1457-
if (input.promptCount >= 2) {
1457+
if (input.promptCount >= 2 || SshAuth.isSshAgentSigningFailure(input.error)) {
14581458
return yield* input.error;
14591459
}
14601460

0 commit comments

Comments
 (0)