From: Willy Tarreau Date: Wed, 25 Nov 2026 17:12:49 +0000 (+0100) Subject: MINOR: http/hardening: implement "reject-unexpected-request-upgrade" X-Git-Tag: v3.5-dev14~4 X-Git-Url: https://git.kaiwu.me/http/%22../static/gitweb.js?a=commitdiff_plain;h=a9a5154133370c365c18ae3d20347a904b977ad4;p=haproxy.git MINOR: http/hardening: implement "reject-unexpected-request-upgrade" 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. --- diff --git a/doc/configuration.txt b/doc/configuration.txt index 7ac9e818e..6c6a16d65 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -35061,6 +35061,7 @@ http-hardening [from ] 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 [from ] 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: diff --git a/include/haproxy/http-t.h b/include/haproxy/http-t.h index 28195ad48..6b8d65542 100644 --- a/include/haproxy/http-t.h +++ b/include/haproxy/http-t.h @@ -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 | \ diff --git a/src/h1.c b/src/h1.c index 7b239b6ec..db70dcb5a 100644 --- 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 */ diff --git a/src/h2.c b/src/h2.c index b1c874b3d..be2f05d13 100644 --- 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"))) diff --git a/src/http.c b/src/http.c index a96cbdd74..2abfaa314 100644 --- a/src/http.c +++ b/src/http.c @@ -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 },