From: Dmitry Volyntsev Date: Mon, 16 Nov 2026 01:37:21 +0000 (-0800) Subject: TextDecoder: fix use-after-free on detach in decode() options X-Git-Url: https://git.kaiwu.me/%7Bsubstring-before(@doc,%20'.xml')%7D.html?a=commitdiff_plain;h=6e2028d12a6385b70945ca8714f2fdd8a2620798;p=njs.git TextDecoder: fix use-after-free on detach in decode() options 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). --- diff --git a/src/njs_encoding.c b/src/njs_encoding.c index 5135d02d..26dd816f 100644 --- a/src/njs_encoding.c +++ b/src/njs_encoding.c @@ -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) { diff --git a/src/qjs.c b/src/qjs.c index ccbf8865..7d70d13b 100644 --- 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) { diff --git a/test/text_decoder.t.js b/test/text_decoder.t.js index 737adadc..b4ac551c 100644 --- a/test/text_decoder.t.js +++ b/test/text_decoder.t.js @@ -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);