//! Minimal safe Markdown renderer for agent-authored text. //! //! Agent output (`assistant` / `plan` / `question` events, pending payloads) //! is untrusted: it may contain raw HTML or `javascript:` links. This module //! parses with `pulldown-cmark` but renders its own HTML subset, so nothing //! reaches the page unescaped: //! //! * emitted tags: `p`, `strong` (also for headings), `em`, `code`, //! `pre>code`, `ul`/`ol`/`li`, `br`, `hr`, `div.md-quote`, and `a[href]` //! for `http(s)` URLs only; //! * raw HTML blocks and inline HTML are ESCAPED and shown as text; //! * links with any other scheme render as plain text (no ``); //! * images render as their alt text (no external fetch under CSP `self`); //! * everything else (tables, footnotes, metadata, …) renders as plain //! escaped text without its wrapper tags. use pulldown_cmark::{Event, Options, Parser, Tag, TagEnd}; /// Escape `&<>"` for HTML text/attribute contexts. fn esc(out: &mut String, s: &str) { for c in s.chars() { match c { '&' => out.push_str("&"), '<' => out.push_str("<"), '>' => out.push_str(">"), '"' => out.push_str("""), _ => out.push(c), } } } /// Only `http://` and `https://` links become ``; anything else /// (`javascript:`, `data:`, relative, …) renders as plain text. fn is_safe_url(url: &str) -> bool { let u = url.trim_start(); if u.chars().any(|c| c.is_control()) { return false; } let lower = u.to_ascii_lowercase(); lower.starts_with("http://") || lower.starts_with("https://") } /// Render the safe subset of `md` to an HTML fragment (already escaped — /// embed with [`maud::PreEscaped`]). pub fn render_markdown(md: &str) -> String { let mut out = String::new(); let parser = Parser::new_ext(md, Options::empty()); // Whether each open `Link` emitted an `` (matters only at `End`). let mut link_stack: Vec = Vec::new(); for ev in parser { match ev { Event::Start(tag) => match tag { Tag::Paragraph => out.push_str("

"), Tag::Heading { .. } => out.push_str("

"), Tag::BlockQuote(_) => out.push_str("

"), Tag::CodeBlock(_) => out.push_str("
"),
                Tag::List(ordered) => {
                    if ordered.is_some() {
                        out.push_str("
    "); } else { out.push_str("
"), TagEnd::CodeBlock => out.push_str(""), TagEnd::List(ordered) => { if ordered { out.push_str(""); } else { out.push_str(""); } } TagEnd::Item => out.push_str(""), TagEnd::Emphasis => out.push_str(""), TagEnd::Strong => out.push_str(""), TagEnd::Link if link_stack.pop().unwrap_or(false) => { out.push_str(""); } _ => {} }, Event::Text(t) => esc(&mut out, &t), Event::Code(t) => { out.push_str(""); esc(&mut out, &t); out.push_str(""); } Event::Html(t) | Event::InlineHtml(t) => esc(&mut out, &t), Event::InlineMath(t) | Event::DisplayMath(t) => esc(&mut out, &t), Event::FootnoteReference(name) => { out.push_str("[^"); esc(&mut out, &name); out.push(']'); } Event::SoftBreak => out.push('\n'), Event::HardBreak => out.push_str("
"), Event::Rule => out.push_str("
"), Event::TaskListMarker(checked) => { out.push_str(if checked { "☑ " } else { "☐ " }); } } } out }