conversationShareMarkdown.test.ts 3.4 KB

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