]> git.kaiwu.me - njs.git/commitdiff
TextDecoder: fix use-after-free on detach in decode() options
authorDmitry Volyntsev <xeioex@nginx.com>
Mon, 16 Nov 2026 01:37:21 +0000 (17:37 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Mon, 16 Nov 2026 17:10:26 +0000 (09:10 -0800)
Previously, decode() fetched the input data pointer before reading the
"stream" option.  The option getter can run user code that detaches
the buffer, for example with ArrayBuffer.prototype.transfer() in
QuickJS.  QuickJS then read the freed memory, and njs decoded the
contents of the detached buffer.

Now the options are read before the input is fetched and checked, so
a detach in the getter is reported as a TypeError, the same way as a
buffer detached before the call.

The issue was introduced in 0570392f (0.4.3) and copied into QuickJS
in 447d66d4 (0.8.10).

src/njs_encoding.c
src/qjs.c
test/text_decoder.t.js

index 5135d02d5a1590e8f46e08d27373aadcae495bfa..26dd816f280b9659173db258e542b74a26f46221 100644 (file)
@@ -515,6 +515,23 @@ njs_text_decoder_decode(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         return NJS_ERROR;
     }
 
+    if (nargs > 2) {
+        options = njs_argument(args, 2);
+
+        if (njs_slow_path(!njs_is_object(options))) {
+            njs_type_error(vm, "The \"options\" argument must be "
+                           "of type object");
+            return NJS_ERROR;
+        }
+
+        ret = njs_value_property(vm, options, NJS_ATOM_STRING_stream, retval);
+        if (njs_slow_path(ret == NJS_ERROR)) {
+            return ret;
+        }
+
+        stream = njs_bool(retval);
+    }
+
     if (njs_fast_path(nargs > 1)) {
         value = njs_argument(args, 1);
 
@@ -545,23 +562,6 @@ njs_text_decoder_decode(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         }
     }
 
-    if (nargs > 2) {
-        options = njs_argument(args, 2);
-
-        if (njs_slow_path(!njs_is_object(options))) {
-            njs_type_error(vm, "The \"options\" argument must be "
-                           "of type object");
-            return NJS_ERROR;
-        }
-
-        ret = njs_value_property(vm, options, NJS_ATOM_STRING_stream, retval);
-        if (njs_slow_path(ret == NJS_ERROR)) {
-            return ret;
-        }
-
-        stream = njs_bool(retval);
-    }
-
     data = njs_object_data(this);
 
     if (!data->stream) {
index ccbf886567ac84bda891dcf3165609df714c384b..7d70d13b9a48236583c7a5dce07f9a3714302c1b 100644 (file)
--- a/src/qjs.c
+++ b/src/qjs.c
@@ -908,11 +908,6 @@ qjs_text_decoder_decode(JSContext *cx, JSValueConst this_val, int argc,
         return JS_ThrowTypeError(cx, "'this' is not a TextDecoder");
     }
 
-    ret = qjs_typed_array_data(cx, argv[0], &data);
-    if (JS_IsException(ret)) {
-        return ret;
-    }
-
     stream = 0;
 
     if (argc > 1) {
@@ -925,6 +920,11 @@ qjs_text_decoder_decode(JSContext *cx, JSValueConst this_val, int argc,
         JS_FreeValue(cx, ret);
     }
 
+    ret = qjs_typed_array_data(cx, argv[0], &data);
+    if (JS_IsException(ret)) {
+        return ret;
+    }
+
     end = data.start + data.length;
 
     if (!td->stream) {
index 737adadcf2ea21871491dddbd7483a3bf1c199e6..b4ac551c940b8e34c8e844c027767c4b9c83ffaf 100644 (file)
@@ -229,6 +229,41 @@ let detached_tsuite = {
     ],
 };
 
+let detached_options_tsuite = {
+    name: "TextDecoder() detach in options test",
+    skip: () => !is_detach_available(),
+
+    T: async (params) => {
+        let td = new TextDecoder('utf-8');
+        let uint8 = new Uint8Array(4096);
+        let input = params.buffer ? uint8.buffer : uint8;
+        let options = {
+            get stream() {
+                detach(uint8.buffer);
+                return false;
+            }
+        };
+
+        try {
+            td.decode(input, options);
+
+        } catch (e) {
+            if (e.toString().startsWith('TypeError:')) {
+                return 'SUCCESS';
+            }
+
+            throw e;
+        }
+
+        throw Error('Expected TypeError not thrown');
+    },
+
+    tests: [
+        { buffer: false },
+        { buffer: true },
+    ],
+};
+
 
 run([
     stream_tsuite,
@@ -236,5 +271,6 @@ run([
     reuse_tsuite,
     ignoreBOM_tsuite,
     detached_tsuite,
+    detached_options_tsuite,
 ])
 .then($DONE, $DONE);