Skip to content

Commit d5caed7

Browse files
committed
throw for WebP image data that ends before its chunk size
1 parent 0eb1662 commit d5caed7

4 files changed

Lines changed: 21 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Upgrading from 3.x? Upgrade to the latest 3.x release first, fix the deprecation
128128

129129
### Fixes
130130

131-
- **Cut-off JPEG and lossy WebP files throw** like the other formats: a JPEG without any scan data, or whose scan ends before its last block, and a WebP whose partitions end early. They decoded without an error, as gray or smeared images. A JPEG that only lacks its end marker still decodes.
131+
- **Cut-off JPEG and lossy WebP files throw** like the other formats: a JPEG without any scan data, or whose scan ends before its last block, and a WebP whose image data (any chunk of it) ends before its size says. They decoded without an error, as gray or smeared images. A JPEG that only lacks its end marker still decodes.
132132
- `Image.pixels()` yields the pixels the image had when the iteration started, where resizing the image meanwhile threw an error from the inside or stopped early.
133133
- `Animation.decode()` gives a still image (PNG, JPEG XL, WebP, JPEG, ...) a frame of 100 ms, the `Frame` default, not 0 ms.
134134
- `JSON.stringify()` and `console.log()` of an `Image`, `Frame` or `Animation` show their size (and duration), not every pixel; `new Colors()` throws.

‎src/codecs/webp.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ const u24 = (d: Uint8Array, o: number): number => d[o] | d[o + 1] << 8 | d[o + 2
8686
function decodeBitstream(list: Chunk[], maxPixels: number): { width: number, height: number, pixels: Uint8Array } {
8787
let alpha: Chunk | null = null;
8888
for (const chunk of list) {
89+
// (the bitstream decoders read missing bytes as zeros, so a chunk that ends before its size says is cut off)
90+
if ((chunk.type === ALPH || chunk.type === VP8L || chunk.type === VP8) && chunk.cut) throw new Error('WebP: the image data is truncated');
8991
if (chunk.type === ALPH) alpha ??= chunk;
9092
else if (chunk.type === VP8L) {
9193
const { width, height, argb } = vp8l.decode(chunk.data, maxPixels);

‎src/image.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -990,12 +990,13 @@ export class Image {
990990
}
991991

992992
/**
993-
* Yields an [x, y, color] array for every pixel in the image (0-based), row by row. The pixels are those the image
994-
* had when the iteration started: changing the image meanwhile (even its size) doesn't affect them
993+
* Yields an [x, y, color] array for every pixel in the image (0-based), row by row. Resizing the image meanwhile
994+
* doesn't affect the iteration (it goes on over the pixels it started with), but other changes to the pixels, like
995+
* `setPixelAt()`, are seen by the pixels not yet yielded
995996
* @yields The coordinates and color of the pixel ([x, y, color])
996997
*/
997998
*pixels(): Generator<[x: number, y: number, color: Color], void, undefined> {
998-
// (the pixels as they are now: resizing the image meanwhile doesn't change what this yields)
999+
// (the pixels and the size as they are now: resizing the image meanwhile doesn't change what this yields)
9991000
const view = new DataView(this.bitmap.buffer, this.bitmap.byteOffset, this.bitmap.byteLength);
10001001
const width = this.width, height = this.height;
10011002
let offset = 0;

‎tests/truncated.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ const panic = message => {
2525
if (failed !== cuts - 1) panic(`${name}: ${cuts - 1 - failed} of ${cuts - 1} cut-off files decoded without an error`);
2626
}
2727

28+
// a small lossy WebP cut anywhere inside of its image data, not only where the data is read to (every cut before the
29+
// bitstream's size is reached)
30+
{
31+
const small = await Image.linearGradient(67, 41, {stops: [0xff2040ff, 0x3040ffff], angle: 135}).encodeWebP({quality: 80});
32+
const view = new DataView(small.buffer, small.byteOffset);
33+
// (the VP8 chunk of the extended format, after the VP8X chunk of 18 bytes)
34+
let at = 12;
35+
while (String.fromCharCode(...small.subarray(at, at + 4)) !== 'VP8 ') at += 8 + view.getUint32(at + 4, true) + (view.getUint32(at + 4, true) & 1);
36+
const end = at + 8 + view.getUint32(at + 4, true);
37+
for (let cut = at + 8; cut < end; cut++)
38+
if (await decodes(small.subarray(0, cut))) panic(`a lossy WebP cut ${end - cut} bytes before the end of its image data decoded`);
39+
if (!await decodes(small)) panic('the whole small WebP does not decode');
40+
}
41+
2842
// a JPEG without its end marker has all its pixels
2943
const jpeg = await photo.encodeJPEG({quality: 80});
3044
if (!await decodes(jpeg.subarray(0, jpeg.length - 2))) panic('a JPEG without its end marker is rejected');

0 commit comments

Comments
 (0)