fix(images): hold an AVIF or HEIC to an ftyp box it could have
upwind's image type detector (packages/core/src/images/detect.ts) took any four bytes for an ftyp box's size. Next.js's did the same before 16.4.5 (GHSA-7qx7-33qx-rqx2). So an SVG that opens with <!--ftypavif--> (or <!--ftypheic-->) was taken for an AVIF (or a HEIC), and was not refused as an SVG when dangerouslyAllowSVG is off. Where it went from there:
- A native Function's optimizer: libvips read it as the SVG it is and rasterised it.
- At the edge: it was handed to the Images binding, or, with none, served as it is under the type it passed for.
This detector is upwind's own, so no Next.js release fixes it here.
Changes
-
Detector. AVIF and HEIC are held to an
ftypbox they could have, as Next.js 16.4.5 holds its own: a size of at least 16 bytes and at most the bytes there are, thenftypand the brand. The check runs on image requests only, over the 1 KiB prefix the detector already reads, and does no I/O. -
Native encoder: unchanged. The first version of this PR also stopped unblocking libvips's SVG loader. That broke
next/og:- A sharp block applies to the whole process.
next/ogrenders its image through the same sharp, from an SVG, so once the optimizer had loaded sharp an og route answered 500. This wasog-api's "after image optimization" case in node 47/64.- Next.js unblocks the loader for the same reason.
The loader stays, with a comment saying why. The detector's check keeps a disguised SVG away from the encoder.
Tests
packages/core/test/images.test.ts:- real AVIF and HEIC boxes are still detected;
- an SVG behind a comment that spells either box is detected as SVG;
- a box larger than the bytes, or smaller than a box can be, is not detected as either.
- The four new cases fail on the old detector.
- The image suites pass locally: core, native and fallback runtime, and edge. oxfmt, ESLint and
tscpass for core and runtime.
Review notes
- Greptile P2 (no test for the SVG loader block). The block is withdrawn rather than tested; see above.