|
| 1 | +import { createElement } from "react"; |
| 2 | +import { renderToStaticMarkup } from "react-dom/server"; |
1 | 3 | import { describe, expect, it } from "vitest"; |
2 | | -import { restrictModelUrls } from "./StreamdownRenderer"; |
| 4 | +import { restrictModelUrls, StreamdownRenderer } from "./StreamdownRenderer"; |
3 | 5 |
|
4 | 6 | // streamdown calls urlTransform(url, key, node) to compute each url attribute; a |
5 | 7 | // returned undefined removes the attribute, so no request is ever issued. |
@@ -42,3 +44,37 @@ describe("restrictModelUrls (link href)", () => { |
42 | 44 | expect(restrictModelUrls("data:text/html,<script>", "href", link)).toBeUndefined(); |
43 | 45 | }); |
44 | 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