Spaces:
Running

AHMDRTAP / src /lib /chat-render.ts
Z User
fix(sonar): wave 7 - cleanup round from live scan (93 findings across 65 files)
6780540
Raw History Blame Contribute Delete
18.9 kB
/**
* 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 <img> 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 `<img src=x onerror=...>` 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("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll("\"", "&quot;")
.replaceAll("'", "&#x27;");
}
/** 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, '<code dir="ltr" class="bg-black/30 border border-[#1e2d3d] px-1.5 py-0.5 rounded text-[12px] text-cyan-300">$1</code>');
// 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, '<a href="$2" target="_blank" rel="noopener noreferrer" class="text-cyan-300 underline decoration-cyan-300/40 hover:decoration-cyan-200 break-all" dir="ltr">$1</a>'); // NOSONAR - S8786: delimiter-exclusive classes, linear
// Bold → highlighted key term (the UI colors important words cyan)
html = html.replaceAll(/\*\*([^*]+)\*\*/g, '<strong class="font-semibold text-cyan-300">$1</strong>');
// Italic
html = html.replaceAll(/(?<!\*)\*([^*]+)\*(?!\*)/g, "<em>$1</em>");
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 += "</ol>";
else if (mode === "ul") out += "</ul>";
mode = "none";
};
const flushPara = () => {
if (para.length) {
out += `<p>${para.join("<br>")}</p>`;
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 += `<h4 class="chat-h">${renderInline(heading[1])}</h4>`;
} else if (step) {
flushPara();
if (mode !== "ol") {
closeList();
out += '<ol class="chat-ol">';
mode = "ol";
}
out += `<li data-n="${step[1]}">${renderInline(step[2])}</li>`;
} else if (bullet) {
flushPara();
if (mode !== "ul") {
closeList();
out += '<ul class="chat-ul">';
mode = "ul";
}
out += `<li>${renderInline(bullet[1])}</li>`;
} else if (!line) {
flushPara();
closeList();
} else {
closeList();
para.push(renderInline(line));
}
}
flushPara();
closeList();
return out;
}
/**
* SLD fence → same-origin diagram image. The spec is re-validated on the
* server (lint + render); client-side we only do a cheap shape check so a
* malformed fence degrades to a plain code block instead of a broken image.
* Returns null whenever anything is off — the caller keeps the code block.
*/
function renderSldFence(rawCode: string): string | null {
try {
const spec = JSON.parse(rawCode) as { title?: unknown; elements?: Array<{ id?: unknown; type?: unknown }>; connections?: Array<[unknown, unknown]> } | null;
if (!spec || typeof spec !== "object") return null;
const elements = Array.isArray(spec.elements) ? spec.elements : [];
if (elements.length === 0 || elements.length > 40) return null;
// S6551: coerce ids/keys explicitly - non-primitive ids are treated as
// missing (fail the cheap shape check) instead of stringifying to
// "[object Object]". The server re-validates the full spec anyway.
const keyOf = (v: unknown): string => {
if (typeof v === "string") return v;
return typeof v === "number" && Number.isFinite(v) ? String(v) : "";
};
const ids = new Set(elements.map((e) => keyOf(e?.id)));
if (elements.some((e) => !ids.has(keyOf(e?.id)) || !keyOf(e?.type))) return null;
const conns = Array.isArray(spec.connections) ? spec.connections : [];
if (conns.some(([a, b]) => !ids.has(keyOf(a)) || !ids.has(keyOf(b)))) return null;
// v = renderer revision — busts stale browser/edge caches when the SLD
// rendering engine itself changes (labels, geometry, themes).
const url = `/api/diagrams/sld?spec=${encodeURIComponent(rawCode)}&v=4`;
const title = typeof spec.title === "string" && spec.title.trim() ? spec.title.trim() : "Single-line diagram";
return `<img src="${escapeHtml(url)}" alt="${escapeHtml(title)}" loading="lazy" decoding="async" class="max-w-full h-auto rounded-lg border border-[#1e2d3d] my-1.5" dir="ltr"/>`;
} catch {
return null;
}
}
// ─── Docgen fence → one-tap file-download card (PDF / DOCX / MD) ─────────────
const DOCGEN_FORMATS = new Set(["pdf", "docx", "md"]);
/** S3358: badge lookup table (replaces the nested ternary chain). */
const DOCGEN_BADGE: Record<string, string> = { pdf: "PDF", docx: "DOCX", md: "MD" };
/** UTF-8-safe base64 (btoa is latin-1 only; chunked to avoid stack overflow). */
function utf8ToBase64(text: string): string {
const bytes = new TextEncoder().encode(text);
let bin = "";
const CHUNK = 0x8000;
for (let i = 0; i < bytes.length; i += CHUNK) {
bin += String.fromCodePoint(...bytes.subarray(i, i + CHUNK));
}
return btoa(bin);
}
/**
* Cheap shape check + card HTML for a ```docgen fence. The spec travels
* base64-encoded in a data attribute; the click handler (ChatBot.tsx, event
* delegation) POSTs it to /api/docs/gen and triggers a blob download. The
* server re-validates everything — here we only guarantee the card renders
* for specs that look right, and a malformed fence degrades to a code block.
*/
type DocgenSpec = { format?: unknown; filename?: unknown; title?: unknown; sections?: unknown };
/** S3358 helper: normalize the requested format against the allow-list. */
function docgenFormat(spec: DocgenSpec): string {
return typeof spec.format === "string" && DOCGEN_FORMATS.has(spec.format) ? spec.format : "pdf";
}
/** S8786/S3358 helper: sanitize the filename into a safe download slug.
* The edge-dash strip is a single quantifier over one character (-+$) —
* anchored, no nesting, provably linear (Sonar S8786). */
function docgenFilename(spec: DocgenSpec): string {
if (typeof spec.filename !== "string" || !spec.filename.trim()) return "elbaz-document";
const slug = spec.filename
.trim()
.replace(/[^A-Za-z0-9-_]+/g, "-")
.replace(/^-+/, "")
.replace(/-+$/, "") // NOSONAR — S8786: bounded single-quantifier pattern (anchored char run), provably linear
.toLowerCase();
return slug || "elbaz-document";
}
function renderDocgenFence(rawCode: string): string | null {
try {
const spec = JSON.parse(rawCode) as DocgenSpec | null;
if (!spec || typeof spec !== "object") return null;
const format = docgenFormat(spec);
const title = typeof spec.title === "string" && spec.title.trim() ? spec.title.trim().slice(0, 160) : "";
const filename = docgenFilename(spec);
const sections = Array.isArray(spec.sections) ? spec.sections : [];
if (!title || sections.length === 0 || sections.length > 30) return null;
const fileLabel = `${filename}.${format}`;
const badge = DOCGEN_BADGE[format] ?? "PDF";
const isAr = ARABIC_RE.test(title);
const label = isAr ? "تحميل الملف" : "Download file";
const meta = isAr ? "مُولَّد داخل المنصة — يُبنى من مواصفات متحقق منها" : "Generated in-platform from a verified spec";
const payload = utf8ToBase64(JSON.stringify({ spec, format }));
return (
`<div class="docgen-card" data-docgen="${payload}" dir="${isAr ? "rtl" : "ltr"}" style="display:flex;align-items:center;gap:12px;flex-wrap:wrap;margin:8px 0;padding:12px 14px;border:1px solid #1e2d3d;border-radius:12px;background:linear-gradient(135deg,rgba(14,116,144,.12),rgba(30,45,61,.35))">` +
`<span style="flex:none;display:inline-flex;align-items:center;justify-content:center;min-width:44px;height:32px;padding:0 8px;border-radius:8px;background:#0e7490;color:#fff;font-size:11px;font-weight:700;letter-spacing:.5px" dir="ltr">${badge}</span>` +
`<span style="flex:1;min-width:180px">` +
`<span style="display:block;color:#e6f1ff;font-size:13.5px;font-weight:600">${escapeHtml(title)}</span>` +
`<span style="display:block;color:#8fa8c7;font-size:11px;margin-top:2px" dir="ltr">${escapeHtml(fileLabel)}</span>` +
`<span style="display:block;color:#64798f;font-size:10.5px;margin-top:2px">${escapeHtml(meta)}</span>` +
`</span>` +
`<button type="button" data-docgen-dl style="flex:none;cursor:pointer;border:1px solid #22d3ee;background:rgba(34,211,238,.12);color:#67e8f9;font-size:13px;font-weight:600;padding:8px 16px;border-radius:10px;transition:background .15s">${escapeHtml(label)}</button>` +
`</div>`
);
} catch {
return null;
}
}
/**
* ─── Unfulfilled diagram-promise detector (pure, React-free) ────────────────
* Powers the "📐 ارسمها دلوقتي" chip: when an assistant reply PROMISES a
* diagram (or offers/announces one) but the message carries NO valid SLD
* board, the chip offers a one-tap follow-up instead of making the user
* retype their request. Real failure modes this covers (observed live):
* - the stream was cut by the output-token cap right after "سأرسم لك مخططاً…"
* - the model emitted a broken ```sld fence (fails the shape check → code block)
* - the model merely offered ("تحب أرسمه لك؟") instead of drawing.
* A message that DID render a diagram never shows the chip, no matter how
* many promise-shaped words surround the board.
*/
/** Same fence grammar as renderMarkdown; true when any ```sld fence passes the
* same cheap shape check the renderer uses (i.e. it WILL show as a diagram). */
function hasValidSldFence(text: string): boolean {
// split yields [text, lang, code, text, lang, code, …] — starting the loop
// at the FIRST lang index (1), the matching code block sits at i+1.
const segments = text.split(/```(\w*)[ \t]*\n?([\s\S]*?)```/); // NOSONAR — S8786: same bounded grammar as renderMarkdown
for (let i = 1; i < segments.length; i += 3) {
if (segments[i] === "sld" && renderSldFence((segments[i + 1] ?? "").trim())) return true;
}
return false;
}
// Arabic: affirmative promises/offers around drawing. Deliberately does NOT
// match the bare imperative "ارسم" (that is the USER's ask, quoted back) nor
// teaching phrases like "لرسم المخطط في ETAP" — only first-person commitments.
const DIAGRAM_PROMISE_AR_RE: RegExp[] = [
/س[أا]رسم/,
/ه[أا]رسم/,
/سوف\s+أرسم/,
/دعني\s+أرسم/,
/دعيني\s+أرسم/,
/أن\s+أرسم/,
/أرسم\s+(?:لك|لكم|ه|ها|هو|هم)/,
/أرسم(?:ه|ها|هم)\s+لك/,
/سأقوم\s+برسم/,
/سوف\s+أقوم\s+برسم/,
/أقوم\s+برسم\s+(?:المخطط|مخطط|الرسمة|رسمة)/,
/جاري\s+رسم/,
/سأجهز\s+(?:لك\s+)?(?:المخطط|مخطط|الرسمة|رسمة)/,
/هجهز\s+(?:لك\s+)?(?:المخطط|مخطط|الرسمة|رسمة)/,
/هعمل\s+(?:لك\s+)?(?:المخطط|مخطط|الرسمة|رسمة)/,
/سأرسل\s+(?:لك\s+)?(?:المخطط|مخطط|الرسمة|رسمة)/,
/سأرفق\s+(?:المخطط|مخطط|الرسمة|رسمة)/,
];
// Arabic: "delivery" wording with no board behind it ("إليك المخطط:" over a
// broken fence, or a past-tense delivery claim "جهزت لك المخطط" that never
// shipped one — both observed live).
const DIAGRAM_DELIVERY_AR_RE: RegExp[] = [
/إليك\s+(?:المخطط|مخطط|الرسمة|رسمة)/,
/هذا\s+هو\s+(?:المخطط|الرسمة)/,
/دي\s+(?:الرسمة|المخطط)/,
/المخطط\s+(?:تحت|اللي\s+فوق|التالي|الآتي)/,
/(?:حضرت|جهزت|عملت|رسمت|أرفقت)\s+(?:لكم\s+|لك\s+)?(?:المخطط|مخطط|الرسمة|رسمة)/,
];
// English: first-person draw commitments ("I'll draw the diagram", "let me
// sketch the SLD"). The ≤3-word gap skips teaching phrasing like
// "I'll show you how to draw…" while still catching "I'll happily draw…".
// S5843: split into lead + action, and the ACTION into head + target so each
// regex stays far below the complexity cap while reproducing the single-regex
// semantics: the head is anchored at the slice start (same lead, same <=3-word
// gap, same verb list), and the target is anchored at the head's end (same
// 60-char window, same target list).
const DIAGRAM_PROMISE_EN_LEAD_RE =
/\b(?:i'?ll|i will|i can|i'?m|i am|let me|allow me to|i'?m going to|i am going to)\b/gi;
const DIAGRAM_PROMISE_EN_ACTION_HEAD_RE =
/^\s+(?:\w+\s+){0,3}?(?:draw|sketch|create|generate|make|build|prepare|drawing)\b/i;
const DIAGRAM_PROMISE_EN_ACTION_TARGET_RE =
/^[^\n]{0,60}?\b(?:diagram|sld|single[- ]line|one[- ]line|schematic|drawing)\b/i;
// English: "here's the diagram" / "the following diagram" with no board behind it.
const DIAGRAM_DELIVERY_EN_RES: RegExp[] = [
/\bhere(?:'s| is)(?:\s+the|\s+your)?\s+(?:diagram|sld|schematic)/i,
/\bdiagram\s+(?:below|here)\b/i,
/\bthe\s+following\s+(?:diagram|sld|schematic)\b/i,
];
/**
* True when this assistant message promised/announced a diagram but the
* rendered result contains NO valid SLD board (→ show the draw-now chip).
*/
export function detectUnfulfilledDiagramPromise(text: string): boolean {
if (!text || text.length > 20_000) return false;
if (hasValidSldFence(text)) return false;
const promisedAr = DIAGRAM_PROMISE_AR_RE.some((re) => re.test(text));
// matchAll (not .test) with /g: avoids lastIndex state and gives us each
// lead position to test the anchored action window against its own slice.
const promisedEn = [...text.matchAll(DIAGRAM_PROMISE_EN_LEAD_RE)].some((m) => {
const rest = text.slice((m.index ?? 0) + m[0].length);
const action = DIAGRAM_PROMISE_EN_ACTION_HEAD_RE.exec(rest);
return action !== null && DIAGRAM_PROMISE_EN_ACTION_TARGET_RE.test(rest.slice(action[0].length));
});
const deliveredAr = DIAGRAM_DELIVERY_AR_RE.some((re) => re.test(text));
const deliveredEn = DIAGRAM_DELIVERY_EN_RES.some((re) => re.test(text));
return promisedAr || promisedEn || deliveredAr || deliveredEn;
}
/**
* Render a chat message to safe HTML. Text segments are escaped BEFORE the
* markdown machine runs (the security invariant above); fenced code blocks
* are escaped verbatim. While streaming, a momentarily unclosed fence is
* closed so partial code renders as a block instead of leaking backticks.
*/
/** S3776 helper: render one fenced block, or null to fall back to the code block. */
function renderFenced(lang: string, raw: string): string | null {
if (lang === "sld") {
const img = renderSldFence(raw);
if (img) return img;
}
if (lang === "docgen") {
const card = renderDocgenFence(raw);
if (card) return card;
}
return null;
}
export function renderMarkdown(text: string): string {
let src = text;
const fenceCount = (src.match(/```/g) || []).length;
if (fenceCount % 2 === 1) src += "\n```";
// S8786: regex is intentionally bounded; `[\s\S]*?` is the canonical
// non-backtracking pattern for delimited multiline captures.
const segments = src.split(/```(\w*)[ \t]*\n?([\s\S]*?)```/); // NOSONAR — S8786: bounded lazy capture between ``` delimiters
// segments: [text, lang, code, text, lang, code, …, text]
let html = "";
for (let i = 0; i < segments.length; i += 3) {
// Tables are converted on the raw text (pipe grammar), then the whole
// segment is HTML-escaped before the line machine sees it — the audit
// fix for the unescaped-innerHTML XSS.
html += renderTextBlock(escapeHtml(convertMarkdownTables(segments[i])));
if (segments[i + 2] !== undefined) {
const lang = segments[i + 1];
const raw = segments[i + 2].trim();
const fenced = renderFenced(lang, raw);
if (fenced) {
html += fenced;
continue;
}
const code = escapeHtml(raw);
html += `<pre dir="ltr" class="bg-black/40 border border-[#1e2d3d] rounded-lg p-2.5 my-1.5 overflow-x-auto text-[12px] leading-5 text-[#b4c6e0]" style="text-align:left"><code>${code}</code></pre>`;
}
}
return html;
}