/** * CHAT MARKDOWN RENDERER — pure, React-free (extracted from ChatBot.tsx) * * SECURITY INVARIANT (audit run-1, finding chatbot.rendermarkdown.*): * EVERY text segment is HTML-escaped BEFORE any markdown transformation. * Only renderer-synthesized tags (h4/ol/ul/li/p/code/a/strong/em/img) may * appear in the output; the link URL regex additionally restricts hrefs to * http(s), and the ONLY allowed is the SLD diagram image synthesized * from a ```sld fence — a same-origin URL under /api/diagrams/ whose SVG is * produced exclusively by the server-side linted renderer (api/lib/sld/). * Model text NEVER becomes markup: a fence that fails spec validation falls * back to the escaped code block. * The old code escaped only fenced code blocks, letting model/user/history * content like `` reach dangerouslySetInnerHTML. * * Bidi support: any message containing Arabic script renders RTL; Latin runs * inside an RTL paragraph are isolated by the Unicode bidi algorithm; code * blocks and inline code are pinned LTR. Markdown supports: fenced code * blocks, inline code, **bold** (cyan key terms), *italic*, * [links](https://…) (safe anchors), ### headings, numbered step lists, * bullet lists, and paragraphs. */ import { convertMarkdownTables } from "@/lib/chat-format"; export const ARABIC_RE = /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]/; export function escapeHtml(s: string): string { return s .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll("\"", """) .replaceAll("'", "'"); } /** Inline markdown (code / links / bold / italic) — input is already escaped. */ function renderInline(md: string): string { // Inline code first, so ** inside code is never bolded. dir="ltr" isolates // the code run from the surrounding RTL/LTR paragraph. let html = md.replaceAll(/`([^`]+)`/g, '$1'); // Markdown links → safe anchors (documented-answer citations from web // search). URL is restricted to http/https; the input was already // HTML-escaped, so `"` cannot break out of the href attribute. // S8786: both character classes exclude their own delimiter (`]` / `)`, // whitespace and parens), so the match decomposes uniquely - bounded, // linear-time pattern. html = html.replaceAll(/\[([^\]\n]+)\]\((https?:\/\/[^\s<>()]+)\)/g, '$1'); // NOSONAR - S8786: delimiter-exclusive classes, linear // Bold → highlighted key term (the UI colors important words cyan) html = html.replaceAll(/\*\*([^*]+)\*\*/g, '$1'); // Italic html = html.replaceAll(/(?$1"); return html; } /** * Render a non-code text segment: headings, numbered step lists, bullet lists * and paragraphs. Line-based so the model's "1. step" instructions render as * real ordered steps (with a number badge) instead of a wall of text. * Input is already HTML-escaped (enforced by renderMarkdown). */ function renderTextBlock(text: string): string { const lines = text.split("\n"); let out = ""; let mode: "none" | "ol" | "ul" = "none"; let para: string[] = []; const closeList = () => { if (mode === "ol") out += ""; else if (mode === "ul") out += ""; mode = "none"; }; const flushPara = () => { if (para.length) { out += `

${para.join("
")}

`; para = []; } }; for (const rawLine of lines) { const line = rawLine.trim(); // S8786: all three patterns are line-anchored with bounded quantifiers // ({1,6} / {1,2}) on an already-split single line; `\s+` vs `(.+)` overlap // only on spaces and every decomposition yields the same captures. const heading = /^#{1,6}\s+(.+)$/.exec(line); // NOSONAR - S8786: bounded {1,6}, line-anchored const step = /^(\d{1,2})[.)]\s+(.+)$/.exec(line); // NOSONAR - S8786: bounded {1,2}, line-anchored const bullet = /^[-*•]\s+(.+)$/.exec(line); // NOSONAR - S8786: single char + bounded gap, line-anchored if (heading) { flushPara(); closeList(); out += `

${renderInline(heading[1])}

`; } else if (step) { flushPara(); if (mode !== "ol") { closeList(); out += '
    '; mode = "ol"; } out += `
  1. ${renderInline(step[2])}
  2. `; } else if (bullet) { flushPara(); if (mode !== "ul") { closeList(); out += '