]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: http/hardening: implement "reject-advanced-methods"
authorWilly Tarreau <w@1wt.eu>
Wed, 25 Nov 2026 15:59:49 +0000 (16:59 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 25 Nov 2026 18:31:08 +0000 (19:31 +0100)
Rejects requests whose method is not one of: HEAD, GET, POST, PUT,
DELETE, OPTIONS, PATCH, QUERY, or extended CONNECT (RFC8441). This
is amply sufficient for the vast majority of regular web sites and
applications accessed by browsers, but may not always be compatible
with API aimed at being joined directly by other applications. This can
block by default a significant number of backend vulnerabilities that
are not yet discovered and that are only triggered by less common
methods. Use only after verifying applications compatibility, though
this is safe for static web serving and simple internet-facing
applications. It is disabled by default.

http_hardening_profiles.vtc was updated accordingly: its "base" profile is
built with "all on", which now also rejects the "get" method used there
to check "reject-lowercase-method", so the new check is turned off there.

doc/configuration.txt
include/haproxy/http-t.h
reg-tests/http-messaging/http_hardening_profiles.vtc
src/h1_htx.c
src/h2.c
src/h3.c
src/http.c

index 75b72c49e0a4674393d357bae47931f40c6dbad8..4260c3ee00a3a07e0866b601b2e346678a1f107a 100644 (file)
@@ -35059,7 +35059,7 @@ http-hardening <name> [from <source>]
         reject-content-length-leading-zero on
         reject-duplicate-content-length on
         reject-forbidden-payload on
-        reject-out-of-range-status on
+        reject-advanced-methods 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
@@ -35209,6 +35209,18 @@ http-hardening <name> [from <source>]
         safe and even recommended to delete such trailers from inbound internet
         requests.
 
+  - reject-advanced-methods (inbound, request)
+        Rejects requests whose method is not one of: HEAD, GET, POST, PUT,
+        DELETE, OPTIONS, PATCH, QUERY, or extended CONNECT (RFC8441). This
+        is amply sufficient for the vast majority of regular web sites and
+        applications accessed by browsers, but may not always be compatible
+        with API aimed at being joined directly by other applications. This can
+        block by default a significant number of backend vulnerabilities that
+        are not yet discovered and that are only triggered by less common
+        methods. Use only after verifying applications compatibility, though
+        this is safe for static web serving and simple internet-facing
+        applications. It is disabled by default.
+
   - reject-content-length-leading-zero (inbound request and response)
         Rejects requests and responses having a Content-Length header field
         whose value contains a leading zero before a digit. Some client and
@@ -35319,6 +35331,7 @@ http-hardening <name> [from <source>]
         so it is safe to enable on untrusted listeners. See also the
         "delete-underscore-in-trailer-name" variant.
 
+
 /*
  * Local variables:
  *  fill-column: 79
index 1fc3fc5ddb1399e747a9b94f2881b3d836f37b33..dda636659eb69d039bf7678654e923d7247be438 100644 (file)
@@ -259,6 +259,7 @@ struct http_violation {
 #define HTTP_HRD_REJ_CL_DUP                 0x00002000
 #define HTTP_HRD_REJ_FORB_PAYLOAD           0x00004000
 #define HTTP_HRD_REJ_OOR_STATUS             0x00008000
+#define HTTP_HRD_REJ_ADV_METH               0x00010000
 
 /* insert new masks above this line */
 /* update this one with new rejects */
@@ -272,7 +273,8 @@ struct http_violation {
                                              HTTP_HRD_REJ_CL_LEAD_ZERO   | \
                                              HTTP_HRD_REJ_CL_DUP         | \
                                              HTTP_HRD_REJ_FORB_PAYLOAD   | \
-                                             HTTP_HRD_REJ_OOR_STATUS)
+                                             HTTP_HRD_REJ_OOR_STATUS     | \
+                                             HTTP_HRD_REJ_ADV_METH)
 
 #define HTTP_HRD_ALL_DELETE                 (HTTP_HRD_DEL_USCORE_HDR_NAM         | \
                                              HTTP_HRD_DEL_USCORE_TLR_NAM         | \
index d2670e85419a5821de843ddfbf3a35c90be7de8a..1166273136e6b44ca92a3c8168afed1ae50c27e6 100644 (file)
@@ -35,10 +35,12 @@ haproxy h1 -conf {
     http-hardening untrusted
        reject-duplicate-host-header on
 
-    # everything but one check
+    # everything but one check (and the methods check, which would also
+    # reject "get")
     http-hardening base
        all on
        reject-lowercase-method off
+       reject-advanced-methods off
 
     # inherits everything from "base", and turns the underscore rejection
     # into a deletion since the latter is still enabled.
index 7d85bf0cfa4c58805f5d408f55c4719b642daa62..ce691ae723f6898aa2dab587c9e574ce94ba2d61 100644 (file)
@@ -214,6 +214,18 @@ static int h1_postparse_req_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx
                }
        }
 
+       /* reject-advanced-methods allows to reject methods that are not among:
+        * HEAD/GET/POST/PUT/OPTIONS/DELETE/PATCH/QUERY.
+        */
+       if (unlikely(h1sl->rq.meth == HTTP_METH_CONNECT ||
+                    h1sl->rq.meth == HTTP_METH_TRACE ||
+                    (h1sl->rq.meth == HTTP_METH_OTHER &&
+                     (h1sl->rq.m.len != 5 || (memcmp(h1sl->rq.m.ptr, "PATCH", 5) != 0 && memcmp(h1sl->rq.m.ptr, "QUERY", 5) != 0)))) &&
+           http_harden(HTTP_HRD_REJ_ADV_METH)) {
+               h1m->err_code = 405; // method not allowed
+               goto error;
+       }
+
        /* Remove Upgrade header if no 'connection: upgrade' found */
        if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_HDR)) == H1_MF_UPG_HDR) {
                int i;
index 75ac04c4d11843de900281cc36a1b6e50dbd1628..0311a8f38b2a50cd0eb1d1e1bcd9efeaa0b6ee16 100644 (file)
--- a/src/h2.c
+++ b/src/h2.c
@@ -282,6 +282,17 @@ static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr,
                flags |= HTX_SL_F_BODYLESS_RESP;
        }
 
+       /* reject-advanced-methods allows to reject methods that are not among:
+        * HEAD/GET/POST/PUT/OPTIONS/DELETE/PATCH/QUERY. CONNECT is only
+        * accepted as extended CONNECT (e.g. WebSocket).
+        */
+       if (unlikely((meth == HTTP_METH_CONNECT && !(*msgf & H2_MSGF_EXT_CONNECT)) ||
+                    meth == HTTP_METH_TRACE ||
+                    (meth == HTTP_METH_OTHER &&
+                     (meth_sl.len != 5 || (memcmp(meth_sl.ptr, "PATCH", 5) != 0 && memcmp(meth_sl.ptr, "QUERY", 5) != 0)))) &&
+           http_harden(HTTP_HRD_REJ_ADV_METH))
+               goto fail;
+
        sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth_sl, uri, ist("HTTP/2.0"));
        if (!sl)
                goto fail;
index 6eb68400d58a0be2390ca291136b919530dd1231..705a9d58f9df8b53ef871788238007efcee18f01 100644 (file)
--- a/src/h3.c
+++ b/src/h3.c
@@ -1057,6 +1057,21 @@ static ssize_t h3_req_headers_to_htx(struct qcs *qcs, const struct buffer *buf,
 
        sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
 
+       /* reject-advanced-methods allows to reject methods that are not among:
+        * HEAD/GET/POST/PUT/OPTIONS/DELETE/PATCH/QUERY.
+        */
+       if (unlikely(sl->info.req.meth == HTTP_METH_CONNECT ||
+                    sl->info.req.meth == HTTP_METH_TRACE ||
+                    (sl->info.req.meth == HTTP_METH_OTHER &&
+                     (meth.len != 5 || (memcmp(meth.ptr, "PATCH", 5) != 0 && memcmp(meth.ptr, "QUERY", 5) != 0)))) &&
+           http_harden(HTTP_HRD_REJ_ADV_METH)) {
+               TRACE_ERROR("hardening rejected advanced method", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
+               h3s->err = H3_ERR_REQUEST_REJECTED;
+               qcc_report_glitch(h3c->qcc, 1);
+               len = -1;
+               goto out;
+       }
+
        if (isttest(authority)) {
                if (!htx_add_header(htx, ist("host"), authority)) {
                        len = -1;
index 1809b452c9fae077425174b8ed61db7913faafcd..fed3cc58a8c9af5143bf3c5cde91e2e7f3d49d58 100644 (file)
@@ -1804,6 +1804,7 @@ const struct http_hardening_kw http_hardening_kws[] = {
        { "reject-duplicate-content-length",   HTTP_HRD_REJ_CL_DUP, HTTP_DIR_BOTH },
        { "reject-forbidden-payload",          HTTP_HRD_REJ_FORB_PAYLOAD, HTTP_DIR_RES },
        { "reject-out-of-range-status",        HTTP_HRD_REJ_OOR_STATUS, HTTP_DIR_RES },
+       { "reject-advanced-methods",           HTTP_HRD_REJ_ADV_METH, 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 },