Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(web): adversarial heading raw-HTML escape test + changelog ref (#1105)

maintainer66255d4c06e1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 80e2a11b..4d008d82 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -49,7 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed the web page view omitting heading IDs, preventing `#anchor` fragments
and section wikilinks from scrolling to target headings. The Markdown renderer
now emits unique, slugified `id` attributes on heading elements (`<h1>`–`<h6>`),
- disambiguating duplicate headings and stripping punctuation.
+ disambiguating duplicate headings and stripping punctuation. (#1105)
- Fixed `memory_handoff_list` MCP tool calls being counted under writes in
`client_activity`: the read-only inspection tool is now classified as a read
in `tool_call_is_write`. (#1088)
diff --git a/crates/ai-memory-web/src/markdown.rs b/crates/ai-memory-web/src/markdown.rs
index 6c4d3ef1..886a1fe8 100644
--- a/crates/ai-memory-web/src/markdown.rs
+++ b/crates/ai-memory-web/src/markdown.rs
@@ -1232,6 +1232,34 @@ mod tests {
assert!(html.contains(r#"<h2 id="section-1">!!!</h2>"#), "{html}");
}
+ #[test]
+ fn heading_with_raw_html_and_quotes_escapes_markup_and_keeps_id_slug_safe() {
+ let md = "## <img src=x onerror=alert(1)> \"q\"";
+ let html = render(md, "default", "scratch");
+ assert!(
+ !html.contains("<img"),
+ "raw heading HTML must not survive as markup: {html}"
+ );
+ // Smart punctuation renders the quotes as “q”; the HTML payload must
+ // stay escaped text inside the heading.
+ assert!(
+ html.contains(r#"<h2 id="q">&lt;img src=x onerror=alert(1)&gt; “q”</h2>"#),
+ "the heading must render escaped text with a slug id: {html}"
+ );
+ let id = html
+ .split("id=\"")
+ .nth(1)
+ .and_then(|rest| rest.split('"').next())
+ .unwrap_or_else(|| panic!("no heading id emitted: {html}"));
+ assert!(
+ !id.is_empty()
+ && id
+ .chars()
+ .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_' || c == '-'),
+ "heading id must match ^[a-z0-9_-]+$, got: {id}"
+ );
+ }
+
#[test]
fn wikilink_anchor_matches_generated_heading_id() {
let md = "Jump to [[notes/foo#my-section|My Section]]\n\n## My Section";