Skip to content

Commit 4aafed1

Browse files
authored
fix(media): bound image-optimize fetch headroom to the image cap (#124838)
When a caller passed an explicit maxBytes with optimizeImages on (the default), the source-read bound was inflated to max(maxBytes, 100MB document cap). A channel with a 5MB attachment cap therefore let a hostile or oversized URL buffer up to 100MB before the final size check — 20x resource amplification, and the inflation applied before content classification, so it was not limited to images. The inflation exists so oversized-but-compressible originals can be fetched and compressed under the delivery cap. Size that headroom off the thing that justifies it: 4x the 6MB image cap (24MB), enough for large phone photos, instead of the document cap. Accepted tradeoff (named at the constant): originals above 24MB that would have compressed under the cap now fail; the fetch error names the bound. Callers without an explicit cap keep per-kind defaults. Live proof on a real localhost HTTP server (no mocked fetch): an 8.1MB PNG under a 5MB cap compresses to 3.75MB and delivers; a 30MB-declared image is rejected at the Content-Length precheck in 7ms with 'content length 31457280 exceeds maxBytes 25165824'. Regression: headroom-bound test fails pre-fix (30MB passes the old 100MB bound); companion test proves compression headroom still works.
1 parent 63a3a95 commit 4aafed1

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

‎src/media/web-media.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,54 @@ describe("loadWebMedia", () => {
15061506
}
15071507
});
15081508

1509+
it("bounds explicit-cap image fetches at the optimize headroom, not the document cap", async () => {
1510+
// 30MB declared original: over the 24MB image-optimize headroom but well
1511+
// under the old 100MB document bound. The Content-Length precheck must
1512+
// reject before any body bytes are read.
1513+
const declaredBytes = 30 * 1024 * 1024;
1514+
const fetchImpl = vi.fn(
1515+
async () =>
1516+
new Response(new ReadableStream<Uint8Array>(), {
1517+
status: 200,
1518+
headers: {
1519+
"content-type": "image/png",
1520+
"content-length": String(declaredBytes),
1521+
},
1522+
}),
1523+
);
1524+
1525+
await expect(
1526+
loadWebMedia("https://example.test/huge.png", {
1527+
maxBytes: 5 * 1024 * 1024,
1528+
fetchImpl,
1529+
ssrfPolicy: { allowedHostnames: ["example.test"] },
1530+
}),
1531+
).rejects.toThrow(/exceeds maxBytes/);
1532+
});
1533+
1534+
it("keeps compression headroom above an explicit cap for oversized originals", async () => {
1535+
// A 10MB-declared image is over the caller's 5MB cap but inside the
1536+
// optimize headroom: the fetch must proceed so compression can shrink it
1537+
// under the delivery cap.
1538+
const original = createSolidPngBuffer(64, 64, { r: 12, g: 34, b: 56 });
1539+
const fetchImpl = vi.fn(
1540+
async () =>
1541+
new Response(Buffer.from(original), {
1542+
status: 200,
1543+
headers: { "content-type": "image/png" },
1544+
}),
1545+
);
1546+
1547+
const result = await loadWebMedia("https://example.test/photo.png", {
1548+
maxBytes: 5 * 1024 * 1024,
1549+
fetchImpl,
1550+
ssrfPolicy: { allowedHostnames: ["example.test"] },
1551+
});
1552+
1553+
expect(result.kind).toBe("image");
1554+
expect(result.buffer.length).toBeLessThanOrEqual(5 * 1024 * 1024);
1555+
});
1556+
15091557
it("applies the shared remote read idle timeout for raw web media loads", async () => {
15101558
const readIdleTimeoutMs = 20;
15111559
const fetchImpl = makeStallingFetch(new Uint8Array([0x25, 0x50, 0x44, 0x46]));

‎src/media/web-media.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ function resolveWebMediaOptions(params: {
160160
};
161161
}
162162

163+
// Pre-compression fetch headroom for callers with an explicit delivery cap:
164+
// enough to pull a large phone photo (~20MB+) and compress it under the cap,
165+
// without letting a tight channel cap buffer up to the 100MB document bound.
166+
const IMAGE_OPTIMIZE_HEADROOM_FACTOR = 4;
167+
163168
const HEIC_MIME_RE = /^image\/hei[cf]$/i;
164169
const HEIC_EXT_RE = /\.(heic|heif)$/i;
165170
const WINDOWS_DRIVE_RE = /^[A-Za-z]:[\\/]/;
@@ -1131,13 +1136,19 @@ async function loadWebMediaInternal(
11311136
};
11321137

11331138
// Bound source reads before buffering. Optimized images may exceed their
1134-
// delivery cap because they are compressed before the final size check.
1139+
// delivery cap because they are compressed before the final size check, so
1140+
// an explicit caller cap gets image-compression headroom — sized off the
1141+
// image cap, not the 100MB document cap, or a tight channel cap would still
1142+
// permit a 100MB buffer from a hostile URL. Accepted tradeoff: originals
1143+
// above the headroom fail even when they would have compressed under the
1144+
// cap; the error names the fetch bound so the user can shrink the source.
11351145
const defaultSourceReadCap = maxBytesForKind("document");
1146+
const imageOptimizeHeadroom = IMAGE_OPTIMIZE_HEADROOM_FACTOR * maxBytesForKind("image");
11361147
const sourceReadCap =
11371148
maxBytes === undefined
11381149
? defaultSourceReadCap
11391150
: optimizeImages
1140-
? Math.max(maxBytes, defaultSourceReadCap)
1151+
? Math.max(maxBytes, imageOptimizeHeadroom)
11411152
: maxBytes;
11421153

11431154
if (hasHttpUrlPrefix(mediaUrl)) {

0 commit comments

Comments
 (0)