From: Remi Tricot-Le Breton Date: Tue, 20 Oct 2026 08:04:47 +0000 (+0200) Subject: BUG/MINOR: jwe: only accept exact "alg" and "enc" names in JWE tokens X-Git-Tag: v3.5-dev10~82 X-Git-Url: https://git.kaiwu.me/?a=commitdiff_plain;h=6e8acfdb7572ecdd297db2ae4cc5bf99c0134665;p=haproxy.git BUG/MINOR: jwe: only accept exact "alg" and "enc" names in JWE tokens The 'alg' and 'enc' parser does not check the value length so a too short value would still be seen as the full one ("A1" instead of "A128CBC-HS256" for instance). This was introduced in 3.4 by commit 416b87d5d ("MINOR: jwe: Add new jwt_decrypt_secret converter"), so the fix must be backported to 3.4. --- diff --git a/reg-tests/jwt/jwt_decrypt.vtc b/reg-tests/jwt/jwt_decrypt.vtc index e7802d632..f22e5a6c9 100644 --- a/reg-tests/jwt/jwt_decrypt.vtc +++ b/reg-tests/jwt/jwt_decrypt.vtc @@ -67,6 +67,11 @@ haproxy h1 -conf { use_backend jwk if { path_beg /jwk } default_backend dflt + # frontend used only to check that jwt_decrypt_secret does not accept + # truncated alg/enc names + frontend algenc_fe + bind "fd@${algencfe}" + http-request return status 200 hdr x-res "<%[req.hdr(x-tok),jwt_decrypt_secret(\"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=\")]>" backend secret_based_alg @@ -323,3 +328,24 @@ client c10 -connect ${h1_mainfe_sock} { expect resp.http.x-decrypted == "Random test message for ECDH-ES+A256KW encrypted tokens" } -run + +# Check that truncated alg/enc names are not accepted +client c11 -connect ${h1_algencfe_sock} { + # "alg":"dir", "enc":"A128CBC-HS256" + txreq -hdr "x-tok: eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0..IzbGkbjTnQ839myrKPqMSQ.-XHg1kkrtAMxn8RC3hBDFw.a0Z1SGUgL06ah5-dKFOFRA" + rxresp + expect resp.status == 200 + expect resp.http.x-res == "" + + # "alg":"d" + txreq -hdr "x-tok: eyJhbGciOiJkIiwiZW5jIjoiQTEyOENCQy1IUzI1NiJ9..ASTxW817QnC0JsyBUQLrew.kn-7BbRJ2nJtIA-C72snQQ.XrzQz7Ovvge_tCy72HJSkg" + rxresp + expect resp.status == 200 + expect resp.http.x-res == "<>" + + # "enc":"A1" + txreq -hdr "x-tok: eyJhbGciOiJkaXIiLCJlbmMiOiJBMSJ9..ZekK-JnLX3_C5uH0YVh7HQ.i9_XPL7jzAat-TgvEU9Csg.dRT33If2uzWh7mgvWYk6wA" + rxresp + expect resp.status == 200 + expect resp.http.x-res == "<>" +} -run diff --git a/src/jwe.c b/src/jwe.c index b16ba4133..146b64738 100644 --- a/src/jwe.c +++ b/src/jwe.c @@ -142,7 +142,8 @@ static inline int parse_alg_enc(struct buffer *buf, struct alg_enc *array) int val = -1; while (item->name) { - if (strncmp(item->name, b_orig(buf), (int)b_data(buf)) == 0) { + if (strlen(item->name) == b_data(buf) && + memcmp(item->name, b_orig(buf), b_data(buf)) == 0) { if (item->enabled == ALG_ENC_DISABLED) return -1; val = item->value;