Skip to content

Commit 683e9b1

Browse files
fix(producer): refuse private and non-HTTPS previewUrl fetches (#6297)
1 parent e938e53 commit 683e9b1

3 files changed

Lines changed: 57 additions & 7 deletions

File tree

‎packages/producer/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The producer can also run as a render server, accepting render requests over HTT
3535
import { startServer } from "@hyperframes/producer";
3636

3737
await startServer({ port: 8080 });
38-
// POST /render or /render/stream with projectDir, html, or previewUrl and render options
38+
// POST /render or /render/stream with projectDir, html, or previewUrl (public HTTPS only) and render options
3939
```
4040

4141
Both HTTP routes accept optional `pageMotionBlur: "off"` to disable authored page

‎packages/producer/src/server.test.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from "bun:test";
1+
import { afterEach, describe, expect, it, mock } from "bun:test";
22
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
33
import { join } from "node:path";
44
import { tmpdir } from "node:os";
@@ -189,3 +189,54 @@ describe("prepareRenderBody — validation", () => {
189189
expect(input.outputResolution).toBe("landscape-4k");
190190
});
191191
});
192+
193+
describe("prepareRenderBody — previewUrl", () => {
194+
const realFetch = globalThis.fetch;
195+
const stubFetch = (impl: () => Promise<Response>) => {
196+
const spy = mock(impl);
197+
globalThis.fetch = spy as unknown as typeof fetch;
198+
return spy;
199+
};
200+
afterEach(() => {
201+
globalThis.fetch = realFetch;
202+
});
203+
204+
it.each([
205+
"http://169.254.169.254/latest/meta-data/",
206+
"https://169.254.169.254/latest/meta-data/",
207+
"https://localhost:8080/index.html",
208+
"https://[::1]/index.html",
209+
"file:///etc/passwd",
210+
])("refuses %s without fetching it", async (previewUrl) => {
211+
const fetchSpy = stubFetch(async () => new Response("secret"));
212+
213+
const result = await prepareRenderBody({ previewUrl });
214+
215+
expect(result).toHaveProperty("error");
216+
expect((result as { error: string }).error).toContain("Failed to fetch previewUrl");
217+
expect(fetchSpy).not.toHaveBeenCalled();
218+
});
219+
220+
it("refuses a public URL that redirects to a metadata address", async () => {
221+
const fetchSpy = stubFetch(
222+
async () =>
223+
new Response(null, {
224+
status: 302,
225+
headers: { location: "https://169.254.169.254/latest/meta-data/" },
226+
}),
227+
);
228+
229+
const result = await prepareRenderBody({ previewUrl: "https://example.com/index.html" });
230+
231+
expect(result).toHaveProperty("error");
232+
expect(fetchSpy).toHaveBeenCalledTimes(1);
233+
});
234+
235+
it("renders the HTML of a public URL", async () => {
236+
stubFetch(async () => new Response("<html><body>ok</body></html>"));
237+
238+
const result = await prepareRenderBody({ previewUrl: "https://example.com/index.html" });
239+
240+
expect(result).toHaveProperty("prepared");
241+
});
242+
});

‎packages/producer/src/server.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import crypto from "node:crypto";
2929
import { Hono, type Context } from "hono";
3030
import { streamSSE } from "hono/streaming";
3131
import { serve } from "@hono/node-server";
32+
import { fetchPublicHttpsText } from "@hyperframes/engine";
3233
import {
3334
RenderCancelledError,
3435
createRenderJob,
@@ -394,6 +395,8 @@ function validateOutputResolutionOverride(body: Record<string, unknown>): string
394395

395396
type PrepareRenderResult = { prepared: PreparedRenderInput } | { error: string };
396397

398+
const MAX_PREVIEW_HTML_BYTES = 20 * 1024 * 1024;
399+
397400
function prepareProjectDirectory(
398401
projectDir: unknown,
399402
options: Omit<RenderInput, "projectDir">,
@@ -423,11 +426,7 @@ async function resolveInlineRenderHtml(body: Record<string, unknown>): Promise<
423426
if (!previewUrl)
424427
return { error: "Missing render source: provide projectDir, previewUrl, or html" };
425428
try {
426-
const response = await fetch(previewUrl, { method: "GET" });
427-
if (!response.ok) {
428-
return { error: `Failed to fetch previewUrl: ${response.status} ${response.statusText}` };
429-
}
430-
return { html: await response.text() };
429+
return { html: await fetchPublicHttpsText(previewUrl, { maxBytes: MAX_PREVIEW_HTML_BYTES }) };
431430
} catch (error) {
432431
return {
433432
error: `Failed to fetch previewUrl: ${error instanceof Error ? error.message : String(error)}`,

0 commit comments

Comments
 (0)