Skip to content

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

Rust

fix(web): block remote markdown image fetches (#491)

Stanislav Cherkasovc2c30594913d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 05e18336..c98ae68b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -239,6 +239,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
read identically whether hooks are broken or a repository simply never opted
in — the exact ambiguity those counters were added to remove (#428, #446).
+### Fixed
+- Prevented stored Markdown from automatically fetching external image URLs
+ when viewed in the web UI, while preserving clickable external links and
+ same-origin relative images (#491).
+
### Docs
- Documented the order of magnitude reported for lifecycle-hook overhead,
including the fact that a completed tool call normally pays for both pre- and
diff --git a/README.md b/README.md
index 4dbd5ee0..d00f2176 100644
--- a/README.md
+++ b/README.md
@@ -772,7 +772,10 @@ Useful entry points:
browser view of the markdown wiki. `--enable-web` also mounts a
read-only JSON frontend API at `/api/v1` (workspaces, projects, pages,
recent, briefing, search) so custom web UIs can read the memory without
- opening SQLite or wiki files directly:
+ opening SQLite or wiki files directly. Rendered pages keep external links
+ clickable, but image sources must be relative or root-relative; absolute
+ external images are neutralized so viewing stored Markdown cannot act as a
+ remote beacon:
```text
GET /api/v1/workspaces
diff --git a/crates/ai-memory-web/src/markdown.rs b/crates/ai-memory-web/src/markdown.rs
index 7737a5cc..9993cb6a 100644
--- a/crates/ai-memory-web/src/markdown.rs
+++ b/crates/ai-memory-web/src/markdown.rs
@@ -258,7 +258,7 @@ fn sanitize_event(event: Event<'_>) -> Event<'_> {
id,
}) => Event::Start(Tag::Image {
link_type,
- dest_url: safe_url(dest_url),
+ dest_url: safe_image_url(dest_url),
title,
id,
}),
@@ -282,6 +282,29 @@ fn safe_url(url: CowStr<'_>) -> CowStr<'_> {
}
}
+/// Allow images only from the current origin.
+///
+/// Unlike links, images are fetched as soon as untrusted markdown is viewed.
+/// An absolute or network-path URL would therefore disclose the viewer's IP,
+/// user agent, and view timing without a click. Keep ordinary external links
+/// clickable, but neutralise every non-relative image target. Backslashes are
+/// rejected because browsers can interpret them as URL path separators.
+fn safe_image_url(url: CowStr<'_>) -> CowStr<'_> {
+ let trimmed = url.trim_start();
+ let is_network_path = trimmed.starts_with("//")
+ || trimmed.starts_with("\\\\")
+ || trimmed.starts_with("/\\")
+ || trimmed.starts_with("\\/");
+ let is_relative =
+ trimmed.starts_with('/') || trimmed.starts_with('#') || !trimmed.contains(':');
+
+ if !trimmed.is_empty() && is_relative && !is_network_path && !trimmed.contains('\\') {
+ url
+ } else {
+ CowStr::Boxed("#".into())
+ }
+}
+
/// True when `target` carries a URL scheme that should never be
/// resolved as a wiki page. Two paths feed this rule:
/// * the wikilink rewriter, which falls back to literal `[[…]]`;
@@ -388,6 +411,49 @@ mod tests {
assert!(!html.contains("javascript:"));
}
+ #[test]
+ fn preserves_clickable_external_links() {
+ let html = render("[docs](https://example.com/docs)", "default", "scratch");
+ assert!(html.contains(r#"href="https://example.com/docs""#));
+ }
+
+ #[test]
+ fn neutralises_external_images_without_hiding_alt_text() {
+ for target in [
+ "https://example.invalid/pixel.png",
+ "http://example.invalid/pixel.png",
+ "//example.invalid/pixel.png",
+ r"\\example.invalid\pixel.png",
+ r"/\example.invalid/pixel.png",
+ "data:image/png;base64,AA==",
+ ] {
+ let markdown = format!("![remote pixel]({target})");
+ let html = render(&markdown, "default", "scratch");
+ assert!(html.contains("src=\"#\""), "target {target}: {html}");
+ assert!(
+ html.contains("alt=\"remote pixel\""),
+ "target {target}: {html}"
+ );
+ assert!(!html.contains("example.invalid"), "target {target}: {html}");
+ }
+ }
+
+ #[test]
+ fn preserves_relative_images() {
+ for target in [
+ "images/diagram.png",
+ "../shared/diagram.png",
+ "/static/logo.svg",
+ ] {
+ let markdown = format!("![local]({target})");
+ let html = render(&markdown, "default", "scratch");
+ assert!(
+ html.contains(&format!(r#"src="{target}""#)),
+ "target {target}: {html}"
+ );
+ }
+ }
+
#[test]
fn escapes_search_snippet_but_keeps_marks() {
let out = escape_snippet("<script>x</script> <mark>hit</mark>");