]> git.kaiwu.me - njs.git/commitdiff
Modules: skip the proxy CONNECT tunnel when no proxy is selected master
authorbasavaraj-sm05 <basavaraj@digiscrypt.com>
Sat, 21 Nov 2026 12:31:38 +0000 (18:01 +0530)
committerDmitry Volyntsev <xeioexception@gmail.com>
Wed, 25 Nov 2026 06:09:18 +0000 (22:09 -0800)
ngx_js_http_connect() and the write handler chose between a forward
proxy CONNECT tunnel and a direct TLS handshake from
ngx_js_conf_proxy(), which only reports that js_fetch_proxy is
configured for the location.

When the proxy URL comes from a variable it is evaluated per request
and an empty value selects a direct connection.  An "https" fetch in
such a location then connected to the origin while still being
treated as proxied: the cleartext CONNECT request was written to the
origin and the TLS handshake was never started.

Use ngx_js_http_proxy(), the per-request state the rest of the fetch
code already uses, in the connect and write paths.  A static
js_fetch_proxy is unaffected because both predicates agree there.

nginx/ngx_js_http.c
nginx/t/js_fetch_proxy_https.t

index b6a2de0903ecb12b00ded401d8e5ec85a243c6de..d9fb8e9612a279db724e5af0ec08802ce85fa7f6 100644 (file)
@@ -316,7 +316,7 @@ ngx_js_http_connect(ngx_js_http_t *http)
     ngx_add_timer(c->write, http->conf->timeout);
 
 #if (NGX_SSL)
-    if (ngx_js_conf_proxy(http->conf) && http->ssl != NULL && c->ssl == NULL) {
+    if (ngx_js_http_proxy(http) && http->ssl != NULL && c->ssl == NULL) {
         http->proxy.pending = ngx_js_chain_to_buf(http->pool, &http->chain);
         if (http->proxy.pending == NULL) {
             ngx_js_http_error(http, "memory error");
@@ -330,14 +330,14 @@ ngx_js_http_connect(ngx_js_http_t *http)
         http->process = ngx_js_http_process_connect_response;
 
     } else {
-        if (ngx_js_conf_proxy(http->conf) && http->ssl != NULL && c->ssl != NULL) {
+        if (ngx_js_http_proxy(http) && http->ssl != NULL && c->ssl != NULL) {
             http->proxy.state = HTTP_STATE_PROXY_TUNNEL_READY;
         }
 
         http->process = ngx_js_http_process_status_line;
     }
 
-    if (http->ssl != NULL && c->ssl == NULL && !ngx_js_conf_proxy(http->conf)) {
+    if (http->ssl != NULL && c->ssl == NULL && !ngx_js_http_proxy(http)) {
         ngx_js_http_ssl_init_connection(http);
         return;
     }
@@ -654,7 +654,7 @@ ngx_js_http_write_handler(ngx_event_t *wev)
 
 #if (NGX_SSL)
     if (http->ssl != NULL && http->peer.connection->ssl == NULL
-        && !ngx_js_conf_proxy(http->conf))
+        && !ngx_js_http_proxy(http))
     {
         ngx_js_http_ssl_init_connection(http);
         return;
index 7b4d10ed0073c77c57b8ceb7f8764c22e574b509..2593e0579308e2b3b3b64956960aadf6eaebcb26 100644 (file)
@@ -72,6 +72,12 @@ http {
         location /https_no_proxy {
             js_content test.https_fetch;
         }
+
+        location /https_empty_variable_proxy {
+            set $proxy_url "";
+            js_fetch_proxy $proxy_url;
+            js_content test.https_fetch;
+        }
     }
 
     server {
@@ -192,7 +198,7 @@ foreach my $name ('example.com') {
                . $t->read_file('intermediate.crt'));
 }
 
-$t->try_run('no js_fetch_proxy')->plan(9);
+$t->try_run('no js_fetch_proxy')->plan(10);
 
 $t->run_daemon(\&https_proxy_daemon, $p2, 1);
 $t->run_daemon(\&https_proxy_daemon, $p4, 0);
@@ -218,6 +224,9 @@ like(http_get('/https_via_proxy_no_auth?domain=example.com'), qr/ORIGIN/,
 like(http_get('/https_no_proxy?domain=example.com'), qr/ORIGIN/,
        'https without proxy');
 
+like(http_get('/https_empty_variable_proxy?domain=example.com'), qr/ORIGIN/,
+       'https with empty variable proxy connects directly');
+
 like(http_get('/https_via_proxy?domain=nonexistent.dest.domain'),
        qr/connect failed/, 'https through proxy nonexistent.dest.domain');
 like(http_get('/https_via_broken_proxy?domain=example.com'),