From ce9b399b4117e6cf3281da8e2164b16e0fb8ca06 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Mon, 30 Nov 2026 16:13:50 -0800 Subject: [PATCH] Zlib: set the inflate dictionary when requested Since e503f572 in njs and bb3dcf21 in QuickJS, inflateSync() has tried to set a supplied dictionary before reading the zlib header. Zlib rejects this for wrapped streams, even if no dictionary is needed. For wrapped streams, wait for Z_NEED_DICT before setting the dictionary. Raw inflate still needs the dictionary up front. --- external/njs_zlib_module.c | 20 ++++++++++++++++---- external/qjs_zlib_module.c | 18 +++++++++++++++--- test/zlib.t.mjs | 5 +++++ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/external/njs_zlib_module.c b/external/njs_zlib_module.c index fab2eaae..7d7b95e9 100644 --- a/external/njs_zlib_module.c +++ b/external/njs_zlib_module.c @@ -466,10 +466,10 @@ njs_zlib_ext_inflate(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, NJS_CHB_MP_INIT(&chain, njs_vm_memory_pool(vm)); - if (dictionary.start != NULL) { + if (raw && dictionary.start != NULL) { rc = inflateSetDictionary(&stream, dictionary.start, dictionary.length); if (njs_slow_path(rc != Z_OK)) { - njs_vm_error(vm, "deflateSetDictionary() failed"); + njs_vm_error(vm, "inflateSetDictionary() failed"); goto fail; } } @@ -491,8 +491,20 @@ njs_zlib_ext_inflate(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, } if (rc == Z_NEED_DICT) { - njs_vm_type_error(vm, "failed to inflate, dictionary is required"); - goto fail; + if (dictionary.start == NULL) { + njs_vm_type_error(vm, + "failed to inflate, dictionary is required"); + goto fail; + } + + rc = inflateSetDictionary(&stream, dictionary.start, + dictionary.length); + if (njs_slow_path(rc != Z_OK)) { + njs_vm_error(vm, "inflateSetDictionary() failed"); + goto fail; + } + + continue; } njs_chb_written(&chain, chunk_size - stream.avail_out); diff --git a/external/qjs_zlib_module.c b/external/qjs_zlib_module.c index ca5260e1..975cda1e 100644 --- a/external/qjs_zlib_module.c +++ b/external/qjs_zlib_module.c @@ -432,7 +432,7 @@ qjs_zlib_ext_inflate(JSContext *ctx, JSValueConst this_val, int argc, initialized = 1; - if (dictionary_copy != NULL) { + if (raw && dictionary_copy != NULL) { rc = inflateSetDictionary(&stream, dictionary_copy, dictionary_length); if (rc != Z_OK) { qjs_throw_error(ctx, "inflateSetDictionary() failed"); @@ -457,8 +457,20 @@ qjs_zlib_ext_inflate(JSContext *ctx, JSValueConst this_val, int argc, } if (rc == Z_NEED_DICT) { - JS_ThrowTypeError(ctx, "failed to inflate, dictionary is required"); - goto fail; + if (dictionary_copy == NULL) { + JS_ThrowTypeError(ctx, + "failed to inflate, dictionary is required"); + goto fail; + } + + rc = inflateSetDictionary(&stream, dictionary_copy, + dictionary_length); + if (njs_slow_path(rc != Z_OK)) { + qjs_throw_error(ctx, "inflateSetDictionary() failed"); + goto fail; + } + + continue; } njs_chb_written(&chain, chunk_size - stream.avail_out); diff --git a/test/zlib.t.mjs b/test/zlib.t.mjs index 7b9c6a28..a4f39e3f 100644 --- a/test/zlib.t.mjs +++ b/test/zlib.t.mjs @@ -105,6 +105,11 @@ let inflateSync_tsuite = { { value: zlib.deflateSync('WAKA WAKA WAKA', {dictionary: Buffer.from('WAKA')}), raw: false, exception: 'TypeError: failed to inflate, dictionary is required' }, + { value: zlib.deflateSync('WAKA', {dictionary: Buffer.from('WAKA')}), + raw: false, options: {dictionary: Buffer.from('WAKA')}, + expected: 'WAKA' }, + { value: zlib.deflateSync('WAKA'), raw: false, + options: {dictionary: Buffer.from('unused')}, expected: 'WAKA' }, ]}; run([ -- 2.47.3