From: basavaraj-sm05 Date: Tue, 22 Sep 2026 14:10:16 +0000 (+0530) Subject: HTTP: fix missing validation of subrequest and redirect arguments X-Git-Url: https://git.kaiwu.me/http/%7Bsubstring-before(@doc,%20'.xml')%7D.html?a=commitdiff_plain;h=418184c28e11c6e72171969cc417b94a60647466;p=njs.git HTTP: fix missing validation of subrequest and redirect arguments 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. --- diff --git a/nginx/ngx_http_js_module.c b/nginx/ngx_http_js_module.c index ad9649ff..4f9ac9b2 100644 --- a/nginx/ngx_http_js_module.c +++ b/nginx/ngx_http_js_module.c @@ -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; } diff --git a/nginx/ngx_js_fetch.c b/nginx/ngx_js_fetch.c index 91ad5d5f..ad786d4a 100644 --- a/nginx/ngx_js_fetch.c +++ b/nginx/ngx_js_fetch.c @@ -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) { diff --git a/nginx/ngx_js_http.c b/nginx/ngx_js_http.c index 7fc02993..b6a2de09 100644 --- a/nginx/ngx_js_http.c +++ b/nginx/ngx_js_http.c @@ -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; diff --git a/nginx/ngx_js_http.h b/nginx/ngx_js_http.h index 939c5ed4..564bbfd9 100644 --- a/nginx/ngx_js_http.h +++ b/nginx/ngx_js_http.h @@ -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, diff --git a/nginx/ngx_qjs_fetch.c b/nginx/ngx_qjs_fetch.c index ef3a9274..84fee7f2 100644 --- a/nginx/ngx_qjs_fetch.c +++ b/nginx/ngx_qjs_fetch.c @@ -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) { diff --git a/nginx/t/js_internal_redirect.t b/nginx/t/js_internal_redirect.t index 721113bb..277babdc 100644 --- a/nginx/t/js_internal_redirect.t +++ b/nginx/t/js_internal_redirect.t @@ -82,6 +82,9 @@ $t->write_file('test.js', <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'); ############################################################################### diff --git a/nginx/t/js_subrequests.t b/nginx/t/js_subrequests.t index eaabf7cd..208210c2 100644 --- a/nginx/t/js_subrequests.t +++ b/nginx/t/js_subrequests.t @@ -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', < { r.return(200, JSON.stringify({code:reply.status})); @@ -545,13 +563,14 @@ $t->write_file('test.js', <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');