Skip to content

Commit 23818e8

Browse files
dansparkclaude
andcommitted
fix(ssh): check agent signing failures in one pass per line
CodeQL alert 153 flagged the agent-signing-failure regex, added upstream since v0.1.4-nightly.20261027.3796. The regex restarts at every occurrence of "sign_and_send_pubkey: signing failed for " and scans to the end of the line each time, so a line that repeats the prefix is quadratic. V8's linear fallback engine does not cover this pattern: 328 KB took 4.3 s and 820 KB took 27.8 s. The message is SSH client output for a host the user chose, so a hostile host could stall the server while classifying a failed login. Match the two literal agent errors with includes, and scan each line once from the first "for" prefix for " from agent:". 400,000 random token strings classify the same as the old regex, 31,869 of them positive. The new test, a 1.6 MB line of repeated prefixes, timed out at 60 s on the pinned source and now passes with the other 16 SSH auth and tunnel tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9ed051f commit 23818e8

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎.github/t2code/overlay.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,16 @@
704704
"path": "apps/server/src/cloud/relayTracing.test.ts",
705705
"before": "it.effect(\"a policy with telemetry off stops relay tracing to T3 Connect\", () =>\n Effect.gen(function* () {\n assert.isTrue(yield* tracesTo());\n",
706706
"after": "it.effect(\"relay tracing to T3 Connect stays off with or without a managed policy\", () =>\n Effect.gen(function* () {\n assert.isFalse(yield* tracesTo());\n"
707+
},
708+
{
709+
"path": "packages/ssh/src/auth.ts",
710+
"before": "// A local agent signing failure cannot be remedied by the remote account password.\nexport function isSshAgentSigningFailure(error: unknown): boolean {\n const message = error instanceof Error ? error.message : String(error);\n return /sign_and_send_pubkey: signing failed(?: for [^\\n]* from agent:|: (?:agent refused operation|communication with agent failed))/u.test(\n message,\n );\n}\n",
711+
"after": "const AGENT_SIGNING_FAILED = \"sign_and_send_pubkey: signing failed\";\n\n// A local agent signing failure cannot be remedied by the remote account password.\nexport function isSshAgentSigningFailure(error: unknown): boolean {\n const message = error instanceof Error ? error.message : String(error);\n if (\n message.includes(`${AGENT_SIGNING_FAILED}: agent refused operation`) ||\n message.includes(`${AGENT_SIGNING_FAILED}: communication with agent failed`)\n ) {\n return true;\n }\n // One scan per line: a regex retried at every repeated prefix is quadratic in the line length.\n return message.split(\"\\n\").some((line) => {\n const start = line.indexOf(`${AGENT_SIGNING_FAILED} for `);\n return (\n start !== -1 &&\n line.includes(\" from agent:\", start + AGENT_SIGNING_FAILED.length + \" for \".length)\n );\n });\n}\n"
712+
},
713+
{
714+
"path": "packages/ssh/src/auth.test.ts",
715+
"before": " assert.isFalse(isSshAgentSigningFailure(new Error(denied)));\n }),\n );\n",
716+
"after": " assert.isFalse(isSshAgentSigningFailure(new Error(denied)));\n }),\n );\n\n it.effect(\"checks a long line of repeated signing-failure prefixes in one pass\", () =>\n Effect.sync(() => {\n const repeated = \"sign_and_send_pubkey: signing failed for \".repeat(40_000);\n assert.isFalse(isSshAgentSigningFailure(new Error(repeated)));\n assert.isTrue(isSshAgentSigningFailure(new Error(`${repeated} from agent: refused`)));\n }),\n );\n"
707717
}
708718
],
709719
"files": [

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ describe("ssh auth", () => {
6363
}),
6464
);
6565

66+
it.effect("checks a long line of repeated signing-failure prefixes in one pass", () =>
67+
Effect.sync(() => {
68+
const repeated = "sign_and_send_pubkey: signing failed for ".repeat(40_000);
69+
assert.isFalse(isSshAgentSigningFailure(new Error(repeated)));
70+
assert.isTrue(isSshAgentSigningFailure(new Error(`${repeated} from agent: refused`)));
71+
}),
72+
);
73+
6674
it.effect("creates askpass env for cached password prompts", () =>
6775
Effect.gen(function* () {
6876
const fs = yield* FileSystem.FileSystem;

‎packages/ssh/src/auth.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,23 @@ export function isSshAuthFailure(error: unknown): boolean {
247247
);
248248
}
249249

250+
const AGENT_SIGNING_FAILED = "sign_and_send_pubkey: signing failed";
251+
250252
// A local agent signing failure cannot be remedied by the remote account password.
251253
export function isSshAgentSigningFailure(error: unknown): boolean {
252254
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-
);
255+
if (
256+
message.includes(`${AGENT_SIGNING_FAILED}: agent refused operation`) ||
257+
message.includes(`${AGENT_SIGNING_FAILED}: communication with agent failed`)
258+
) {
259+
return true;
260+
}
261+
// One scan per line: a regex retried at every repeated prefix is quadratic in the line length.
262+
return message.split("\n").some((line) => {
263+
const start = line.indexOf(`${AGENT_SIGNING_FAILED} for `);
264+
return (
265+
start !== -1 &&
266+
line.includes(" from agent:", start + AGENT_SIGNING_FAILED.length + " for ".length)
267+
);
268+
});
256269
}

0 commit comments

Comments
 (0)