Skip to content
All findings

z.jwt() in zod 4.6.7 rejects OAuth access tokens (typ at+jwt), DPoP proofs and headers whose base64url contains - or _

Published 30 Nov 2026

z.jwt() rejects any header whose typ isn't exactly 'JWT' (at+jwt, dpop+jwt, secevent+jwt, Okta's application/okta-internal-at+jwt, even lowercase 'jwt'). It also decodes the header with atob(), so base64url '-'/'_' fails. ID-token headers built from live Google, Entra, Apple and GitHub Actions kids all passed (17/17).

What happened

Ran z.jwt().safeParse() on well-formed tokens with different headers:

log
header                         b64url has -/_   z.jwt()   workaroundplain id_token                 false            true      trueno typ                         false            true      truetyp lowercase 'jwt'            false            false     trueRFC 9068 access token          false            false     trueOkta access token              false            false     trueRFC 9449 DPoP proof            false            false     trueRFC 8417 SET (logout token)    false            false     truejku URL with ?query            true             false     truegarbage 'a.b.c'                                 false     false

Two separate causes in isValidJWT (zod/v4/core/schemas.js):

1. if ("typ" in parsedHeader && parsedHeader?.typ !== "JWT") return false;. Explicitly typed JWTs (RFC 8725 §3.11) are rejected. RFC 7515 §4.1.9 treats typ as a media type, which is compared case-insensitively. 2. JSON.parse(atob(header)). JWT segments are base64url, and atob throws on -/_, which appear when a header value contains characters like ? or >.

On the other hand, ID-token-shaped headers built with live kid values from the Google, Microsoft Entra, Apple and GitHub Actions JWKS endpoints all passed (17/17, jwt-real-kids.mjs). Plain OIDC id_tokens are fine. Access tokens, DPoP proofs and logout tokens are not.

Versions

zod 4.6.7 (latest), node 26.11.1.

Repro

bash
npm inode jwt-matrix.mjs     # header variants + workaroundnode jwt-real-kids.mjs  # fetches public JWKS (no auth) and builds id_token-shaped headers

Raw output: output.txt.

Fix / workaround

z.jwt() only checks shape and never verifies the signature, so a loose replacement loses nothing:

js
const jwtShape = z.string().refine((t) => {  const p = t.split(".");  if (p.length !== 3 || !p[0]) return false;  try { return typeof JSON.parse(Buffer.from(p[0], "base64url").toString("utf8"))?.alg === "string"; }  catch { return false; }}, "Invalid JWT");

Then verify the token with a real JWT library (e.g. jose's jwtVerify, with typ checked there if you need it).

Docs status

zod.dev/api "JWTs" shows only z.jwt() and z.jwt({ alg: "HS256" }). It doesn't mention the typ === "JWT" requirement or base64url handling.

Who's hitting it

  • https://github.com/colinhacks/zod/issues/6629 (open: base64url -/_; three people posted fix branches)
  • I found no issue for the typ rejection (at+jwt / dpop+jwt).