]> git.kaiwu.me - nginx.git/commitdiff
Stream ssl_preread: forbid duplicate SNI and ALPN
authorAleksei Bavshin <a.bavshin@nginx.com>
Tue, 29 Sep 2026 16:19:30 +0000 (09:19 -0700)
committerRoman Arutyunyan <arutyunyan.roman@gmail.com>
Wed, 11 Nov 2026 12:14:17 +0000 (16:14 +0400)
As stated in the TLS Extensions specification (RFC 3546) and all
subsequent TLS specification updates,

: There MUST NOT be more than one extension of the same type.

Such a ClientHello is now treated as malformed, as with other parsing
errors.

src/stream/ngx_stream_ssl_preread_module.c

index 8f2d333d9f8298d31094714a06a461e742eb52c6..7aa86699ded49a6ff1d57b14428ae6cd6e1d9031 100644 (file)
@@ -358,16 +358,30 @@ ngx_stream_ssl_preread_parse_record(ngx_stream_ssl_preread_ctx_t *ctx,
         case sw_ext_header:
             ext = (p[2] << 8) + p[3];
 
-            if (p[0] == 0 && p[1] == 0 && ctx->host.data == NULL) {
+            if (p[0] == 0 && p[1] == 0) {
                 /* SNI extension */
+
+                if (ctx->host.data != NULL) {
+                    ngx_log_debug0(NGX_LOG_DEBUG_STREAM, ctx->log, 0,
+                                   "ssl preread: duplicate SNI");
+                    return NGX_DECLINED;
+                }
+
                 state = sw_sni_len;
                 dst = p;
                 size = 2;
                 break;
             }
 
-            if (p[0] == 0 && p[1] == 16 && ctx->alpn.data == NULL) {
+            if (p[0] == 0 && p[1] == 16) {
                 /* ALPN extension */
+
+                if (ctx->alpn.data != NULL) {
+                    ngx_log_debug0(NGX_LOG_DEBUG_STREAM, ctx->log, 0,
+                                   "ssl preread: duplicate ALPN");
+                    return NGX_DECLINED;
+                }
+
                 state = sw_alpn_len;
                 dst = p;
                 size = 2;