]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: http/hardening: implement "reject-unexpected-request-upgrade"
authorWilly Tarreau <w@1wt.eu>
Wed, 25 Nov 2026 17:12:49 +0000 (18:12 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 25 Nov 2026 18:31:08 +0000 (19:31 +0100)
Rejects Upgrade and RFC8441 Extended CONNECT for protocols other than
"websocket" and "webtransport". For Upgrade, all offending tokens are
removed and the header will be removed if empty, so that it remains
harmless to clients sending tentative upgrades. Upgrade and CONNECT are
commonly abused to try to establish a tunnel with an application and
bypass the reverse-proxy layer, and most applications and browsers do
not implement other protocols, so there is no reason for letting them
pass through with common web applications. It is generally safe for
static web serving and simple internet-facing applications. This is
disabled by default. Note that tunnels established via standard CONNECT
are NOT blocked with this rule, see 'reject-advanced-methods' for this.

doc/configuration.txt
include/haproxy/http-t.h
src/h1.c
src/h2.c
src/http.c

index 7ac9e818ea1134b8bbce8833431bb577b9336c38..6c6a16d657e5e0fc40f2ba93d88f2a8e1dced576 100644 (file)
@@ -35061,6 +35061,7 @@ http-hardening <name> [from <source>]
         reject-forbidden-payload on
         reject-advanced-methods off
         reject-unexpected-request-payload off
+        reject-unexpected-request-upgrade off
 
   It is possible to change their settings from the defaults, by declaring the
   profiles once, along with the settings to change, in which case they will
@@ -35349,6 +35350,18 @@ http-hardening <name> [from <source>]
         static web serving and simple internet-facing applications. It is
         disabled by default.
 
+  - reject-unexpected-request-upgrade (inbound, request)
+        Rejects Upgrade and RFC8441 Extended CONNECT for protocols other than
+        "websocket" and "webtransport". For Upgrade, all offending tokens are
+        removed and the header will be removed if empty, so that it remains
+        harmless to clients sending tentative upgrades. Upgrade and CONNECT are
+        commonly abused to try to establish a tunnel with an application and
+        bypass the reverse-proxy layer, and most applications and browsers do
+        not implement other protocols, so there is no reason for letting them
+        pass through with common web applications. It is generally safe for
+        static web serving and simple internet-facing applications. This is
+        disabled by default. Note that tunnels established via standard CONNECT
+        are NOT blocked with this rule, see 'reject-advanced-methods' for this.
 
 /*
  * Local variables:
index 28195ad4880c8164a9c0a650e716970d38b2f115..6b8d65542b1e027b043b5f96af60c1eade67c4eb 100644 (file)
@@ -261,6 +261,7 @@ struct http_violation {
 #define HTTP_HRD_REJ_OOR_STATUS             0x00008000
 #define HTTP_HRD_REJ_ADV_METH               0x00010000
 #define HTTP_HRD_REJ_UNEXP_REQ_PLD          0x00020000
+#define HTTP_HRD_REJ_UNEXP_REQ_UPG          0x00040000
 
 /* insert new masks above this line */
 /* update this one with new rejects */
@@ -276,7 +277,8 @@ struct http_violation {
                                              HTTP_HRD_REJ_FORB_PAYLOAD   | \
                                              HTTP_HRD_REJ_OOR_STATUS     | \
                                              HTTP_HRD_REJ_ADV_METH       | \
-                                             HTTP_HRD_REJ_UNEXP_REQ_PLD)
+                                             HTTP_HRD_REJ_UNEXP_REQ_PLD  | \
+                                             HTTP_HRD_REJ_UNEXP_REQ_UPG)
 
 #define HTTP_HRD_ALL_DELETE                 (HTTP_HRD_DEL_USCORE_HDR_NAM         | \
                                              HTTP_HRD_DEL_USCORE_TLR_NAM         | \
index 7b239b6ecdf64a28a7bc0891aeca2a785cc75449..db70dcb5a8b5b42d1b663b3853d3b0ecb9136faa 100644 (file)
--- a/src/h1.c
+++ b/src/h1.c
@@ -319,6 +319,13 @@ void h1_parse_upgrade_header(struct h1m *h1m, struct ist *value)
                else if ((h1m->flags & H1_MF_CLEAN_CONN_HDR) &&
                         (isteqi(word, ist("h2c")) || isteqi(word, ist("h2"))))
                        goto skip_val;
+               else if (!isteqi(word, ist("webtransport"))) {
+                       /* neither websocket nor h2c nor n2 nor webtransport,
+                        * maybe we have to delete other values.
+                        */
+                       if (http_harden(HTTP_HRD_REJ_UNEXP_REQ_UPG))
+                               goto skip_val;
+               }
 
                if (value->ptr + value->len == p) {
                        /* no rewrite done till now */
index b1c874b3d16f6bb7ee8a40f5466b67c738c975c2..be2f05d1395fd7767a6d9ccc03a04d930634d8b4 100644 (file)
--- a/src/h2.c
+++ b/src/h2.c
@@ -537,6 +537,15 @@ int h2_make_htx_request(struct http_hdr *list, struct htx *htx, unsigned int *ms
                        goto fail;
                }
 
+               if (!isteqi(phdr_val[H2_PHDR_IDX_PROT], ist("websocket")) &&
+                   !isteqi(phdr_val[H2_PHDR_IDX_PROT], ist("webtransport")) &&
+                   http_harden(HTTP_HRD_REJ_UNEXP_REQ_UPG)) {
+                       /* neither websocket nor h2c nor n2 nor webtransport,
+                        * and hardening enabled => reject.
+                        */
+                       goto fail;
+               }
+
                if (!htx_add_header(htx, ist("upgrade"), phdr_val[H2_PHDR_IDX_PROT]))
                        goto fail;
                if (!htx_add_header(htx, ist("connection"), ist("upgrade")))
index a96cbdd74901399bf75ae8b55303c1ad36ff156a..2abfaa314d12cab5a7413d892294adb50555780d 100644 (file)
@@ -1806,6 +1806,7 @@ const struct http_hardening_kw http_hardening_kws[] = {
        { "reject-out-of-range-status",        HTTP_HRD_REJ_OOR_STATUS, HTTP_DIR_RES },
        { "reject-advanced-methods",           HTTP_HRD_REJ_ADV_METH, HTTP_DIR_REQ },
        { "reject-unexpected-request-payload", HTTP_HRD_REJ_UNEXP_REQ_PLD, HTTP_DIR_REQ },
+       { "reject-unexpected-request-upgrade", HTTP_HRD_REJ_UNEXP_REQ_UPG, HTTP_DIR_REQ },
        /* insert new hardening options above this line */
        { "all-reject",                     HTTP_HRD_ALL_REJECT,       HTTP_DIR_BOTH },
        { "all-delete",                     HTTP_HRD_ALL_DELETE,       HTTP_DIR_BOTH },