]> git.kaiwu.me - njs.git/commitdiff
HTTP: fix missing validation of subrequest and redirect arguments
authorbasavaraj-sm05 <basavaraj@digiscrypt.com>
Tue, 22 Sep 2026 14:10:16 +0000 (19:40 +0530)
committerDmitry Volyntsev <xeioexception@gmail.com>
Sat, 14 Nov 2026 02:24:42 +0000 (18:24 -0800)
Previously, the target of r.subrequest() and r.internalRedirect() was
checked by ngx_http_parse_unsafe_uri(), which inspects only the path.
The arguments, both the part after "?" and the args argument of
r.subrequest(), were stored in r->args as is.  Values of r.args are
percent-decoded, so "q=" + r.args.q could carry CR and LF into
r->args, and the proxy module copies it into the upstream request line
byte for byte.  The upstream saw injected headers or a second request.

Now the arguments are rejected if they contain a C0 control character,
space, or DEL, as ngx.fetch() does for its request target.
r.subrequest() throws a TypeError and r.internalRedirect() fails the
request with 500.  Percent-encoded values are left unchanged.

ngx_js_check_request_line_component() is renamed to
ngx_js_check_no_space_or_ctrl_chars(), because it validates a method
and arguments as well as a request target.

nginx/ngx_http_js_module.c
nginx/ngx_js_fetch.c
nginx/ngx_js_http.c
nginx/ngx_js_http.h
nginx/ngx_qjs_fetch.c
nginx/t/js_internal_redirect.t
nginx/t/js_subrequests.t

index ad9649ff4beda56891c8634cbec9a8dec883a8b7..4f9ac9b2f33e3330a8dd31abe8c0a5945953eb8b 100644 (file)
@@ -1853,6 +1853,12 @@ ngx_http_js_internal_redirect(ngx_http_request_t *r, ngx_http_js_ctx_t *ctx)
         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }
 
+    if (ngx_js_check_no_space_or_ctrl_chars(args.data, args.len) != NGX_OK) {
+        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                      "unsafe URI arguments were detected");
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
+    }
+
     return ngx_http_internal_redirect(r, &ctx->redirect_uri, &args);
 }
 
@@ -7902,7 +7908,9 @@ ngx_http_qjs_ext_subrequest(JSContext *cx, JSValueConst this_val,
 
     flags = NGX_HTTP_LOG_UNSAFE;
 
-    if (ngx_http_parse_unsafe_uri(r, &uri, &args, &flags) != NGX_OK) {
+    if (ngx_http_parse_unsafe_uri(r, &uri, &args, &flags) != NGX_OK
+        || ngx_js_check_no_space_or_ctrl_chars(args.data, args.len) != NGX_OK)
+    {
         return JS_ThrowTypeError(cx, "unsafe uri");
     }
 
@@ -10421,7 +10429,10 @@ ngx_http_js_parse_unsafe_uri(ngx_http_request_t *r, njs_str_t *uri,
     args_arg.data = args->start;
     args_arg.len = args->length;
 
-    if (ngx_http_parse_unsafe_uri(r, &uri_arg, &args_arg, &flags) != NGX_OK) {
+    if (ngx_http_parse_unsafe_uri(r, &uri_arg, &args_arg, &flags) != NGX_OK
+        || ngx_js_check_no_space_or_ctrl_chars(args_arg.data, args_arg.len)
+           != NGX_OK)
+    {
         return NGX_ERROR;
     }
 
index 91ad5d5fb7d91a4444101873a0fb7ca2c6fdad2d..ad786d4acb1a875c3257229769299d1a64e6d45a 100644 (file)
@@ -642,7 +642,7 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         resolve_host = &u.host;
     }
 
-    if (ngx_js_check_request_line_component(u.uri.data, u.uri.len) != NGX_OK) {
+    if (ngx_js_check_no_space_or_ctrl_chars(u.uri.data, u.uri.len) != NGX_OK) {
         njs_vm_type_error(vm, "invalid url");
         goto fail;
     }
@@ -923,7 +923,7 @@ ngx_js_method_process(njs_vm_t *vm, ngx_js_request_t *request)
     str.length = request->method.len;
 
     if (request->method.len == 0
-        || ngx_js_check_request_line_component(request->method.data,
+        || ngx_js_check_no_space_or_ctrl_chars(request->method.data,
                                                request->method.len)
            != NGX_OK)
     {
index 7fc029938875bda1157466c410a105d127480e66..b6a2de0903ecb12b00ded401d8e5ec85a243c6de 100644 (file)
@@ -1883,7 +1883,7 @@ ngx_js_check_header_name(u_char *name, size_t len)
 
 
 ngx_int_t
-ngx_js_check_request_line_component(u_char *value, size_t len)
+ngx_js_check_no_space_or_ctrl_chars(u_char *value, size_t len)
 {
     u_char  *p, *end;
 
index 939c5ed4f1b2f65e72a7f4db185029e9ba0c6efc..564bbfd9ffa768e4507b6a8f5f81c57037b6c7cc 100644 (file)
@@ -191,7 +191,7 @@ void ngx_js_http_trim(u_char **value, size_t *len,
     int trim_c0_control_or_space);
 void ngx_js_http_trim_ows(u_char **value, size_t *len);
 ngx_int_t ngx_js_check_header_name(u_char *name, size_t len);
-ngx_int_t ngx_js_check_request_line_component(u_char *value, size_t len);
+ngx_int_t ngx_js_check_no_space_or_ctrl_chars(u_char *value, size_t len);
 ngx_int_t ngx_js_check_header_value(u_char *value, size_t len);
 const char *ngx_js_headers_error(ngx_js_headers_rc_t rc);
 ngx_js_headers_rc_t ngx_js_headers_modify(ngx_pool_t *pool,
index ef3a927405d19b542f400f630a5a37ac2b1eb7cb..84fee7f268196b07544dc230a54038b62e69e848 100644 (file)
@@ -422,7 +422,7 @@ ngx_qjs_ext_fetch(JSContext *cx, JSValueConst this_val, int argc,
         resolve_host = &u.host;
     }
 
-    if (ngx_js_check_request_line_component(u.uri.data, u.uri.len) != NGX_OK) {
+    if (ngx_js_check_no_space_or_ctrl_chars(u.uri.data, u.uri.len) != NGX_OK) {
         JS_ThrowTypeError(cx, "invalid url");
         goto fail;
     }
@@ -1383,7 +1383,7 @@ ngx_qjs_method_process(JSContext *cx, ngx_js_request_t *request)
     };
 
     if (request->method.len == 0
-        || ngx_js_check_request_line_component(request->method.data,
+        || ngx_js_check_no_space_or_ctrl_chars(request->method.data,
                                                request->method.len)
            != NGX_OK)
     {
index 721113bb43dcb70bafe2ff96f02f7f48a1576590..277babdc7799993a0477f97c8531ea9c023dccd0 100644 (file)
@@ -82,6 +82,9 @@ $t->write_file('test.js', <<EOF);
         } else if (r.variables.arg_quoted) {
             r.internalRedirect('/red%69rect');
 
+        } else if (r.variables.arg_decoded) {
+            r.internalRedirect('/redirect?b=' + r.args.decoded);
+
         } else {
             if (r.variables.arg_a) {
                 r.internalRedirect('/redirect?b=' + r.variables.arg_a);
@@ -105,7 +108,7 @@ $t->write_file('test.js', <<EOF);
 
 EOF
 
-$t->try_run('no njs available')->plan(5);
+$t->try_run('no njs available')->plan(6);
 
 ###############################################################################
 
@@ -117,6 +120,8 @@ like(http_get('/test?unsafe=1'), qr/500 Internal Server/s,
        'unsafe redirect');
 like(http_get('/test?quoted=1'), qr/200 .*redirect/s,
        'quoted redirect');
+like(http_get('/test?decoded=A%0d%0aInjected:%201'), qr/500 Internal Server/s,
+       'unsafe redirect args');
 get('/destroyed_ctx', 'If-Match: tt');
 
 ###############################################################################
index eaabf7cd878c1cdad4a7d1aa6e751438234d095f..208210c2604a0ac1352349f49b597b4f44673b40 100644 (file)
@@ -161,6 +161,14 @@ http {
             js_content test.sr_unsafe;
         }
 
+        location /sr_unsafe_args {
+            js_content test.sr_unsafe_args;
+        }
+
+        location /sr_unsafe_uri_args {
+            js_content test.sr_unsafe_uri_args;
+        }
+
         location /sr_broken {
             js_content test.sr_broken;
         }
@@ -422,6 +430,16 @@ $t->write_file('test.js', <<EOF);
         r.return(200);
     }
 
+    function sr_unsafe_args(r) {
+        r.subrequest('/p/sub1', 'a=' + r.args.a);
+        r.return(200);
+    }
+
+    function sr_unsafe_uri_args(r) {
+        r.subrequest('/p/sub1?a=' + r.args.a);
+        r.return(200);
+    }
+
     function sr_broken(r) {
         r.subrequest('/daemon/unfinished', reply => {
             r.return(200, JSON.stringify({code:reply.status}));
@@ -545,13 +563,14 @@ $t->write_file('test.js', <<EOF);
                     sr_js_in_subrequest, sr_js_in_subrequest_pr, js_sub,
                     sr_in_sr_callback, sr_out_of_order, sr_except_not_a_func,
                     sr_uri_except, sr_except_failed_to_convert_options_arg,
-                    sr_unsafe, sr_error_in_callback, sr_limit};
+                    sr_unsafe, sr_unsafe_args, sr_unsafe_uri_args,
+                    sr_error_in_callback, sr_limit};
 
 EOF
 
 $t->write_file('t', '["SEE-THIS"]');
 
-$t->try_run('no njs available')->plan(35);
+$t->try_run('no njs available')->plan(37);
 $t->run_daemon(\&http_daemon);
 
 ###############################################################################
@@ -615,6 +634,11 @@ like(http_get('/sr_unsafe'), qr/500/s, 'unsafe subrequest uri');
 
 }
 
+like(http_get('/sr_unsafe_args?a=x%0d%0aInjected:%201'), qr/500/s,
+       'unsafe subrequest args');
+like(http_get('/sr_unsafe_uri_args?a=x%0d%0aInjected:%201'), qr/500/s,
+       'unsafe subrequest uri args');
+
 TODO: {
 local $TODO = 'not yet' unless has_version('0.8.5');