]> git.kaiwu.me - njs.git/commitdiff
Zlib: set the inflate dictionary when requested master
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 1 Dec 2026 00:13:50 +0000 (16:13 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Tue, 1 Dec 2026 01:29:33 +0000 (17:29 -0800)
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
external/qjs_zlib_module.c
test/zlib.t.mjs

index fab2eaaec2fa8d7413de17fd6942ef50293fef5b..7d7b95e9b762a3e37018208dca54d049e3d64c73 100644 (file)
@@ -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);
index ca5260e143c1dd39c5d62416e52c32ec950cc66d..975cda1e6fc26ee9f73cd80b6290f5b1756f32e7 100644 (file)
@@ -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);
index 7b9c6a28a4ff9a8b72ef57ad008c2d333bb25da2..a4f39e3fac9b5e43862073387f8309e8bffa6367 100644 (file)
@@ -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([