]> git.kaiwu.me - nginx.git/commitdiff
Proxy: fixed segfault in URI change.
authorSergey Kandaurov <pluknet@nginx.com>
Mon, 24 Nov 2025 11:57:09 +0000 (15:57 +0400)
committerSergey Kandaurov <s.kandaurov@f5.com>
Tue, 23 Dec 2025 18:40:33 +0000 (22:40 +0400)
If request URI was shorter than location prefix, as after replacement
with try_files, location length was used to copy the remaining URI part
leading to buffer overread.

The fix is to replace full request URI in this case.  In the following
configuration, request "/123" is changed to "/" when sent to backend.

    location /1234 {
        try_files /123 =404;
        proxy_pass http://127.0.0.1:8080/;
    }

Closes #983 on GitHub.

src/http/modules/ngx_http_proxy_module.c

index 7ee5ff3e878c6f278931a8110baedd90ab552137..0778ec728d50724ab16d761b82f33bf746f6e048 100644 (file)
@@ -1206,7 +1206,8 @@ ngx_http_proxy_create_key(ngx_http_request_t *r)
         return NGX_OK;
     }
 
-    loc_len = (r->valid_location && ctx->vars.uri.len) ? plcf->location.len : 0;
+    loc_len = (r->valid_location && ctx->vars.uri.len)
+              ? ngx_min(plcf->location.len, r->uri.len) : 0;
 
     if (r->quoted_uri || r->internal) {
         escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,
@@ -1318,8 +1319,8 @@ ngx_http_proxy_create_request(ngx_http_request_t *r)
         uri_len = r->unparsed_uri.len;
 
     } else {
-        loc_len = (r->valid_location && ctx->vars.uri.len) ?
-                      plcf->location.len : 0;
+        loc_len = (r->valid_location && ctx->vars.uri.len)
+                  ? ngx_min(plcf->location.len, r->uri.len) : 0;
 
         if (r->quoted_uri || r->internal) {
             escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,