Skip to content

Commit 93eeca1

Browse files
committed
reject a JPEG XL blend alpha channel that doesn't exist
1 parent 995701d commit 93eeca1

4 files changed

Lines changed: 15 additions & 4 deletions

File tree

‎src/codecs/jxl/frame-header.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ const DEFAULT_EPF = (): EPF => ({
8181

8282
const DEFAULT_GAB = [0.115169525, 0.061248592, 0.115169525, 0.061248592, 0.115169525, 0.061248592];
8383

84-
function blendingInfo(r: BitReader, haveExtra: boolean, resets: (mode: number) => boolean, mainMode = -1): BlendingInfo {
84+
function blendingInfo(r: BitReader, numExtra: number, resets: (mode: number) => boolean, mainMode = -1): BlendingInfo {
8585
const mode = r.u32(0, 1, 2, [3, 2]);
8686
if (mode > 4) throw new Error('JPEG XL: invalid blend mode');
87-
const usesAlpha = haveExtra && (mode === BLEND_BLEND || mode === BLEND_MULADD);
87+
const usesAlpha = numExtra > 0 && (mode === BLEND_BLEND || mode === BLEND_MULADD);
8888
const alphaChannel = usesAlpha ? r.u32(0, 1, 2, [3, 3]) : 0;
89+
if (alphaChannel >= Math.max(1, numExtra)) throw new Error('JPEG XL: invalid blend alpha channel');
8990
const clamp = usesAlpha || mode === BLEND_MUL ? r.bool() : false;
9091
const source = resets(mainMode >= 0 ? mainMode : mode) ? 0 : r.bits(2);
9192
return { mode, alphaChannel, clamp, source };
@@ -164,8 +165,8 @@ export function frameHeader(r: BitReader, image: ImageHeader): FrameHeader {
164165
let blending: BlendingInfo = { mode: BLEND_REPLACE, alphaChannel: 0, clamp: false, source: 0 };
165166
const ecBlending: BlendingInfo[] = [];
166167
if (!allDefault && normal) {
167-
blending = blendingInfo(r, numExtra > 0, resets);
168-
for (let i = 0; i < numExtra; i++) ecBlending.push(blendingInfo(r, true, resets, blending.mode));
168+
blending = blendingInfo(r, numExtra, resets);
169+
for (let i = 0; i < numExtra; i++) ecBlending.push(blendingInfo(r, numExtra, resets, blending.mode));
169170
} else for (let i = 0; i < numExtra; i++) ecBlending.push({ ...blending });
170171
let duration = 0, isLast = type === FRAME_REGULAR, saveAsReference = 0;
171172
if (!allDefault && normal) {

‎tests/jxl.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,14 @@ const difference = (bitmap, width, height, reference, step = 1, premultiplied =
7979
if (Buffer.compare(Buffer.from(animation.frames[0].image.bitmap), Buffer.from(one.bitmap))) panic('threads, Animation.decode: different pixels');
8080
}
8181
for (const threads of [0, 1.5]) if (!await Image.decode(lossy, {threads}).then(() => false, e => e instanceof RangeError)) panic(`threads ${threads} should throw`);
82+
83+
// damaged blending info (blendmodes.jxl with flipped bits): a blend alpha channel that doesn't exist is an error,
84+
// not a TypeError from the blending
85+
for (const name of await fs.readdir('./tests/jxl/damaged')) {
86+
const data = await fs.readFile(`./tests/jxl/damaged/${name}`);
87+
for (const decode of [() => Image.decode(data, {threads: 1}), () => Animation.decode(data, {threads: 1})]) {
88+
const error = await decode().then(() => null, e => e);
89+
if (!error || error instanceof TypeError || !/JPEG XL|damaged/.test(error.message)) panic(`damaged/${name}: ${error && error.constructor.name}: ${error && error.message}`);
90+
}
91+
}
8292
})();
145 Bytes
Binary file not shown.
145 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)