From: Remi Tricot-Le Breton Date: Fri, 20 Nov 2026 10:08:45 +0000 (+0100) Subject: MINOR: jwt: New jwt_verify_secret converter X-Git-Url: https://git.kaiwu.me/http/control.html?a=commitdiff_plain;h=32bb9ecae26e452b91e92f3ed7a7c6310baf9953;p=haproxy.git MINOR: jwt: New jwt_verify_secret converter The 'jwt_verify' converter interprets its second parameter differently depending on its first one: for HMAC algorithms it is used as a shared secret, while for asymmetric ones it holds the path to a certificate or a public key. Letting client-provided data decide how a configuration parameter is interpreted is risky. Splitting this logic into two converters removes the ambiguity. The new 'jwt_verify_secret' converter only accepts HMAC algorithms and always interprets its second parameter as a secret, while the existing 'jwt_verify_cert' converter only accepts asymmetric algorithms and always interprets it as a certificate or public key path. The historical 'jwt_verify' converter, which keeps both behaviors for backward compatibility, is left untouched for now, but it is the converter that should eventually disappear once users migrated to the two new ones. --- diff --git a/doc/configuration.txt b/doc/configuration.txt index 4edb162ef..4b63abbca 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -3155,10 +3155,10 @@ issuers-chain-path used, or no chain was provided in the PEM. jwt.allow_none_alg - Allow JWT signed token with the "none" algorithm in the 'jwt_verify' - converter. They will always be considered valid when this option is set. By - default unsigned tokens are rejected since they are considered unsafe (see - RFC8725 section 3.1). + Allow JWT signed token with the "none" algorithm in the 'jwt_verify', + 'jwt_verify_secret' or 'jwt_verify_cert' converters. They will always be + considered valid when this option is set. By default unsigned tokens are + rejected since they are considered unsafe (see RFC8725 section 3.1). jwt.decrypt_alg_list Set the list of algorithms allowed in the jwt_decrypt_XXX converters. JWT @@ -22345,6 +22345,7 @@ jwt_payload_query([json_path[,output_type]]) string string -- keyword -------------------------------------+- input type + output type - jwt_verify(alg,key) string integer jwt_verify_cert(alg,cert) string integer +jwt_verify_secret(alg,cert) string integer language(value[,default]) string string length string integer lower string string @@ -23487,11 +23488,13 @@ jwt_verify_cert(,) ensure that the header and payload contents are fully valid JSONs once decoded for instance, and no checks are performed regarding their respective contents. + This converter cannot be used to validate tokens signed with HMAC algorithm, + it takes a certificate or a public key as second parameter. - can be either a string or a variable name (See also "set-var") that holds the name of the algorithm used to verify. Unlike the "jwt_verify" - converter, this converter only expects a certificate as second parameter so - it should not be used for tokens using HMAC algorithms. + converter, this converter only expects a certificate or a public key as + second parameter so it should not be used for tokens using HMAC algorithms. Algorithms mentioned in section 3.1 of RFC7518 are managed (apart from HMAC ones): @@ -23513,7 +23516,7 @@ jwt_verify_cert(,) +--------------+---------------------------------------------------------+ - can be either a string or a variable name (See also "set-var") that - holds a certificate path. + holds a certificate or public key path. Certificates must be standard PEM certificates (starting with BEGIN CERTIFICATE). Their path can be passed directly to the converter or @@ -23532,6 +23535,10 @@ jwt_verify_cert(,) using the stats socket. See also "set ssl cert" and "new ssl cert" in the management guide. + All the public keys that might be used to verify JWTs must be known during + init in order to be added into a dedicated cache so that no disk access is + required during runtime. + Returns 1 in case of verification success, 0 in case of verification failure and a strictly negative value for any other error. Because of all those non-null error return values, the result of this converter should never be @@ -23565,6 +23572,71 @@ jwt_verify_cert(,) http-request deny unless { var(txn.jwt_alg) -m str "RS256" } http-request deny unless { var(txn.bearer),jwt_verify_cert(txn.jwt_alg,"/path/to/cert.pem") 1 } +jwt_verify_secret(,) + + Performs a signature verification for the JSON Web Token (JWT) given in input + by using the algorithm and the parameter. + For now, only JWS tokens using the Compact Serialization format can be + processed (three dot-separated base64-url encoded strings). + This converter only verifies the signature of the token and does not perform + a full JWT validation as specified in section 7.2 of RFC7519. We do not + ensure that the header and payload contents are fully valid JSONs once + decoded for instance, and no checks are performed regarding their respective + contents. + This converter can only be used to validate tokens signed with HMAC + algorithm. + + - can be either a string or a variable name (See also "set-var") that + holds the name of the algorithm used to verify. + + Among the algorithms mentioned in section 3.1 of RFC7518, only the HMAC + ones are supported in this converter and the "none" special case. + + +--------------+---------------------------------------------------------+ + | "alg" Param | Digital Signature or MAC Algorithm | + | Value | | + +--------------+---------------------------------------------------------+ + | HS256 | HMAC using SHA-256 | + | HS384 | HMAC using SHA-384 | + | HS512 | HMAC using SHA-512 | + | none | No digital signature or MAC performed (see note below) | + +--------------+---------------------------------------------------------+ + + - can be either a string or a variable name (See also "set-var") + that holds the secret used to verify the token. + + Returns 1 in case of verification success, 0 in case of verification failure + and a strictly negative value for any other error. Because of all those + non-null error return values, the result of this converter should never be + converted to a boolean. See below for a full list of the possible return + values. + + The possible return values are the following : + + +----+----------------------------------------------------------------------+ + | ID | message | + +----+----------------------------------------------------------------------+ + | 1 | "Verification success" | + | 0 | "Verification failure" | + | -1 | "Unknown algorithm (not mentioned in RFC7518)" | + | -2 | "Unmanaged algorithm" | + | -3 | "Invalid token" | + | -4 | "Out of memory" | + | -6 | "Internal error" | + +----+----------------------------------------------------------------------+ + + Please note that this converter is only available when HAProxy has been + compiled with USE_OPENSSL. + + Example: + # Get a JWT from the authorization header, extract the "alg" field of its + # JOSE header and use a public certificate to verify a signature + http-request set-var(txn.bearer) http_auth_bearer + http-request set-var(txn.jwt_alg) var(txn.bearer),jwt_header_query('$.alg') + http-request deny unless { var(txn.jwt_alg) -m str "HS256" } + http-request set-var(txn.secret) str(YQBiY2RlZg==),b64dec + http-request return status 200 hdr x-res "%[req.hdr(x-tok),jwt_verify_secret(txn.jwt_alg,txn.secret)]" + language([,]) Returns the value with the highest q-factor from a list as extracted from the "accept-language" header using "req.fhdr". Values with no q-factor have a diff --git a/include/haproxy/jwt-t.h b/include/haproxy/jwt-t.h index 0830b75e5..97279e568 100644 --- a/include/haproxy/jwt-t.h +++ b/include/haproxy/jwt-t.h @@ -43,6 +43,12 @@ enum jwt_alg { JWS_ALG_PS512, }; +typedef enum { + JWT_VRFY_MODE_DFLT = 0, + JWT_VRFY_MODE_SECRET, + JWT_VRFY_MODE_CERT +} jwt_verify_mode; + struct jwt_item { char *start; size_t length; @@ -55,7 +61,7 @@ struct jwt_ctx { struct jwt_item signature; char *key; unsigned int key_length; - int is_x509; /* 1 if 'key' field is a certificate, 0 otherwise */ + jwt_verify_mode mode; /* default, secret-only, cert-only */ }; enum jwt_elt { diff --git a/include/haproxy/jwt.h b/include/haproxy/jwt.h index 38387ae18..074464e85 100644 --- a/include/haproxy/jwt.h +++ b/include/haproxy/jwt.h @@ -29,10 +29,10 @@ #ifdef USE_OPENSSL enum jwt_alg jwt_parse_alg(const char *alg_str, unsigned int alg_len); int jwt_tokenize(const struct buffer *jwt, struct jwt_item *items, unsigned int item_num); -int jwt_tree_load_cert(char *path, int pathlen, int tryload_cert, const char *file, int line, char **err); +int jwt_tree_load_cert(char *path, int pathlen, const char *file, int line, char **err); enum jwt_vrfy_status jwt_verify(const struct buffer *token, const struct buffer *alg, - const struct buffer *key, int is_x509); + const struct buffer *key, jwt_verify_mode mode); int jwt_tree_create_entry_from_store(struct ckch_store *store, char **err); int jwt_tree_delete_entry(char *path); diff --git a/reg-tests/jwt/jws_verify.vtc b/reg-tests/jwt/jws_verify.vtc index 2577b87e7..f1174c117 100644 --- a/reg-tests/jwt/jws_verify.vtc +++ b/reg-tests/jwt/jws_verify.vtc @@ -81,6 +81,10 @@ haproxy h1 -conf { http-response set-header x-jwt-verify-HS256 %[var(txn.bearer),jwt_verify(txn.jwt_alg,"hmac key hs256")] if { var(txn.jwt_alg) -m str "HS256" } http-response set-header x-jwt-verify-HS384 %[var(txn.bearer),jwt_verify(txn.jwt_alg,"hmac key hs384")] if { var(txn.jwt_alg) -m str "HS384" } http-response set-header x-jwt-verify-HS512 %[var(txn.bearer),jwt_verify(txn.jwt_alg,"hmac key hs512")] if { var(txn.jwt_alg) -m str "HS512" } + + http-response set-header x-jwt-verify-secret-HS256 %[var(txn.bearer),jwt_verify_secret(txn.jwt_alg,"hmac key hs256")] if { var(txn.jwt_alg) -m str "HS256" } + http-response set-header x-jwt-verify-secret-HS384 %[var(txn.bearer),jwt_verify_secret(txn.jwt_alg,"hmac key hs384")] if { var(txn.jwt_alg) -m str "HS384" } + http-response set-header x-jwt-verify-secret-HS512 %[var(txn.bearer),jwt_verify_secret(txn.jwt_alg,"hmac key hs512")] if { var(txn.jwt_alg) -m str "HS512" } server s1 ${s1_addr}:${s1_port} backend rsXXX_be @@ -181,6 +185,7 @@ client c1 -connect ${h1_mainfe_sock} { expect resp.status == 200 expect resp.http.x-jwt-alg == "HS256" expect resp.http.x-jwt-verify-HS256 == "1" + expect resp.http.x-jwt-verify-secret-HS256 == "1" # the two parts of that token are {"alg":"HS256","typ":"JWT"} and # {"sub":"1234567890","name":"John Doe","iat":1516239022} expect resp.http.x-jwt-header ~ "\"alg\":\"HS256\"" @@ -201,6 +206,7 @@ client c2 -connect ${h1_mainfe_sock} { expect resp.status == 200 expect resp.http.x-jwt-alg == "HS384" expect resp.http.x-jwt-verify-HS384 == "1" + expect resp.http.x-jwt-verify-secret-HS384 == "1" } -run client c3 -connect ${h1_mainfe_sock} { @@ -214,6 +220,7 @@ client c3 -connect ${h1_mainfe_sock} { expect resp.status == 200 expect resp.http.x-jwt-alg == "HS512" expect resp.http.x-jwt-verify-HS512 == "1" + expect resp.http.x-jwt-verify-secret-HS512 == "1" } -run # The following token is invalid (it has three extra characters at the end of the signature) @@ -228,6 +235,7 @@ client c4 -connect ${h1_mainfe_sock} { expect resp.status == 200 expect resp.http.x-jwt-alg == "HS512" expect resp.http.x-jwt-verify-HS512 == "-3" + expect resp.http.x-jwt-verify-secret-HS512 == "-3" } -run diff --git a/src/jwt.c b/src/jwt.c index 945936bfe..4d2d7c953 100644 --- a/src/jwt.c +++ b/src/jwt.c @@ -327,7 +327,7 @@ end: * Parse a public certificate and insert it into the jwt_cert_tree. * Returns 0 in case of success. */ -int jwt_tree_load_cert(char *path, int pathlen, int tryload_cert, const char *file, int line, char **err) +int jwt_tree_load_cert(char *path, int pathlen, const char *file, int line, char **err) { int retval = -1; EVP_PKEY *pubkey = NULL; @@ -376,9 +376,6 @@ int jwt_tree_load_cert(char *path, int pathlen, int tryload_cert, const char *fi } } - if (!tryload_cert) - goto end; - /* Look for an actual certificate or crt-store with the given name. * If the path corresponds to an actual certificate that was not loaded * yet we will create the corresponding ckch_store. */ @@ -448,10 +445,6 @@ jwt_jwsverify_hmac(const struct jwt_ctx *ctx, const struct buffer *decoded_signa unsigned char *hmac_res = NULL; enum jwt_vrfy_status retval = JWT_VRFY_KO; - if (ctx->is_x509) { - return JWT_VRFY_UNMANAGED_ALG; - } - switch(ctx->alg) { case JWS_ALG_HS256: evp = EVP_sha256(); @@ -652,7 +645,7 @@ end: * Returns 1 in case of success. */ enum jwt_vrfy_status jwt_verify(const struct buffer *token, const struct buffer *alg, - const struct buffer *key, int is_x509_path) + const struct buffer *key, jwt_verify_mode mode) { struct jwt_item items[JWT_ELT_MAX] = { { 0 } }; unsigned int item_num = JWT_ELT_MAX; @@ -698,7 +691,7 @@ enum jwt_vrfy_status jwt_verify(const struct buffer *token, const struct buffer decoded_sig->data = ret; ctx.key = key->area; ctx.key_length = key->data; - ctx.is_x509 = is_x509_path; + ctx.mode = mode; /* We have all three sections, signature calculation can begin. */ @@ -708,6 +701,10 @@ enum jwt_vrfy_status jwt_verify(const struct buffer *token, const struct buffer case JWS_ALG_HS384: case JWS_ALG_HS512: /* HMAC + SHA-XXX */ + if (mode == JWT_VRFY_MODE_CERT) { + retval = JWT_VRFY_UNMANAGED_ALG; + goto end; + } retval = jwt_jwsverify_hmac(&ctx, decoded_sig); break; case JWS_ALG_RS256: @@ -719,6 +716,12 @@ enum jwt_vrfy_status jwt_verify(const struct buffer *token, const struct buffer case JWS_ALG_PS256: case JWS_ALG_PS384: case JWS_ALG_PS512: + + if (mode == JWT_VRFY_MODE_SECRET) { + retval = JWT_VRFY_UNMANAGED_ALG; + goto end; + } + /* RSASSA-PKCS1-v1_5 + SHA-XXX */ /* ECDSA using P-XXX and SHA-XXX */ /* RSASSA-PSS using SHA-XXX and MGF1 with SHA-XXX */ diff --git a/src/sample.c b/src/sample.c index 7507ad557..544816603 100644 --- a/src/sample.c +++ b/src/sample.c @@ -5258,10 +5258,17 @@ static int sample_conv_jwt_verify_check(struct arg *args, struct sample_conv *co { enum jwt_alg alg = JWT_ALG_DEFAULT; int retval = 0; + int expect_cert = 0; + int expect_secret = 0; vars_check_arg(&args[0], NULL); vars_check_arg(&args[1], NULL); + if (strcmp(conv->kw, "jwt_verify_cert") == 0) + expect_cert = 1; + else if (strcmp(conv->kw, "jwt_verify_secret") == 0) + expect_secret = 1; + if (args[0].type == ARGT_STR) { alg = jwt_parse_alg(args[0].data.str.area, args[0].data.str.data); @@ -5273,15 +5280,40 @@ static int sample_conv_jwt_verify_check(struct arg *args, struct sample_conv *co if (args[1].type == ARGT_STR) { switch (alg) { + case JWS_ALG_NONE: + /* The "none" algorithm does not use any key, the + * second parameter is simply ignored. Whether the + * algorithm is allowed or not is checked at runtime + * through the global "jwt.allow_none_alg" option. + */ + retval = 1; + break; case JWS_ALG_HS256: case JWS_ALG_HS384: case JWS_ALG_HS512: /* don't try to load a file with HMAC algorithms */ + if (expect_cert) { + memprintf(err, "Can't use HMAC-based tokens with %s converter", conv->kw); + return 0; + } retval = 1; break; default: + if (expect_secret) { + if (args[0].type != ARGT_VAR) { + memprintf(err, "Can't use non-HMAC-based tokens with %s converter", conv->kw); + return 0; + } + /* The algorithm is stored in a variable, it + * might be "none" or an HMAC one at runtime and + * the second parameter is a secret, so there is + * nothing to load. + */ + retval = 1; + break; + } retval = (jwt_tree_load_cert(args[1].data.str.area, args[1].data.str.data, - 0, file, line, err) == 0); + file, line, err) == 0); /* The second arg might be an HMAC secret but * the 'alg' is stored in a var */ if (!retval && args[0].type == ARGT_VAR) @@ -5299,51 +5331,8 @@ static int sample_conv_jwt_verify_check(struct arg *args, struct sample_conv *co return retval; } -static int sample_conv_jwt_verify_cert_check(struct arg *args, struct sample_conv *conv, - const char *file, int line, char **err) -{ - enum jwt_alg alg = JWT_ALG_DEFAULT; - int retval = 0; - - vars_check_arg(&args[0], NULL); - vars_check_arg(&args[1], NULL); - - if (args[0].type == ARGT_STR) { - alg = jwt_parse_alg(args[0].data.str.area, args[0].data.str.data); - - if (alg == JWT_ALG_DEFAULT) { - memprintf(err, "unknown JWT algorithm: %s", args[0].data.str.area); - return 0; - } - } - - if (args[1].type == ARGT_STR) { - switch (alg) { - case JWS_ALG_HS256: - case JWS_ALG_HS384: - case JWS_ALG_HS512: - /* We can't have a certificate as second parameter for - * HMAC-signed JWT tokens */ - memprintf(err, "HMAC-signed tokens can't be processed by this converter"); - break; - default: - retval = (jwt_tree_load_cert(args[1].data.str.area, args[1].data.str.data, - 1, file, line, err) == 0); - break; - } - } else if (args[1].type == ARGT_VAR) { - /* We will try to resolve the var during runtime because the - * processing might work if it actually points to an already - * existing ckch_store. - */ - retval = 1; - } - - return retval; -} - /* Check that a JWT's signature is correct */ -static int sample_conv_jwt_verify(const struct arg *args, struct sample *smp, void *private) +static int sample_conv_jwt_verify(const struct arg *args, struct sample *smp, jwt_verify_mode mode) { struct sample alg_smp, key_smp; enum jwt_vrfy_status ret; @@ -5398,7 +5387,7 @@ static int sample_conv_jwt_verify(const struct arg *args, struct sample *smp, vo goto end; key->area[key->data] = 0; - ret = jwt_verify(input, alg, key, 0); + ret = jwt_verify(input, alg, key, mode); smp->data.type = SMP_T_SINT; smp->data.u.sint = ret; @@ -5412,57 +5401,19 @@ end: return retval; } -static int sample_conv_jwt_verify_cert(const struct arg *args, struct sample *smp, void *private) +static int sample_conv_jwt_verify_legacy(const struct arg *args, struct sample *smp, void *private) { - struct sample alg_smp, key_smp; - enum jwt_vrfy_status ret; - struct buffer *input = NULL; - struct buffer *alg = NULL; - struct buffer *key = NULL; - int retval = 0; - - /* The two following calls to 'sample_conv_var2smp_str' will both make - * use of the preallocated trash buffer (via get_trash_chunk call in - * smp_dup) which would end up erasing the contents of the 'smp' input - * buffer. - */ - input = alloc_trash_chunk_sz(smp->data.u.str.data); - if (!input) - return 0; - alg = alloc_trash_chunk(); - if (!alg) - goto end; - key = alloc_trash_chunk(); - if (!key) - goto end; - - if (!chunk_cpy(input, &smp->data.u.str)) - goto end; - - smp_set_owner(&alg_smp, smp->px, smp->sess, smp->strm, smp->opt); - if (!sample_conv_var2smp_str(&args[0], &alg_smp)) - goto end; - if (chunk_printf(alg, "%.*s", (int)b_data(&alg_smp.data.u.str), b_orig(&alg_smp.data.u.str)) <= 0) - goto end; - - smp_set_owner(&key_smp, smp->px, smp->sess, smp->strm, smp->opt); - if (!sample_conv_var2smp_str(&args[1], &key_smp)) - goto end; - if (chunk_printf(key, "%.*s", (int)b_data(&key_smp.data.u.str), b_orig(&key_smp.data.u.str)) <= 0) - goto end; - - ret = jwt_verify(input, alg, key, 1); - - smp->data.type = SMP_T_SINT; - smp->data.u.sint = ret; + return sample_conv_jwt_verify(args, smp, JWT_VRFY_MODE_DFLT); +} - retval = 1; +static int sample_conv_jwt_verify_secret(const struct arg *args, struct sample *smp, void *private) +{ + return sample_conv_jwt_verify(args, smp, JWT_VRFY_MODE_SECRET); +} -end: - free_trash_chunk(input); - free_trash_chunk(alg); - free_trash_chunk(key); - return retval; +static int sample_conv_jwt_verify_cert(const struct arg *args, struct sample *smp, void *private) +{ + return sample_conv_jwt_verify(args, smp, JWT_VRFY_MODE_CERT); } @@ -6528,8 +6479,9 @@ static struct sample_conv_kw_list sample_conv_kws = {ILH, { /* JSON Web Token converters */ { "jwt_header_query", sample_conv_jwt_header_query, ARG2(0,STR,STR), sample_conv_jwt_query_check, SMP_T_BIN, SMP_T_ANY }, { "jwt_payload_query", sample_conv_jwt_payload_query, ARG2(0,STR,STR), sample_conv_jwt_query_check, SMP_T_BIN, SMP_T_ANY }, - { "jwt_verify", sample_conv_jwt_verify, ARG2(2,STR,STR), sample_conv_jwt_verify_check, SMP_T_BIN, SMP_T_SINT }, - { "jwt_verify_cert", sample_conv_jwt_verify_cert, ARG2(2,STR,STR), sample_conv_jwt_verify_cert_check, SMP_T_BIN, SMP_T_SINT }, + { "jwt_verify", sample_conv_jwt_verify_legacy, ARG2(2,STR,STR), sample_conv_jwt_verify_check, SMP_T_BIN, SMP_T_SINT }, + { "jwt_verify_secret", sample_conv_jwt_verify_secret, ARG2(2,STR,STR), sample_conv_jwt_verify_check, SMP_T_BIN, SMP_T_SINT }, + { "jwt_verify_cert", sample_conv_jwt_verify_cert, ARG2(2,STR,STR), sample_conv_jwt_verify_check, SMP_T_BIN, SMP_T_SINT }, #endif { "when", sample_conv_when, ARG3(1,STR,STR,STR), check_when_cond, SMP_T_ANY, SMP_T_ANY }, { NULL, NULL, 0, 0, 0 },