Skip to content

addKey() never detects the ssh-add passphrase prompt, so encrypted keys always wait 60 s (ADDKEY_TIMEOUT) #83

Open

Description

@k-ota-sec

Summary

When keyFile points to a passphrase-protected private key, the passphrase callback is called only after about 60 seconds, on every connection.

addKey() in lib/agent.js checks the ssh-add output with rePhPrompt. That regex matches the prompt of ssh, but not the prompt of ssh-add. So addKey() never answers the prompt and fails with ADDKEY_TIMEOUT (ADDKEY_TIMEOUT_SEC = 60). Only then does ensureAgentForHost() fall back to the legacy pty auth, which finally calls the callback.

As a result, agent-based auth never works for encrypted keys, and every first connection is delayed by 60 s.

Environment

  • ssh-client-wrapper: 3.1.0 (observed). The same code is still in v3.3.4 (lib/util.js:44, lib/agent.js:300).
  • OpenSSH_9.2p1 Debian-2+deb12u10 (node:24-slim based container)
  • Used from OPEN-WHEEL, with passphrase set to an async callback that asks the user in the browser

Steps to reproduce

import SshClientWrapper from "ssh-client-wrapper";

const t0 = Date.now();
const ssh = new SshClientWrapper({
  host: "<host>",
  user: "<user>",
  keyFile: "/root/.ssh/id_ed25519", // passphrase-protected key
  passphrase: async ()=>{
    console.log(`passphrase callback after ${(Date.now() - t0) / 1000}s`);
    return "<passphrase>";
  }
});
await ssh.canConnect(120);

Results

Condition Time until the passphrase callback
default (agent is used) 63.7 s
useAgent: false 0.31 s
reference: plain ssh shows its prompt 0.30 s

Debug log (DEBUG=sshClientWrapper:debug*):

09:43:34.085Z sshClientWrapper:debug:agent spawning shared ssh-agent at /tmp/scw-0/agent.sock
09:44:34.105Z sshClientWrapper:debug:agent agent setup did not complete (ADDKEY_TIMEOUT); using legacy pty auth
...
09:44:37.791Z sshClientWrapper:debug:fork call passphrase callback function

Root cause

// lib/util.js:44
export const rePhPrompt = /Enter passphrase for key/;

The two commands print different prompts:

Command Prompt Matches rePhPrompt
ssh Enter passphrase for key '/root/.ssh/id_ed25519': yes
ssh-add Enter passphrase for /root/.ssh/id_ed25519: no
ssh-add (key with a comment) Enter passphrase for /path/to/key (comment): no

The unit tests do not catch this, because test/unit/agent.js (lines 133 and 170) mocks the ssh-add pty with the ssh style prompt "Enter passphrase for key '/k': ".

Workaround

Setting useAgent: false in hostInfo forces the legacy pty passphrase path, so the callback is called right away. Agent-based auth is not used in that case.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions