🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(trace): tighten data-URI detection so ordinary strings aren't mistaken for media (LFE-10990) (#15156)
Nikita Kabardin181967817814
diff --git a/web/src/components/ui/media/mediaUtils.clienttest.ts b/web/src/components/ui/media/mediaUtils.clienttest.tsindex 7584be4cd9..e7a3299bfd 100644--- a/web/src/components/ui/media/mediaUtils.clienttest.ts+++ b/web/src/components/ui/media/mediaUtils.clienttest.ts@@ -26,8 +26,99 @@ describe("classifyMediaValue", () => {});});+ it("classifies a multi-megabyte base64 data URI (the huge-IO case)", () => {+ // One unbroken 8 MB base64 token — the payload shape from LFE-10152 seeding.+ const src = "data:image/png;base64," + "A".repeat(8 * 1024 * 1024);+ const descriptor = classifyMediaValue(src);+ expect(descriptor).toMatchObject({+ kind: "dataUri",+ contentType: "image/png",+ });+ // Whole payload is preserved as the src; only the head is scanned.+ expect(descriptor?.kind === "dataUri" && descriptor.src).toBe(src);+ });++ it("classifies genuine data URIs with header variants", () => {+ // Percent-encoded (non-base64) SVG image.+ expect(+ classifyMediaValue("data:image/svg+xml,%3Csvg%3E%3C/svg%3E")?.contentType,+ ).toBe("image/svg+xml");+ // A parameter (charset) before the mandatory comma.+ expect(+ classifyMediaValue("data:image/svg+xml;charset=utf-8,<svg></svg>")+ ?.contentType,+ ).toBe("image/svg+xml");+ // Audio + video top-level types are previewable too.+ expect(classifyMediaValue("data:audio/mpeg;base64,AAAA")?.contentType).toBe(+ "audio/mpeg",+ );+ expect(classifyMediaValue("data:video/mp4;base64,AAAA")?.contentType).toBe(+ "video/mp4",+ );+ // Empty payload is still a well-formed (comma-terminated) data URI.+ expect(classifyMediaValue("data:image/png;base64,")?.contentType).toBe(+ "image/png",+ );+ });++ it("accepts the base64 token case-insensitively (WHATWG data-URL spec)", () => {+ // Browsers accept `;BASE64,` / `;Base64,`; the regex's `i` flag matches+ // them. The lowercase mediatype still gates previewability.+ expect(classifyMediaValue("data:image/png;BASE64,AAAA")?.contentType).toBe(+ "image/png",+ );+ expect(classifyMediaValue("data:audio/mpeg;Base64,AAAA")?.contentType).toBe(+ "audio/mpeg",+ );+ });++ it("does not classify a data URI whose header exceeds the scan window", () => {+ // Documents the intentional bound: only the first 256 chars of the head are+ // scanned, so a (valid but) pathologically long header pushes its mandatory+ // comma out of range and the value falls through to plain-string handling.+ // Real image/audio/video headers are well under 40 chars, so this never+ // affects genuine media.+ const longHeader = "data:image/png;charset=" + "a".repeat(300) + ",AAAA";+ expect(classifyMediaValue(longHeader)).toBeNull();+ // Same header, comfortably within the window, is classified normally.+ const shortHeader = "data:image/png;charset=" + "a".repeat(10) + ",AAAA";+ expect(classifyMediaValue(shortHeader)?.contentType).toBe("image/png");+ });+it("ignores non-previewable data URIs", () => {expect(classifyMediaValue("data:text/plain,hello")).toBeNull();+ expect(classifyMediaValue("data:application/json,{}")).toBeNull();+ });++ it("does not mistake prose or malformed 'data:' strings for media", () => {+ // No comma at all -> not a data URI, just a string that starts like one.+ expect(classifyMediaValue("data:image/png")).toBeNull();+ // Prose after the MIME head: the header is not comma-terminated.+ expect(+ classifyMediaValue("data:image/png is the best format for screenshots"),+ ).toBeNull();+ // A comma appears, but only later inside prose — the header itself is not+ // well-formed up to a comma.+ expect(+ classifyMediaValue("data:audio/mpeg not supported, sadly"),+ ).toBeNull();+ // "base64" without the terminating comma is still malformed.+ expect(classifyMediaValue("data:image/png;base64")).toBeNull();+ // A huge non-data-URI string that merely starts with the prefix must fall+ // through (so the caller truncates it) instead of being handed off whole.+ expect(+ classifyMediaValue("data:image/png " + "x".repeat(5_000_000)),+ ).toBeNull();+ });++ it("does not treat a bare base64-ish token or embedded substring as media", () => {+ // Short base64-looking tokens are not data URIs.+ expect(classifyMediaValue("aGVsbG8gd29ybGQ=")).toBeNull();+ expect(classifyMediaValue("iVBORw0KGgoAAAANSUhEUgAA")).toBeNull();+ // A JSON string that merely contains a "data:" substring, not at the start.+ expect(+ classifyMediaValue('{"note":"see data:image/png;base64,AAAA"}'),+ ).toBeNull();});it("classifies image/audio/video URLs by extension", () => {diff --git a/web/src/components/ui/media/mediaUtils.ts b/web/src/components/ui/media/mediaUtils.tsindex 34b0e8e6ad..119ad44f97 100644--- a/web/src/components/ui/media/mediaUtils.ts+++ b/web/src/components/ui/media/mediaUtils.ts@@ -38,6 +38,26 @@ const PREVIEWABLE_TOP_LEVEL = new Set(["image", "audio", "video"]);// Guards the per-value hot path: bail before parsing a URL out of long strings.const MAX_URL_LENGTH = 2048;+// A data: payload can be many megabytes, so we only ever validate the head.+// This is comfortably longer than any real image/audio/video data-URI header+// (`data:<mediatype>(;param=value)*(;base64)?,` — typically well under 40+// chars) while keeping the regex cost bounded regardless of payload size.+const MAX_DATA_URI_HEADER_SCAN = 256;++// RFC 2397 data-URI header shape: `data:<type>/<subtype>(;<param>=<value>)*+// (;base64)?,`. The trailing comma that separates the header from the payload+// is MANDATORY — requiring it (and a well-formed parameter list up to it) is+// what stops ordinary strings that merely start with "data:image/…" from being+// mistaken for previewable media. The first capture group is the MIME type.+//+// The `i` flag matches the `;base64` token ASCII-case-insensitively, per the+// WHATWG data-URL spec (browsers accept `;BASE64,` / `;Base64,`). It does NOT+// widen which media is previewable: the top-level type is still gated by the+// lowercase `PREVIEWABLE_TOP_LEVEL` check below, so `data:IMAGE/PNG,…` remains+// non-previewable regardless of the flag.+const DATA_URI_HEADER_PATTERN =+ /^data:([\w.+-]+\/[\w.+-]+)(?:;[\w.+-]+=[^;,]*)*(?:;base64)?,/i;+/*** Classifies a string value as previewable media, or returns null. Pure and* cheap: a prefix check gates every branch so non-media strings (the common@@ -59,8 +79,14 @@ export function classifyMediaValue(value: unknown) {}if (value.startsWith(DATA_URI_PREFIX)) {- // Read only the head — a base64 payload can be megabytes long.- const match = /^data:([\w.+-]+\/[\w.+-]+)/.exec(value.slice(0, 100));+ // Validate the head against the RFC 2397 shape — reading only the head,+ // since a base64 payload can be megabytes long. Requiring a well-formed+ // header terminated by its mandatory comma rejects prose / JSON strings+ // that happen to start with "data:image/…" (false positives that would+ // otherwise render a broken media chip and skip the normal truncation).+ const match = DATA_URI_HEADER_PATTERN.exec(+ value.slice(0, MAX_DATA_URI_HEADER_SCAN),+ );const contentType = match?.[1];if (!contentType) return null;if (!PREVIEWABLE_TOP_LEVEL.has(contentType.split("/")[0]!)) return null;