mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-17 21:10:48 +00:00
Fixes #4222 describeHostKey previously hashed the entire certificate blob, so load-balanced or re-issued certs for the same host key looked like a fingerprint change. Pin the underlying public key instead. Share extraction across verifier, renderer migration, and system known_hosts. Migrate legacy full-cert fingerprints only when the stored certificate body is available, preserving certificate metadata and raw rekey checks. Co-authored-by: Codex <codex@openai.com>
342 lines
11 KiB
JavaScript
342 lines
11 KiB
JavaScript
const crypto = require("node:crypto");
|
|
const { randomUUID } = require("node:crypto");
|
|
const { utils: sshUtils } = require("ssh2");
|
|
const { extractCertificatePublicKey } = require("../../domain/sshPublicKey.shared.cjs");
|
|
|
|
const REQUEST_TTL_MS = 2 * 60 * 1000;
|
|
const hostKeyRequests = new Map();
|
|
|
|
const normalizeFingerprint = (value) => {
|
|
if (typeof value !== "string") return "";
|
|
return value
|
|
.trim()
|
|
.replace(/^SHA256:/i, "")
|
|
.replace(/=+$/g, "");
|
|
};
|
|
|
|
const normalizeHostname = (value) => String(value || "").trim().toLowerCase();
|
|
|
|
const parseKnownHostPattern = (hostname) => {
|
|
const value = String(hostname || "").trim();
|
|
if (!value) return { hostname: "", port: undefined };
|
|
const first = value.split(",")[0];
|
|
const bracketMatch = first.match(/^\[([^\]]+)\]:(\d+)$/);
|
|
if (bracketMatch) {
|
|
return {
|
|
hostname: normalizeHostname(bracketMatch[1]),
|
|
port: Number.parseInt(bracketMatch[2], 10),
|
|
};
|
|
}
|
|
return { hostname: normalizeHostname(first), port: undefined };
|
|
};
|
|
|
|
const getKnownHostPort = (knownHost) => {
|
|
const parsed = parseKnownHostPattern(knownHost?.hostname);
|
|
if (Number.isFinite(knownHost?.port)) return Number(knownHost.port);
|
|
if (Number.isFinite(parsed.port)) return Number(parsed.port);
|
|
return 22;
|
|
};
|
|
|
|
const matchesHostAndPort = (knownHost, hostname, port) => {
|
|
const parsed = parseKnownHostPattern(knownHost?.hostname);
|
|
if (!parsed.hostname || parsed.hostname === "(hashed)") return false;
|
|
return parsed.hostname === normalizeHostname(hostname) && getKnownHostPort(knownHost) === (port || 22);
|
|
};
|
|
|
|
const describeRawPublicKeyBlob = (key) => {
|
|
if (!Buffer.isBuffer(key) || key.length < 8) return null;
|
|
const typeLength = key.readUInt32BE(0);
|
|
if (typeLength <= 0 || typeLength > 128 || 4 + typeLength > key.length) return null;
|
|
|
|
const keyType = key.subarray(4, 4 + typeLength).toString("ascii");
|
|
if (!/^[A-Za-z0-9@._+-]+$/.test(keyType)) return null;
|
|
|
|
return {
|
|
keyType,
|
|
publicKey: `${keyType} ${key.toString("base64")}`,
|
|
};
|
|
};
|
|
|
|
const fingerprintFromPublicKey = (publicKey) => {
|
|
if (typeof publicKey !== "string") return "";
|
|
const trimmed = publicKey.trim();
|
|
if (!trimmed) return "";
|
|
if (/^SHA256:/i.test(trimmed)) return normalizeFingerprint(trimmed);
|
|
|
|
const parts = trimmed.split(/\s+/);
|
|
if (parts.length >= 2 && /^ssh-|^ecdsa-|^sk-/.test(parts[0])) {
|
|
try {
|
|
const blob = Buffer.from(parts[1], "base64");
|
|
return crypto.createHash("sha256")
|
|
.update(extractCertificatePublicKey(blob) || blob)
|
|
.digest("base64")
|
|
.replace(/=+$/g, "");
|
|
} catch {
|
|
return "";
|
|
}
|
|
}
|
|
return normalizeFingerprint(trimmed);
|
|
};
|
|
|
|
const getKnownHostFingerprint = (knownHost) => {
|
|
const stored = normalizeFingerprint(knownHost?.fingerprint);
|
|
const parts = knownHost?.publicKey?.trim().split(/\s+/) || [];
|
|
if (parts.length >= 2 && /-cert-v0[01]@openssh\.com$/.test(parts[0])) {
|
|
const blob = Buffer.from(parts[1], "base64");
|
|
// Only replace an explicit fingerprint if it is the legacy full-cert hash.
|
|
// Fingerprint-only records cannot be migrated without the certificate body.
|
|
const legacy = crypto.createHash("sha256").update(blob).digest("base64").replace(/=+$/g, "");
|
|
if (!stored || stored === legacy) return fingerprintFromPublicKey(knownHost.publicKey);
|
|
}
|
|
return stored
|
|
|| fingerprintFromPublicKey(knownHost?.publicKey);
|
|
};
|
|
|
|
// Classification rules, in order:
|
|
// 1. Any record for (host, port) whose fingerprint matches the live key →
|
|
// trusted. Fingerprint is the ground truth; key type is metadata.
|
|
// 2. A record matching (host, port, keyType) *exactly* with a non-matching
|
|
// fingerprint → changed. Only this case is a real "key rotated" alarm —
|
|
// the user already trusted this exact algorithm on this host and the
|
|
// server now presents a different key of the same type.
|
|
// 3. Otherwise → unknown. This includes the case where the server presents
|
|
// a key of an algorithm we have no record for, even if the host has
|
|
// records for other algorithms. Tabby and OpenSSH both treat that as a
|
|
// first-time prompt rather than a mismatch warning (#972).
|
|
const classifyHostKey = ({ knownHosts = [], hostname, port = 22, keyType, fingerprint }) => {
|
|
const normalizedFingerprint = normalizeFingerprint(fingerprint);
|
|
const candidates = Array.isArray(knownHosts)
|
|
? knownHosts.filter((knownHost) => matchesHostAndPort(knownHost, hostname, port))
|
|
: [];
|
|
|
|
if (candidates.length === 0) {
|
|
return { status: "unknown" };
|
|
}
|
|
|
|
const comparableCandidates = candidates
|
|
.map((knownHost) => ({
|
|
knownHost,
|
|
fingerprint: getKnownHostFingerprint(knownHost),
|
|
}))
|
|
.filter((entry) => entry.fingerprint);
|
|
|
|
const match = comparableCandidates.find((entry) => entry.fingerprint === normalizedFingerprint);
|
|
if (match) {
|
|
return { status: "trusted", knownHost: match.knownHost };
|
|
}
|
|
|
|
const normalizedKeyType = typeof keyType === "string" ? keyType.trim() : "";
|
|
if (normalizedKeyType && normalizedKeyType !== "unknown") {
|
|
const sameTypeMismatch = comparableCandidates.find(
|
|
(entry) => entry.knownHost.keyType === normalizedKeyType,
|
|
);
|
|
if (sameTypeMismatch) {
|
|
return {
|
|
status: "changed",
|
|
knownHost: sameTypeMismatch.knownHost,
|
|
expectedFingerprint: sameTypeMismatch.fingerprint,
|
|
};
|
|
}
|
|
}
|
|
|
|
return { status: "unknown" };
|
|
};
|
|
|
|
const describeHostKey = (rawKey) => {
|
|
const key = Buffer.isBuffer(rawKey) ? rawKey : Buffer.from(rawKey || "");
|
|
const fingerprint = crypto.createHash("sha256")
|
|
.update(extractCertificatePublicKey(key) || key)
|
|
.digest("base64")
|
|
.replace(/=+$/g, "");
|
|
let keyType = "unknown";
|
|
let publicKey;
|
|
|
|
const rawPublicKey = describeRawPublicKeyBlob(key);
|
|
if (rawPublicKey) {
|
|
keyType = rawPublicKey.keyType;
|
|
publicKey = rawPublicKey.publicKey;
|
|
}
|
|
|
|
try {
|
|
const parsed = sshUtils.parseKey(key);
|
|
const parsedKey = Array.isArray(parsed) ? parsed[0] : parsed;
|
|
// Keep the presented certificate identity, even if a parser returns its
|
|
// base algorithm or underlying public key as metadata.
|
|
if (parsedKey && !(parsedKey instanceof Error) && !/-cert-v0[01]@openssh\.com$/.test(keyType)) {
|
|
keyType = parsedKey.type || keyType;
|
|
const publicSsh = parsedKey.getPublicSSH?.();
|
|
if (publicSsh) publicKey = publicSsh.toString("utf8");
|
|
}
|
|
} catch {
|
|
// Keep the fingerprint; key type/public key are best-effort metadata.
|
|
}
|
|
|
|
return { keyType, fingerprint, publicKey };
|
|
};
|
|
|
|
const generateRequestId = () => `hostkey-${randomUUID()}`;
|
|
|
|
const settleRequest = (requestId, response) => {
|
|
const pending = hostKeyRequests.get(requestId);
|
|
if (!pending) return { success: false, error: "Request not found" };
|
|
if (pending.timeoutId) clearTimeout(pending.timeoutId);
|
|
hostKeyRequests.delete(requestId);
|
|
pending.resolve(response);
|
|
return { success: true };
|
|
};
|
|
|
|
const requestHostKeyVerification = (sender, info) => new Promise((resolve) => {
|
|
if (!sender || sender.isDestroyed?.()) {
|
|
resolve({ accept: false });
|
|
return;
|
|
}
|
|
|
|
const requestId = generateRequestId();
|
|
const timeoutId = setTimeout(() => {
|
|
settleRequest(requestId, { accept: false, timeout: true });
|
|
}, REQUEST_TTL_MS);
|
|
|
|
hostKeyRequests.set(requestId, {
|
|
resolve,
|
|
timeoutId,
|
|
createdAt: Date.now(),
|
|
webContentsId: sender.id,
|
|
sessionId: info.sessionId,
|
|
});
|
|
|
|
try {
|
|
sender.send("netcatty:host-key:verify", {
|
|
requestId,
|
|
...info,
|
|
});
|
|
} catch {
|
|
settleRequest(requestId, { accept: false });
|
|
}
|
|
});
|
|
|
|
const toKeyBuffer = (rawKey) => (Buffer.isBuffer(rawKey) ? rawKey : Buffer.from(rawKey || ""));
|
|
|
|
const createHostVerifier = ({
|
|
sender,
|
|
sessionId,
|
|
hostname,
|
|
port = 22,
|
|
knownHosts = [],
|
|
verifyHostKeys = true,
|
|
bootEpoch,
|
|
// Transfer dials have no SFTP-page prompt owner. Waiting out the request
|
|
// TTL leaves the upload on "Waiting..." with no dialog and no error.
|
|
unattended = false,
|
|
onAccepted,
|
|
}) => {
|
|
// ssh2 calls the verifier again on every rekey. A prompt there is async,
|
|
// and while it waits ssh2 keeps sending channel data ahead of its NEWKEYS,
|
|
// so the server drops the connection mid-transfer ("Bad packet length").
|
|
// Like OpenSSH, a rekey only has to present the key already accepted for
|
|
// this connection: answer it synchronously and never prompt again.
|
|
let acceptedKey = null;
|
|
|
|
return (rawKey, callback) => {
|
|
if (verifyHostKeys !== false && acceptedKey) {
|
|
callback(acceptedKey.equals(toKeyBuffer(rawKey)));
|
|
return;
|
|
}
|
|
|
|
const keyInfo = describeHostKey(rawKey);
|
|
const noteAccepted = () => {
|
|
acceptedKey = Buffer.from(toKeyBuffer(rawKey));
|
|
try {
|
|
onAccepted?.({
|
|
hostname,
|
|
port,
|
|
keyType: keyInfo.keyType,
|
|
fingerprint: keyInfo.fingerprint,
|
|
publicKey: keyInfo.publicKey,
|
|
});
|
|
} catch {
|
|
// Recording the accepted key must not change the verifier result.
|
|
}
|
|
};
|
|
|
|
if (verifyHostKeys === false) {
|
|
noteAccepted();
|
|
callback(true);
|
|
return;
|
|
}
|
|
|
|
const decision = classifyHostKey({
|
|
knownHosts,
|
|
hostname,
|
|
port,
|
|
keyType: keyInfo.keyType,
|
|
fingerprint: keyInfo.fingerprint,
|
|
});
|
|
|
|
if (decision.status === "trusted") {
|
|
noteAccepted();
|
|
callback(true);
|
|
return;
|
|
}
|
|
|
|
if (unattended) {
|
|
callback(false);
|
|
return;
|
|
}
|
|
|
|
void requestHostKeyVerification(sender, {
|
|
sessionId,
|
|
hostname,
|
|
port,
|
|
status: decision.status,
|
|
keyType: keyInfo.keyType,
|
|
fingerprint: keyInfo.fingerprint,
|
|
publicKey: keyInfo.publicKey,
|
|
knownHostId: decision.knownHost?.id,
|
|
knownFingerprint: decision.expectedFingerprint,
|
|
...(Number.isFinite(bootEpoch) ? { bootEpoch: Number(bootEpoch) } : {}),
|
|
}).then((response) => {
|
|
if (response?.accept) noteAccepted();
|
|
callback(Boolean(response?.accept));
|
|
}).catch(() => {
|
|
callback(false);
|
|
});
|
|
};
|
|
};
|
|
|
|
const handleResponse = (_event, payload) => {
|
|
const { requestId, accept, addToKnownHosts } = payload || {};
|
|
const pending = hostKeyRequests.get(requestId);
|
|
if (!pending) return { success: false, error: "Request not found" };
|
|
if (_event?.sender?.id !== pending.webContentsId) {
|
|
return { success: false, error: "Wrong sender" };
|
|
}
|
|
return settleRequest(requestId, {
|
|
accept: Boolean(accept),
|
|
addToKnownHosts: Boolean(addToKnownHosts),
|
|
});
|
|
};
|
|
|
|
const registerHandler = (ipcMain) => {
|
|
ipcMain.handle("netcatty:host-key:respond", handleResponse);
|
|
};
|
|
|
|
const getRequests = () => hostKeyRequests;
|
|
const cancelRequestsForSession = (sessionId) => {
|
|
for (const [id, pending] of hostKeyRequests) {
|
|
if (pending.sessionId === sessionId) settleRequest(id, { accept: false, cancelled: true });
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
classifyHostKey,
|
|
createHostVerifier,
|
|
describeHostKey,
|
|
getKnownHostFingerprint,
|
|
handleResponse,
|
|
normalizeFingerprint,
|
|
registerHandler,
|
|
requestHostKeyVerification,
|
|
getRequests,
|
|
cancelRequestsForSession,
|
|
};
|