export-html-xss.test.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { readFileSync } from "fs";
  2. import { describe, expect, it } from "vitest";
  3. describe("export HTML markdown link sanitization", () => {
  4. const templateJs = readFileSync(new URL("../src/core/export-html/template.js", import.meta.url), "utf-8");
  5. it("overrides the marked link renderer to use scheme allow-list sanitization", () => {
  6. expect(templateJs).toMatch(/link\s*\(\s*token\s*\)/);
  7. expect(templateJs).toMatch(/sanitizeMarkdownUrl\(token\.href\)/);
  8. expect(templateJs).toMatch(/\^\(https\?\|mailto\|tel\|ftp\)/);
  9. });
  10. it("overrides the marked image renderer to use scheme allow-list sanitization", () => {
  11. expect(templateJs).toMatch(/image\s*\(\s*token\s*\)/);
  12. expect(templateJs).toMatch(/sanitizeMarkdownUrl\(token\.href\)/);
  13. });
  14. it("strips C0 controls before checking and emitting markdown URLs", () => {
  15. expect(templateJs).toContain("replace(/[\\x00-\\x1f\\x7f]/g, '')");
  16. expect(templateJs).not.toMatch(/\^\\s\*\(javascript\|vbscript\|data\):/i);
  17. });
  18. it("escapes href attributes in the custom link renderer", () => {
  19. // The link renderer must escape href values to prevent attribute breakout
  20. expect(templateJs).toMatch(/escapeHtml\(href\)/);
  21. });
  22. it("escapes image mimeType attributes", () => {
  23. // Image mimeType must be escaped to prevent attribute breakout
  24. expect(templateJs).not.toMatch(/\$\{img\.mimeType\}/);
  25. expect(templateJs).toMatch(/escapeHtml\(img\.mimeType/);
  26. });
  27. it("escapes image data attributes", () => {
  28. // Image data is embedded in src attributes and must not allow attribute breakout.
  29. expect(templateJs).not.toMatch(/;base64,\$\{img\.data\}"/);
  30. expect(templateJs).toMatch(/;base64,\$\{escapeHtml\(img\.data \|\| (?:''|"")\)\}"/);
  31. });
  32. it("escapes entry IDs before inserting them into attributes", () => {
  33. // Session entry IDs are embedded in id and data-entry-id attributes.
  34. expect(templateJs).not.toMatch(/id="\$\{entryId\}"/);
  35. expect(templateJs).not.toMatch(/data-entry-id="\$\{entryId\}"/);
  36. expect(templateJs).toMatch(/entry-\$\{escapeHtml\(entry\.id\)\}/);
  37. expect(templateJs).toMatch(/data-entry-id="\$\{escapeHtml\(entryId\)\}"/);
  38. });
  39. it("escapes tree metadata rendered from session fields", () => {
  40. // The tree renders session metadata via innerHTML, so dynamic fields must be escaped.
  41. expect(templateJs).not.toMatch(/\[\$\{msg\.toolName \|\| 'tool'\}\]/);
  42. expect(templateJs).not.toMatch(/\[\$\{msg\.role\}\]/);
  43. expect(templateJs).not.toMatch(/\[model: \$\{entry\.modelId\}\]/);
  44. expect(templateJs).not.toMatch(/\[thinking: \$\{entry\.thinkingLevel\}\]/);
  45. expect(templateJs).not.toMatch(/\[\$\{entry\.type\}\]/);
  46. expect(templateJs).toMatch(/\$\{escapeHtml\(msg\.toolName \|\| 'tool'\)\}/);
  47. expect(templateJs).toMatch(/\$\{escapeHtml\(msg\.role\)\}/);
  48. expect(templateJs).toMatch(/\$\{escapeHtml\(entry\.modelId\)\}/);
  49. expect(templateJs).toMatch(/\$\{escapeHtml\(entry\.thinkingLevel\)\}/);
  50. expect(templateJs).toMatch(/\$\{escapeHtml\(entry\.type\)\}/);
  51. });
  52. it("escapes model names in the exported header", () => {
  53. // Assistant message provider/model values are collected from the session and rendered with innerHTML.
  54. expect(templateJs).not.toMatch(/\$\{globalStats\.models\.join\(', '\) \|\| 'unknown'\}/);
  55. expect(templateJs).toMatch(/\$\{escapeHtml\(globalStats\.models\.join\(', '\) \|\| 'unknown'\)\}/);
  56. });
  57. });