sshAuth.test.ts 5.4 KB

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