From: Dmitry Volyntsev Date: Wed, 11 Nov 2026 22:24:35 +0000 (-0800) Subject: Stream: check optional arguments in QuickJS direct send methods X-Git-Url: https://git.kaiwu.me/http/%7Bsubstring-before(@doc,%20'.xml')%7D.html?a=commitdiff_plain;h=196711b01b0c9843ab248743459f7769ec2b8506;p=njs.git Stream: check optional arguments in QuickJS direct send methods 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). --- diff --git a/nginx/ngx_stream_js_module.c b/nginx/ngx_stream_js_module.c index aac1ebe5..9c97bfc5 100644 --- a/nginx/ngx_stream_js_module.c +++ b/nginx/ngx_stream_js_module.c @@ -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; diff --git a/nginx/t/stream_js_send_async.t b/nginx/t/stream_js_send_async.t index 8c4c2aeb..b30cdf2b 100644 --- a/nginx/t/stream_js_send_async.t +++ b/nginx/t/stream_js_send_async.t @@ -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; } }