[nginx] Disabled control characters and space in header names.

Maxim Dounin mdounin at mdounin.ru
Mon Jun 28 18:36:35 UTC 2021


details:   https://hg.nginx.org/nginx/rev/41f4bd4c51f1
branches:  
changeset: 7883:41f4bd4c51f1
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Mon Jun 28 18:01:18 2021 +0300
description:
Disabled control characters and space in header names.

Control characters (0x00-0x1f, 0x7f), space, and colon were never allowed in
header names.  The only somewhat valid use is header continuation which nginx
never supported and which is explicitly obsolete by RFC 7230.

Previously, such headers were considered invalid and were ignored by default
(as per ignore_invalid_headers directive).  With this change, such headers
are unconditionally rejected.

It is expected to make nginx more resilient to various attacks, in particular,
with ignore_invalid_headers switched off (which is inherently unsecure, though
nevertheless sometimes used in the wild).

diffstat:

 src/http/modules/ngx_http_grpc_module.c |  2 +-
 src/http/ngx_http_parse.c               |  4 ++--
 src/http/v2/ngx_http_v2.c               |  2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diffs (45 lines):

diff -r b4073527be81 -r 41f4bd4c51f1 src/http/modules/ngx_http_grpc_module.c
--- a/src/http/modules/ngx_http_grpc_module.c	Mon Jun 28 18:01:15 2021 +0300
+++ b/src/http/modules/ngx_http_grpc_module.c	Mon Jun 28 18:01:18 2021 +0300
@@ -3384,7 +3384,7 @@ ngx_http_grpc_validate_header_name(ngx_h
             return NGX_ERROR;
         }
 
-        if (ch == '\0' || ch == CR || ch == LF) {
+        if (ch <= 0x20 || ch == 0x7f) {
             return NGX_ERROR;
         }
     }
diff -r b4073527be81 -r 41f4bd4c51f1 src/http/ngx_http_parse.c
--- a/src/http/ngx_http_parse.c	Mon Jun 28 18:01:15 2021 +0300
+++ b/src/http/ngx_http_parse.c	Mon Jun 28 18:01:18 2021 +0300
@@ -893,7 +893,7 @@ ngx_http_parse_header_line(ngx_http_requ
                     break;
                 }
 
-                if (ch == '\0') {
+                if (ch <= 0x20 || ch == 0x7f || ch == ':') {
                     return NGX_HTTP_PARSE_INVALID_HEADER;
                 }
 
@@ -961,7 +961,7 @@ ngx_http_parse_header_line(ngx_http_requ
                 break;
             }
 
-            if (ch == '\0') {
+            if (ch <= 0x20 || ch == 0x7f) {
                 return NGX_HTTP_PARSE_INVALID_HEADER;
             }
 
diff -r b4073527be81 -r 41f4bd4c51f1 src/http/v2/ngx_http_v2.c
--- a/src/http/v2/ngx_http_v2.c	Mon Jun 28 18:01:15 2021 +0300
+++ b/src/http/v2/ngx_http_v2.c	Mon Jun 28 18:01:18 2021 +0300
@@ -3457,7 +3457,7 @@ ngx_http_v2_validate_header(ngx_http_req
             continue;
         }
 
-        if (ch == '\0' || ch == LF || ch == CR || ch == ':'
+        if (ch <= 0x20 || ch == 0x7f || ch == ':'
             || (ch >= 'A' && ch <= 'Z'))
         {
             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,


More information about the nginx-devel mailing list