mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-20 07:04:53 +00:00
* chore(deps): bump fast-xml-parser and @aws-sdk/xml-builder Bumps [fast-xml-parser](https://github.com/NaturalIntelligence/fast-xml-parser) and [@aws-sdk/xml-builder](https://github.com/aws/aws-sdk-js-v3/tree/HEAD/packages-internal/xml-builder). These dependencies needed to be updated together. Updates `fast-xml-parser` from 5.3.4 to 5.5.8 - [Release notes](https://github.com/NaturalIntelligence/fast-xml-parser/releases) - [Changelog](https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/CHANGELOG.md) - [Commits](https://github.com/NaturalIntelligence/fast-xml-parser/compare/v5.3.4...v5.5.8) Updates `@aws-sdk/xml-builder` from 3.972.4 to 3.972.18 - [Release notes](https://github.com/aws/aws-sdk-js-v3/releases) - [Changelog](https://github.com/aws/aws-sdk-js-v3/blob/main/packages-internal/xml-builder/CHANGELOG.md) - [Commits](https://github.com/aws/aws-sdk-js-v3/commits/HEAD/packages-internal/xml-builder) --- updated-dependencies: - dependency-name: fast-xml-parser dependency-version: 5.5.8 dependency-type: indirect - dependency-name: "@aws-sdk/xml-builder" dependency-version: 3.972.18 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps-dev): bump follow-redirects from 1.15.11 to 1.16.0 Bumps [follow-redirects](https://github.com/follow-redirects/follow-redirects) from 1.15.11 to 1.16.0. - [Release notes](https://github.com/follow-redirects/follow-redirects/releases) - [Commits](https://github.com/follow-redirects/follow-redirects/compare/v1.15.11...v1.16.0) --- updated-dependencies: - dependency-name: follow-redirects dependency-version: 1.16.0 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps): bump hono from 4.12.7 to 4.12.14 Bumps [hono](https://github.com/honojs/hono) from 4.12.7 to 4.12.14. - [Release notes](https://github.com/honojs/hono/releases) - [Commits](https://github.com/honojs/hono/compare/v4.12.7...v4.12.14) --- updated-dependencies: - dependency-name: hono dependency-version: 4.12.14 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps-dev): bump vite from 7.3.1 to 7.3.2 Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 7.3.1 to 7.3.2. - [Release notes](https://github.com/vitejs/vite/releases) - [Changelog](https://github.com/vitejs/vite/blob/v7.3.2/packages/vite/CHANGELOG.md) - [Commits](https://github.com/vitejs/vite/commits/v7.3.2/packages/vite) --- updated-dependencies: - dependency-name: vite dependency-version: 7.3.2 dependency-type: direct:development ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps-dev): bump flatted from 3.3.3 to 3.4.2 Bumps [flatted](https://github.com/WebReflection/flatted) from 3.3.3 to 3.4.2. - [Commits](https://github.com/WebReflection/flatted/compare/v3.3.3...v3.4.2) --- updated-dependencies: - dependency-name: flatted dependency-version: 3.4.2 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps-dev): bump minimatch from 3.1.2 to 3.1.5 Bumps [minimatch](https://github.com/isaacs/minimatch) from 3.1.2 to 3.1.5. - [Changelog](https://github.com/isaacs/minimatch/blob/main/changelog.md) - [Commits](https://github.com/isaacs/minimatch/compare/v3.1.2...v3.1.5) --- updated-dependencies: - dependency-name: minimatch dependency-version: 3.1.5 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps-dev): bump lodash from 4.17.23 to 4.18.1 Bumps [lodash](https://github.com/lodash/lodash) from 4.17.23 to 4.18.1. - [Release notes](https://github.com/lodash/lodash/releases) - [Commits](https://github.com/lodash/lodash/compare/4.17.23...4.18.1) --- updated-dependencies: - dependency-name: lodash dependency-version: 4.18.1 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps): bump @hono/node-server from 1.19.11 to 1.19.14 Bumps [@hono/node-server](https://github.com/honojs/node-server) from 1.19.11 to 1.19.14. - [Release notes](https://github.com/honojs/node-server/releases) - [Commits](https://github.com/honojs/node-server/compare/v1.19.11...v1.19.14) --- updated-dependencies: - dependency-name: "@hono/node-server" dependency-version: 1.19.14 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps): bump rollup from 4.57.1 to 4.60.2 Bumps [rollup](https://github.com/rollup/rollup) from 4.57.1 to 4.60.2. - [Release notes](https://github.com/rollup/rollup/releases) - [Changelog](https://github.com/rollup/rollup/blob/master/CHANGELOG.md) - [Commits](https://github.com/rollup/rollup/compare/v4.57.1...v4.60.2) --- updated-dependencies: - dependency-name: rollup dependency-version: 4.60.2 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps-dev): bump electron from 40.1.0 to 40.8.5 Bumps [electron](https://github.com/electron/electron) from 40.1.0 to 40.8.5. - [Release notes](https://github.com/electron/electron/releases) - [Commits](https://github.com/electron/electron/compare/v40.1.0...v40.8.5) --- updated-dependencies: - dependency-name: electron dependency-version: 40.8.5 dependency-type: direct:development ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps): bump path-to-regexp from 8.3.0 to 8.4.2 Bumps [path-to-regexp](https://github.com/pillarjs/path-to-regexp) from 8.3.0 to 8.4.2. - [Release notes](https://github.com/pillarjs/path-to-regexp/releases) - [Changelog](https://github.com/pillarjs/path-to-regexp/blob/master/History.md) - [Commits](https://github.com/pillarjs/path-to-regexp/compare/v8.3.0...v8.4.2) --- updated-dependencies: - dependency-name: path-to-regexp dependency-version: 8.4.2 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps-dev): bump picomatch from 2.3.1 to 2.3.2 Bumps [picomatch](https://github.com/micromatch/picomatch) from 2.3.1 to 2.3.2. - [Release notes](https://github.com/micromatch/picomatch/releases) - [Changelog](https://github.com/micromatch/picomatch/blob/master/CHANGELOG.md) - [Commits](https://github.com/micromatch/picomatch/compare/2.3.1...2.3.2) --- updated-dependencies: - dependency-name: picomatch dependency-version: 2.3.2 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps): bump yaml from 2.8.2 to 2.8.3 Bumps [yaml](https://github.com/eemeli/yaml) from 2.8.2 to 2.8.3. - [Release notes](https://github.com/eemeli/yaml/releases) - [Commits](https://github.com/eemeli/yaml/compare/v2.8.2...v2.8.3) --- updated-dependencies: - dependency-name: yaml dependency-version: 2.8.3 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps-dev): bump @xmldom/xmldom from 0.8.11 to 0.8.13 Bumps [@xmldom/xmldom](https://github.com/xmldom/xmldom) from 0.8.11 to 0.8.13. - [Release notes](https://github.com/xmldom/xmldom/releases) - [Changelog](https://github.com/xmldom/xmldom/blob/master/CHANGELOG.md) - [Commits](https://github.com/xmldom/xmldom/compare/0.8.11...0.8.13) --- updated-dependencies: - dependency-name: "@xmldom/xmldom" dependency-version: 0.8.13 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps-dev): bump brace-expansion from 1.1.12 to 1.1.14 Bumps [brace-expansion](https://github.com/juliangruber/brace-expansion) from 1.1.12 to 1.1.14. - [Release notes](https://github.com/juliangruber/brace-expansion/releases) - [Commits](https://github.com/juliangruber/brace-expansion/compare/v1.1.12...v1.1.14) --- updated-dependencies: - dependency-name: brace-expansion dependency-version: 1.1.14 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps-dev): bump tar from 7.5.7 to 7.5.13 Bumps [tar](https://github.com/isaacs/node-tar) from 7.5.7 to 7.5.13. - [Release notes](https://github.com/isaacs/node-tar/releases) - [Changelog](https://github.com/isaacs/node-tar/blob/main/CHANGELOG.md) - [Commits](https://github.com/isaacs/node-tar/compare/v7.5.7...v7.5.13) --- updated-dependencies: - dependency-name: tar dependency-version: 7.5.13 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * Security Fixes Security fixes: Added input validation for uncontrolled command lines. Added Proper Shell Escaping for useTerminalAutocomplete Fixed 4 race condition alerts by atomic stat+read(s) without following symlinks. Misc: Use Crypto randomness instead of Math.random() (Not a security issue but convenient) * Fix OS quirk fallbacks * Review fix - use lstat before open to skip FIFO/devices early to prevent blocks - SFTP skip UUID tag could be dubiously long * allow symlinks alongside regular files. * Use acutal target size for reading * Fix Destructed import / fix to use full shellEscape charset - Destructed import - Guard now matches full shellEscape charset * Supress Codex complaints Replaced manual fd.read with fs.promises.readFile(fd) to ensure complete file reads to EOF. --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
673 lines
23 KiB
JavaScript
673 lines
23 KiB
JavaScript
/**
|
|
* SSH Authentication Helper - Shared authentication logic for SSH connections
|
|
* Used by sshBridge, sftpBridge, and portForwardingBridge
|
|
*/
|
|
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const os = require("node:os");
|
|
const { exec } = require("node:child_process");
|
|
const keyboardInteractiveHandler = require("./keyboardInteractiveHandler.cjs");
|
|
const passphraseHandler = require("./passphraseHandler.cjs");
|
|
|
|
// Default SSH key names in priority order
|
|
const PREFERRED_KEY_NAMES = ["id_ed25519", "id_ecdsa", "id_rsa"];
|
|
const SSH_KEY_PATTERN = /^id_[\w-]+$/;
|
|
|
|
async function readFileNoFollow(filePath) {
|
|
const lstat = await fs.promises.lstat(filePath);
|
|
if (!lstat.isFile() && !lstat.isSymbolicLink()) return null;
|
|
const fd = await fs.promises.open(filePath, "r", 0o0);
|
|
try {
|
|
return await fs.promises.readFile(fd, { encoding: "utf8" });
|
|
} finally {
|
|
await fd.close();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Quick check if file content looks like an SSH private key.
|
|
* Rejects non-key files that happen to match the id_* filename pattern.
|
|
*/
|
|
function looksLikePrivateKey(content) {
|
|
if (!content || typeof content !== "string") return false;
|
|
const trimmed = content.trimStart();
|
|
return trimmed.startsWith("-----BEGIN") ||
|
|
trimmed.startsWith("openssh-key-v1") ||
|
|
trimmed.startsWith("PuTTY-User-Key-File");
|
|
}
|
|
|
|
/**
|
|
* Check if an SSH private key is encrypted (requires passphrase)
|
|
* @param {string} keyContent - The content of the private key file
|
|
* @returns {boolean} - True if the key is encrypted
|
|
*/
|
|
function isKeyEncrypted(keyContent) {
|
|
if (!keyContent || typeof keyContent !== "string") return false;
|
|
|
|
// Check for PuTTY PPK encrypted format (Encryption: aes256-cbc, etc.)
|
|
// PPK keys with "Encryption: none" are unencrypted
|
|
const ppkEncMatch = keyContent.match(/^Encryption:\s*(.+)$/m);
|
|
if (ppkEncMatch && ppkEncMatch[1].trim() !== "none") {
|
|
return true;
|
|
}
|
|
|
|
// Check for PKCS#8 encrypted format (-----BEGIN ENCRYPTED PRIVATE KEY-----)
|
|
if (keyContent.includes("-----BEGIN ENCRYPTED PRIVATE KEY-----")) {
|
|
return true;
|
|
}
|
|
|
|
// Check for legacy PEM format encryption (e.g., RSA PRIVATE KEY with encryption)
|
|
if (keyContent.includes("Proc-Type:") && keyContent.includes("ENCRYPTED")) {
|
|
return true;
|
|
}
|
|
|
|
// Check for DEK-Info header (legacy PEM encryption indicator)
|
|
if (keyContent.includes("DEK-Info:")) return true;
|
|
|
|
// Check for OpenSSH format keys
|
|
if (keyContent.includes("-----BEGIN OPENSSH PRIVATE KEY-----")) {
|
|
try {
|
|
// Extract the base64 content between the markers
|
|
const base64Match = keyContent.match(
|
|
/-----BEGIN OPENSSH PRIVATE KEY-----\s*([\s\S]*?)\s*-----END OPENSSH PRIVATE KEY-----/
|
|
);
|
|
if (base64Match) {
|
|
const base64Content = base64Match[1].replace(/\s/g, "");
|
|
const keyBuffer = Buffer.from(base64Content, "base64");
|
|
|
|
// OpenSSH key format: "openssh-key-v1\0" followed by cipher name
|
|
// If ciphername is "none", the key is not encrypted
|
|
const authMagic = "openssh-key-v1\0";
|
|
if (keyBuffer.toString("ascii", 0, authMagic.length) === authMagic) {
|
|
// After magic, read ciphername (length-prefixed string)
|
|
let offset = authMagic.length;
|
|
const cipherNameLen = keyBuffer.readUInt32BE(offset);
|
|
offset += 4;
|
|
const cipherName = keyBuffer.toString("ascii", offset, offset + cipherNameLen);
|
|
return cipherName !== "none";
|
|
}
|
|
}
|
|
} catch {
|
|
// If parsing fails, assume it might be encrypted to be safe
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Find default SSH private key from user's ~/.ssh directory
|
|
* Skips encrypted keys that require a passphrase
|
|
* @returns {Promise<{ privateKey: string, keyPath: string, keyName: string } | null>}
|
|
*/
|
|
async function findDefaultPrivateKey() {
|
|
const sshDir = path.join(os.homedir(), ".ssh");
|
|
let allNames = [];
|
|
try {
|
|
const entries = await fs.promises.readdir(sshDir);
|
|
allNames = entries.filter(f => SSH_KEY_PATTERN.test(f));
|
|
} catch {
|
|
return null;
|
|
}
|
|
const preferred = PREFERRED_KEY_NAMES.filter(n => allNames.includes(n));
|
|
const rest = allNames.filter(n => !PREFERRED_KEY_NAMES.includes(n)).sort();
|
|
const sorted = [...preferred, ...rest];
|
|
|
|
for (const name of sorted) {
|
|
const keyPath = path.join(sshDir, name);
|
|
try {
|
|
const privateKey = await readFileNoFollow(keyPath);
|
|
if (!privateKey) continue;
|
|
if (!looksLikePrivateKey(privateKey)) continue;
|
|
if (isKeyEncrypted(privateKey)) continue;
|
|
return { privateKey, keyPath, keyName: name };
|
|
} catch {
|
|
continue;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Find ALL default SSH private keys from user's ~/.ssh directory
|
|
* @param {Object} [options]
|
|
* @param {boolean} [options.includeEncrypted=false] - If true, include encrypted keys with isEncrypted flag
|
|
* @returns {Promise<Array<{ privateKey: string, keyPath: string, keyName: string, isEncrypted?: boolean }>>}
|
|
*/
|
|
async function findAllDefaultPrivateKeys(options = {}) {
|
|
const { includeEncrypted = false } = options;
|
|
const sshDir = path.join(os.homedir(), ".ssh");
|
|
|
|
let allNames = [];
|
|
try {
|
|
const entries = await fs.promises.readdir(sshDir);
|
|
allNames = entries.filter(f => SSH_KEY_PATTERN.test(f));
|
|
} catch {
|
|
return [];
|
|
}
|
|
const preferred = PREFERRED_KEY_NAMES.filter(n => allNames.includes(n));
|
|
const rest = allNames.filter(n => !PREFERRED_KEY_NAMES.includes(n)).sort();
|
|
const sorted = [...preferred, ...rest];
|
|
|
|
const promises = sorted.map(async (name) => {
|
|
const keyPath = path.join(sshDir, name);
|
|
try {
|
|
const privateKey = await readFileNoFollow(keyPath);
|
|
if (!privateKey) return null;
|
|
if (!looksLikePrivateKey(privateKey)) return null;
|
|
const encrypted = isKeyEncrypted(privateKey);
|
|
if (encrypted && !includeEncrypted) {
|
|
return null;
|
|
}
|
|
return {
|
|
privateKey,
|
|
keyPath,
|
|
keyName: name,
|
|
...(includeEncrypted ? { isEncrypted: encrypted } : {})
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
});
|
|
|
|
const results = await Promise.all(promises);
|
|
return results.filter(Boolean);
|
|
}
|
|
|
|
const WIN_SSH_AGENT_PIPE = "\\\\.\\pipe\\openssh-ssh-agent";
|
|
|
|
/**
|
|
* Check if a Windows named pipe is connectable.
|
|
* fs.statSync is unreliable for named pipes (returns EBUSY even when the
|
|
* pipe is usable), so we attempt an actual net.connect() which is the
|
|
* authoritative check.
|
|
* @param {string} pipePath
|
|
* @param {number} [timeoutMs=1000]
|
|
* @returns {Promise<boolean>}
|
|
*/
|
|
function windowsPipeConnectable(pipePath, timeoutMs = 1000) {
|
|
const net = require("net");
|
|
return new Promise((resolve) => {
|
|
const socket = net.connect(pipePath);
|
|
let settled = false;
|
|
const finish = (ok) => {
|
|
if (settled) return;
|
|
settled = true;
|
|
try { socket.destroy(); } catch {}
|
|
resolve(ok);
|
|
};
|
|
socket.setTimeout(timeoutMs);
|
|
socket.once("connect", () => finish(true));
|
|
socket.once("timeout", () => finish(false));
|
|
socket.once("error", () => finish(false));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Check if an SSH agent is available on Windows.
|
|
* Probes the well-known named pipe via net.connect(). This supports any
|
|
* agent that provides the pipe — Bitwarden, 1Password, gpg-agent, etc.
|
|
* @returns {Promise<boolean>}
|
|
*/
|
|
function checkWindowsSshAgentRunning() {
|
|
if (process.platform !== "win32") {
|
|
return Promise.resolve(true);
|
|
}
|
|
return windowsPipeConnectable(WIN_SSH_AGENT_PIPE);
|
|
}
|
|
|
|
/**
|
|
* Get ssh-agent socket path based on platform (synchronous, best-effort)
|
|
* @returns {string|null}
|
|
*/
|
|
function getSshAgentSocket() {
|
|
if (process.platform === "win32") {
|
|
// On Windows, always return the pipe path; the caller should use
|
|
// getAvailableAgentSocket() for a reliable async check.
|
|
return "\\\\.\\pipe\\openssh-ssh-agent";
|
|
}
|
|
const agentSocket = process.env.SSH_AUTH_SOCK;
|
|
if (!agentSocket) return null;
|
|
|
|
try {
|
|
const stats = fs.statSync(agentSocket);
|
|
return typeof stats.isSocket === "function" && stats.isSocket()
|
|
? agentSocket
|
|
: null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get ssh-agent socket path with async validation (checks Windows service status)
|
|
* @returns {Promise<string|null>}
|
|
*/
|
|
async function getAvailableAgentSocket() {
|
|
if (process.platform === "win32") {
|
|
const running = await checkWindowsSshAgentRunning();
|
|
return running ? "\\\\.\\pipe\\openssh-ssh-agent" : null;
|
|
}
|
|
return getSshAgentSocket();
|
|
}
|
|
|
|
/**
|
|
* Build authentication handler with default key fallback support
|
|
* @param {Object} options
|
|
* @param {string} [options.privateKey] - Explicitly configured private key
|
|
* @param {string} [options.password] - Password for authentication
|
|
* @param {string} [options.passphrase] - Passphrase for encrypted private key
|
|
* @param {Object} [options.agent] - SSH agent (NetcattyAgent or socket path)
|
|
* @param {string} options.username - SSH username
|
|
* @param {string} [options.logPrefix] - Log prefix for debugging
|
|
* @returns {{ authHandler: Function|Array, privateKey: string|null, agent: string|Object|null, usedDefaultKeys: boolean }}
|
|
* @param {Array} [options.unlockedEncryptedKeys] - Array of unlocked encrypted keys with passphrases
|
|
*/
|
|
function buildAuthHandler(options) {
|
|
const { privateKey, password, passphrase, agent, username, logPrefix = "[SSH]", unlockedEncryptedKeys = [], defaultKeys = [], sshAgentSocketOverride, onAuthAttempt } = options;
|
|
|
|
// Determine what type of explicit auth the user configured
|
|
const hasExplicitKey = !!privateKey;
|
|
const hasExplicitPassword = !!password;
|
|
const hasExplicitAgent = !!agent;
|
|
const hasExplicitAuth = hasExplicitKey || hasExplicitPassword || hasExplicitAgent;
|
|
|
|
// Determine if this is a password-only or key-only connection
|
|
const isPasswordOnly = hasExplicitPassword && !hasExplicitKey && !hasExplicitAgent;
|
|
const isKeyOnly = hasExplicitKey && !hasExplicitAgent;
|
|
|
|
// 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();
|
|
|
|
// Only use system ssh-agent BEFORE user's auth when:
|
|
// - User explicitly configured agent, OR
|
|
// - No explicit auth is configured (pure fallback mode)
|
|
// When user configured key/password, system agent should only be used AFTER as fallback
|
|
const useAgentFirst = hasExplicitAgent || !hasExplicitAuth;
|
|
|
|
// Determine effective agent
|
|
const effectiveAgent = agent || (useAgentFirst ? sshAgentSocket : null);
|
|
|
|
// Determine effective privateKey (user-provided takes priority)
|
|
const effectivePrivateKey = privateKey || (!hasExplicitAuth && defaultKeys.length > 0 ? defaultKeys[0].privateKey : null);
|
|
|
|
// Determine fallback keys (keys to try after user's primary auth fails)
|
|
// - If user provided a key: all default keys are fallbacks
|
|
// - If no explicit auth: first default key is primary, rest are fallbacks
|
|
// - If password-only or agent-only: all default keys are fallbacks (tried after primary)
|
|
const fallbackKeys = hasExplicitKey
|
|
? defaultKeys
|
|
: !hasExplicitAuth
|
|
? defaultKeys.slice(1)
|
|
: defaultKeys;
|
|
|
|
// Check if we need dynamic handler (have fallback options)
|
|
const hasFallbackOptions = fallbackKeys.length > 0 ||
|
|
(!hasExplicitAgent && sshAgentSocket) ||
|
|
(isPasswordOnly && defaultKeys.length > 0);
|
|
|
|
// If only simple auth methods and no fallback keys needed, use array-based handler
|
|
if (hasExplicitAuth && !hasFallbackOptions) {
|
|
const authMethods = ["none"]; // Always try none first per RFC 4252
|
|
if (effectiveAgent) authMethods.push("agent");
|
|
if (privateKey) authMethods.push("publickey");
|
|
if (password) authMethods.push("password");
|
|
authMethods.push("keyboard-interactive");
|
|
|
|
return {
|
|
authHandler: authMethods,
|
|
privateKey: effectivePrivateKey,
|
|
agent: effectiveAgent,
|
|
usedDefaultKeys: false,
|
|
};
|
|
}
|
|
|
|
// Build comprehensive authMethods array with all auth options
|
|
// Order depends on what user explicitly configured:
|
|
// - Password-only: password -> agent -> default keys -> keyboard-interactive
|
|
// - Key-only: user key -> password -> agent -> default keys -> keyboard-interactive
|
|
// - Agent configured: agent -> user key -> password -> default keys -> keyboard-interactive
|
|
// - No explicit auth: agent -> default keys -> keyboard-interactive
|
|
const authMethods = [];
|
|
|
|
if (isPasswordOnly) {
|
|
// Password-only: respect user's explicit choice, no key/agent fallback
|
|
authMethods.push({ type: "password", id: "password" });
|
|
} else if (isKeyOnly) {
|
|
// Key-only: user key first, then password (if any), then agent/default keys as fallback
|
|
|
|
// 1. User-provided key first
|
|
authMethods.push({
|
|
type: "publickey",
|
|
key: privateKey,
|
|
passphrase: passphrase,
|
|
id: "publickey-user"
|
|
});
|
|
|
|
// 2. Password (if configured alongside key)
|
|
if (password) {
|
|
authMethods.push({ type: "password", id: "password" });
|
|
}
|
|
|
|
// 3. System agent as fallback (AFTER user's key)
|
|
if (sshAgentSocket) {
|
|
authMethods.push({ type: "agent", id: "agent" });
|
|
}
|
|
|
|
// 4. Default keys as fallback
|
|
for (const keyInfo of fallbackKeys) {
|
|
authMethods.push({
|
|
type: "publickey",
|
|
key: keyInfo.privateKey,
|
|
id: `publickey-default-${keyInfo.keyName}`
|
|
});
|
|
}
|
|
} else {
|
|
// Agent configured or no explicit auth: agent -> user key -> password -> default keys
|
|
|
|
// 1. Agent (user-provided or system)
|
|
if (effectiveAgent) {
|
|
authMethods.push({ type: "agent", id: "agent" });
|
|
}
|
|
|
|
// 2. User-provided key
|
|
if (privateKey) {
|
|
authMethods.push({
|
|
type: "publickey",
|
|
key: privateKey,
|
|
passphrase: passphrase,
|
|
id: "publickey-user"
|
|
});
|
|
}
|
|
|
|
// 3. Password (if configured)
|
|
if (password) {
|
|
authMethods.push({ type: "password", id: "password" });
|
|
}
|
|
|
|
// 4. Default keys as fallback
|
|
for (const keyInfo of fallbackKeys) {
|
|
authMethods.push({
|
|
type: "publickey",
|
|
key: keyInfo.privateKey,
|
|
id: `publickey-default-${keyInfo.keyName}`
|
|
});
|
|
}
|
|
|
|
// 5. If no user key provided, add first default key at the beginning (after agent)
|
|
if (!privateKey && defaultKeys.length > 0) {
|
|
const insertIndex = effectiveAgent ? 1 : 0;
|
|
authMethods.splice(insertIndex, 0, {
|
|
type: "publickey",
|
|
key: defaultKeys[0].privateKey,
|
|
id: `publickey-default-${defaultKeys[0].keyName}`
|
|
});
|
|
}
|
|
}
|
|
|
|
// Add unlocked encrypted default keys (user provided passphrases for these)
|
|
for (const keyInfo of unlockedEncryptedKeys) {
|
|
authMethods.push({
|
|
type: "publickey",
|
|
key: keyInfo.privateKey,
|
|
passphrase: keyInfo.passphrase,
|
|
id: `publickey-encrypted-${keyInfo.keyName}`
|
|
});
|
|
}
|
|
|
|
// Keyboard-interactive as last resort
|
|
authMethods.push({ type: "keyboard-interactive", id: "keyboard-interactive" });
|
|
|
|
console.log(`${logPrefix} Auth methods configured`, {
|
|
isPasswordOnly,
|
|
hasUserKey: !!privateKey,
|
|
hasPassword: !!password,
|
|
hasAgent: !!effectiveAgent,
|
|
methodCount: authMethods.length,
|
|
methods: authMethods.map(m => m.id),
|
|
});
|
|
|
|
// Use dynamic authHandler to try all keys
|
|
let authIndex = 0;
|
|
let lastAttemptedLabel = null;
|
|
const attemptedMethodIds = new Set();
|
|
|
|
let triedNone = false;
|
|
|
|
const authHandler = (methodsLeft, partialSuccess, callback) => {
|
|
// Per RFC 4252, always try "none" first to discover available methods
|
|
// and to support passwordless login (e.g. embedded devices).
|
|
// This matches the behavior of OpenSSH and Tabby.
|
|
if (methodsLeft === null && !triedNone) {
|
|
triedNone = true;
|
|
lastAttemptedLabel = "none (no credentials)";
|
|
onAuthAttempt?.("none (no credentials)");
|
|
return callback("none");
|
|
}
|
|
|
|
const availableMethods = methodsLeft || ["publickey", "password", "keyboard-interactive", "agent"];
|
|
|
|
// Log rejection of previous method (authHandler is called again when server rejects)
|
|
if (lastAttemptedLabel && !partialSuccess) {
|
|
onAuthAttempt?.(`${lastAttemptedLabel} rejected`);
|
|
}
|
|
|
|
while (authIndex < authMethods.length) {
|
|
const method = authMethods[authIndex];
|
|
authIndex++;
|
|
|
|
if (attemptedMethodIds.has(method.id)) continue;
|
|
attemptedMethodIds.add(method.id);
|
|
|
|
if (method.type === "agent" && (availableMethods.includes("publickey") || availableMethods.includes("agent"))) {
|
|
console.log(`${logPrefix} Trying agent auth`);
|
|
lastAttemptedLabel = "SSH agent";
|
|
onAuthAttempt?.("SSH agent");
|
|
return callback("agent");
|
|
} else if (method.type === "publickey" && availableMethods.includes("publickey")) {
|
|
console.log(`${logPrefix} Trying publickey auth:`, method.id);
|
|
// Build a readable label for the key
|
|
const keyLabel = method.id.startsWith("publickey-default-")
|
|
? `key ${method.id.replace("publickey-default-", "")}`
|
|
: method.id.startsWith("publickey-encrypted-")
|
|
? `key ${method.id.replace("publickey-encrypted-", "")} (encrypted)`
|
|
: method.id === "publickey-user"
|
|
? "configured key"
|
|
: method.id;
|
|
lastAttemptedLabel = keyLabel;
|
|
onAuthAttempt?.(keyLabel);
|
|
const pubkeyAuth = {
|
|
type: "publickey",
|
|
username,
|
|
key: method.key,
|
|
};
|
|
if (method.passphrase) {
|
|
pubkeyAuth.passphrase = method.passphrase;
|
|
}
|
|
return callback(pubkeyAuth);
|
|
} else if (method.type === "password" && availableMethods.includes("password")) {
|
|
console.log(`${logPrefix} Trying password auth`);
|
|
lastAttemptedLabel = "password";
|
|
onAuthAttempt?.("password");
|
|
return callback({
|
|
type: "password",
|
|
username,
|
|
password,
|
|
});
|
|
} else if (method.type === "keyboard-interactive" && availableMethods.includes("keyboard-interactive")) {
|
|
lastAttemptedLabel = "keyboard-interactive";
|
|
onAuthAttempt?.("keyboard-interactive");
|
|
return callback("keyboard-interactive");
|
|
}
|
|
}
|
|
onAuthAttempt?.("all methods exhausted");
|
|
return callback(false);
|
|
};
|
|
|
|
// Determine the agent to return - if authMethods includes agent, we need to provide the socket
|
|
// even if effectiveAgent is null (for fallback scenarios)
|
|
const hasAgentInMethods = authMethods.some(m => m.type === "agent");
|
|
const returnAgent = effectiveAgent || (hasAgentInMethods ? sshAgentSocket : null);
|
|
|
|
return {
|
|
authHandler,
|
|
privateKey: effectivePrivateKey,
|
|
agent: returnAgent,
|
|
usedDefaultKeys: true,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Create a keyboard-interactive event handler
|
|
* @param {Object} options
|
|
* @param {Object} options.sender - Electron webContents sender
|
|
* @param {string} options.sessionId - Session/connection ID
|
|
* @param {string} options.hostname - Host being connected to
|
|
* @param {string} [options.password] - Saved password for fill button
|
|
* @param {string} [options.logPrefix] - Log prefix for debugging
|
|
* @returns {Function} - Event handler for 'keyboard-interactive' event
|
|
*/
|
|
function createKeyboardInteractiveHandler(options) {
|
|
const { sender, sessionId, hostname, password, logPrefix = "[SSH]" } = options;
|
|
|
|
return (name, instructions, instructionsLang, prompts, finish) => {
|
|
console.log(`${logPrefix} ${hostname} keyboard-interactive auth requested`, {
|
|
name,
|
|
instructions,
|
|
promptCount: prompts?.length || 0,
|
|
});
|
|
|
|
// If there are no prompts, just call finish with empty array
|
|
if (!prompts || prompts.length === 0) {
|
|
console.log(`${logPrefix} No prompts, finishing keyboard-interactive`);
|
|
finish([]);
|
|
return;
|
|
}
|
|
|
|
// Forward prompts to user via IPC
|
|
const requestId = keyboardInteractiveHandler.generateRequestId('ssh');
|
|
keyboardInteractiveHandler.storeRequest(requestId, (userResponses) => {
|
|
console.log(`${logPrefix} Received user responses, finishing keyboard-interactive`);
|
|
finish(userResponses);
|
|
}, sender.id, sessionId);
|
|
|
|
const promptsData = prompts.map((p) => ({
|
|
prompt: p.prompt,
|
|
echo: p.echo,
|
|
}));
|
|
|
|
console.log(`${logPrefix} Showing modal for ${promptsData.length} prompts`);
|
|
|
|
safeSend(sender, "netcatty:keyboard-interactive", {
|
|
requestId,
|
|
sessionId,
|
|
name: name || hostname,
|
|
instructions: instructions || "",
|
|
prompts: promptsData,
|
|
hostname: hostname,
|
|
savedPassword: password || null,
|
|
});
|
|
};
|
|
}
|
|
|
|
const { safeSend } = require("./ipcUtils.cjs");
|
|
|
|
/**
|
|
* Apply auth configuration to connection options
|
|
* Convenience function that combines buildAuthHandler results with connOpts
|
|
* @param {Object} connOpts - SSH connection options to modify
|
|
* @param {Object} authConfig - Auth configuration from buildAuthHandler
|
|
*/
|
|
function applyAuthToConnOpts(connOpts, authConfig) {
|
|
connOpts.authHandler = authConfig.authHandler;
|
|
if (authConfig.privateKey) {
|
|
connOpts.privateKey = authConfig.privateKey;
|
|
}
|
|
if (authConfig.agent) {
|
|
connOpts.agent = authConfig.agent;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Request passphrases for encrypted default keys
|
|
* Shows a modal for each encrypted key and collects passphrases
|
|
* @param {Object} sender - Electron webContents sender
|
|
* @param {string} [hostname] - Optional hostname for context
|
|
* @returns {Promise<{ keys: Array<{ privateKey: string, keyPath: string, keyName: string, passphrase: string }>, cancelled: boolean }>}
|
|
*/
|
|
async function requestPassphrasesForEncryptedKeys(sender, hostname) {
|
|
const allKeys = await findAllDefaultPrivateKeys({ includeEncrypted: true });
|
|
const encryptedKeys = allKeys.filter(k => k.isEncrypted);
|
|
|
|
if (encryptedKeys.length === 0) {
|
|
return { keys: [], cancelled: false };
|
|
}
|
|
|
|
console.log(`[SSHAuth] Found ${encryptedKeys.length} encrypted default key(s), requesting passphrases`);
|
|
|
|
const unlockedKeys = [];
|
|
let wasCancelled = false;
|
|
|
|
for (const keyInfo of encryptedKeys) {
|
|
const result = await passphraseHandler.requestPassphrase(
|
|
sender,
|
|
keyInfo.keyPath,
|
|
keyInfo.keyName,
|
|
hostname
|
|
);
|
|
|
|
// Handle different response types
|
|
if (!result) {
|
|
// Timeout or error - continue with next key
|
|
console.log(`[SSHAuth] No response for ${keyInfo.keyName}, continuing...`);
|
|
continue;
|
|
}
|
|
|
|
if (result.cancelled) {
|
|
// User clicked Cancel - stop the entire flow
|
|
console.log(`[SSHAuth] User cancelled passphrase flow at ${keyInfo.keyName}`);
|
|
wasCancelled = true;
|
|
break;
|
|
}
|
|
|
|
if (result.skipped) {
|
|
// User clicked Skip - continue with next key
|
|
console.log(`[SSHAuth] User skipped passphrase for ${keyInfo.keyName}`);
|
|
continue;
|
|
}
|
|
|
|
if (result.passphrase) {
|
|
// User provided passphrase
|
|
unlockedKeys.push({
|
|
privateKey: keyInfo.privateKey,
|
|
keyPath: keyInfo.keyPath,
|
|
keyName: keyInfo.keyName,
|
|
passphrase: result.passphrase,
|
|
});
|
|
}
|
|
}
|
|
|
|
return { keys: unlockedKeys, cancelled: wasCancelled };
|
|
}
|
|
|
|
module.exports = {
|
|
PREFERRED_KEY_NAMES,
|
|
SSH_KEY_PATTERN,
|
|
looksLikePrivateKey,
|
|
isKeyEncrypted,
|
|
findDefaultPrivateKey,
|
|
findAllDefaultPrivateKeys,
|
|
getSshAgentSocket,
|
|
getAvailableAgentSocket,
|
|
buildAuthHandler,
|
|
createKeyboardInteractiveHandler,
|
|
applyAuthToConnOpts,
|
|
safeSend,
|
|
requestPassphrasesForEncryptedKeys,
|
|
readFileNoFollow,
|
|
};
|