fix: honor SSH agent opt-out

This commit is contained in:
bincxz committed 2026-07-12 17:49:57 +08:00
1 parent 7cb722a5c5
commit 0046291376
14 files changed
+61 -13

No files matched your search

@@ -258,6 +258,7 @@ async function startPortForward(event, payload) {
username: connectOpts.username,
logPrefix: "[PortForward]",
defaultKeys,
allowAgentFallback: useSshAgent !== false,
});
applyAuthToConnOpts(connectOpts, authConfig);
if (isTunnelCancelled(tunnelState)) {
@@ -193,6 +193,7 @@ function createOpenConnectionApi(ctx) {
unlockedEncryptedKeys: options._unlockedEncryptedKeys || [],
defaultKeys,
sshAgentSocketOverride: defaultAgentSocket,
allowAgentFallback: jump.useSshAgent !== false,
onAuthAttempt: (method) => {
sendSftpProgress(sender, connId, hopLabel, 'auth-attempt', method);
},
@@ -859,6 +860,7 @@ function createOpenConnectionApi(ctx) {
logPrefix: "[SFTP]",
defaultKeys: systemAuthAgent && options.identitiesOnly ? [] : defaultKeys,
sshAgentSocketOverride: agentSocket,
allowAgentFallback: options.useSshAgent !== false,
onAuthAttempt: (method) => {
sendSftpProgress(event.sender, connId, options.hostname, 'auth-attempt', method);
},
+4 -1
View File
@@ -831,6 +831,7 @@ function isPasswordProvided(password) {
*/
function buildAuthHandler(options) {
const { privateKey, password, passphrase, agent, username, logPrefix = "[SSH]", unlockedEncryptedKeys = [], defaultKeys = [], sshAgentSocketOverride, onAuthAttempt } = options;
const allowAgentFallback = options.allowAgentFallback !== false;
// Determine what type of explicit auth the user configured
const hasExplicitKey = !!privateKey;
@@ -845,7 +846,9 @@ function buildAuthHandler(options) {
// Allow callers to pass in a pre-validated agent socket (e.g. from async
// getAvailableAgentSocket). Fall back to synchronous getSshAgentSocket()
// which on Windows always returns the pipe path without checking the service.
const sshAgentSocket = sshAgentSocketOverride !== undefined ? sshAgentSocketOverride : getSshAgentSocket();
const sshAgentSocket = allowAgentFallback
? (sshAgentSocketOverride !== undefined ? sshAgentSocketOverride : getSshAgentSocket())
: null;
// Only use system ssh-agent BEFORE user's auth when:
// - User explicitly configured agent, OR
@@ -9,6 +9,7 @@ const path = require("node:path");
const { Duplex } = require("node:stream");
const {
buildAuthHandler,
getAvailableAgentSocket,
getNativeOpenSshAgentSocket,
cygwinAgentConnectable,
@@ -18,6 +19,16 @@ const {
resolveIdentityAgentPath,
} = require("./sshAuthHelper.cjs");
test("explicit agent opt-out suppresses automatic socket fallback", () => {
const auth = buildAuthHandler({
username: "deploy",
allowAgentFallback: false,
sshAgentSocketOverride: "/tmp/agent.sock",
});
assert.equal(auth.agent, null);
});
test("IdentityAgent paths expand standard OpenSSH connection tokens", () => {
const resolved = resolveIdentityAgentPath(
"%d/.ssh/agent-%h-%p-%r-%u-%l-%L-%i-%%-%C.sock",
+4 -2
View File
@@ -707,6 +707,7 @@ async function connectThroughChain(event, options, jumpHosts, targetHost, target
logPrefix: `[Chain] Hop ${i + 1}`,
unlockedEncryptedKeys: options._unlockedEncryptedKeys || [],
defaultKeys,
allowAgentFallback: jump.useSshAgent !== false,
onAuthAttempt: (method) => {
sendProgress(i + 1, totalHops + 1, hopLabel, 'auth-attempt', method);
},
@@ -1292,11 +1293,12 @@ function registerHandlers(ipcMain, options = {}) {
ipcMain.handle("netcatty:key:generate", generateKeyPair);
ipcMain.handle("netcatty:sshDebugLog:info", getSshDebugLogInfo);
ipcMain.handle("netcatty:sshDebugLog:openDir", openSshDebugLogDir);
ipcMain.handle("netcatty:ssh:check-agent", async (_event, identityAgent) => {
ipcMain.handle("netcatty:ssh:check-agent", async (_event, options = {}) => {
const identityAgent = typeof options === "string" ? options : options.identityAgent;
if (process.platform === "win32" && !identityAgent) {
return await checkWindowsSshAgent();
}
const socketPath = await getAvailableSystemAgentSocket(identityAgent);
const socketPath = await getAvailableSystemAgentSocket(identityAgent, typeof options === "string" ? {} : options);
return {
running: Boolean(socketPath),
startupType: socketPath ? "running" : "stopped",
@@ -191,6 +191,7 @@ function createExecCommandApi(ctx) {
username: connectOpts.username,
logPrefix: "[SSH Exec]",
defaultKeys,
allowAgentFallback: payload.useSshAgent !== false,
});
applyAuthToConnOpts(connectOpts, authConfig);
@@ -257,7 +257,7 @@ function createMoshStatsConnectionApi(ctx) {
// any saved password (ssh2 tries agent before password), so a
// public-key host that also happens to have a stored password still
// authenticates via the agent instead of failing on password-only.
if (!connectOpts.agent && !agent && !connectOpts.privateKey) {
if (auth.useSshAgent !== false && !connectOpts.agent && !agent && !connectOpts.privateKey) {
const agentSocket = getSshAgentSocket(auth.identityAgent);
if (agentSocket) {
connectOpts.agent = agentSocket;
+1 -1
View File
@@ -778,7 +778,7 @@ function createStartSessionApi(ctx) {
// If no primary auth method configured, try ssh-agent first, then ALL default keys.
// Skip default-key primaries when the user explicitly chose a key (inline or
// identityFilePaths) even if loading that key failed (issue #1614).
if (!connectOpts.privateKey && !connectOpts.password && !connectOpts.agent) {
if (options.useSshAgent !== false && !connectOpts.privateKey && !connectOpts.password && !connectOpts.agent) {
// First, try to use ssh-agent if available (this is what regular SSH does)
const sshAgentSocket = await getAvailableAgentSocket();
+2 -2
View File
@@ -231,8 +231,8 @@ function createPreloadApi(ctx) {
generateKeyPair: async (options) => {
return ipcRenderer.invoke("netcatty:key:generate", options);
},
checkSshAgent: async (identityAgent) => {
return ipcRenderer.invoke("netcatty:ssh:check-agent", identityAgent);
checkSshAgent: async (options) => {
return ipcRenderer.invoke("netcatty:ssh:check-agent", options);
},
getDefaultKeys: async () => {
return ipcRenderer.invoke("netcatty:ssh:get-default-keys");