From: Dmitry Volyntsev Date: Wed, 11 Nov 2026 01:44:27 +0000 (-0800) Subject: Modules: keep HTTP request JS state in its host context X-Git-Url: https://git.kaiwu.me/http/%7Bsubstring-before(@doc,%20'.xml')%7D.html?a=commitdiff_plain;h=292556aa9d053b1f6dbc09b9f33d9ab9e9d9660a;p=njs.git Modules: keep HTTP request JS state in its host context A subrequest handler could receive the parent request JS object from another context. Changing that object used foreign QuickJS shapes, and setting a return value through a reply or parent stored a value from one context in another context result slot. Expose r.parent only on a reply whose parent is the executing request, which matches the documented API. Allow setReturnValue() only for the executing request. Check both engines so mixed njs and QuickJS locations never interpret each other values. The QuickJS parent getter dates back to 201b1276. Changed behaviour: - r.parent is undefined in a subrequest handler, as the API reference says. It used to be the parent request object of another context. - setReturnValue() called through a reply or a parent throws TypeError instead of storing the value in another request. --- diff --git a/nginx/ngx_http_js_module.c b/nginx/ngx_http_js_module.c index 8a9c0033..f85b0b65 100644 --- a/nginx/ngx_http_js_module.c +++ b/nginx/ngx_http_js_module.c @@ -3201,7 +3201,19 @@ ngx_http_js_ext_set_return_value(njs_vm_t *vm, njs_value_t *args, return NJS_ERROR; } + if (r != njs_vm_external_ptr(vm)) { + njs_vm_type_error(vm, "cannot set return value for another request"); + return NJS_ERROR; + } + ctx = ngx_http_get_module_ctx(r, ngx_http_js_module); + if (ctx == NULL || ctx->engine == NULL + || ctx->engine->type != NGX_ENGINE_NJS + || ctx->engine->u.njs.vm != vm) + { + njs_vm_type_error(vm, "request belongs to a different context"); + return NJS_ERROR; + } njs_value_assign(&ctx->retval, njs_arg(args, nargs, 1)); njs_value_undefined_set(retval); @@ -5117,10 +5129,16 @@ ngx_http_js_ext_get_parent(njs_vm_t *vm, njs_object_prop_t *prop, return NJS_DECLINED; } - ctx = r->parent ? ngx_http_get_module_ctx(r->parent, ngx_http_js_module) - : NULL; + if (r->parent == NULL || r->parent != njs_vm_external_ptr(vm)) { + njs_value_undefined_set(retval); + return NJS_DECLINED; + } - if (ctx == NULL) { + ctx = ngx_http_get_module_ctx(r->parent, ngx_http_js_module); + if (ctx == NULL || ctx->engine == NULL + || ctx->engine->type != NGX_ENGINE_NJS + || ctx->engine->u.njs.vm != vm) + { njs_value_undefined_set(retval); return NJS_DECLINED; } @@ -6529,10 +6547,15 @@ ngx_http_qjs_ext_parent(JSContext *cx, JSValueConst this_val) return JS_EXCEPTION; } - ctx = r->parent ? ngx_http_get_module_ctx(r->parent, ngx_http_js_module) - : NULL; + if (r->parent == NULL || r->parent != JS_GetContextOpaque(cx)) { + return JS_UNDEFINED; + } - if (ctx == NULL) { + ctx = ngx_http_get_module_ctx(r->parent, ngx_http_js_module); + if (ctx == NULL || ctx->engine == NULL + || ctx->engine->type != NGX_ENGINE_QJS + || ctx->engine->u.qjs.ctx != cx) + { return JS_UNDEFINED; } @@ -7594,7 +7617,18 @@ ngx_http_qjs_ext_set_return_value(JSContext *cx, JSValueConst this_val, return JS_EXCEPTION; } + if (r != JS_GetContextOpaque(cx)) { + return JS_ThrowTypeError(cx, + "cannot set return value for another request"); + } + ctx = ngx_http_get_module_ctx(r, ngx_http_js_module); + if (ctx == NULL || ctx->engine == NULL + || ctx->engine->type != NGX_ENGINE_QJS + || ctx->engine->u.qjs.ctx != cx) + { + return JS_ThrowTypeError(cx, "request belongs to a different context"); + } JS_FreeValue(cx, ngx_qjs_arg(ctx->retval)); ngx_qjs_arg(ctx->retval) = JS_DupValue(cx, argv[0]); diff --git a/nginx/t/js_engine.t b/nginx/t/js_engine.t index e188ccea..8ad7cd23 100644 --- a/nginx/t/js_engine.t +++ b/nginx/t/js_engine.t @@ -68,6 +68,15 @@ http { js_engine qjs; js_content test.test; } + + location /parent { + js_content test.parent; + } + + location /child { + js_engine qjs; + js_content test.child; + } } server { @@ -84,6 +93,15 @@ http { js_engine njs; js_content test.test; } + + location /parent { + js_content test.parent; + } + + location /child { + js_engine njs; + js_content test.child; + } } } @@ -98,11 +116,27 @@ $t->write_file('test.js', <try_run('no njs js_engine')->plan(4); +$t->try_run('no njs js_engine')->plan(6); ############################################################################### @@ -114,6 +148,11 @@ like(http_get('/njs/override'), qr/QuickJS/, 'js_engine override'); like(http_get('/qjs/test'), qr/QuickJS/, 'js_engine qjs server'); like(http_get('/qjs/override'), qr/njs/, 'js_engine override'); +like(http_get('/njs/parent'), qr/njs:QuickJS:true:true:rejected$/m, + 'njs host with QuickJS child'); +like(http_get('/qjs/parent'), qr/QuickJS:njs:true:true:rejected$/m, + 'QuickJS host with njs child'); + } $t->stop(); diff --git a/nginx/t/js_http_runtime_isolation.t b/nginx/t/js_http_runtime_isolation.t new file mode 100644 index 00000000..a8b979aa --- /dev/null +++ b/nginx/t/js_http_runtime_isolation.t @@ -0,0 +1,166 @@ +#!/usr/bin/perl + +# (C) Dmitry Volyntsev +# (C) F5, Inc. + +# Tests for HTTP request host bindings and JavaScript runtime isolation. + +############################################################################### + +use warnings; +use strict; +use Test::More; + +BEGIN { use FindBin; chdir($FindBin::Bin); } + +use lib 'lib'; +use Test::Nginx; + +############################################################################### + +select STDERR; $| = 1; +select STDOUT; $| = 1; + +my $t = Test::Nginx->new()->has(qw/http rewrite/) + ->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; +worker_processes 1; + +events { +} + +http { + %%TEST_GLOBALS_HTTP%% + + js_import test.js; + js_set $retval test.variable; + + server { + listen 127.0.0.1:8080; + server_name localhost; + + location /zero { + js_context_reuse 0; + js_content test.content; + } + + location /reuse { + js_context_reuse 1; + js_content test.content; + } + + location /zero_child { + js_context_reuse 0; + js_content test.child; + } + + location /reuse_child { + js_context_reuse 1; + js_content test.child; + } + + location /native { + return 200 native; + } + + location /variable { + return 200 $retval; + } + } +} + +EOF + +$t->write_file('test.js', <<'EOF'); + function reject_set(request, value) { + try { + request.setReturnValue(value); + return 'accepted'; + } catch (e) { + return e instanceof TypeError ? 'rejected' : e.name; + } + } + + async function content(r) { + const mode = r.args.mode; + const uri = mode === 'native_set' ? '/native' + : r.uri + '_child?mode=' + mode; + + if (mode === 'callback') { + r.subrequest(uri, reply => { + reply.parent.return(200, String(reply.parent === r)); + }); + return; + } + + const reply = await r.subrequest(uri); + if (mode === 'reply_set_object' || mode === 'native_set') { + r.return(200, reject_set(reply, {local: [1, 2, 3]})); + + } else if (mode === 'reply_set_string') { + r.return(200, reject_set(reply, 'local')); + + } else if (mode === 'own_set') { + reply.parent.setReturnValue('local'); + r.return(200, String(reply.parent === r)); + + } else { + r.return(200, reply.responseText + ':' + (reply.parent === r)); + } + } + + function child(r) { + const parent = r.parent; + if (parent === undefined) { + r.return(200, 'undefined'); + return; + } + + const mode = r.args.mode; + if (mode === 'shape') { + parent.localRuntimeProperty = {local: [1, 2, 3]}; + delete parent.localRuntimeProperty; + + } else if (mode === 'args') { + parent.args.local = r.args; + + } else if (mode === 'parent_set') { + parent.setReturnValue({local: [1, 2, 3]}); + } + + r.return(200, 'parent exposed'); + } + + async function variable(r) { + await Promise.resolve(); + r.setReturnValue('own result'); + } + + export default {content, child, variable}; +EOF + +$t->try_run('no njs available')->plan(19); + +for my $scope ('zero', 'reuse') { + for my $mode ('parent', 'shape', 'args', 'parent_set') { + like(http_get("/$scope?mode=$mode"), qr/undefined:true$/m, + "$scope $mode stays in host runtime"); + } + + for my $mode ('reply_set_object', 'reply_set_string', 'native_set') { + like(http_get("/$scope?mode=$mode"), qr/rejected$/m, + "$scope $mode rejects foreign result slot"); + } + + for my $mode ('callback', 'own_set') { + like(http_get("/$scope?mode=$mode"), qr/true$/m, + "$scope $mode preserves original parent"); + } +} + +like(http_get('/variable'), qr/own result$/m, 'own async js_set result'); + +###############################################################################