From: Willy Tarreau Date: Mon, 12 Oct 2026 18:33:35 +0000 (+0000) Subject: MINOR: global: add tune.h2.fe.enabled to disable HTTP/2 on frontends X-Git-Url: https://git.kaiwu.me/web/%22data:,/doc/static/gitweb.js?a=commitdiff_plain;h=ba31c1d5736ca4d5875cf1edf47a061b0707ffb5;p=haproxy.git MINOR: global: add tune.h2.fe.enabled to disable HTTP/2 on frontends In case of trouble in the HTTP/2 stack, users currently have no simple way to stop serving HTTP/2 to clients: the default ALPN of SSL "bind" lines silently advertises "h2", explicit "alpn" settings have to be edited on every "bind" line and crt-list entry, HTTP/1 connections starting with the H2 preface are upgraded unless "option disable-h2-upgrade" is set everywhere, and "proto h2" binds have to be found and changed. This patch adds a new global setting, "tune.h2.fe.enabled", which defaults to "on" and is stored as GTUNE2_DISABLE_H2_FE in tune.options2. When set to "off", three layers are touched: - h2 is prevented from being configured: "h2" is removed from explicit alpn via alpn_remove_token() when proxies are finalized, as well as from crt-list entries. The default ALPN (or the one derived from "proto") is processed the same way in proxy_finalize(). If "h2" is the only protocol, the string ends up empty, which is equivalent to "no-alpn". Nothing changes on the data path: the SSL contexts are created afterwards from the final strings. - h1 is prevented from upgrading to h2 in proxy_finalize(), which sets PR_O2_NO_H2_UPGRADE on HTTP frontends, so that the H1 mux responds a 405 to the H2 preface as with "option disable-h2-upgrade", indicating that method "PRI" is rejected; - finally, h2_init() refuses frontend new connections, so anything that cannot be fixed above (e.g. "proto h2" or NPN) is immediately closed. The keyword is parsed in cfgparse-global.c so that it remains accepted when building without USE_H2. Backend connections are not affected since the main purpose it to provide an emergency switch to protect against an exploitable flaw for example with limited impact. This may change later with a new option if deemed useful. One limitation is that the keyword must be seen before parsing "alpn" keyword on "bind" lines, which is normally granted since "global" section isn't supposed to appear after proxies. In any case, any residual case would still be blocked by h2_init(), by closing the connection. The goal is to offer an emergency kill-switch, so this is meant to be backported to stable versions, along with the two preceding commits: MINOR: ssl: add alpn_remove_token() to drop a protocol from an ALPN string MINOR: global: add tune.options2 to host new GTUNE2_* flags --- diff --git a/doc/configuration.txt b/doc/configuration.txt index e23f6ccce..55e8725ba 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -1966,6 +1966,7 @@ The following keywords are supported in the "global" section : - tune.h2.be.max-concurrent-streams - tune.h2.be.max-frames-at-once - tune.h2.be.rxbuf + - tune.h2.fe.enabled - tune.h2.fe.glitches-threshold - tune.h2.fe.initial-window-size - tune.h2.fe.max-concurrent-streams @@ -4852,6 +4853,31 @@ tune.h2.be.rxbuf See also: tune.h2.be.initial-window-size, tune.h2.fe.rxbuf, http-reuse. +tune.h2.fe.enabled { on | off } + Enables ('on') or disables ('off') HTTP/2 on all frontends. The default is + 'on'. This is meant as an emergency kill-switch to instantly stop serving + HTTP/2 to clients (e.g. in case of a problem in the HTTP/2 stack) without + having to modify every "bind" line. When set to 'off': + - "h2" is removed from the ALPN of "bind" lines and crt-list entries, + whether it comes from the default ALPN list or from an explicit "alpn" + setting. When "h2" was the only protocol listed, this is equivalent to + "no-alpn". Clients offering other protocols such as "http/1.1" will + negotiate them instead, and those offering only "h2" will get no ALPN + at all, hence will be processed as HTTP/1; + - "option disable-h2-upgrade" is implicitly set on all frontends, so that + HTTP/1 connections starting with the HTTP/2 preface are not upgraded to + HTTP/2 anymore and receive a 405 error; + - incoming connections which would still be processed by the HTTP/2 + multiplexer (e.g. on "bind" lines forcing "proto h2", or an "h2" chosen + via NPN) are immediately closed. + + Outgoing HTTP/2 connections to servers are not affected, since these are + only enabled by explicit configuration. Since explicit "alpn" settings are + adjusted when they are parsed, this setting must appear in the global + section before the proxies, which is normally the case. + + See also: "option disable-h2-upgrade", "alpn", "no-alpn", "proto" + tune.h2.fe.glitches-threshold Sets the threshold for the number of glitches on a frontend connection, where that connection will automatically be killed. This allows to automatically diff --git a/include/haproxy/global-t.h b/include/haproxy/global-t.h index d676bf73e..6d85a118f 100644 --- a/include/haproxy/global-t.h +++ b/include/haproxy/global-t.h @@ -90,6 +90,7 @@ #define GTUNE_NO_TG_FD_SHARING (1U<<31) /* Global tuning options for tune.options2, since tune.options is full */ +#define GTUNE2_DISABLE_H2_FE (1U<<0) /* subsystem-specific debugging options for tune.debug */ #define GDBG_CPU_AFFINITY (1U<< 0) diff --git a/src/cfgparse-global.c b/src/cfgparse-global.c index a0ef86a78..b3770c91c 100644 --- a/src/cfgparse-global.c +++ b/src/cfgparse-global.c @@ -714,6 +714,17 @@ static int cfg_parse_global_tune_opts(char **args, int section_type, return -1; } } + else if (strcmp(args[0], "tune.h2.fe.enabled") == 0) { + /* parsed here so that it is accepted even without USE_H2 */ + if (strcmp(args[1], "on") == 0) + global.tune.options2 &= ~GTUNE2_DISABLE_H2_FE; + else if (strcmp(args[1], "off") == 0) + global.tune.options2 |= GTUNE2_DISABLE_H2_FE; + else { + memprintf(err, "'%s' expects 'on' or 'off'.", args[0]); + return -1; + } + } else if (strcmp(args[0], "tune.glitches.kill.cpu-usage") == 0) { if (*(args[1]) == 0) { memprintf(err, "'%s' expects a numeric value between 0 and 100", args[0]); @@ -2100,6 +2111,7 @@ static struct cfg_kw_list cfg_kws = {ILH, { { CFG_GLOBAL, "tune.disable-zero-copy-forwarding", cfg_parse_global_tune_forward_opts }, { CFG_GLOBAL, "tune.fd.tables", cfg_parse_global_tune_opts, KWF_EXPERIMENTAL }, { CFG_GLOBAL, "tune.glitches.kill.cpu-usage", cfg_parse_global_tune_opts }, + { CFG_GLOBAL, "tune.h2.fe.enabled", cfg_parse_global_tune_opts }, { CFG_GLOBAL, "tune.http.cookielen", cfg_parse_global_tune_opts }, { CFG_GLOBAL, "tune.http.logurilen", cfg_parse_global_tune_opts }, { CFG_GLOBAL, "tune.http.maxhdr", cfg_parse_global_tune_opts }, diff --git a/src/cfgparse-ssl.c b/src/cfgparse-ssl.c index 1b9862e1b..ad1173ddb 100644 --- a/src/cfgparse-ssl.c +++ b/src/cfgparse-ssl.c @@ -1435,6 +1435,8 @@ static int ssl_bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struc ret = ssl_sock_parse_alpn(args[cur_arg + 1], &conf->alpn_str, &conf->alpn_len, err); if (ret) memprintf(err, "'%s' : %s", args[cur_arg], *err); + else if (global.tune.options2 & GTUNE2_DISABLE_H2_FE) + alpn_remove_token(conf->alpn_str, &conf->alpn_len, ist("h2")); return ret; #else memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); diff --git a/src/mux_h2.c b/src/mux_h2.c index 78cfbac9f..96207a16e 100644 --- a/src/mux_h2.c +++ b/src/mux_h2.c @@ -1434,6 +1434,15 @@ static int h2_init(struct connection *conn, struct proxy *prx, struct session *s TRACE_ENTER(H2_EV_H2C_NEW); + /* "tune.h2.fe.enabled off" refuses any frontend H2 connection that + * could still reach us (e.g. "proto h2", or "h2" negotiated via NPN). + * Nothing was touched yet so we can leave without any cleanup. + */ + if (!conn_is_back(conn) && (global.tune.options2 & GTUNE2_DISABLE_H2_FE)) { + TRACE_ERROR("H2 disabled on frontends", H2_EV_H2C_NEW|H2_EV_H2C_END|H2_EV_H2C_ERR, conn); + return -1; + } + h2c = pool_alloc(pool_head_h2c); if (!h2c) goto fail_no_h2c; diff --git a/src/proxy.c b/src/proxy.c index 2601d8b29..77bc44a0a 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -59,6 +59,7 @@ #include #include #include +#include #include #include #include @@ -1845,6 +1846,11 @@ int proxy_finalize(struct proxy *px, int *err_code) char *err = NULL; int i; + /* "tune.h2.fe.enabled off" implies "option disable-h2-upgrade" */ + if ((px->cap & PR_CAP_FE) && (px->mode == PR_MODE_HTTP) && + (global.tune.options2 & GTUNE2_DISABLE_H2_FE)) + px->options2 |= PR_O2_NO_H2_UPGRADE; + /* check and reduce the bind-proc of each listener */ list_for_each_entry(bind_conf, &px->conf.bind, by_fe) { int mode = conn_pr_mode_to_proto_mode(px->mode); @@ -1950,6 +1956,8 @@ int proxy_finalize(struct proxy *px, int *err_code) } #endif /* TLSEXT_TYPE_application_layer_protocol_negotiation */ + if ((global.tune.options2 & GTUNE2_DISABLE_H2_FE) && px->mode == PR_MODE_HTTP && bind_conf->ssl_conf.alpn_str) + alpn_remove_token(bind_conf->ssl_conf.alpn_str, &bind_conf->ssl_conf.alpn_len, ist("h2")); if (px->mode == PR_MODE_HTTP && global.tune.bufsize < 16384) { #ifdef OPENSSL_NPN_NEGOTIATED