Files
Netcatty/electron/bridges/ssh2SkKeyParser.test.cjs
T
陈大猫andnetcatty-bot 48f1a1cecf feat(ssh): restore FIDO2 security key support (#2823)
* feat(ssh): restore FIDO2 security key support (#2308)

Consolidate the restored feature and its review fixes before rebasing onto current main. Original PR head: 5d0a0c6c9a550364d3ec5c2331395cedfc9ea7ae.

* fix(ssh): verify and repair FIDO authentication with simulated hardware

Preserve complete OpenSSH SK signature payloads, classify combined PIN/presence prompts correctly, expand IdentityFile tokens, retain certificate selectors, and route shared-agent signing prompts to their originating window. Dismiss prompts when notification helpers exit. Add a software SK provider and opt-in real OpenSSH integration coverage for both algorithms, certificates, SFTP, forwarding, and concurrent windows.

* refactor(ui): refine security key forms and authentication motion

Use shared Select and Switch controls, concise localized copy and compact token-based dialogs. Add scoped enter/exit, field transition and presence animations with reduced-motion support. Preserve raw prompt details and visible PIN errors.

* fix(ssh): align security key prompts with hardware presence and verify Docker logins

* fix(ui): keep security key dialog above its remounted overlay

* fix: address Codex review on PR #2823

* feat(ssh): guide security key recovery and explicit authentication fallback

* test(ssh): exercise security key authentication on native Windows

* test(ssh): verify Windows fixture signatures and explicit fallback

* test(ssh): configure Windows OpenSSH toolchain and document coverage

---------

Co-authored-by: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com>
2026-10-17 21:53:44 +08:00

149 lines
5.3 KiB
JavaScript

"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const {
parseKey,
getOpenSSHCertSignatureAlgorithm,
} = require("ssh2/lib/protocol/keyParser.js");
function buildSkEd25519PublicLine() {
const type = Buffer.from("sk-ssh-ed25519@openssh.com");
const pub = Buffer.alloc(32, 9);
const app = Buffer.from("ssh:");
const parts = [type, pub, app];
let len = 0;
for (const part of parts) len += 4 + part.length;
const buf = Buffer.alloc(len);
let offset = 0;
for (const part of parts) {
buf.writeUInt32BE(part.length, offset);
offset += 4;
part.copy(buf, offset);
offset += part.length;
}
return `sk-ssh-ed25519@openssh.com ${buf.toString("base64")} netcatty@sk`;
}
function buildSkEcdsaPublicLine() {
// Uncompressed P-256 point: 0x04 || X(32) || Y(32)
const point = Buffer.alloc(65, 0);
point[0] = 0x04;
point[64] = 1;
const type = Buffer.from("sk-ecdsa-sha2-nistp256@openssh.com");
const curve = Buffer.from("nistp256");
const app = Buffer.from("ssh:");
const parts = [type, curve, point, app];
let len = 0;
for (const part of parts) len += 4 + part.length;
const buf = Buffer.alloc(len);
let offset = 0;
for (const part of parts) {
buf.writeUInt32BE(part.length, offset);
offset += 4;
part.copy(buf, offset);
offset += part.length;
}
return `sk-ecdsa-sha2-nistp256@openssh.com ${buf.toString("base64")} netcatty@sk`;
}
test("ssh2 parseKey accepts sk-ssh-ed25519 public keys", () => {
const key = parseKey(buildSkEd25519PublicLine());
assert.equal(key instanceof Error, false, key instanceof Error ? key.message : "");
assert.equal(key.type, "sk-ssh-ed25519@openssh.com");
assert.equal(typeof key.getPublicSSH, "function");
assert.equal(key.getApplication?.(), "ssh:");
const signErr = key.sign(Buffer.from("challenge"));
assert.equal(signErr instanceof Error, true);
assert.match(String(signErr.message), /security-key|ssh-agent|FIDO/i);
});
test("ssh2 parseKey accepts sk-ecdsa-sha2-nistp256 public keys", () => {
const key = parseKey(buildSkEcdsaPublicLine());
assert.equal(key instanceof Error, false, key instanceof Error ? key.message : "");
assert.equal(key.type, "sk-ecdsa-sha2-nistp256@openssh.com");
assert.equal(key.getApplication?.(), "ssh:");
});
function buildSkEd25519CertPublicLine() {
// Minimal OpenSSH SK cert wire shape: type, nonce, then SK public fields.
const type = Buffer.from("sk-ssh-ed25519-cert-v01@openssh.com");
const nonce = Buffer.alloc(16, 7);
const pub = Buffer.alloc(32, 9);
const app = Buffer.from("ssh:");
const parts = [type, nonce, pub, app];
let len = 0;
for (const part of parts) len += 4 + part.length;
const buf = Buffer.alloc(len);
let offset = 0;
for (const part of parts) {
buf.writeUInt32BE(part.length, offset);
offset += 4;
part.copy(buf, offset);
offset += part.length;
}
return `sk-ssh-ed25519-cert-v01@openssh.com ${buf.toString("base64")} netcatty@sk-cert`;
}
test("ssh2 parseKey accepts OpenSSH-order SK certificate public keys", () => {
const line = buildSkEd25519CertPublicLine();
const certBlob = Buffer.from(line.split(/\s+/)[1], "base64");
const key = parseKey(line);
assert.equal(key instanceof Error, false, key instanceof Error ? key.message : "");
assert.equal(key.type, "sk-ssh-ed25519-cert-v01@openssh.com");
assert.equal(key.getApplication?.(), "ssh:");
assert.deepEqual(key.getPublicSSH(), certBlob);
});
test("ssh2 maps SK certificate identities to full wire signature algorithms", () => {
assert.equal(
getOpenSSHCertSignatureAlgorithm("sk-ssh-ed25519-cert-v01@openssh.com"),
"sk-ssh-ed25519@openssh.com",
);
assert.equal(
getOpenSSHCertSignatureAlgorithm("sk-ecdsa-sha2-nistp256-cert-v01@openssh.com"),
"sk-ecdsa-sha2-nistp256@openssh.com",
);
assert.equal(
getOpenSSHCertSignatureAlgorithm("rsa-sha2-256-cert-v01@openssh.com"),
"rsa-sha2-256",
);
assert.equal(
getOpenSSHCertSignatureAlgorithm("ecdsa-sha2-nistp256-cert-v01@openssh.com"),
"ecdsa-sha2-nistp256",
);
assert.equal(
getOpenSSHCertSignatureAlgorithm("sk-ssh-ed25519@openssh.com"),
"sk-ssh-ed25519@openssh.com",
);
});
test("ssh2 agent protocol preserves SK signature framing, flags and counter", async () => {
const { AgentProtocol } = require("ssh2/lib/agent.js");
for (const line of [buildSkEd25519PublicLine(), buildSkEcdsaPublicLine(), buildSkEd25519CertPublicLine()]) {
const client = new AgentProtocol(true);
const server = new AgentProtocol(false);
client.pipe(server).pipe(client);
const raw = Buffer.alloc(64, 7);
const payload = Buffer.alloc(4 + raw.length + 5);
payload.writeUInt32BE(raw.length, 0);
raw.copy(payload, 4);
payload[4 + raw.length] = 5; // presence and verification
payload.writeUInt32BE(0x12345678, 5 + raw.length);
server.on("sign", (request, key) => {
assert.equal(request.getContext(), getOpenSSHCertSignatureAlgorithm(key.type));
server.signReply(request, payload);
});
try {
const returned = await new Promise((resolve, reject) => client.sign(
parseKey(line), Buffer.from("challenge"), {},
(error, signature) => error ? reject(error) : resolve(signature),
));
assert.deepEqual(returned, payload);
} finally {
client.destroy();
server.destroy();
}
}
});