jwt: enforce a callback's JWT_ALG_NONE alg pin on the JWS JSON path
A verify callback may set config->alg to pin the algorithm a signature
must use. Setting it to JWT_ALG_NONE means "the key's own alg": the
Compact path resolves that in __setkey_check() (jwt-common.c), so a key
that declares an "alg" pins it and a key without one verifies nothing.
The JWS JSON Serialization path in verify_entry() did not. It applied
the exact-alg pin only when config.alg != JWT_ALG_NONE, and otherwise
left only the key-type gate (for an alg-less explicit key) or the
per-key declared-alg check in the ring scan (for an alg-less ring key).
So a callback that reset the pin to JWT_ALG_NONE on a checker keyed with
an alg-less key let any algorithm of the key's type verify (for example
RS512 or PS256 after jwt_checker_setkey(c, JWT_ALG_RS256, key)), while
the Compact path rejected the same token. Compact tokens checked against
a keyring take the JSON path, so they inherited the bypass too. This is
an algorithm-pinning (RFC 8725 section 3.1) conformance bug, not a
forgery: the signature still had to verify and the key-type gate still
held.
Resolve a JWT_ALG_NONE pin on the JSON path exactly as the Compact path
does:
- For an explicit key (a single key, a kid-named ring key, or one the
callback supplied), resolve config.alg to the key's own "alg" before
the exact-alg pin; a key with no "alg" of its own verifies nothing.
- In the kid-less ring scan, skip an alg-less candidate when the pin
is JWT_ALG_NONE, matching the explicit-key rule. A candidate that
declares an "alg" is still pinned to it by the existing check.
The seed for config.alg is never JWT_ALG_NONE (a JSON signature cannot
carry "alg":"none"), so only a callback reaches this; no callback, or
one that sets a concrete alg, is unaffected. The change is contained in
the static verify_entry(); no API or ABI change.
Add test_json_alg_none_cb to tests/jws_json.c, covering the single-key,
keyring-scan and kid-named cases across Compact, Flattened and General,
under both JSON backends and every crypto backend, and clarify the
jwt_checker_setkey/setkeyring/setcb docs to state that a JWT_ALG_NONE
pin verifies nothing with a key that has no "alg" of its own.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Ben Collins <bcollins@libjwt.io>
diff --git a/include/jwt.h b/include/jwt.h
index 014b9b4..3a5d07c 100644
--- a/include/jwt.h
+++ b/include/jwt.h
@@ -1031,15 +1031,16 @@
*
* A signature, of a Compact token or of a JWS JSON Serialization alike,
* verifies only if it uses exactly that algorithm (with ::JWT_ALG_NONE, the
- * key's ``"alg"``), not merely one the key could check (e.g. RS512 or PS256
- * when pinned to RS256). A verify callback that sets config->alg pins that
- * algorithm instead. The key is used whatever its ``"use"`` or
- * ``"key_ops"``, unlike the keys of a keyring (see jwt_checker_setkeyring()),
- * but a key that is in error (jwks_item_error()) is refused. The checker keeps
- * the key or keyring it has, and one that has neither is not left unkeyed: it
- * rejects every token, an unsigned one included, until it is given a key or
- * keyring, its verify callback supplies the key, or this is called with a
- * NULL key.
+ * key's ``"alg"``, and a key with none verifies nothing), not merely one the
+ * key could check (e.g. RS512 or PS256 when pinned to RS256). A verify
+ * callback that sets config->alg pins that algorithm instead. The key is
+ * used whatever its ``"use"`` or ``"key_ops"``, unlike the keys of a
+ * keyring (see jwt_checker_setkeyring()), but a key that is in error
+ * (jwks_item_error()) is refused. The checker keeps the key or keyring it
+ * has, and one that has neither is not left unkeyed: it rejects every
+ * token, an unsigned one included, until it is given a key or keyring,
+ * its verify callback supplies the key, or this is called with a NULL
+ * key.
*
* A NULL key (with ::JWT_ALG_NONE) also drops a keyring. Unless embedded-JWK
* verification is enabled (this does not turn it off), the checker is then
@@ -1108,7 +1109,9 @@
* an unsigned (``"alg":"none"``) token is never accepted. The callback may
* supply the key, which is then the only one tried, whatever its ``"use"`` or
* ``"key_ops"`` (unless it is in error: then none is); if it sets
- * config->alg, a signature with another ``"alg"`` does not verify.
+ * config->alg, a signature with another ``"alg"`` does not verify, and if it
+ * sets it to ::JWT_ALG_NONE each candidate is pinned to the candidate's own
+ * ``"alg"`` (a candidate without one then verifies nothing).
*
* A ring key is not tied to an issuer: when the ring holds the keys of several
* issuers, a token without a ``kid`` verifies with whichever of them signed
@@ -1339,7 +1342,8 @@
* algorithm given to jwt_checker_setkey(), if any, or else that signature's
* own ``"alg"``, and the signature verifies only if its ``"alg"`` is the one
* in config->alg when the callback returns (for ::JWT_ALG_NONE, the key's
- * ``"alg"``, if it has one). A non-zero return only leaves that signature
+ * ``"alg"``, if it has one, and nothing if it has none, just as on the
+ * single-key Compact path). A non-zero return only leaves that signature
* unverified, for the ::jwt_verify_policy_t to decide: under
* ::JWT_VERIFY_POLICY_ANY the token is still accepted if another of its
* signatures verifies, and under ::JWT_VERIFY_POLICY_ALL it is rejected. A
@@ -1385,6 +1389,12 @@
* setting the callback will allow updating the ctx passed to the callback.
* Calling with both values as NULL will disable the callback completely.
*
+ * @note Before 3.7.0 a verify callback that set config->alg to ::JWT_ALG_NONE
+ * unpinned the algorithm on the JWS JSON Serialization path (and on a Compact
+ * token checked against a keyring, which takes that path): any algorithm of
+ * the key's type then verified, instead of the key's own ``"alg"`` (or
+ * nothing, for a key without one) as on the single-key Compact path (#353).
+ *
* @param checker Pointer to a checker object
* @param cb Pointer to a callback function
* @param ctx Pointer to data to pass to the callback function
diff --git a/libjwt/jwt-verify.c b/libjwt/jwt-verify.c
index 8f3fd09..52aa814 100644
--- a/libjwt/jwt-verify.c
+++ b/libjwt/jwt-verify.c
@@ -1110,6 +1110,23 @@
return 0;
}
+ /* @rfc{8725,3.1} Resolve a JWT_ALG_NONE pin to the explicit key's
+ * own "alg", as the Compact path's __setkey_check() does: a
+ * callback that sets config->alg = JWT_ALG_NONE means "the key's
+ * alg", so a key that carries one pins it and a key without one
+ * verifies nothing, rather than dropping the pin and letting any
+ * alg of the right key type through (the pinning bypass, #353).
+ * The seed is never JWT_ALG_NONE (a JSON signature has no
+ * "alg":"none"), so only the callback reaches here with it NONE.
+ * The keyring scan applies the same rule per candidate below. */
+ if (config.alg == JWT_ALG_NONE && config.key != NULL) {
+ config.alg = jwks_item_alg(config.key);
+ if (config.alg == JWT_ALG_NONE) {
+ jwt->headers = NULL;
+ return 0;
+ }
+ }
+
/* @rfc{8725,3.1} Pin the EXACT algorithm, as the Compact path does: a
* signature whose "alg" is not the one pinned (by jwt_checker_setkey()
* or in config->alg by the callback) does not verify, even with a key
@@ -1129,6 +1146,13 @@
const jwk_item_t *k = jwks_item_get(ring, i);
jwt_alg_t kalg = jwks_item_alg(k);
+ /* config.alg is JWT_ALG_NONE here only if the
+ * callback reset the pin to the key's own alg (the
+ * seed is this signature's concrete alg): a ring key
+ * with no "alg" then verifies nothing, matching the
+ * explicit-key rule above (#353). */
+ if (config.alg == JWT_ALG_NONE && kalg == JWT_ALG_NONE)
+ continue;
if (kalg != JWT_ALG_NONE && kalg != s->alg)
continue;
if (!ring_key_verifies(k))
diff --git a/tests/jws_json.c b/tests/jws_json.c
index a059bfa..4ce15ff 100644
--- a/tests/jws_json.c
+++ b/tests/jws_json.c
@@ -1366,6 +1366,142 @@
}
END_TEST
+struct cb_none_ctx {
+ const jwk_item_t *key; /* given to config->key when @setkey */
+ int setkey;
+};
+
+/* A verify callback that FORCES config->alg = JWT_ALG_NONE (cb_pin only sets
+ * it for a non-NONE pin), to prove this no longer unpins the JWS JSON path. */
+static int cb_none(jwt_t *jwt, jwt_config_t *config)
+{
+ struct cb_none_ctx *x = config->ctx;
+
+ (void)jwt;
+ config->alg = JWT_ALG_NONE;
+ if (x != NULL && x->setkey)
+ config->key = x->key;
+
+ return 0;
+}
+
+/* ---- A callback resetting the pin to JWT_ALG_NONE verifies nothing with an
+ * alg-less key, and only the key's own "alg" with one, in EVERY serialization
+ * (issue #353: it used to unpin the JWS JSON Serialization path, so any alg of
+ * the key's type verified there while the Compact path rejected it) ---- */
+START_TEST(test_json_alg_none_cb)
+{
+ /* The RSA key with and without an "alg" are byte-identical but for
+ * that member, so one set of tokens verifies against either, and both
+ * carry this "kid". */
+ static const char kid[] = "28c243a4-a9ac-473f-ab61-d3a68f408279";
+ static const jwt_alg_t algs[] = {
+ JWT_ALG_RS256, JWT_ALG_RS512, JWT_ALG_PS256,
+ };
+ struct cb_none_ctx none = { NULL, 0 };
+ jwk_set_t *ks_noalg, *ks_alg;
+ const jwk_item_t *k_noalg, *k_alg;
+ struct cb_none_ctx supply;
+ char_auto *kid_hdr = NULL;
+ size_t f, a;
+
+ SET_OPS();
+
+ ks_noalg = load_one("rsa_key_2048_no_alg.json");
+ ks_alg = load_one("rsa_key_2048.json");
+ k_noalg = jwks_item_get(ks_noalg, 0);
+ k_alg = jwks_item_get(ks_alg, 0);
+ supply.key = k_noalg;
+ supply.setkey = 1;
+
+ ck_assert_int_gt(asprintf(&kid_hdr, "{\"kid\":\"%s\"}", kid), 0);
+
+ /* The same verdict must hold in Compact, Flattened and General. */
+ for (f = 0; f < ARRAY_SIZE(all_fmt); f++) {
+ for (a = 0; a < ARRAY_SIZE(algs); a++) {
+ char_auto *tok = fmt_tok(algs[a], k_noalg, NULL,
+ all_fmt[f]);
+ char_auto *ktok = fmt_tok(algs[a], k_noalg, kid_hdr,
+ all_fmt[f]);
+ int is_rs256 = (algs[a] == JWT_ALG_RS256);
+ jwt_checker_auto_t *cA = NULL, *cB = NULL, *cC = NULL;
+ jwt_checker_auto_t *cC2 = NULL, *cD = NULL, *cE = NULL;
+
+ /* A. A single alg-less key with the pin reset to NONE
+ * means "the key's alg"; it has none, so no token
+ * verifies, whatever its alg. */
+ cA = jwt_checker_new();
+ ck_assert_int_eq(jwt_checker_setkey(cA, JWT_ALG_RS256,
+ k_noalg), 0);
+ ck_assert_int_eq(jwt_checker_setcb(cA, cb_none, &none),
+ 0);
+ ck_assert_int_ne(jwt_checker_verify(cA, tok), 0);
+
+ /* B. A single alg-bearing key: NONE resolves to its
+ * "alg" (RS256), so only RS256 verifies. */
+ cB = jwt_checker_new();
+ ck_assert_int_eq(jwt_checker_setkey(cB, JWT_ALG_RS256,
+ k_alg), 0);
+ ck_assert_int_eq(jwt_checker_setcb(cB, cb_none, &none),
+ 0);
+ if (is_rs256)
+ ck_assert_int_eq(jwt_checker_verify(cB, tok), 0);
+ else
+ ck_assert_int_ne(jwt_checker_verify(cB, tok), 0);
+
+ /* C. A keyring of the alg-less key, token with no
+ * "kid" (a scan): the alg-less candidate is skipped,
+ * so nothing verifies. */
+ cC = jwt_checker_new();
+ ck_assert_int_eq(jwt_checker_setkeyring(cC, ks_noalg,
+ JWT_VERIFY_POLICY_ANY), 0);
+ ck_assert_int_eq(jwt_checker_setcb(cC, cb_none, &none),
+ 0);
+ ck_assert_int_ne(jwt_checker_verify(cC, tok), 0);
+
+ /* C2. A keyring of the alg-bearing key, scan: the
+ * candidate's "alg" (RS256) is pinned, so only RS256
+ * verifies. Keeps the scan skip's second operand
+ * (kalg == JWT_ALG_NONE) covered on its false side. */
+ cC2 = jwt_checker_new();
+ ck_assert_int_eq(jwt_checker_setkeyring(cC2, ks_alg,
+ JWT_VERIFY_POLICY_ANY), 0);
+ ck_assert_int_eq(jwt_checker_setcb(cC2, cb_none, &none),
+ 0);
+ if (is_rs256)
+ ck_assert_int_eq(jwt_checker_verify(cC2, tok),
+ 0);
+ else
+ ck_assert_int_ne(jwt_checker_verify(cC2, tok),
+ 0);
+
+ /* D. A keyring of the alg-less key, token WITH the
+ * matching "kid" (a kid-named explicit candidate): the
+ * alg-less key verifies nothing. */
+ cD = jwt_checker_new();
+ ck_assert_int_eq(jwt_checker_setkeyring(cD, ks_noalg,
+ JWT_VERIFY_POLICY_ANY), 0);
+ ck_assert_int_eq(jwt_checker_setcb(cD, cb_none, &none),
+ 0);
+ ck_assert_int_ne(jwt_checker_verify(cD, ktok), 0);
+
+ /* E. A keyring of the alg-less key, the callback both
+ * resets the pin and supplies that alg-less key: still
+ * nothing verifies. */
+ cE = jwt_checker_new();
+ ck_assert_int_eq(jwt_checker_setkeyring(cE, ks_noalg,
+ JWT_VERIFY_POLICY_ANY), 0);
+ ck_assert_int_eq(jwt_checker_setcb(cE, cb_none,
+ &supply), 0);
+ ck_assert_int_ne(jwt_checker_verify(cE, tok), 0);
+ }
+ }
+
+ jwks_free(ks_noalg);
+ jwks_free(ks_alg);
+}
+END_TEST
+
/* ---- A keyring needs any "kid" to be a string ---- */
START_TEST(test_keyring_kid_type)
{
@@ -1670,6 +1806,7 @@
tcase_add_loop_test(tc_core, test_compact_keyring_reuse, 0, i);
tcase_add_loop_test(tc_core, test_compact_keyring_embedded, 0, i);
tcase_add_loop_test(tc_core, test_json_alg_pin, 0, i);
+ tcase_add_loop_test(tc_core, test_json_alg_none_cb, 0, i);
tcase_add_loop_test(tc_core, test_keyring_kid_type, 0, i);
tcase_add_loop_test(tc_core, test_keyring_key_use, 0, i);