jwt: reject unsigned tokens unless JWT_ALG_NONE is allowed
A jwt_checker_t with no key, no keyring and no embedded-JWK verification
accepted an unsigned ("alg":"none") Compact token by default. That is
easy to hit by accident: a keyless checker whose verify callback picks the
key by "kid" leaves config->key NULL on a miss and returns 0, and a forged
unsigned token then verifies (the keyed/keyring variants were closed by
GHSA-mhc9-xvg5-qg29; the keyless default remained).
Per RFC 8725 ยง3.2 ("Do not trust received claims ... SHOULD NOT accept
'none'") accept an unsigned token only when the application asks for it.
jwt_typ_alg_ok() already enforces jwt_checker_setalgs(): a non-empty
allowlist that lists JWT_ALG_NONE accepts a none token and one that omits
it rejects it. The only gap was the empty-allowlist default, which
accepted any alg. It now accepts any signed algorithm but rejects
jwt->alg == JWT_ALG_NONE, with a specific "Unsigned tokens are not
allowed" message. Listing JWT_ALG_NONE via jwt_checker_setalgs() is the
single, supported opt-in; no new exported symbol is added, so this stays a
revision-only ABI change.
Keyed, keyring and embedded-JWK checkers are unchanged (a none token to
them stays rejected). The JSON-serialization path is unaffected:
build_sig_entry() already refuses "alg":"none" there, so the new default
never fires on it.
jwt-verify(1) followed the old library default; it now opts in explicitly
with "-a none" (wired to jwt_checker_setalgs()), and rejects an unsigned
token without it. Docs (jwt_builder_setkey() table note, jwt_checker_setkey(),
jwt_checker_setalgs() and the verify-callback warning) and the tests
(check + bats, both JSON backends) are updated for the new default.
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 2ea6137..795cd0c 100644
--- a/include/jwt.h
+++ b/include/jwt.h
@@ -525,8 +525,10 @@
* ``alg-A`` | ``alg-B`` | \emoji :x:
* ``alg-A`` | ``NULL`` | \emoji :x:
*
- * @warning The warning represents an insecure token. Using insecure tokens is
- * not very useful and strongly discouraged.
+ * @warning The warning represents an insecure (unsigned, ``"alg":"none"``)
+ * token. Producing one is rarely useful and strongly discouraged, and a
+ * jwt_checker_t rejects it by default: the verifier must opt in by listing
+ * ::JWT_ALG_NONE in jwt_checker_setalgs() to accept it (@rfc{8725} §3.2).
*
* A key that is in error (jwks_item_error()) is refused, whatever @p alg is,
* and the builder keeps the key it has. A builder that has none is not left to
@@ -1040,12 +1042,15 @@
* 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 (which leaves it unkeyed; see below).
*
* 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
- * unkeyed: it verifies no JWS JSON Serialization, and of Compact tokens only
- * unsigned (``"alg":"none"``) ones, unless a verify callback supplies a key.
+ * unkeyed: it verifies no JWS JSON Serialization, and no Compact token unless a
+ * verify callback supplies a key. An unsigned (``"alg":"none"``) Compact token
+ * is the one kind an unkeyed checker can still verify, but only once unsigned
+ * tokens are allowed by listing ::JWT_ALG_NONE in jwt_checker_setalgs();
+ * otherwise it, too, is rejected (@rfc{8725} §3.2).
*
* Called from a verify or jti callback, though, it only removes the key: a
* checker that was given a key or keyring still requires one, so every later
@@ -1189,6 +1194,16 @@
* set is rejected, which also blocks an ``alg:none`` downgrade. Passing @p n as
* 0 (or @p algs as NULL) clears the allowlist.
*
+ * With no allowlist set (the default, or once cleared) any signed algorithm is
+ * accepted, but an unsigned (``"alg":"none"``) token is not: accepting one is
+ * opt-in (@rfc{8725} §3.2). List ::JWT_ALG_NONE here (alone, or alongside the
+ * signed algorithms) to accept unsigned tokens; a signed-only set keeps them
+ * out, as it always has. This is the supported way to consume unsigned tokens
+ * on purpose, and the only one.
+ *
+ * @note Before 3.7.0 a checker with no key, keyring or allowlist accepted an
+ * unsigned token by default; now that too requires the ::JWT_ALG_NONE opt-in.
+ *
* @param checker Pointer to a checker object
* @param algs An array of acceptable ::jwt_alg_t values (copied)
* @param n The number of entries in @p algs
@@ -1364,13 +1379,18 @@
* sets is the only one tried; a NULL key lets a signature without a ``kid``
* be tried against every compatible candidate.
* - Otherwise it is NULL. If the callback leaves it so, and has not given the
- * checker a key or keyring, only an unsigned (``"alg":"none"``) token can
- * verify.
+ * checker a key or keyring, no token verifies -- not even an unsigned
+ * (``"alg":"none"``) one, unless unsigned tokens were allowed by listing
+ * ::JWT_ALG_NONE in jwt_checker_setalgs() (@rfc{8725} §3.2, issue #352).
*
- * @warning In that last case a forged unsigned token is accepted, so a
- * callback that selects the key (e.g. by ``kid``) on a checker without a key,
- * keyring or embedded-JWK verification must return non-zero when it finds
- * none, or set config->alg.
+ * @warning A callback that selects the key (e.g. by ``kid``) on a checker with
+ * no key, keyring or embedded-JWK verification fails closed when it finds none:
+ * leaving config->key NULL rejects the token rather than accepting it unsigned.
+ * Returning non-zero (or setting config->alg) still makes the intent explicit.
+ * Only a checker that opted in to unsigned tokens (::JWT_ALG_NONE in
+ * jwt_checker_setalgs()) accepts a forged ``"alg":"none"`` token here, which is
+ * then the application's deliberate choice. Before 3.7.0 such a token was
+ * accepted by default (GHSA-mhc9-xvg5-qg29 for the keyed/keyring variants).
*
* The ctx value is also passed to the callback as part of the jwt_config_t
* struct.
diff --git a/libjwt/jwt-verify.c b/libjwt/jwt-verify.c
index 52aa814..931f3ff 100644
--- a/libjwt/jwt-verify.c
+++ b/libjwt/jwt-verify.c
@@ -583,13 +583,23 @@
}
/* @rfc{8725} Does the parsed header (@jwt->headers) and algorithm (@jwt->alg)
- * satisfy the checker's "typ" expectation and algorithm allowlist? Returns 1 if
- * acceptable (or nothing is configured), 0 if it should be rejected. */
-static int jwt_typ_alg_ok(jwt_t *jwt)
+ * satisfy the checker's "typ" expectation and algorithm policy? Returns 1 if
+ * acceptable, 0 if it should be rejected; on a 0 return @reason (when not NULL)
+ * is set to a static message naming the cause.
+ *
+ * @rfc{8725,3.2} With no explicit allowlist (jwt_checker_setalgs()), any signed
+ * algorithm is accepted but an unsigned ("alg":"none") token is not: consuming
+ * one requires opting in by listing ::JWT_ALG_NONE. A non-empty allowlist is
+ * authoritative either way, so JWT_ALG_NONE in it opts in and a signed-only
+ * allowlist keeps unsigned tokens out, as before. */
+static int jwt_typ_alg_ok(jwt_t *jwt, const char **reason)
{
jwt_checker_t *checker = jwt->checker;
size_t i;
+ if (reason != NULL)
+ *reason = "Token rejected by \"typ\" or algorithm policy";
+
if (checker == NULL)
return 1; // LCOV_EXCL_LINE
@@ -609,16 +619,27 @@
return 0;
}
+ /* @rfc{8725,3.2} No allowlist: accept any signed algorithm, but an
+ * unsigned token only when JWT_ALG_NONE was listed (handled above). */
+ if (jwt->alg == JWT_ALG_NONE) {
+ if (reason != NULL)
+ *reason = "Unsigned tokens are not allowed";
+ return 0;
+ }
+
return 1;
}
static int __verify_config_post(jwt_t *jwt, const jwt_config_t *config,
unsigned int sig_len)
{
- /* @rfc{8725} Enforce the typ expectation / algorithm allowlist first. */
- if (!jwt_typ_alg_ok(jwt)) {
- jwt_write_error(jwt,
- "Token rejected by \"typ\" or algorithm policy");
+ const char *reason;
+
+ /* @rfc{8725} Enforce the typ expectation / algorithm policy first. This
+ * also rejects an unsigned ("alg":"none") token by default
+ * (@rfc{8725,3.2}); @reason names which it was. */
+ if (!jwt_typ_alg_ok(jwt, &reason)) {
+ jwt_write_error(jwt, "%s", reason);
return 1;
}
@@ -1083,8 +1104,11 @@
}
/* @rfc{8725} A signature whose "typ"/alg does not meet the checker's
- * expectation is simply not accepted (skipped), so the policy decides. */
- if (!jwt_typ_alg_ok(jwt)) {
+ * expectation is simply not accepted (skipped), so the policy decides.
+ * (The unsigned-token default never fires here: build_sig_entry()
+ * already refuses "alg":"none" in a JSON serialization, so this is the
+ * typ/allowlist policy only, and the reason is not reported.) */
+ if (!jwt_typ_alg_ok(jwt, NULL)) {
jwt->headers = NULL;
return 0;
}
diff --git a/tests/jwt-cli.bats b/tests/jwt-cli.bats
index 2ffede7..6530778 100755
--- a/tests/jwt-cli.bats
+++ b/tests/jwt-cli.bats
@@ -24,7 +24,16 @@
}
@test "Verify a JWT with alg none" {
- ./tools/jwt-verify -v ${BASIC_RES}
+ # A keyless checker rejects unsigned tokens by default (issue #352);
+ # "-a none" is the explicit opt-in that accepts one.
+ ./tools/jwt-verify -v -a none ${BASIC_RES}
+}
+
+@test "Verify rejects an unsigned token without -a none" {
+ run ./tools/jwt-verify ${BASIC_RES}
+ [ "${status}" -ne 0 ]
+ echo "${output}" | grep -q "Unsigned tokens are not allowed"
+ refute_sanitizer
}
HS256_RES="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.CM4dD95Nj0vSfMGtDas432AUW1HAo7feCiAbt5Yjuds"
diff --git a/tests/jwt_checker.c b/tests/jwt_checker.c
index 5113a13..7228ef1 100644
--- a/tests/jwt_checker.c
+++ b/tests/jwt_checker.c
@@ -34,11 +34,68 @@
ck_assert_ptr_nonnull(checker);
ck_assert_int_eq(jwt_checker_error(checker), 0);
+ /* @rfc{8725,3.2} An unsigned token verifies only when the application
+ * opts in by listing JWT_ALG_NONE (issue #352). */
+ checker_allow_none(checker);
+
ret = jwt_checker_verify(checker, token);
ck_assert_int_eq(ret, 0);
}
END_TEST
+/* @rfc{8725,3.2} The default (no opt-in) rejects an unsigned token, with a
+ * specific error, whether or not a verify callback is set, and whether or not
+ * that callback leaves config->key NULL (issue #352, GHSA-mhc9-xvg5-qg29). */
+static int __leave_key_null(jwt_t *jwt, jwt_config_t *config)
+{
+ (void)jwt;
+ config->key = NULL; /* e.g. a "kid" lookup that found nothing */
+ return 0;
+}
+
+START_TEST(verify_none_rejected_by_default)
+{
+ const char token[] = "eyJhbGciOiJub25lIn0.eyJpc3MiOiJka"
+ "XNrLnN3aXNzZGlzay5jb20ifQ.";
+ jwt_checker_auto_t *checker = NULL;
+ int ret;
+
+ SET_OPS();
+
+ /* No key, no keyring, no allowlist, no callback: rejected. */
+ checker = jwt_checker_new();
+ ck_assert_ptr_nonnull(checker);
+ ret = jwt_checker_verify(checker, token);
+ ck_assert_int_ne(ret, 0);
+ ck_assert_str_eq(jwt_checker_error_msg(checker),
+ "Unsigned tokens are not allowed");
+ jwt_checker_free(checker);
+
+ /* A verify callback that leaves config->key NULL does not change it. */
+ checker = jwt_checker_new();
+ ck_assert_ptr_nonnull(checker);
+ ck_assert_int_eq(jwt_checker_setcb(checker, __leave_key_null, NULL), 0);
+ ret = jwt_checker_verify(checker, token);
+ ck_assert_int_ne(ret, 0);
+ ck_assert_str_eq(jwt_checker_error_msg(checker),
+ "Unsigned tokens are not allowed");
+
+ /* Opting in with JWT_ALG_NONE accepts it, callback and all. */
+ checker_allow_none(checker);
+ ret = jwt_checker_verify(checker, token);
+ ck_assert_int_eq(ret, 0);
+
+ /* And a signed-only allowlist (no JWT_ALG_NONE) keeps it out. */
+ {
+ const jwt_alg_t signed_only[] = { JWT_ALG_ES256 };
+
+ ck_assert_int_eq(jwt_checker_setalgs(checker, signed_only, 1), 0);
+ ret = jwt_checker_verify(checker, token);
+ ck_assert_int_ne(ret, 0);
+ }
+}
+END_TEST
+
START_TEST(alg_none_with_sig)
{
const char token[] = "eyJhbGciOiJub25lIn0.eyJpc3MiOiJka"
@@ -52,6 +109,10 @@
ck_assert_ptr_nonnull(checker);
ck_assert_int_eq(jwt_checker_error(checker), 0);
+ /* Allow "none" so the malformed token reaches the signature-block
+ * check rather than the unsigned-token default (issue #352). */
+ checker_allow_none(checker);
+
ret = jwt_checker_verify(checker, token);
ck_assert_int_ne(ret, 0);
ck_assert_str_eq(jwt_checker_error_msg(checker),
@@ -225,6 +286,9 @@
ret = jwt_checker_setcb(checker, __verify_wcb, "testing");
ck_assert_int_eq(ret, 0);
+ /* This token is unsigned; opt in so the callback path is exercised. */
+ checker_allow_none(checker);
+
ret = jwt_checker_verify(checker, token);
ck_assert_int_eq(ret, 0);
@@ -247,6 +311,8 @@
ck_assert_ptr_nonnull(checker);
ck_assert_int_eq(jwt_checker_error(checker), 0);
+ checker_allow_none(checker);
+
for (i = 0; i < 1000; i++) {
int ret = jwt_checker_verify(checker, token);
ck_assert_int_eq(ret, 0);
@@ -903,6 +969,9 @@
ret = jwt_checker_setkey(checker, JWT_ALG_NONE, NULL);
ck_assert_int_eq(ret, 0);
+ /* These tokens are unsigned; opt in so the "aud" checks are reached. */
+ checker_allow_none(checker);
+
ret = jwt_checker_claim_set(checker, JWT_CLAIM_AUD, "me");
ck_assert_int_eq(ret, 0);
@@ -1178,6 +1247,8 @@
ret = jwt_checker_understands(checker, "exp1");
ck_assert_int_eq(ret, 0);
+ checker_allow_none(checker); /* the token is unsigned (issue #352) */
+
ret = jwt_checker_verify(checker, token);
ck_assert_int_eq(ret, 0);
}
@@ -1263,6 +1334,8 @@
ret = jwt_checker_understands(checker, "exp2");
ck_assert_int_eq(ret, 0);
+ checker_allow_none(checker); /* the token is unsigned (issue #352) */
+
ret = jwt_checker_verify(checker, token);
ck_assert_int_eq(ret, 0);
}
@@ -1332,6 +1405,7 @@
jwt_checker_error_clear(checker);
ret = jwt_checker_understands(checker, "exp1");
ck_assert_int_eq(ret, 0);
+ checker_allow_none(checker); /* the built token is unsigned (#352) */
ret = jwt_checker_verify(checker, out);
ck_assert_int_eq(ret, 0);
}
@@ -1384,6 +1458,8 @@
ret = jwt_checker_understands(checker, "exp2");
ck_assert_int_eq(ret, 0);
+ checker_allow_none(checker); /* the token is unsigned (issue #352) */
+
ret = jwt_checker_verify(checker, token);
ck_assert_int_eq(ret, 0);
}
@@ -1443,6 +1519,8 @@
ret = jwt_checker_setjti(checker, __jti_check, "pool");
ck_assert_int_eq(ret, 0);
+ checker_allow_none(checker); /* the token is unsigned (issue #352) */
+
ret = jwt_checker_verify(checker, token);
ck_assert_int_eq(ret, 0);
}
@@ -1471,6 +1549,10 @@
ret = jwt_checker_setjti(checker, __jti_reject, NULL);
ck_assert_int_eq(ret, 0);
+ /* Unsigned token: opt in so the jti check (not the unsigned-token
+ * default) is what rejects it (issue #352). */
+ checker_allow_none(checker);
+
ret = jwt_checker_verify(checker, token);
ck_assert_int_ne(ret, 0);
ck_assert_str_eq(jwt_checker_error_msg(checker),
@@ -1541,6 +1623,8 @@
ret = jwt_checker_setjti(checker, __jti_reject, NULL);
ck_assert_int_eq(ret, 0);
+ checker_allow_none(checker); /* the token is unsigned (issue #352) */
+
ret = jwt_checker_verify(checker, token);
ck_assert_int_ne(ret, 0);
ck_assert_str_eq(jwt_checker_error_msg(checker),
@@ -1565,6 +1649,8 @@
ret = jwt_checker_setjti(checker, __jti_reject, NULL);
ck_assert_int_eq(ret, 0);
+ checker_allow_none(checker); /* the token is unsigned (issue #352) */
+
ret = jwt_checker_verify(checker, token);
ck_assert_int_ne(ret, 0);
ck_assert_str_eq(jwt_checker_error_msg(checker),
@@ -1646,6 +1732,8 @@
ret = jwt_checker_setjti(checker, __jti_pool_check, &seen);
ck_assert_int_eq(ret, 0);
+ checker_allow_none(checker); /* the built token is unsigned (#352) */
+
/* First use: accepted and consumed. */
ret = jwt_checker_verify(checker, out);
ck_assert_int_eq(ret, 0);
@@ -1965,6 +2053,7 @@
tc_core = tcase_create("Verify");
tcase_set_timeout(tc_core, 30);
tcase_add_loop_test(tc_core, verify, 0, i);
+ tcase_add_loop_test(tc_core, verify_none_rejected_by_default, 0, i);
tcase_add_loop_test(tc_core, verify_bad_header, 0, i);
tcase_add_loop_test(tc_core, verify_bad_payload, 0, i);
tcase_add_loop_test(tc_core, verify_rsapss384, 0, i);
diff --git a/tests/jwt_key_error.c b/tests/jwt_key_error.c
index 415591a..c2e99e6 100644
--- a/tests/jwt_key_error.c
+++ b/tests/jwt_key_error.c
@@ -238,11 +238,17 @@
ck_assert_int_eq(jwt_checker_verify(c1, jtok), 0);
ck_assert_int_ne(jwt_checker_verify(c1, FORGED_NONE), 0);
- /* or the application itself makes it unkeyed. */
+ /* or the application itself makes it unkeyed. The latch is then gone: an
+ * unsigned token is turned away by the unsigned-token default (issue
+ * #352), not ERR_KEY, and the JWT_ALG_NONE opt-in accepts it. */
c2 = jwt_checker_new();
ck_assert_ptr_nonnull(c2);
ck_assert_int_ne(jwt_checker_setkey(c2, JWT_ALG_ES256, bad), 0);
ck_assert_int_eq(jwt_checker_setkey(c2, JWT_ALG_NONE, NULL), 0);
+ ck_assert_int_ne(jwt_checker_verify(c2, FORGED_NONE), 0);
+ ck_assert_str_eq(jwt_checker_error_msg(c2),
+ "Unsigned tokens are not allowed");
+ checker_allow_none(c2);
ck_assert_int_eq(jwt_checker_verify(c2, FORGED_NONE), 0);
/* A refused key does not replace the one the checker has. */
@@ -645,8 +651,8 @@
/* Checker: an alg/kty mismatch is refused, again not for being in error,
* so the checker is simply left unkeyed (not the key_required
* fail-closed): a latched checker would reject even an unsigned token
- * with ERR_KEY, where an unkeyed one takes it, as it would with no setkey
- * at all. */
+ * with ERR_KEY, where an unkeyed one takes it (with the JWT_ALG_NONE
+ * opt-in, issue #352), as it would with no setkey at all. */
{
jwt_checker_auto_t *c = jwt_checker_new();
@@ -655,6 +661,7 @@
ck_assert_str_eq(jwt_checker_error_msg(c),
"Key type does not match algorithm");
jwt_checker_error_clear(c);
+ checker_allow_none(c);
ck_assert_int_eq(jwt_checker_verify(c, FORGED_NONE), 0);
}
diff --git a/tests/jwt_security.c b/tests/jwt_security.c
index dd6f2d0..4abd8a7 100644
--- a/tests/jwt_security.c
+++ b/tests/jwt_security.c
@@ -842,6 +842,9 @@
checker = jwt_checker_new();
ck_assert_ptr_nonnull(checker);
+ /* An unsigned token verifies only with the opt-in (issue #352). */
+ checker_allow_none(checker);
+
/* {"alg":"none"} . {} . (empty sig) */
ret = jwt_checker_verify(checker,
"eyJhbGciOiJub25lIn0.e30.");
@@ -1416,6 +1419,9 @@
checker = jwt_checker_new();
ck_assert_ptr_nonnull(checker);
ck_assert_int_eq(jwt_checker_setkey(checker, JWT_ALG_NONE, NULL), 0);
+ /* These are unsigned tokens; the duplicate-member check is what is
+ * under test, so opt in past the unsigned-token default (issue #352). */
+ checker_allow_none(checker);
ret = jwt_checker_verify(checker, dup_hdr);
#ifdef HAVE_JSON_C
@@ -1450,6 +1456,7 @@
checker = jwt_checker_new();
ck_assert_ptr_nonnull(checker);
ck_assert_int_eq(jwt_checker_setkey(checker, JWT_ALG_NONE, NULL), 0);
+ checker_allow_none(checker); /* unsigned token (issue #352) */
/* The default checker enables EXP/NBF, but a token lacking them is
* accepted (absence is not a failure). */
@@ -1554,18 +1561,24 @@
ck_assert_int_ne(jwt_checker_verify(c, signed_tok), 0);
ck_assert_str_eq(jwt_checker_error_msg(c), nokey);
- /* Replacing the key is still allowed. */
+ /* Replacing the key is still allowed. The forged
+ * unsigned token is now turned away by the
+ * unsigned-token default (issue #352) before the
+ * missing-signature check the keyed checker used to
+ * report; either way it does not verify. */
ks.key = g_item;
ks.clear_alg = 0;
ck_assert_int_eq(jwt_checker_verify(c, signed_tok), 0);
ck_assert_int_ne(jwt_checker_verify(c, forged), 0);
ck_assert_str_eq(jwt_checker_error_msg(c),
- "Expected a signature, but JWT has none");
+ "Unsigned tokens are not allowed");
}
}
- /* Unchanged: a checker given no key at all, whose callback finds none
- * either, verifies only unsigned tokens (see jwt_checker_setcb()). */
+ /* @rfc{8725,3.2} A checker given no key at all, whose callback finds
+ * none either, now rejects the forged unsigned token by default
+ * (issue #352): leaving config->key NULL is no longer enough to accept
+ * it. A signed token still needs a key. */
{
jwt_checker_auto_t *c = jwt_checker_new();
@@ -1573,10 +1586,18 @@
ks.key = NULL;
ks.clear_alg = 0;
ck_assert_int_eq(jwt_checker_setcb(c, key_swap_cb, &ks), 0);
- ck_assert_int_eq(jwt_checker_verify(c, forged), 0);
+ ck_assert_int_ne(jwt_checker_verify(c, forged), 0);
+ ck_assert_str_eq(jwt_checker_error_msg(c),
+ "Unsigned tokens are not allowed");
+ ck_assert_uint_eq(jwt_checker_sig_count(c), 0);
ck_assert_int_ne(jwt_checker_verify(c, signed_tok), 0);
ck_assert_str_eq(jwt_checker_error_msg(c),
"JWT has signature, but no key was given");
+
+ /* Opting in with JWT_ALG_NONE accepts the unsigned token on
+ * purpose, for an application that wants exactly that. */
+ checker_allow_none(c);
+ ck_assert_int_eq(jwt_checker_verify(c, forged), 0);
}
free_key();
@@ -1752,9 +1773,13 @@
ck_assert_str_eq(jwt_checker_error_msg(c),
"A callback removed the checker's key");
- /* The application itself can still make the checker unkeyed. */
+ /* The application itself can still take the key back, outside a verify.
+ * The checker is then unkeyed, and with the unsigned-token opt-in
+ * (issue #352) it accepts the forged unsigned token on purpose, while a
+ * signed one has no key (and is not in the allowlist) to verify with. */
ck_assert_int_eq(jwt_checker_setjti(c, NULL, NULL), 0);
ck_assert_int_eq(jwt_checker_setkey(c, JWT_ALG_NONE, NULL), 0);
+ checker_allow_none(c);
ck_assert_int_eq(jwt_checker_verify(c, forged), 0);
ck_assert_int_ne(jwt_checker_verify(c, signed_tok), 0);
diff --git a/tests/jwt_tests.h b/tests/jwt_tests.h
index 509c3e8..837c584 100644
--- a/tests/jwt_tests.h
+++ b/tests/jwt_tests.h
@@ -101,6 +101,17 @@
}
#define char_auto char __attribute__((cleanup(jwt_freememp)))
+/* @rfc{8725,3.2} A checker rejects an unsigned ("alg":"none") token by
+ * default (issue #352); listing ::JWT_ALG_NONE via jwt_checker_setalgs() is
+ * the opt-in that accepts one. A test that verifies a genuine unsigned token
+ * calls this on its checker first. */
+static inline void checker_allow_none(jwt_checker_t *checker)
+{
+ const jwt_alg_t algs[] = { JWT_ALG_NONE };
+
+ ck_assert_int_eq(jwt_checker_setalgs(checker, algs, 1), 0);
+}
+
__attribute__((unused)) static jwk_set_t *g_jwk_set;
__attribute__((unused)) static const jwk_item_t *g_item;
diff --git a/tools/jwt-verify.c b/tools/jwt-verify.c
index 29ba627..2bd2993 100644
--- a/tools/jwt-verify.c
+++ b/tools/jwt-verify.c
@@ -41,7 +41,9 @@
-h, --help This help information\n\
-l, --list List supported algorithms and exit\n\
-a, --algorithm=ALG JWT algorithm to use (e.g. ES256). Only needed if the key\n\
- provided with -k does not have an \"alg\" attribute\n\
+ provided with -k does not have an \"alg\" attribute. With\n\
+ no key, \"-a none\" accepts an unsigned (\"alg\":\"none\")\n\
+ token; without it such a token is rejected\n\
-p, --print=CMD When printing JSON, pipe through CMD (requires -v)\n\
-k, --key=FILE Filename containing a JSON Web Key\n\
-r, --keyring=FILE Filename containing a JWKS to verify against (RFC 7515),\n\
@@ -57,8 +59,9 @@
from stdin, one per line.\n\
\n\
The exit status is 0 if every token was verified (its signature checked\n\
-with -k or -r; with neither, an unsigned token is only decoded). It is 1\n\
-if any token failed, or if there was no token to verify.\n\
+with -k or -r). With neither, a token is rejected unless it is unsigned and\n\
+\"-a none\" was given, which decodes it without checking a signature. It is\n\
+1 if any token failed, or if there was no token to verify.\n\
\n\
-h and -l verify nothing and exit 0 only when given alone. An argument that\n\
starts with - is read as an option unless it comes after --. To check a\n\
@@ -412,6 +415,26 @@
}
}
+ /* With neither a key nor a keyring the checker is keyless and, by
+ * default, rejects every token -- an unsigned ("alg":"none") one
+ * included (RFC 8725 §3.2). An explicit "-a none" is how the caller
+ * opts in to unsigned tokens, the one thing a keyless checker still
+ * accepts (it only decodes them). Wire it to the allowlist so the
+ * library applies the policy. */
+ if (key_file == NULL && keyring_file == NULL && alg_given &&
+ alg == JWT_ALG_NONE) {
+ const jwt_alg_t none_alg[] = { JWT_ALG_NONE };
+
+ if (jwt_checker_setalgs(checker, none_alg, 1)) {
+ // LCOV_EXCL_START - defensive: setalgs with a valid
+ // algorithm on a fresh checker does not fail
+ fprintf(stderr, "ERR allowing unsigned tokens: %s\n",
+ jwt_checker_error_msg(checker));
+ return EXIT_FAILURE;
+ // LCOV_EXCL_STOP
+ }
+ }
+
if (verbose && jwt_checker_setcb(checker, __jwt_wcb, NULL)) {
// LCOV_EXCL_START - defensive: setting our own static callback
// does not fail in practice