diff --git a/src/node_zlib.cc b/src/node_zlib.cc index 246bb5bf58c..d25662bdb98 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -2302,6 +2302,7 @@ void DefineZlibConstants(Local target) { NODE_DEFINE_CONSTANT(target, BROTLI_DECODER_ERROR_FORMAT_PADDING_1); NODE_DEFINE_CONSTANT(target, BROTLI_DECODER_ERROR_FORMAT_PADDING_2); NODE_DEFINE_CONSTANT(target, BROTLI_DECODER_ERROR_FORMAT_DISTANCE); + NODE_DEFINE_CONSTANT(target, BROTLI_DECODER_ERROR_FORMAT_BLOCK_SWITCH); NODE_DEFINE_CONSTANT(target, BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET); NODE_DEFINE_CONSTANT(target, BROTLI_DECODER_ERROR_INVALID_ARGUMENTS); NODE_DEFINE_CONSTANT(target, BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES); diff --git a/test/parallel/test-zlib-invalid-input.js b/test/parallel/test-zlib-invalid-input.js index 7a0146a5d64..40958b07444 100644 --- a/test/parallel/test-zlib-invalid-input.js +++ b/test/parallel/test-zlib-invalid-input.js @@ -89,6 +89,33 @@ Object.defineProperty(spoofedLength, 'byteLength', { get: () => 5000 }); deflate.close(); } +{ + // A block switch is invalid when the stream declares only one block type. + const invalidBlockSwitch = Buffer.from( + '200010414141fcffff07002228f00b60f7f41f000000', + 'hex', + ); + const expectedError = { + code: 'ERR__ERROR_FORMAT_BLOCK_SWITCH', + errno: zlib.constants.BROTLI_DECODER_ERROR_FORMAT_BLOCK_SWITCH, + }; + + assert.strictEqual(expectedError.errno, -17); + assert.throws( + () => zlib.brotliDecompressSync(invalidBlockSwitch), + expectedError, + ); + + const brotli = zlib.createBrotliDecompress(); + brotli.resume(); + brotli.on('error', common.expectsError(expectedError)); + brotli.on('end', common.mustNotCall()); + for (let i = 0; i < invalidBlockSwitch.length; i++) { + brotli.write(invalidBlockSwitch.subarray(i, i + 1)); + } + brotli.end(); +} + unzips.forEach(common.mustCall((uz, i) => { uz.on('error', common.mustCall()); uz.on('end', common.mustNotCall());