|
| 1 | +import { createElement } from "react"; |
| 2 | +import { renderToStaticMarkup } from "react-dom/server"; |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | +import { restrictModelUrls, StreamdownRenderer } from "./StreamdownRenderer"; |
| 5 | + |
| 6 | +// streamdown calls urlTransform(url, key, node) to compute each url attribute; a |
| 7 | +// returned undefined removes the attribute, so no request is ever issued. |
| 8 | +const img = { tagName: "img" } as any; |
| 9 | +const link = { tagName: "a" } as any; |
| 10 | + |
| 11 | +describe("restrictModelUrls (image src)", () => { |
| 12 | + it("drops a remote model-authored image (the favicon beacon)", () => { |
| 13 | + expect( |
| 14 | + restrictModelUrls("https://www.google.com/s2/favicons?domain=evil", "src", img) |
| 15 | + ).toBeUndefined(); |
| 16 | + }); |
| 17 | + |
| 18 | + it("drops any absolute or protocol-relative remote image", () => { |
| 19 | + expect(restrictModelUrls("http://evil.tld/pixel.gif", "src", img)).toBeUndefined(); |
| 20 | + expect(restrictModelUrls("//evil.tld/pixel.gif", "src", img)).toBeUndefined(); |
| 21 | + }); |
| 22 | + |
| 23 | + it("keeps inline and same-origin images", () => { |
| 24 | + expect(restrictModelUrls("data:image/png;base64,AAAA", "src", img)).toBe( |
| 25 | + "data:image/png;base64,AAAA" |
| 26 | + ); |
| 27 | + expect(restrictModelUrls("blob:abc", "src", img)).toBe("blob:abc"); |
| 28 | + expect(restrictModelUrls("/local/pic.png", "src", img)).toBe("/local/pic.png"); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +describe("restrictModelUrls (link href)", () => { |
| 33 | + it("keeps http(s), mailto and relative links", () => { |
| 34 | + expect(restrictModelUrls("https://trigger.dev/docs", "href", link)).toBe( |
| 35 | + "https://trigger.dev/docs" |
| 36 | + ); |
| 37 | + expect(restrictModelUrls("http://example.com", "href", link)).toBe("http://example.com"); |
| 38 | + expect(restrictModelUrls("mailto:hi@trigger.dev", "href", link)).toBe("mailto:hi@trigger.dev"); |
| 39 | + expect(restrictModelUrls("/runs/123", "href", link)).toBe("/runs/123"); |
| 40 | + }); |
| 41 | + |
| 42 | + it("drops unsafe link schemes", () => { |
| 43 | + expect(restrictModelUrls("javascript:alert(1)", "href", link)).toBeUndefined(); |
| 44 | + expect(restrictModelUrls("data:text/html,<script>", "href", link)).toBeUndefined(); |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +// Force the lazy component to load, then return its resolved default so we can render it |
| 49 | +// synchronously. This proves the policy is actually wired into the JSX, not just exported. |
| 50 | +async function resolveStreamdownRenderer() { |
| 51 | + const lazy = StreamdownRenderer as unknown as { |
| 52 | + _payload: unknown; |
| 53 | + _init: (payload: unknown) => (props: { children: string }) => JSX.Element; |
| 54 | + }; |
| 55 | + try { |
| 56 | + lazy._init(lazy._payload); |
| 57 | + } catch (thenable) { |
| 58 | + await thenable; |
| 59 | + } |
| 60 | + return lazy._init(lazy._payload); |
| 61 | +} |
| 62 | + |
| 63 | +describe("StreamdownRenderer (rendered markdown)", () => { |
| 64 | + it("never lets a model-authored remote image src reach the DOM", async () => { |
| 65 | + const Renderer = await resolveStreamdownRenderer(); |
| 66 | + const markdown = [ |
| 67 | + "", |
| 68 | + "", |
| 69 | + "", |
| 70 | + ].join("\n\n"); |
| 71 | + const html = renderToStaticMarkup(createElement(Renderer, null, markdown)); |
| 72 | + |
| 73 | + // No remote host is ever fetched: no absolute or protocol-relative image src survives. |
| 74 | + expect(html).not.toContain('src="http'); |
| 75 | + expect(html).not.toContain('src="//'); |
| 76 | + expect(html).not.toContain("SECRET.evil.tld"); |
| 77 | + // A same-origin relative image is untouched, so the policy does not over-block. |
| 78 | + expect(html).toContain('src="/local/pic.png"'); |
| 79 | + }); |
| 80 | +}); |
0 commit comments