]> git.kaiwu.me - nginx.git/commitdiff
Upstream: detect premature plain text response from SSL backend.
authorRoman Arutyunyan <arut@nginx.com>
Thu, 29 Jan 2026 09:27:32 +0000 (13:27 +0400)
committerRoman Arutyunyan <arutyunyan.roman@gmail.com>
Wed, 4 Feb 2026 17:22:23 +0000 (21:22 +0400)
When connecting to a backend, the connection write event is triggered
first in most cases.  However if a response arrives quickly enough, both
read and write events can be triggered together within the same event loop
iteration.  In this case the read event handler is called first and the
write event handler is called after it.

SSL initialization for backend connections happens only in the write event
handler since SSL handshake starts with sending Client Hello.  Previously,
if a backend sent a quick plain text response, it could be parsed by the
read event handler prior to starting SSL handshake on the connection.
The change adds protection against parsing such responses on SSL-enabled
connections.

src/http/ngx_http_upstream.c

index df577ad6721a6c11616890f9c2c23808c2a461e7..cadc74479d4857a0fb124147b8bc08e51c29b811 100644 (file)
@@ -2508,6 +2508,15 @@ ngx_http_upstream_process_header(ngx_http_request_t *r, ngx_http_upstream_t *u)
             return;
         }
 
+#if (NGX_HTTP_SSL)
+        if (u->ssl && c->ssl == NULL) {
+            ngx_log_error(NGX_LOG_ERR, c->log, 0,
+                          "upstream prematurely sent response");
+            ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
+            return;
+        }
+#endif
+
         u->state->bytes_received += n;
 
         u->buffer.last += n;