sourceServerConnectionLog.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { describe, expect, it } from "vitest";
  2. import { sanitizeRemoteConnectionLog } from "../src/source-server/connectionLog.js";
  3. describe("用户连接日志路径边界", () => {
  4. it.each([
  5. ["apiKey: test-key", "apiKey: [redacted]"],
  6. ['{"api_key":"test-key","status":"failed"}', '{"api_key":[redacted],"status":"failed"}'],
  7. ["client_secret=test-secret", "client_secret=[redacted]"],
  8. ["x-api-key: test-key", "x-api-key: [redacted]"],
  9. [
  10. "providerApiKey=test-key provider-secret=test-secret",
  11. "providerApiKey=[redacted] provider-secret=[redacted]",
  12. ],
  13. ["Authorization: Basic dXNlcjpwYXNz", "Authorization: [redacted]"],
  14. ["Authorization: Bearer test-token", "Authorization: [redacted]"],
  15. ["Cookie: session=first; other=second", "Cookie: [redacted]"],
  16. ['Cookie: "session=first"; other=second', "Cookie: [redacted]"],
  17. ['Authorization: "Basic first" trailing-secret', "Authorization: [redacted]"],
  18. ["Set-Cookie: session=first; Path=/; HttpOnly", "Set-Cookie: [redacted]"],
  19. [
  20. '{"authorization":"Basic dXNlcjpwYXNz","cookie":"a=first; b=second"}',
  21. '{"authorization":[redacted],"cookie":[redacted]}',
  22. ],
  23. [
  24. '{"api_key":"prefix\\\"suffix","status":"failed"}',
  25. '{"api_key":[redacted],"status":"failed"}',
  26. ],
  27. ["Basic dXNlcjpwYXNz", "Basic [redacted]"],
  28. ["Bearer test-token", "Bearer [redacted]"],
  29. [
  30. "Authorization: Basic test-secret\r\nCookie: a=first; b=second\r\nprobe failed",
  31. "Authorization: [redacted]\r\nCookie: [redacted]\r\nprobe failed",
  32. ],
  33. ])("凭据日志 %s 不残留秘密且重复过滤幂等", (raw, expected) => {
  34. const result = sanitizeRemoteConnectionLog(raw);
  35. expect(result).toBe(expected);
  36. expect(sanitizeRemoteConnectionLog(result)).toBe(result);
  37. });
  38. it.each([
  39. "/home/dev/.zcode/server",
  40. "~/server",
  41. "C:\\Users\\dev\\server",
  42. "\\\\host\\share\\server",
  43. ])("遮蔽 %s 后仍保留失败原因", (path) => {
  44. expect(
  45. sanitizeRemoteConnectionLog(`Installing ZCode Server to ${path} failed with exit code 1`),
  46. ).toBe("Installing ZCode Server to [redacted-path] failed with exit code 1");
  47. });
  48. it.each([
  49. '"/home/dev/My Server"',
  50. "'/home/dev/My Server'",
  51. '"C:\\Users\\Some Name\\server"',
  52. "'\\\\host\\Shared Folder\\server'",
  53. ])("整体遮蔽带引号空格路径 %s", (path) => {
  54. expect(sanitizeRemoteConnectionLog(`Installing to ${path} failed with exit code 1`)).toBe(
  55. "Installing to [redacted-path] failed with exit code 1",
  56. );
  57. });
  58. it("已知秘密按完整 token 遮蔽,重复过滤不破坏诊断", () => {
  59. const raw = "File /tmp/server failed with exit code 1 password=prefixdevsuffix";
  60. const result = sanitizeRemoteConnectionLog(raw, ["dev"]);
  61. expect(result).toBe("File [redacted-path] failed with exit code 1 password=[redacted]");
  62. expect(sanitizeRemoteConnectionLog(result)).toBe(result);
  63. expect(sanitizeRemoteConnectionLog("auth dev failed: 'dev', (dev)", ["dev"])).toBe(
  64. "auth [redacted] failed: '[redacted]', ([redacted])",
  65. );
  66. });
  67. it("短密码出现在普通单词内部时不替换,避免从固定文案反推出密码", () => {
  68. expect(sanitizeRemoteConnectionLog("Checking the remote device", ["dev"])).toBe(
  69. "Checking the remote device",
  70. );
  71. // 中文没有空格分词,紧邻中文仍视为独立 token,必须遮蔽。
  72. expect(sanitizeRemoteConnectionLog("使用密码dev登录失败", ["dev"])).toBe(
  73. "使用密码[redacted]登录失败",
  74. );
  75. });
  76. it("秘密两端是标点时,紧邻单词字符仍会被遮蔽", () => {
  77. expect(sanitizeRemoteConnectionLog("login x!p@ss! end", ["!p@ss!"])).toBe(
  78. "login x[redacted] end",
  79. );
  80. });
  81. it("没有引号的空格路径仍保守过滤,只保留可明确界定的诊断后缀", () => {
  82. expect(
  83. sanitizeRemoteConnectionLog("Installing /home/dev/My Private Server failed with exit code 1"),
  84. ).toBe("Installing [redacted-path] failed with exit code 1");
  85. expect(sanitizeRemoteConnectionLog("File C:\\Users\\Private Name\\Project Files")).toBe(
  86. "File [redacted-path]",
  87. );
  88. });
  89. it.each([
  90. "C:\\Users\\Doe, Jane\\secret",
  91. "C:\\Users\\Secret (Private)\\token",
  92. "\\\\host\\private;folder\\secret",
  93. ])("Windows/UNC 的合法标点不能被当作路径边界:%s", (path) => {
  94. expect(sanitizeRemoteConnectionLog(`File ${path} failed with exit code 1`)).toBe(
  95. "File [redacted-path] failed with exit code 1",
  96. );
  97. });
  98. it("日志生产者的 JSON 路径转义不会提前结束脱敏边界", () => {
  99. const path = '/home/dev/private "quoted" directory';
  100. expect(
  101. sanitizeRemoteConnectionLog(`File ${JSON.stringify(path)} failed with exit code 1`),
  102. ).toBe("File [redacted-path] failed with exit code 1");
  103. });
  104. });