From: basavaraj-sm05 Date: Sat, 21 Nov 2026 12:31:38 +0000 (+0530) Subject: Modules: skip the proxy CONNECT tunnel when no proxy is selected X-Git-Url: https://git.kaiwu.me/web/%22data:,/doc/static//%22%22)?a=commitdiff_plain;h=HEAD;p=njs.git Modules: skip the proxy CONNECT tunnel when no proxy is selected 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. --- diff --git a/nginx/ngx_js_http.c b/nginx/ngx_js_http.c index b6a2de09..d9fb8e96 100644 --- a/nginx/ngx_js_http.c +++ b/nginx/ngx_js_http.c @@ -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; diff --git a/nginx/t/js_fetch_proxy_https.t b/nginx/t/js_fetch_proxy_https.t index 7b4d10ed..2593e057 100644 --- a/nginx/t/js_fetch_proxy_https.t +++ b/nginx/t/js_fetch_proxy_https.t @@ -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'),