| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- import { afterEach, describe, expect, it } from "vitest";
- import { normalizeRemoteConnectionError, publicZCodeErrorCodeSchema } from "@zcode/shared";
- import {
- buildSSHConnectConfig,
- createKeyboardInteractiveResponder,
- normalizeSSHConnectError,
- } from "../src/remote/sshAuth.js";
- const originalAuthSock = process.env["SSH_AUTH_SOCK"];
- afterEach(() => {
- if (originalAuthSock == null) {
- delete process.env["SSH_AUTH_SOCK"];
- return;
- }
- process.env["SSH_AUTH_SOCK"] = originalAuthSock;
- });
- describe("sshAuth", () => {
- it("密码登录时默认禁用隐式 agent,并启用 keyboard-interactive", () => {
- process.env["SSH_AUTH_SOCK"] = "/tmp/test.sock";
- const config = buildSSHConnectConfig({
- host: "example.com",
- username: "root",
- password: "secret",
- });
- expect(config.agent).toBeUndefined();
- expect(config.tryKeyboard).toBe(true);
- expect(config.password).toBe("secret");
- });
- it("无密码时保留隐式 agent", () => {
- process.env["SSH_AUTH_SOCK"] = "/tmp/test.sock";
- const config = buildSSHConnectConfig({
- host: "example.com",
- username: "root",
- });
- expect(config.agent).toBe("/tmp/test.sock");
- expect(config.tryKeyboard).toBe(false);
- });
- it("显式传入 agent 时应覆盖默认策略", () => {
- const config = buildSSHConnectConfig({
- host: "example.com",
- username: "root",
- password: "secret",
- agent: "/tmp/custom.sock",
- });
- expect(config.agent).toBe("/tmp/custom.sock");
- });
- it("连接配置应显式设置 readyTimeout 和 keepalive,避免静默断连长期悬挂", () => {
- const config = buildSSHConnectConfig({
- host: "example.com",
- username: "root",
- });
- expect(config.readyTimeout).toBe(60_000);
- expect(config.keepaliveInterval).toBe(15_000);
- expect(config.keepaliveCountMax).toBe(3);
- });
- it("私钥口令只用于解密私钥,不应改变密码登录分支判断", () => {
- process.env["SSH_AUTH_SOCK"] = "/tmp/test.sock";
- const config = buildSSHConnectConfig({
- host: "example.com",
- username: "root",
- privateKey: "test-private-key",
- passphrase: "key-secret",
- });
- expect(config.passphrase).toBe("key-secret");
- expect(config.agent).toBe("/tmp/test.sock");
- expect(config.tryKeyboard).toBe(false);
- expect(config.password).toBeUndefined();
- });
- it("keyboard-interactive 回调应使用同一密码响应全部 prompt", () => {
- const responder = createKeyboardInteractiveResponder("secret");
- let responses: string[] = [];
- responder(
- "",
- "",
- "",
- [
- { prompt: "Password:", echo: false },
- { prompt: "OTP:", echo: false },
- ],
- (nextResponses) => {
- responses = nextResponses;
- },
- );
- expect(responses).toEqual(["secret", "secret"]);
- });
- it("client-authentication 错误应转换为可读信息", () => {
- const error = normalizeSSHConnectError({ level: "client-authentication" });
- expect(error.message).toContain("SSH 认证失败");
- });
- it.each([
- Object.assign(new Error("authentication wording may change"), { level: "client-authentication" }),
- Object.assign(new Error("Permission denied"), { level: "client-authentication" }),
- new Error("OpenSSH key integrity check failed -- bad passphrase?"),
- new Error("Encrypted private OpenSSH key detected, but no passphrase given"),
- ])("认证错误经过预检仍保留公开认证码及原始 cause", (cause) => {
- const error = normalizeSSHConnectError(cause);
- expect(error.cause).toBe(cause);
- const failure = normalizeRemoteConnectionError(error, "preflight");
- expect(failure.code).toBe("remote-authentication-failed");
- expect(publicZCodeErrorCodeSchema.parse(failure.code)).toBe(failure.code);
- });
- it("文件权限错误不被认证分类吞掉", () => {
- const error = Object.assign(new Error("Permission denied"), { code: "EACCES" });
- expect(normalizeRemoteConnectionError(normalizeSSHConnectError(error), "preflight").code)
- .toBe("remote-permission-denied");
- });
- it("client-timeout 错误应说明是 SSH 连接握手超时", () => {
- const error = normalizeSSHConnectError({ level: "client-timeout" });
- expect(error.message).toBe(
- "SSH 连接握手超时:未能在 60 秒内建立 SSH 会话,请检查网络、服务器 SSH 服务或终端 SSH 配置差异",
- );
- });
- it("私钥缺少口令的错误文案变体应统一转换为可读提示", () => {
- const expectedMessage = "SSH 私钥需要口令:检测到加密私钥,但当前未提供私钥口令";
- const cases = [
- "Encrypted private OpenSSH key detected, but no passphrase given",
- "Encrypted OpenSSH private key detected, but no passphrase given",
- "Encrypted PPK private key detected, but no passphrase given",
- ];
- for (const message of cases) {
- expect(normalizeSSHConnectError(new Error(message)).message).toBe(expectedMessage);
- }
- });
- it("私钥口令错误应转换为可读提示", () => {
- const expectedMessage = "SSH 私钥口令错误:无法解密私钥,请检查私钥口令是否正确";
- const cases = [
- "OpenSSH key integrity check failed -- bad passphrase?",
- "PPK private key integrity check failed -- bad passphrase?",
- ];
- for (const message of cases) {
- expect(normalizeSSHConnectError(new Error(message)).message).toBe(expectedMessage);
- }
- });
- });
|