]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: ssl: allow prefix lookup for alpn_remove_token()
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 20 Nov 2026 08:42:24 +0000 (09:42 +0100)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 20 Nov 2026 11:02:24 +0000 (12:02 +0100)
Extend alpn_remove_token() by adding an extra boolean argument <prefix>.
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.

include/haproxy/ssl_utils.h
src/cfgparse-ssl.c
src/proxy.c
src/ssl_utils.c

index 392436c8fb83dbb6faf26319867901f2b176da53..dc85118b4951cb0446f625ca7f79fb93a5f504de 100644 (file)
@@ -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);
index ad1173ddb6844043a5f3defa60ac699614e9733d..55a3b3df82eb2836f3b0bf98260db1877ff5c67e 100644 (file)
@@ -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]);
index 77bc44a0add0982067b60e4092050f35f839d0e6..69f5c82c0ab909b5f42fad6142aefc02abb491c2 100644 (file)
@@ -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
index ef5e6fc0cf0a169103f79468599d4afc11c92e92..dc27e0206f39541d6e7715a3ffbffe2836650d8a 100644 (file)
@@ -844,12 +844,17 @@ end:
 
 /* Removes all occurrences of protocol <token> from the ALPN string <alpn>
  * 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 <prefix> 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;