sshAuth.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { afterEach, describe, expect, it } from "vitest";
  2. import {
  3. buildSSHConnectConfig,
  4. createKeyboardInteractiveResponder,
  5. normalizeSSHConnectError,
  6. } from "../src/remote/sshAuth.js";
  7. const originalAuthSock = process.env["SSH_AUTH_SOCK"];
  8. afterEach(() => {
  9. if (originalAuthSock == null) {
  10. delete process.env["SSH_AUTH_SOCK"];
  11. return;
  12. }
  13. process.env["SSH_AUTH_SOCK"] = originalAuthSock;
  14. });
  15. describe("sshAuth", () => {
  16. it("密码登录时默认禁用隐式 agent,并启用 keyboard-interactive", () => {
  17. process.env["SSH_AUTH_SOCK"] = "/tmp/test.sock";
  18. const config = buildSSHConnectConfig({
  19. host: "example.com",
  20. username: "root",
  21. password: "secret",
  22. });
  23. expect(config.agent).toBeUndefined();
  24. expect(config.tryKeyboard).toBe(true);
  25. expect(config.password).toBe("secret");
  26. });
  27. it("无密码时保留隐式 agent", () => {
  28. process.env["SSH_AUTH_SOCK"] = "/tmp/test.sock";
  29. const config = buildSSHConnectConfig({
  30. host: "example.com",
  31. username: "root",
  32. });
  33. expect(config.agent).toBe("/tmp/test.sock");
  34. expect(config.tryKeyboard).toBe(false);
  35. });
  36. it("显式传入 agent 时应覆盖默认策略", () => {
  37. const config = buildSSHConnectConfig({
  38. host: "example.com",
  39. username: "root",
  40. password: "secret",
  41. agent: "/tmp/custom.sock",
  42. });
  43. expect(config.agent).toBe("/tmp/custom.sock");
  44. });
  45. it("连接配置应显式设置 readyTimeout 和 keepalive,避免静默断连长期悬挂", () => {
  46. const config = buildSSHConnectConfig({
  47. host: "example.com",
  48. username: "root",
  49. });
  50. expect(config.readyTimeout).toBe(60_000);
  51. expect(config.keepaliveInterval).toBe(15_000);
  52. expect(config.keepaliveCountMax).toBe(3);
  53. });
  54. it("私钥口令只用于解密私钥,不应改变密码登录分支判断", () => {
  55. process.env["SSH_AUTH_SOCK"] = "/tmp/test.sock";
  56. const config = buildSSHConnectConfig({
  57. host: "example.com",
  58. username: "root",
  59. privateKey: "test-private-key",
  60. passphrase: "key-secret",
  61. });
  62. expect(config.passphrase).toBe("key-secret");
  63. expect(config.agent).toBe("/tmp/test.sock");
  64. expect(config.tryKeyboard).toBe(false);
  65. expect(config.password).toBeUndefined();
  66. });
  67. it("keyboard-interactive 回调应使用同一密码响应全部 prompt", () => {
  68. const responder = createKeyboardInteractiveResponder("secret");
  69. let responses: string[] = [];
  70. responder(
  71. "",
  72. "",
  73. "",
  74. [
  75. { prompt: "Password:", echo: false },
  76. { prompt: "OTP:", echo: false },
  77. ],
  78. (nextResponses) => {
  79. responses = nextResponses;
  80. },
  81. );
  82. expect(responses).toEqual(["secret", "secret"]);
  83. });
  84. it("client-authentication 错误应转换为可读信息", () => {
  85. const error = normalizeSSHConnectError({ level: "client-authentication" });
  86. expect(error.message).toContain("SSH 认证失败");
  87. });
  88. it("client-timeout 错误应说明是 SSH 连接握手超时", () => {
  89. const error = normalizeSSHConnectError({ level: "client-timeout" });
  90. expect(error.message).toBe(
  91. "SSH 连接握手超时:未能在 60 秒内建立 SSH 会话,请检查网络、服务器 SSH 服务或终端 SSH 配置差异",
  92. );
  93. });
  94. it("私钥缺少口令的错误文案变体应统一转换为可读提示", () => {
  95. const expectedMessage = "SSH 私钥需要口令:检测到加密私钥,但当前未提供私钥口令";
  96. const cases = [
  97. "Encrypted private OpenSSH key detected, but no passphrase given",
  98. "Encrypted OpenSSH private key detected, but no passphrase given",
  99. "Encrypted PPK private key detected, but no passphrase given",
  100. ];
  101. for (const message of cases) {
  102. expect(normalizeSSHConnectError(new Error(message)).message).toBe(expectedMessage);
  103. }
  104. });
  105. it("私钥口令错误应转换为可读提示", () => {
  106. const expectedMessage = "SSH 私钥口令错误:无法解密私钥,请检查私钥口令是否正确";
  107. const cases = [
  108. "OpenSSH key integrity check failed -- bad passphrase?",
  109. "PPK private key integrity check failed -- bad passphrase?",
  110. ];
  111. for (const message of cases) {
  112. expect(normalizeSSHConnectError(new Error(message)).message).toBe(expectedMessage);
  113. }
  114. });
  115. });