From: Dmitry Volyntsev Date: Wed, 2 Dec 2026 00:04:55 +0000 (-0800) Subject: TextEncoder: check Uint8Array before reading buffer in QuickJS X-Git-Url: https://git.kaiwu.me/web/http/static/gitweb.js?a=commitdiff_plain;h=11ddffcdd07a1ba67b61e9f524f915700a60cb7a;p=njs.git TextEncoder: check Uint8Array before reading buffer in QuickJS Previously, qjs_text_encoder_encode_into() extracted the destination buffer pointer and length with qjs_typed_array_data() before calling qjs_is_uint8_array(). Because qjs_is_uint8_array() looks up globalThis.Uint8Array and calls JS_IsInstanceOf(), a getter or a Symbol.hasInstance handler could detach or transfer the underlying ArrayBuffer, so the subsequent UTF-8 encoding loop wrote through a stale pointer. In addition, an exception thrown during the check was ignored: encodeInto() returned a result with the exception left pending. Perform the Uint8Array check and propagate its exceptions before extracting the destination buffer pointer and length. The issue was introduced in 447d66d4 (0.8.10). This closes #1177 issue on Github. --- diff --git a/src/qjs.c b/src/qjs.c index 99a732d1..c336bc2a 100644 --- a/src/qjs.c +++ b/src/qjs.c @@ -1156,7 +1156,7 @@ static JSValue qjs_text_encoder_encode_into(JSContext *cx, JSValueConst this_val, int argc, JSValueConst *argv) { - int read, written; + int rc, read, written; void *te; size_t size; u_char *to, *to_end; @@ -1175,16 +1175,21 @@ qjs_text_encoder_encode_into(JSContext *cx, JSValueConst this_val, int argc, return JS_ThrowTypeError(cx, "The input argument must be a string"); } + rc = qjs_is_uint8_array(cx, argv[1]); + if (rc <= 0) { + if (rc == 0) { + return JS_ThrowTypeError(cx, "The output argument must be a" + " Uint8Array"); + } + + return JS_EXCEPTION; + } + ret = qjs_typed_array_data(cx, argv[1], &dst); if (JS_IsException(ret)) { return ret; } - if (!qjs_is_uint8_array(cx, argv[1])) { - return JS_ThrowTypeError(cx, "The output argument must be a" - " Uint8Array"); - } - utf8.start = (u_char *) JS_ToCStringLen(cx, &utf8.length, argv[0]); if (utf8.start == NULL) { return JS_EXCEPTION; diff --git a/test/text_encoder.t.js b/test/text_encoder.t.js index f9274ceb..2d824562 100644 --- a/test/text_encoder.t.js +++ b/test/text_encoder.t.js @@ -73,9 +73,24 @@ let detached_tsuite = { T: async (params) => { let td = new TextEncoder(); - let uint8 = new Uint8Array(10); - - detach(uint8.buffer); + let uint8 = new Uint8Array(params.size || 10); + + if (params.hasInstance) { + Object.defineProperty(Uint8Array, Symbol.hasInstance, { + value: () => { + if (params.throws) { + throw new TypeError('hasInstance'); + } + + detach(uint8.buffer); + return true; + }, + configurable: true + }); + + } else { + detach(uint8.buffer); + } try { td.encodeInto("test", uint8); @@ -86,6 +101,9 @@ let detached_tsuite = { } else { throw e; } + + } finally { + delete Uint8Array[Symbol.hasInstance]; } throw Error('Expected TypeError not thrown'); @@ -93,6 +111,10 @@ let detached_tsuite = { tests: [ { }, + { hasInstance: true, size: 4096, + skip: () => (typeof njs == 'undefined' || njs.engine != 'QuickJS') }, + { hasInstance: true, throws: true, + skip: () => (typeof njs == 'undefined' || njs.engine != 'QuickJS') }, ], };