]> git.kaiwu.me - njs.git/commitdiff
Stream: check optional arguments in QuickJS direct send methods
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 11 Nov 2026 22:24:35 +0000 (14:24 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Thu, 12 Nov 2026 22:29:31 +0000 (14:29 -0800)
sendUpstream() and sendDownstream() have a declared length of one,
but their shared handler accessed the second argument unconditionally.
QuickJS pads missing arguments only up to the declared length, so calls
without options could read beyond the argument array and crash.

Check the argument count before accessing the optional options object,
without changing the methods length.  The issue was introduced in
201b1276 (Modules: introduced QuickJS engine).

nginx/ngx_stream_js_module.c
nginx/t/stream_js_send_async.t

index aac1ebe537dd2c0ae01f0c6f649200d90d4c6a15..9c97bfc57db51918a0e6ede197862cc5f86374ae 100644 (file)
@@ -2439,7 +2439,7 @@ ngx_stream_qjs_ext_send(JSContext *cx, JSValueConst this_val, int argc,
         last_buf = 0;
     }
 
-    if (JS_IsObject(argv[1])) {
+    if (argc > 1 && JS_IsObject(argv[1])) {
         val = JS_GetPropertyStr(cx, argv[1], "flush");
         if (JS_IsException(val)) {
             return JS_EXCEPTION;
index 8c4c2aeb18b182a976cad803cbebabf5de24cf20..b30cdf2bb16bd1e16952912807a1379147324a11 100644 (file)
@@ -56,6 +56,12 @@ stream {
         js_filter   test.send_direct;
         proxy_pass  127.0.0.1:8090;
     }
+
+    server {
+        listen      127.0.0.1:8084;
+        js_filter   test.send_upstream;
+        proxy_pass  127.0.0.1:8090;
+    }
 }
 
 EOF
@@ -98,11 +104,28 @@ $t->write_file('test.js', <<'EOF');
         });
     }
 
-    export default {send, send_options, send_direct};
+    function send_upstream(s) {
+        const stale = {
+            get flush() {
+                throw Error('stale options');
+            }
+        };
+
+        later(s, () => {
+            /*
+             * Leaves an options-like object in the argument slot that
+             * sendUpstream() could read past its declared length.
+             */
+            Math.max(0, stale);
+            s.sendUpstream('up');
+        });
+    }
+
+    export default {send, send_options, send_direct, send_upstream};
 EOF
 
 $t->run_daemon(\&stream_daemon, port(8090));
-$t->try_run('no stream njs available')->plan(3);
+$t->try_run('no stream njs available')->plan(4);
 $t->waitforsocket('127.0.0.1:' . port(8090));
 
 ###############################################################################
@@ -113,6 +136,8 @@ is(stream('127.0.0.1:' . port(8082))->io('go'), 'TypeError',
     'async send with options without direction');
 is(stream('127.0.0.1:' . port(8083))->io('go', read => 2), 'ab',
     'async send with direction');
+is(stream('127.0.0.1:' . port(8084))->io('go', read => 2), 'up',
+    'async send upstream without options');
 
 $t->stop();
 
@@ -132,6 +157,7 @@ sub stream_daemon {
     while (my $client = $server->accept()) {
         $client->autoflush(1);
         $client->sysread(my $buffer, 65536);
+        $client->syswrite($buffer) if $buffer eq 'up';
         close $client;
     }
 }