feedbackPrivacyBoundary.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { mkdtemp, readFile, rm, access } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join, basename } from "node:path";
  4. import { createFeedbackService } from "../src/feedback/feedbackService.js";
  5. import type { IOAuthService } from "../src/oauth/oauth.js";
  6. import { getFeedbackAttachmentDir, setDataBaseDir } from "../src/paths.js";
  7. import { describe, expect, it, vi } from "vitest";
  8. import { FeedbackHttpClient } from "../src/feedback/feedbackHttpClient.js";
  9. import type { ApiRequestInit } from "@zcode/shared";
  10. describe("工单发送隐私边界", () => {
  11. it("创建和补充均脱敏文本,保留用户联系方式并丢弃模型列表", async () => {
  12. const bodies: string[] = [];
  13. const request = vi.fn(async (_url: string | URL | Request, init?: ApiRequestInit) => {
  14. bodies.push(String(init?.body));
  15. return new Response(
  16. JSON.stringify({
  17. code: 0,
  18. data: { ticket_id: "ticket", message_id: "message", created_at: 1 },
  19. }),
  20. );
  21. });
  22. const client = new FeedbackHttpClient({
  23. baseUrl: "https://example.invalid/api/v1",
  24. apiClient: { request },
  25. getAuthHeaders: async () => ({ "X-Device-Mid": "device" }),
  26. });
  27. await client.create({
  28. title: "https://hooks.slack.com/services/T000/B000/title-path-canary API_KEY=title-canary",
  29. description: [
  30. "请帮助我,token=body-canary",
  31. "https://example.com/reset/body-path-canary",
  32. JSON.stringify({ url: "https://files.example.com/object-path-canary?sig=sig-canary" }),
  33. JSON.stringify({ error: 'failed config: {"apiKey":"embedded-canary"}' }),
  34. '[info] {"apiKey":"prefix-canary"} {"status":401}',
  35. '{"cookie":"truncated-canary',
  36. ].join("\n"),
  37. type: "bug",
  38. contact: "user@example.com",
  39. device: {
  40. agentModelOptionsPreview: ["private-model-canary"],
  41. hostname: "host-canary",
  42. appVersion: "1.0",
  43. },
  44. });
  45. await client.comment(
  46. "ticket",
  47. "https://example.com/invite/comment-path-canary\nAuthorization: Bearer comment-canary",
  48. );
  49. const sent = bodies.join("\n");
  50. for (const secret of [
  51. "title-canary",
  52. "title-path-canary",
  53. "body-path-canary",
  54. "object-path-canary",
  55. "sig-canary",
  56. "comment-path-canary",
  57. "body-canary",
  58. "embedded-canary",
  59. "prefix-canary",
  60. "truncated-canary",
  61. "comment-canary",
  62. "private-model-canary",
  63. "host-canary",
  64. ])
  65. expect(sent).not.toContain(secret);
  66. expect(sent).toContain("user@example.com");
  67. expect(sent).toContain("请帮助我");
  68. expect(sent).toContain("1.0");
  69. });
  70. });
  71. it("附件文件名不能越出临时目录,上传后清理暂存数据", async () => {
  72. const root = await mkdtemp(join(tmpdir(), "feedback-attachment-privacy-"));
  73. setDataBaseDir(root);
  74. let uploadedPath = "";
  75. const upload = vi
  76. .spyOn(FeedbackHttpClient.prototype, "uploadFile")
  77. .mockImplementation(async (_id, _kind, path, filename) => {
  78. uploadedPath = path;
  79. expect(path.startsWith(getFeedbackAttachmentDir())).toBe(true);
  80. expect(basename(path)).toBe("content");
  81. expect(filename).toBe("private.png");
  82. expect(await readFile(path, "utf8")).toBe("synthetic-image");
  83. return { id: 1 } as Awaited<ReturnType<FeedbackHttpClient["uploadFile"]>>;
  84. });
  85. try {
  86. const service = createFeedbackService({
  87. credentialService: { load: async () => null, save: async () => {}, delete: async () => {} },
  88. oauthService: {} as IOAuthService,
  89. apiClient: { request: vi.fn() },
  90. });
  91. await service.uploadAttachmentData("ticket", "image", {
  92. filename: "../../private.png",
  93. dataBase64: Buffer.from("synthetic-image").toString("base64"),
  94. contentType: "image/png",
  95. });
  96. expect(upload).toHaveBeenCalledOnce();
  97. await expect(access(uploadedPath)).rejects.toThrow();
  98. } finally {
  99. upload.mockRestore();
  100. setDataBaseDir(null);
  101. await rm(root, { recursive: true, force: true });
  102. }
  103. });