ssh-client-wrapper 3.4.7 → 3.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/agent.js +29 -16
- package/package.json +1 -1
package/lib/agent.js
CHANGED
|
@@ -6,7 +6,7 @@ import { promisify } from "node:util";
|
|
|
6
6
|
import { setTimeout as setTimeoutPromise } from "node:timers/promises";
|
|
7
7
|
import Debug from "debug";
|
|
8
8
|
import { spawn } from "node-pty";
|
|
9
|
-
import {
|
|
9
|
+
import { resolveExecutable, releasePty } from "./util.js";
|
|
10
10
|
|
|
11
11
|
const debug = Debug("sshClientWrapper:debug:agent");
|
|
12
12
|
const debugVerbose = Debug("sshClientWrapper:verbose:agent");
|
|
@@ -19,6 +19,12 @@ const LOCK_STALE_MS = 60000;
|
|
|
19
19
|
const MAX_BAD_PASSPHRASE = 3;
|
|
20
20
|
const MAX_SOCKET_PATH = 103;
|
|
21
21
|
|
|
22
|
+
//the prompts of ssh-add differ from the one of ssh ("Enter passphrase for key '/k': "):
|
|
23
|
+
//"Enter passphrase for /k: " and, after a wrong one, "Bad passphrase, try again for /k: "
|
|
24
|
+
//both have "(comment)" after the path in some versions of OpenSSH
|
|
25
|
+
const reAddKeyPrompt = /(?:Enter passphrase for|Bad passphrase, try again for) /;
|
|
26
|
+
const reBadPassphrase = /Bad passphrase|incorrect passphrase/i;
|
|
27
|
+
|
|
22
28
|
//sentinel candidate meaning "run ssh-add with the inherited environment and let
|
|
23
29
|
//OpenSSH use its default agent (e.g. the Windows OpenSSH Authentication Agent pipe)"
|
|
24
30
|
const AGENT_DEFAULT = "__scw_default_agent__";
|
|
@@ -289,6 +295,23 @@ function addKey(sock, keyFile, phCallback, ttl) {
|
|
|
289
295
|
fail("ADDKEY_TIMEOUT", `ssh-add did not finish within ${ADDKEY_TIMEOUT_SEC} sec`);
|
|
290
296
|
}, ADDKEY_TIMEOUT_SEC * 1000);
|
|
291
297
|
|
|
298
|
+
const answerPrompt = ()=>{
|
|
299
|
+
resolvePassphrase(phCallback)
|
|
300
|
+
.then((v)=>{
|
|
301
|
+
if (settled) {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (v === null) {
|
|
305
|
+
fail("NO_PASSPHRASE", "key is encrypted but no passphrase is available");
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
pty.write(`${v}\n`);
|
|
309
|
+
})
|
|
310
|
+
.catch((e)=>{
|
|
311
|
+
finish(reject, e);
|
|
312
|
+
});
|
|
313
|
+
};
|
|
314
|
+
|
|
292
315
|
pty.onData((data)=>{
|
|
293
316
|
const output = data.toString();
|
|
294
317
|
debugVerbose(output);
|
|
@@ -297,25 +320,15 @@ function addKey(sock, keyFile, phCallback, ttl) {
|
|
|
297
320
|
fail("NO_AGENT", "no ssh-agent is reachable");
|
|
298
321
|
return;
|
|
299
322
|
}
|
|
300
|
-
if (
|
|
301
|
-
resolvePassphrase(phCallback)
|
|
302
|
-
.then((v)=>{
|
|
303
|
-
if (v === null) {
|
|
304
|
-
fail("NO_PASSPHRASE", "key is encrypted but no passphrase is available");
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
pty.write(`${v}\n`);
|
|
308
|
-
})
|
|
309
|
-
.catch((e)=>{
|
|
310
|
-
finish(reject, e);
|
|
311
|
-
});
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
314
|
-
if (/Bad passphrase|incorrect passphrase/i.test(output)) {
|
|
323
|
+
if (reBadPassphrase.test(output)) {
|
|
315
324
|
badTries += 1;
|
|
316
325
|
if (badTries >= MAX_BAD_PASSPHRASE || typeof phCallback !== "function") {
|
|
317
326
|
fail("BAD_PASSPHRASE", "bad passphrase for private key");
|
|
327
|
+
return;
|
|
318
328
|
}
|
|
329
|
+
}
|
|
330
|
+
if (reAddKeyPrompt.test(output)) {
|
|
331
|
+
answerPrompt();
|
|
319
332
|
return;
|
|
320
333
|
}
|
|
321
334
|
if (/Identity added/.test(output)) {
|