From: Willy Tarreau Date: Wed, 25 Nov 2026 15:59:49 +0000 (+0100) Subject: MINOR: http/hardening: implement "reject-advanced-methods" X-Git-Tag: v3.5-dev14~8 X-Git-Url: https://git.kaiwu.me/http/ngx_http_ssi_module.html?a=commitdiff_plain;h=3bba6379c65079f86ff2346a6442e0b8070e77d0;p=haproxy.git MINOR: http/hardening: implement "reject-advanced-methods" 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. --- diff --git a/doc/configuration.txt b/doc/configuration.txt index 75b72c49e..4260c3ee0 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -35059,7 +35059,7 @@ http-hardening [from ] 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 [from ] 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 [from ] so it is safe to enable on untrusted listeners. See also the "delete-underscore-in-trailer-name" variant. + /* * Local variables: * fill-column: 79 diff --git a/include/haproxy/http-t.h b/include/haproxy/http-t.h index 1fc3fc5dd..dda636659 100644 --- a/include/haproxy/http-t.h +++ b/include/haproxy/http-t.h @@ -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 | \ diff --git a/reg-tests/http-messaging/http_hardening_profiles.vtc b/reg-tests/http-messaging/http_hardening_profiles.vtc index d2670e854..116627313 100644 --- a/reg-tests/http-messaging/http_hardening_profiles.vtc +++ b/reg-tests/http-messaging/http_hardening_profiles.vtc @@ -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. diff --git a/src/h1_htx.c b/src/h1_htx.c index 7d85bf0cf..ce691ae72 100644 --- a/src/h1_htx.c +++ b/src/h1_htx.c @@ -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; diff --git a/src/h2.c b/src/h2.c index 75ac04c4d..0311a8f38 100644 --- 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; diff --git a/src/h3.c b/src/h3.c index 6eb68400d..705a9d58f 100644 --- 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; diff --git a/src/http.c b/src/http.c index 1809b452c..fed3cc58a 100644 --- a/src/http.c +++ b/src/http.c @@ -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 },