conversationShareMarkdown.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { describe, expect, it } from "vitest";
  2. import { normalizeConversationShareMarkdown } from "@/v4/conversationShareMarkdown.js";
  3. describe("normalizeConversationShareMarkdown", () => {
  4. it("removes local citation paths while retaining a uniquely matched public artifact name", () => {
  5. const markdown =
  6. '结论见 ::zcode-file-citation{path="/Users/demo/report.pdf" purpose="output"}。';
  7. const result = normalizeConversationShareMarkdown(
  8. markdown,
  9. new Map([["share-artifact-1", "report.pdf"]]),
  10. );
  11. expect(result).toBe("结论见 report.pdf。");
  12. });
  13. it("does not rewrite citation-looking text inside a fenced code block", () => {
  14. const markdown = ["```md", '::zcode-file-citation{path="example.pdf"}', "```"].join("\n");
  15. expect(normalizeConversationShareMarkdown(markdown)).toBe(markdown);
  16. });
  17. // Bug 回归:公开分享页无 workspacePath/readAttachment,MarkdownImage 会 fallback 到
  18. // <img src={远程}>,在匿名访客浏览器自动发起跨域请求,等价于发布者可控的 tracking
  19. // pixel(泄露 IP/UA/Referer)。这里把远程图片降级成链接,点击才发请求。
  20. describe("远程图片降级", () => {
  21. it("把 http/https 图片降级为普通链接", () => {
  22. expect(normalizeConversationShareMarkdown("![图](https://evil.example/x.png)")).toBe(
  23. "[图](https://evil.example/x.png)",
  24. );
  25. expect(normalizeConversationShareMarkdown("![图](http://evil.example/x.png)")).toBe(
  26. "[图](http://evil.example/x.png)",
  27. );
  28. });
  29. it("覆盖 title 与尖括号写法,以及协议相对地址", () => {
  30. expect(normalizeConversationShareMarkdown('![图](https://evil.example/x.png "标题")')).toBe(
  31. '[图](https://evil.example/x.png "标题")',
  32. );
  33. expect(normalizeConversationShareMarkdown("![图](<https://evil.example/a b.png>)")).toBe(
  34. "[图](<https://evil.example/a b.png>)",
  35. );
  36. expect(normalizeConversationShareMarkdown("![图](//evil.example/x.png)")).toBe(
  37. "[图](//evil.example/x.png)",
  38. );
  39. });
  40. it("保留 data: 与相对路径图片", () => {
  41. const dataImage = "![本地](data:image/png;base64,AAAA)";
  42. expect(normalizeConversationShareMarkdown(dataImage)).toBe(dataImage);
  43. const relative = "![本地](./assets/x.png)";
  44. expect(normalizeConversationShareMarkdown(relative)).toBe(relative);
  45. });
  46. it("不改写代码围栏内的远程图片语法", () => {
  47. const markdown = ["```md", "![图](https://evil.example/x.png)", "```"].join("\n");
  48. expect(normalizeConversationShareMarkdown(markdown)).toBe(markdown);
  49. });
  50. it("同一段里的多张远程图片全部降级", () => {
  51. expect(
  52. normalizeConversationShareMarkdown(
  53. "前 ![a](https://a.example/1.png) 中 ![b](https://b.example/2.png) 后",
  54. ),
  55. ).toBe("前 [a](https://a.example/1.png) 中 [b](https://b.example/2.png) 后");
  56. });
  57. it("与 citation 剥离同时生效", () => {
  58. const result = normalizeConversationShareMarkdown(
  59. '![图](https://evil.example/x.png) 见 ::zcode-file-citation{path="/Users/demo/report.pdf"}。',
  60. new Map([["share-artifact-1", "report.pdf"]]),
  61. );
  62. expect(result).toBe("[图](https://evil.example/x.png) 见 report.pdf。");
  63. });
  64. });
  65. });