]> git.kaiwu.me - njs.git/commitdiff
Modules: keep HTTP request JS state in its host context
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 11 Nov 2026 01:44:27 +0000 (17:44 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Thu, 12 Nov 2026 22:29:31 +0000 (14:29 -0800)
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.

nginx/ngx_http_js_module.c
nginx/t/js_engine.t
nginx/t/js_http_runtime_isolation.t [new file with mode: 0644]

index 8a9c00338d083875956fdfc2839284d3bee367ba..f85b0b657264eb82a73abc3e729edf30debbf4d1 100644 (file)
@@ -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]);
index e188cceafb4476eea256fe1cb604060cbfef9c1c..8ad7cd23b2df5d802bf1ef146920ec800659a428 100644 (file)
@@ -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', <<EOF);
         r.return(200, njs.engine);
     }
 
-    export default {njs: test_njs, test};
+    async function parent(r) {
+        const reply = await r.subrequest('/child');
+        let result = 'accepted';
+        try {
+            reply.setReturnValue({local: [1, 2, 3]});
+        } catch (e) {
+            result = e instanceof TypeError ? 'rejected' : e.name;
+        }
+        r.return(200, [njs.engine, reply.responseText,
+                       reply.parent === r, result].join(':'));
+    }
+
+    function child(r) {
+        r.return(200, njs.engine + ':' + (r.parent === undefined));
+    }
+
+    export default {njs: test_njs, test, parent, child};
 
 EOF
 
-$t->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 (file)
index 0000000..a8b979a
--- /dev/null
@@ -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');
+
+###############################################################################