]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: global: add tune.h2.fe.enabled to disable HTTP/2 on frontends
authorWilly Tarreau <w@1wt.eu>
Mon, 12 Oct 2026 18:33:35 +0000 (18:33 +0000)
committerWilly Tarreau <w@1wt.eu>
Tue, 17 Nov 2026 10:08:46 +0000 (11:08 +0100)
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

doc/configuration.txt
include/haproxy/global-t.h
src/cfgparse-global.c
src/cfgparse-ssl.c
src/mux_h2.c
src/proxy.c

index e23f6ccce43074687b1403ec867503fa030be275..55e8725ba634d40fedd2f75230c39fe211fa9399 100644 (file)
@@ -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 <size>
 
   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 <number>
   Sets the threshold for the number of glitches on a frontend connection, where
   that connection will automatically be killed. This allows to automatically
index d676bf73e7890fa6d29d0ad265455fa7a58d5db8..6d85a118f3b206ce035758c5d9faeec30a64dd9a 100644 (file)
@@ -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)
index a0ef86a78378492eb6719845d51e65a468fb6f17..b3770c91cedb6eda33ca4dabb6cb9cda377cd383 100644 (file)
@@ -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 },
index 1b9862e1b06f2b214bff9f332b3cb5faf1a6ff17..ad1173ddb6844043a5f3defa60ac699614e9733d 100644 (file)
@@ -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]);
index 78cfbac9f5f686625468b587143ed9a45a5773c8..96207a16e8f4ee2b10dd9af81b14ef26993838d3 100644 (file)
@@ -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;
index 2601d8b2962e4d0b06acb3b82f34f2c9e0b65c33..77bc44a0add0982067b60e4092050f35f839d0e6 100644 (file)
@@ -59,6 +59,7 @@
 #include <haproxy/quic_tune.h>
 #include <haproxy/server-t.h>
 #include <haproxy/signal.h>
+#include <haproxy/ssl_utils.h>
 #include <haproxy/stats.h>
 #include <haproxy/stconn.h>
 #include <haproxy/stream.h>
@@ -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