]> git.kaiwu.me - njs.git/commitdiff
TextEncoder: check Uint8Array before reading buffer in QuickJS
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 2 Dec 2026 00:04:55 +0000 (16:04 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Thu, 3 Dec 2026 01:27:35 +0000 (17:27 -0800)
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.

src/qjs.c
test/text_encoder.t.js

index 99a732d111ad6ab851ea23db432a934bb368ee6a..c336bc2a77e59d12e6f0ca4cf8b36d52e2e89138 100644 (file)
--- 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;
index f9274ceba5fbebe23ec8b16a3e76f431ba10001c..2d8245625c55f74570f6f607d4fc4fbbe6a5c59f 100644 (file)
@@ -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') },
     ],
 };