fix(webapp): block remote images in model-authored markdown
Model-authored markdown could emit a remote image whose URL the browser fetches on render — a zero-click data beacon. Constrain the shared markdown renderer with a urlTransform that strips the src of any non-local image and limits links to safe schemes.
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
---
|
||||
area: webapp
|
||||
type: fix
|
||||
---
|
||||
|
||||
Images in AI and agent responses are no longer loaded from arbitrary remote websites, closing a way a response could quietly signal an outside server.
|
||||
@@ -0,0 +1,44 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { restrictModelUrls } from "./StreamdownRenderer";
|
||||
|
||||
// streamdown calls urlTransform(url, key, node) to compute each url attribute; a
|
||||
// returned undefined removes the attribute, so no request is ever issued.
|
||||
const img = { tagName: "img" } as any;
|
||||
const link = { tagName: "a" } as any;
|
||||
|
||||
describe("restrictModelUrls (image src)", () => {
|
||||
it("drops a remote model-authored image (the favicon beacon)", () => {
|
||||
expect(
|
||||
restrictModelUrls("https://www.google.com/s2/favicons?domain=evil", "src", img)
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("drops any absolute or protocol-relative remote image", () => {
|
||||
expect(restrictModelUrls("http://evil.tld/pixel.gif", "src", img)).toBeUndefined();
|
||||
expect(restrictModelUrls("//evil.tld/pixel.gif", "src", img)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("keeps inline and same-origin images", () => {
|
||||
expect(restrictModelUrls("data:image/png;base64,AAAA", "src", img)).toBe(
|
||||
"data:image/png;base64,AAAA"
|
||||
);
|
||||
expect(restrictModelUrls("blob:abc", "src", img)).toBe("blob:abc");
|
||||
expect(restrictModelUrls("/local/pic.png", "src", img)).toBe("/local/pic.png");
|
||||
});
|
||||
});
|
||||
|
||||
describe("restrictModelUrls (link href)", () => {
|
||||
it("keeps http(s), mailto and relative links", () => {
|
||||
expect(restrictModelUrls("https://trigger.dev/docs", "href", link)).toBe(
|
||||
"https://trigger.dev/docs"
|
||||
);
|
||||
expect(restrictModelUrls("http://example.com", "href", link)).toBe("http://example.com");
|
||||
expect(restrictModelUrls("mailto:hi@trigger.dev", "href", link)).toBe("mailto:hi@trigger.dev");
|
||||
expect(restrictModelUrls("/runs/123", "href", link)).toBe("/runs/123");
|
||||
});
|
||||
|
||||
it("drops unsafe link schemes", () => {
|
||||
expect(restrictModelUrls("javascript:alert(1)", "href", link)).toBeUndefined();
|
||||
expect(restrictModelUrls("data:text/html,<script>", "href", link)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,32 @@
|
||||
import { lazy } from "react";
|
||||
import type { CodeHighlighterPlugin } from "streamdown";
|
||||
import type { CodeHighlighterPlugin, UrlTransform } from "streamdown";
|
||||
|
||||
const SAFE_LINK_SCHEMES = new Set(["http:", "https:", "mailto:"]);
|
||||
|
||||
/**
|
||||
* URL policy for model-authored markdown. A remote image is fetched the moment it
|
||||
* renders — no click — so it is a zero-click data beacon; we drop the src of any
|
||||
* non-local image. Links stay clickable but only for safe, human-followable schemes.
|
||||
* streamdown removes an attribute whose transform returns undefined, so no request fires.
|
||||
*/
|
||||
export const restrictModelUrls: UrlTransform = (url, key, node) => {
|
||||
const value = url.trim();
|
||||
const isImage = node.tagName === "img" || key === "src" || key === "srcset";
|
||||
|
||||
if (isImage) {
|
||||
// Inline images carry their own bytes; a relative path resolves to our own origin.
|
||||
if (/^data:/i.test(value) || /^blob:/i.test(value)) return url;
|
||||
// Absolute or protocol-relative means a remote host — strip it so nothing is fetched.
|
||||
if (/^[a-z][a-z0-9+.-]*:/i.test(value) || value.startsWith("//")) return undefined;
|
||||
return url;
|
||||
}
|
||||
|
||||
// Links: relative and protocol-relative are fine; otherwise require a safe scheme.
|
||||
if (value.startsWith("//")) return url;
|
||||
const schemeMatch = /^([a-z][a-z0-9+.-]*):/i.exec(value);
|
||||
if (!schemeMatch) return url;
|
||||
return SAFE_LINK_SCHEMES.has(`${schemeMatch[1].toLowerCase()}:`) ? url : undefined;
|
||||
};
|
||||
|
||||
export const StreamdownRenderer = lazy(() =>
|
||||
Promise.all([import("streamdown"), import("@streamdown/code"), import("./shikiTheme")]).then(
|
||||
@@ -23,6 +50,7 @@ export const StreamdownRenderer = lazy(() =>
|
||||
isAnimating={isAnimating}
|
||||
plugins={{ code: codePlugin }}
|
||||
controls={{ code: { copy: false, download: false } }}
|
||||
urlTransform={restrictModelUrls}
|
||||
linkSafety={{ enabled: false }}
|
||||
>
|
||||
{children}
|
||||
|
||||
@@ -18,6 +18,7 @@ export default defineConfig({
|
||||
"app/runEngine/concerns/**/*.test.ts",
|
||||
"app/runEngine/services/**/*.test.ts",
|
||||
"app/utils/**/*.test.ts",
|
||||
"app/components/code/**/*.test.ts",
|
||||
"app/components/dashboard-agent/**/*.test.ts",
|
||||
"app/presenters/v3/reports/**/*.test.ts",
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user