/** * REGRESSION TESTS — audit run-1 fixes (chatbot.rendermarkdown.* XSS) * * The chat renderer used to escape only fenced code blocks, letting * model/user/history content like `` reach * dangerouslySetInnerHTML. The fix escapes every text segment BEFORE the * markdown machine runs. These tests lock that invariant. */ import { describe, it, expect } from "vitest"; import { renderMarkdown, escapeHtml, ARABIC_RE, detectUnfulfilledDiagramPromise } from "./chat-render"; describe("renderMarkdown — XSS regression (audit run-1)", () => { it("never emits a raw tag from message content", () => { const out = renderMarkdown(""); expect(out).not.toContain(" tag from message content", () => { const out = renderMarkdown(""); expect(out.toLowerCase()).not.toContain(" { const out = renderMarkdown('[x](https://a"onmouseover="alert(1))'); expect(out).not.toContain('"onmouseover'); }); it("refuses javascript: link schemes", () => { const out = renderMarkdown("[x](javascript:alert(1))"); // The text stays inert escaped prose — the security property is that no // anchor with a javascript: href is ever synthesized. expect(out).not.toContain('href="javascript'); expect(out).not.toContain(" { const out = renderMarkdown("### عنوان\n1. افتح قائمة **Analyse**\n- نقطة أولى\nمائل *خفيف* هنا\n`code`"); expect(out).toContain('class="chat-h"'); expect(out).toContain('
  • '); expect(out).toContain(" { const out = renderMarkdown("```\n\n```"); expect(out).toContain("<img"); expect(out).not.toContain(" { const src = ["| a | b |", "| --- | --- |", "| | y |"].join("\n"); const out = renderMarkdown(src); expect(out).not.toContain(""); expect(out).toContain("<b>"); }); it("unterminated fence mid-stream is closed, not leaked", () => { const out = renderMarkdown("```\npartial"); expect(out).toContain(" { it("escapes the five dangerous characters", () => { expect(escapeHtml(`<>&"'`)).toBe("<>&"'"); }); }); describe("ARABIC_RE", () => { it("detects Arabic script for RTL direction", () => { expect(ARABIC_RE.test("مرحبا")).toBe(true); expect(ARABIC_RE.test("hello")).toBe(false); }); }); describe("renderMarkdown — SLD diagram fences", () => { const GOOD = JSON.stringify({ title: "شبكة 11kV", elements: [ { id: "G1", type: "grid", x: 2, y: 0 }, { id: "B1", type: "busbar", x: 1, y: 2, len: 3 }, ], connections: [["G1", "B1"]], }); it("renders a valid sld fence as a same-origin diagram image", () => { const out = renderMarkdown(`قبل\n\`\`\`sld\n${GOOD}\n\`\`\`\nبعدها`); expect(out).toContain(` { const out = renderMarkdown("```sld\n{not json at all\n```"); expect(out).toContain(" { const bad = JSON.stringify({ elements: [{ id: "G1", type: "grid", x: 0, y: 0 }], connections: [["G1", "GHOST"]], }); const out = renderMarkdown(`\`\`\`sld\n${bad}\n\`\`\``); expect(out).toContain(" { const evil = JSON.stringify({ title: '', elements: [{ id: "A1", type: "grid", x: 0, y: 0 }], connections: [] }).replace(/"); expect(out).toContain("<script>"); } else { expect(out).toContain(" { const GOOD = JSON.stringify({ title: "شبكة 11kV", elements: [ { id: "G1", type: "grid", x: 2, y: 0 }, { id: "B1", type: "busbar", x: 1, y: 2, len: 3 }, ], connections: [["G1", "B1"]], }); it("happy: Arabic promise with no board → chip shows", () => { expect(detectUnfulfilledDiagramPromise("حاضر، سأرسم لك مخطط الشبكة.")).toBe(true); expect(detectUnfulfilledDiagramPromise("تمام، هجهز لك المخطط في الرد الجاي.")).toBe(true); expect(detectUnfulfilledDiagramPromise("سأقوم برسم مخطط تدفق الأحمال الآن.")).toBe(true); }); it("happy: English first-person promise with no board → chip shows", () => { expect(detectUnfulfilledDiagramPromise("I'll draw the diagram for you.")).toBe(true); expect(detectUnfulfilledDiagramPromise("Let me sketch the single-line diagram.")).toBe(true); expect(detectUnfulfilledDiagramPromise("I can generate an SLD for this network.")).toBe(true); }); it("happy: bare offer instead of drawing → chip shows", () => { expect(detectUnfulfilledDiagramPromise("تحب أن أرسمه لك؟")).toBe(true); expect(detectUnfulfilledDiagramPromise("أرسمه لك في خطوات؟")).toBe(true); }); it("happy: delivery wording over a broken fence → chip shows", () => { expect(detectUnfulfilledDiagramPromise("إليك المخطط:\n```sld\n{not json\n```")).toBe(true); expect(detectUnfulfilledDiagramPromise("Here's the diagram:\n```sld\n{oops\n```")).toBe(true); }); it("happy: past-tense delivery claims with no board → chip shows (observed live)", () => { expect(detectUnfulfilledDiagramPromise("جهزت لك المخطط.")).toBe(true); expect(detectUnfulfilledDiagramPromise("حضرت المخطط التالي:")).toBe(true); expect(detectUnfulfilledDiagramPromise("رسمت المخطط بناءً على طلبك.")).toBe(true); expect(detectUnfulfilledDiagramPromise("The following diagram shows the topology:")).toBe(true); }); it("edge: promise wording AROUND a valid board → no chip (diagram delivered)", () => { const withBoard = `سأرسم لك المخطط:\n\`\`\`sld\n${GOOD}\n\`\`\`\nافترضت: جهد شبكة 66kV.`; expect(detectUnfulfilledDiagramPromise(withBoard)).toBe(false); }); it("edge: token-cap cut right after the promise sentence → chip shows", () => { expect(detectUnfulfilledDiagramPromise("سأرسم لك مخططاً يوضح")).toBe(true); }); it("edge: teaching phrasing is NOT a promise → no chip", () => { expect(detectUnfulfilledDiagramPromise("لرسم المخطط في ETAP: افتح قائمة Transient Analysis ثم اختر...")).toBe(false); expect(detectUnfulfilledDiagramPromise("I'll show you how to draw the diagram in ETAP step by step.")).toBe(false); }); it("edge: user-echo imperative is NOT a promise → no chip", () => { expect(detectUnfulfilledDiagramPromise("اكتب: ارسم المخطط ثم اضغط Enter.")).toBe(false); expect(detectUnfulfilledDiagramPromise("الرسمة تحتاج بيانات الجهد أولاً.")).toBe(false); }); it("failure mode: broken sld fence + no promise wording → no chip (degrades to code block silently)", () => { expect(detectUnfulfilledDiagramPromise("```sld\n{broken\n```")).toBe(false); }); it("failure mode: empty/oversized input never throws or matches", () => { expect(detectUnfulfilledDiagramPromise("")).toBe(false); expect(detectUnfulfilledDiagramPromise("x".repeat(21_000) + "سأرسم")).toBe(false); }); it("invariant: detection agrees with the renderer on what counts as a board", () => { // Whatever renderMarkdown turns into an must // never trigger the chip — same shape check on both sides. const out = renderMarkdown(`\`\`\`sld\n${GOOD}\n\`\`\``); expect(out).toContain("/api/diagrams/sld"); expect(detectUnfulfilledDiagramPromise(`\`\`\`sld\n${GOOD}\n\`\`\``)).toBe(false); }); }); describe("renderMarkdown — docgen file-export fences", () => { const GOOD = JSON.stringify({ format: "pdf", filename: "cable-selection-report", title: "تقرير اختيار كابل التغذية", sections: [ { heading: "الملخص التنفيذي", paragraphs: ["اختيار 70 مم² بعد التحقق."], bullets: ["الأمبيراسية: 187 أ"] }, ], citations: ["Elsewedy Power Cables 2015 — ص 12"], }); it("renders a valid docgen fence as a download card with base64 payload", () => { const out = renderMarkdown(`قبل\n\`\`\`docgen\n${GOOD}\n\`\`\`\nبعدها`); expect(out).toContain('class="docgen-card" data-docgen="'); expect(out).toContain("data-docgen-dl"); expect(out).toContain("تحميل الملف"); expect(out).toContain("cable-selection-report.pdf"); expect(out).not.toContain(" ch.charCodeAt(0)))); expect(payload.format).toBe("pdf"); expect(payload.spec.title).toBe("تقرير اختيار كابل التغذية"); }); it("defaults the format to pdf when the spec omits it", () => { const noFormat = JSON.stringify({ title: "Doc", filename: "doc", sections: [{ heading: "h" }] }); const out = renderMarkdown(`\`\`\`docgen\n${noFormat}\n\`\`\``); expect(out).toContain("doc.pdf"); }); it("falls back to an escaped code block when the fence is malformed", () => { const out = renderMarkdown("```docgen\n{broken json\n```"); expect(out).toContain(" { const bad = JSON.stringify({ filename: "x", sections: [] }); const out = renderMarkdown(`\`\`\`docgen\n${bad}\n\`\`\``); expect(out).toContain(" { const evil = JSON.stringify({ title: '', filename: "x", sections: [{ heading: "" }], }); const out = renderMarkdown(`\`\`\`docgen\n${evil}\n\`\`\``); if (out.includes("docgen-card")) { expect(out).not.toContain("