feedbackPrivacy.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { describe, expect, it } from "vitest";
  2. import { redactFeedbackText } from "../src/feedbackPrivacy.js";
  3. describe("反馈文本脱敏", () => {
  4. it.each([false, true])("diagnostic=%s 隐藏路径型凭据,包括嵌套 JSON 和编码路径", (diagnostic) => {
  5. const urls = [
  6. "https://hooks.slack.com/services/T000/B000/slack-canary",
  7. "https://example.webhook.office.com/webhookb2/teams-canary/IncomingWebhook/key",
  8. "https://example.com/password-reset/reset-canary",
  9. "https://example.com/invite/invite-canary",
  10. "https://example.com/callback/callback-canary",
  11. "https://example.com/download/download-canary",
  12. "https://example.com/%72eset/encoded-canary",
  13. "https://files.example.com/object-canary?X-Amz-Signature=signature-canary",
  14. "https://files.example.com/object-canary?sig=signature-canary",
  15. ];
  16. for (const url of urls) {
  17. for (const source of [`请求失败 ${url}`, JSON.stringify({ detail: { url } })]) {
  18. const output = redactFeedbackText(source, { diagnostic });
  19. expect(output).not.toContain("canary");
  20. expect(output).toContain(new URL(url).host);
  21. expect(redactFeedbackText(output, { diagnostic })).toBe(output);
  22. }
  23. }
  24. });
  25. it("诊断隐藏未知路径,用户正文保留普通排障链接", () => {
  26. const url = "https://example.com:8443/project/opaque-canary?view=details#section";
  27. expect(redactFeedbackText(url, { diagnostic: true })).toBe(
  28. "https://example.com:8443/[REDACTED]",
  29. );
  30. expect(redactFeedbackText(url)).toBe("https://example.com:8443/project/opaque-canary");
  31. expect(redactFeedbackText("https://example.com/")).toBe("https://example.com/");
  32. expect(redactFeedbackText("https://example.com/%invalid/opaque-canary")).not.toContain(
  33. "canary",
  34. );
  35. });
  36. it("递归清洗嵌套 JSON 和字符串中的密钥,同时保留诊断元数据", () => {
  37. const source = JSON.stringify({
  38. event: "failure",
  39. config: { apiKey: "nested-canary" },
  40. note: "API_KEY=embedded-canary",
  41. request: { messages: [{ content: "private-prompt" }] },
  42. toolOutput: "private-file",
  43. status: 401,
  44. });
  45. const output = redactFeedbackText(source, { diagnostic: true });
  46. for (const secret of ["nested-canary", "embedded-canary", "private-prompt", "private-file"]) {
  47. expect(output).not.toContain(secret);
  48. }
  49. expect(output).toContain("failure");
  50. expect(output).toContain("401");
  51. });
  52. it("覆盖日志前缀、转义字符串、URL、私钥和用户路径", () => {
  53. const source =
  54. '[ERROR] {"outer":{"token":"json-canary"},"note":"Authorization: Bearer bearer-canary"}\n' +
  55. "url=https://user:password-canary@example.com/path?api_key=query-canary\n" +
  56. "-----BEGIN PRIVATE KEY-----\npem-canary\n-----END PRIVATE KEY-----\n" +
  57. "/Users/alice/private/work.ts C:\\Users\\bob\\private\\file.txt";
  58. const output = redactFeedbackText(source, { diagnostic: true });
  59. for (const secret of [
  60. "json-canary",
  61. "bearer-canary",
  62. "password-canary",
  63. "query-canary",
  64. "pem-canary",
  65. "alice",
  66. "bob",
  67. ]) {
  68. expect(output).not.toContain(secret);
  69. }
  70. });
  71. it("版本号和普通文本保持原样,Basic 认证整段清洗", () => {
  72. expect(redactFeedbackText("1.0")).toBe("1.0");
  73. expect(redactFeedbackText("Authorization: Basic basic-canary")).not.toContain("basic-canary");
  74. });
  75. it("用户正文保留内容意图,仍清除已知凭证", () => {
  76. expect(redactFeedbackText("我的 content 字段坏了,API_KEY=user-canary")).toContain(
  77. "我的 content 字段坏了",
  78. );
  79. expect(redactFeedbackText("我的 content 字段坏了,API_KEY=user-canary")).not.toContain(
  80. "user-canary",
  81. );
  82. });
  83. it.each([false, true])("diagnostic=%s 清洗嵌入字符串和同行所有 JSON 片段", (diagnostic) => {
  84. const source = [
  85. JSON.stringify({ error: 'failed config: {"apiKey":"nested-canary"}', status: 500 }),
  86. '[info] {"apiKey":"prefix-canary"} {"status":401}',
  87. '[info] {"cookie":"cookie-canary"} trailing text',
  88. ].join("\n");
  89. const output = redactFeedbackText(source, { diagnostic });
  90. expect(output).not.toContain("canary");
  91. expect(output).toContain("500");
  92. expect(output).toContain("401");
  93. expect(output).toContain("trailing text");
  94. });
  95. it.each(["cookie", "passwd", "passphrase", "privateKey", "access_key", "api\\u004bey"])(
  96. "%s 在多行及截断结构中仍脱敏",
  97. (key) => {
  98. for (const source of [
  99. `[info] config {\n "${key}": "pretty-canary"\n}`,
  100. `{"${key}":"truncated-canary`,
  101. `"${key}":"bare-canary`,
  102. `[info] {"${key}": {\n "value": "nested-canary"\n`,
  103. ]) {
  104. expect(redactFeedbackText(source)).not.toContain("canary");
  105. }
  106. },
  107. );
  108. it.each(["input", "output", "args", "env", "headers", "stdout", "data", "params"])(
  109. "%s 的正文只在诊断模式清洗,兼容多行和截断",
  110. (key) => {
  111. for (const source of [
  112. `[info] details {\n "${key}": {"value":"body-canary"}\n}`,
  113. `[info] details {\n "${key}": {\n "value":"body-canary"`,
  114. `${key}: body-canary with spaces`,
  115. ]) {
  116. expect(redactFeedbackText(source, { diagnostic: true })).not.toContain("body-canary");
  117. expect(redactFeedbackText(source)).toContain("body-canary");
  118. }
  119. },
  120. );
  121. it("括号和转义引号不破坏片段边界,普通日志保留", () => {
  122. const source = '[info] {"note":"a } and \\\" quote","status":200} [done]';
  123. expect(redactFeedbackText(source, { diagnostic: true })).toBe(source);
  124. });
  125. it("普通凭据中的括号与已脱敏标记不能掩盖其他字段", () => {
  126. for (const source of [
  127. 'password="first{canary}last"',
  128. "Bearer first[canary]last",
  129. "token=known passphrase=other-canary",
  130. "passphrase=[REDACTED]canary",
  131. ]) {
  132. expect(redactFeedbackText(source)).not.toContain("canary");
  133. }
  134. expect(
  135. redactFeedbackText("input: first [canary] last-canary", { diagnostic: true }),
  136. ).not.toContain("canary");
  137. });
  138. });