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);
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;
}
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;
}
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]);
js_engine qjs;
js_content test.test;
}
+
+ location /parent {
+ js_content test.parent;
+ }
+
+ location /child {
+ js_engine qjs;
+ js_content test.child;
+ }
}
server {
js_engine njs;
js_content test.test;
}
+
+ location /parent {
+ js_content test.parent;
+ }
+
+ location /child {
+ js_engine njs;
+ js_content test.child;
+ }
}
}
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);
###############################################################################
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();
--- /dev/null
+#!/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');
+
+###############################################################################