From: Amaury Denoyelle Date: Fri, 20 Nov 2026 08:42:24 +0000 (+0100) Subject: MINOR: ssl: allow prefix lookup for alpn_remove_token() X-Git-Url: https://git.kaiwu.me/http/%7B@url%7D?a=commitdiff_plain;h=227cbbd2ab9e172b90068e4c525384b76ed9dc53;p=haproxy.git MINOR: ssl: allow prefix lookup for alpn_remove_token() Extend alpn_remove_token() by adding an extra boolean argument . This changes how ALPN entries are compared, so that strings are considered as matching if they begin with the specified value. This will allow clearing ALPNs which may take various suffixes, for example for hq-interop/h3 with an IETF draft version. --- diff --git a/include/haproxy/ssl_utils.h b/include/haproxy/ssl_utils.h index 392436c8f..dc85118b4 100644 --- a/include/haproxy/ssl_utils.h +++ b/include/haproxy/ssl_utils.h @@ -47,7 +47,7 @@ const char *x509_v_err_int_to_str(int code); long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d); const char *x509_get_notbefore(X509 *cert); const char *x509_get_notafter(X509 *cert); -int alpn_remove_token(char *alpn, int *len, const struct ist token); +int alpn_remove_token(char *alpn, int *len, const struct ist token, int prefix); #ifdef HAVE_ASN1_TIME_TO_TM time_t ASN1_to_time_t(ASN1_TIME *asn1_time); time_t x509_get_notafter_time_t(X509 *cert); diff --git a/src/cfgparse-ssl.c b/src/cfgparse-ssl.c index ad1173ddb..55a3b3df8 100644 --- a/src/cfgparse-ssl.c +++ b/src/cfgparse-ssl.c @@ -1436,7 +1436,7 @@ static int ssl_bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struc 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")); + alpn_remove_token(conf->alpn_str, &conf->alpn_len, ist("h2"), 0); return ret; #else memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); diff --git a/src/proxy.c b/src/proxy.c index 77bc44a0a..69f5c82c0 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -1957,7 +1957,7 @@ 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")); + alpn_remove_token(bind_conf->ssl_conf.alpn_str, &bind_conf->ssl_conf.alpn_len, ist("h2"), 0); if (px->mode == PR_MODE_HTTP && global.tune.bufsize < 16384) { #ifdef OPENSSL_NPN_NEGOTIATED diff --git a/src/ssl_utils.c b/src/ssl_utils.c index ef5e6fc0c..dc27e0206 100644 --- a/src/ssl_utils.c +++ b/src/ssl_utils.c @@ -844,12 +844,17 @@ end: /* Removes all occurrences of protocol from the ALPN string * whose length is <*len>, in the binary format produced by - * ssl_sock_parse_alpn() (each name preceded by its length byte). The string - * is modified in place, remains zero-terminated and <*len> is updated. It may - * end up empty, which is then equivalent to "no-alpn". Returns the number of - * entries removed. + * ssl_sock_parse_alpn() (each name preceded by its length byte). + * + * If is true, all tokens which begin with the searched value are + * considered as matching. + * + * The string is modified in place, remains zero-terminated and <*len> is + * updated. It may end up empty, which is then equivalent to "no-alpn". + * + * Returns the number of entries removed. */ -int alpn_remove_token(char *alpn, int *len, const struct ist token) +int alpn_remove_token(char *alpn, int *len, const struct ist token, int prefix) { int pos = 0, removed = 0; int next; @@ -861,7 +866,8 @@ int alpn_remove_token(char *alpn, int *len, const struct ist token) if (next > *len) break; - if ((uchar)alpn[pos] == istlen(token) && + if (((uchar)alpn[pos] == istlen(token) || + (prefix && (uchar)alpn[pos] > istlen(token))) && memcmp(alpn + pos + 1, istptr(token), istlen(token)) == 0) { memmove(alpn + pos, alpn + next, *len - next); *len -= next - pos;