Skip to content

fix(sshconfig): expand IdentityFile tokens, honor .pub agent identities and IdentityAgent - #90

Merged
flymysql merged 2 commits into
flymysql:mainfrom
FlintyLemming:fix/sshconfig-agent-identity
Nov 5, 2026
Merged

flymysql merged 2 commits into
flymysql:mainfrom
FlintyLemming:fix/sshconfig-agent-identity

Conversation

@FlintyLemming

Copy link
Copy Markdown
Contributor

Problem

With a common agent-based ~/.ssh/config (Bitwarden / 1Password / rbw style):

Host *
    IdentityAgent /path/to/agent.sock
Host *
    IdentitiesOnly yes
    IdentityFile ~/.ssh/%n.pub

an ssh-config alias machine can be added, but connecting (e.g. picking a workspace) fails with:

cannot read private key "/Users/me/.ssh/%n.pub": ENOENT: no such file or directory

Three gaps:

  1. %n and the other ssh_config(5) tokens in IdentityFile were not expanded.
  2. A .pub IdentityFile, which is OpenSSH's way of saying "use the agent key matching this public key", was read as a private key.
  3. IdentityAgent was ignored; only the host process SSH_AUTH_SOCK was used.

Fix

  • lib/sshconfig.js: new expandSshTokens (%% %d %h %n %p %r %u %i %l %L, plus ${VAR}), applied to IdentityFile / IdentityAgent. IdentityAgent and IdentitiesOnly are now parsed and returned in the resolution (agentSocket, identitiesOnly), for jump hops too. A hop only gets these fields when they are set, so its existing shape is unchanged.
  • lib/pool.js: a .pub key path means "authenticate through the agent". PreferredKeyAgent wraps the agent so that the matching key is offered first, or only that key with IdentitiesOnly yes. This avoids hitting MaxAuthTries when the agent holds many keys. IdentityAgent takes precedence over SSH_AUTH_SOCK, and none disables the agent. If a .pub identity is configured but no agent is available, the error now says so instead of reporting a private-key read failure.
  • lib/index.js, lib/registry.js: plumbing for agentSocket / identitiesOnly (active pool, per-machine pools, connection probe).

No new dependencies. The system-OpenSSH backend is unaffected: an alias is still handed to ssh verbatim.

Tests

  • New test/sshconfig-agent-identity.test.js:
    • resolver unit tests for token expansion and IdentityAgent / IdentitiesOnly;
    • an end-to-end test that runs a real SshPool against an in-process ssh2 server through an in-process agent socket holding 3 keys. It asserts that the server is offered only the key matching the .pub file.
  • npm test: 875 pass, 0 fail. node check.mjs OK.
  • Manually verified against a real host using rbw-agent with the config above.

🤖 Generated with Claude Code

FlintyLemming and others added 2 commits November 3, 2026 21:03
…es and IdentityAgent

A config such as

    Host *
        IdentityAgent /path/to/agent.sock
    Host *
        IdentitiesOnly yes
        IdentityFile ~/.ssh/%n.pub

failed to connect with `cannot read private key ".../%n.pub": ENOENT`:

- `%n` and the other ssh_config(5) tokens were not expanded
  (new expandSshTokens: %% %d %h %n %p %r %u %i %l %L, plus ${VAR}).
- A `.pub` IdentityFile, which is OpenSSH's way of saying "use this agent
  key", was read as a private key. The pool now authenticates through the
  agent and offers the matching key first, or only that key with
  IdentitiesOnly yes, so an agent holding many keys no longer trips
  MaxAuthTries.
- IdentityAgent was ignored. It now takes precedence over the process
  SSH_AUTH_SOCK (`none`, `SSH_AUTH_SOCK` and `$VAR` are honored), for
  jump hops too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Windows cannot listen on a file path (EACCES in the temp dir); an agent
there is a named pipe, which ssh2's createAgent treats as OpenSSH.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@flymysql
flymysql merged commit 094ad63 into flymysql:main Nov 5, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants