Skip to content

Commit 6dfdfa0

Browse files
committed
refuse a baseline JPEG too small for the blocks it declares before allocating them
1 parent d61e3fe commit 6dfdfa0

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎src/codecs/jpeg.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export function decode(data: Uint8Array, { transform, maxPixels = MAX_PIXELS }:
177177
if (end - start < 6) throw new Error('JPEG: truncated frame header');
178178
if (data[start] !== 8) throw new Error(`JPEG: ${data[start]}-bit precision is not supported`);
179179
checkPixels(u16(start + 3), u16(start + 1), maxPixels);
180-
frame = createFrame(marker === 0xc2, u16(start + 1), u16(start + 3), data.subarray(start + 5, end));
180+
frame = createFrame(marker === 0xc2, u16(start + 1), u16(start + 3), data.subarray(start + 5, end), data.length - end);
181181
break;
182182
}
183183

@@ -251,7 +251,7 @@ export function decode(data: Uint8Array, { transform, maxPixels = MAX_PIXELS }:
251251
return { width: frame.width, height: frame.height, pixels: output(frame, adobe, jfif, transform), orientation, icc };
252252
}
253253

254-
function createFrame(progressive: boolean, height: number, width: number, componentData: Uint8Array): Frame {
254+
function createFrame(progressive: boolean, height: number, width: number, componentData: Uint8Array, available: number): Frame {
255255
const count = componentData[0];
256256
const sampling: Array<{ id: number, h: number, v: number, tq: number }> = [];
257257

@@ -268,6 +268,14 @@ function createFrame(progressive: boolean, height: number, width: number, compon
268268
const vmax = Math.max(...sampling.map(c => c.v));
269269
const mcusPerLine = Math.ceil(width / (8 * hmax));
270270
const mcusPerColumn = Math.ceil(height / (8 * vmax));
271+
// a sequential scan codes every block in 2 bits at least (a DC and an end-of-block code): a file with fewer bytes
272+
// after the frame header than its blocks need is cut off, and is refused before the planes are allocated (a 19 KB
273+
// file declaring 16383x16383 took 16 s and 1.1 GB to end the same way). Progressive scans can skip runs of blocks
274+
if (!progressive) {
275+
let blocks = 0;
276+
for (const c of sampling) blocks += Math.ceil(Math.ceil(width * c.h / hmax) / 8) * Math.ceil(Math.ceil(height * c.v / vmax) / 8);
277+
if (blocks * 2 > available * 8) throw new Error('JPEG: the scan data is truncated');
278+
}
271279

272280
const components = sampling.map((c): Component => {
273281
const componentWidth = Math.ceil(width * c.h / hmax);

‎tests/jpeg.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,13 @@ const withoutExif = data => {
111111
const error = await Image.decode(await fs.readFile('./tests/jpegs/damaged-truncated-frame-header.jpg')).then(() => null, e => e);
112112
if (!error || error.constructor !== Error) panic(`a truncated frame header should throw an Error, got ${error}`);
113113
}
114+
115+
// a baseline file far too small for the size it declares (every block takes at least 2 bits of scan data) is
116+
// refused before its planes are allocated: 19 KB declaring 16383x16383 took 16 s and 1.1 GB to end truncated
117+
// (damaged-declares-16383.jpg: a CMYK baseline JPEG of 19 KB)
118+
{
119+
const start = Date.now();
120+
const error = await Image.decode(await fs.readFile('./tests/jpegs/damaged-declares-16383.jpg')).then(() => null, e => e);
121+
if (!error || error.constructor !== Error || !/truncated/.test(error.message) || Date.now() - start > 1000) panic(`a 19 KB baseline JPEG declaring 16383x16383 should throw a truncated Error at once, got ${error} after ${Date.now() - start} ms`);
122+
}
114123
})();
18.9 KB
Loading

0 commit comments

Comments
 (0)