[njs] Restricted usage of r.subrequest() and r.parent.

Dmitry Volyntsev xeioex at nginx.com
Mon Jul 23 15:05:15 UTC 2018


details:   http://hg.nginx.org/njs/rev/673d8a9c92e2
branches:  
changeset: 566:673d8a9c92e2
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Jul 23 18:04:53 2018 +0300
description:
Restricted usage of r.subrequest() and r.parent.

Thanks to ??? (Hong Zhi Dao).

diffstat:

 nginx/ngx_http_js_module.c |  22 ++++++++++++++++------
 1 files changed, 16 insertions(+), 6 deletions(-)

diffs (56 lines):

diff -r b1d34de39abd -r 673d8a9c92e2 nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Mon Jul 23 16:35:32 2018 +0300
+++ b/nginx/ngx_http_js_module.c	Mon Jul 23 18:04:53 2018 +0300
@@ -1825,7 +1825,7 @@ ngx_http_js_ext_get_response(njs_vm_t *v
 
     ctx = ngx_http_get_module_ctx(r, ngx_http_js_module);
 
-    njs_vm_retval_set(ctx->vm, njs_value_arg(&ctx->args[1]));
+    njs_vm_retval_set(vm, njs_value_arg(&ctx->args[1]));
 
     return NJS_OK;
 }
@@ -1840,6 +1840,7 @@ ngx_http_js_ext_subrequest(njs_vm_t *vm,
     ngx_uint_t                cb_index, method, n, has_body;
     njs_value_t              *arg2, *options, *value;
     njs_function_t           *callback;
+    ngx_http_js_ctx_t        *ctx;
     ngx_http_request_t       *r, *sr;
     ngx_http_request_body_t  *rb;
 
@@ -1875,6 +1876,14 @@ ngx_http_js_ext_subrequest(njs_vm_t *vm,
 
     r = njs_value_data(njs_argument(args, 0));
 
+    ctx = ngx_http_get_module_ctx(r, ngx_http_js_module);
+
+    if (ctx->vm != vm) {
+        njs_vm_error(vm, "subrequest can only be created for "
+                         "the primary request");
+        return NJS_ERROR;
+    }
+
     if (njs_vm_value_to_ext_string(vm, &uri_arg, njs_argument(args, 1), 0)
         == NJS_ERROR)
     {
@@ -2150,14 +2159,15 @@ ngx_http_js_ext_get_parent(njs_vm_t *vm,
 
     r = (ngx_http_request_t *) obj;
 
-    ctx = ngx_http_get_module_ctx(r->parent, ngx_http_js_module);
-
-    if (ctx == NULL) {
-        njs_vm_error(vm, "failed to get the parent context");
+    ctx = r->parent ? ngx_http_get_module_ctx(r->parent, ngx_http_js_module)
+                    : NULL;
+
+    if (ctx == NULL || ctx->vm != vm) {
+        njs_vm_error(vm, "parent can only be returned for a subrequest");
         return NJS_ERROR;
     }
 
-    njs_vm_retval_set(ctx->vm, njs_value_arg(&ctx->args[0]));
+    njs_vm_retval_set(vm, njs_value_arg(&ctx->args[0]));
 
     return NJS_OK;
 }


More information about the nginx-devel mailing list