`z.jwt()` rejects valid tokens whose header contains `-` or `_`
isValidJWT decodes the header segment with atob(header), but JWT segments are base64url (RFC 7515). atob only accepts the standard alphabet, so it throws on - and _, the catch returns false, and a valid token fails with "Invalid JWT". Whether a token hits this depends only on what its header encodes to: a ? or > anywhere in a header value produces one of those characters, so a jku URL with a query string is enough.
import * as z from "zod";
const b64url = (o: object) => Buffer.from(JSON.stringify(o)).toString("base64url");
const header = b64url({ alg: "RS256", jku: "https://idp.example.com/jwks?tenant=abc" });
const token = `${header}.${b64url({ sub: "1" })}.sig`;
z.jwt().safeParse(token).success; // false
atob(header); // InvalidCharacterErrorReproduced on 4.6.5 and on main (10dda3a). v3's isValidJWT maps base64url back to base64 before calling atob, and util.base64urlToUint8Array does the same for the base64url codecs; the v4 check skips that step. Out of 20,000 generated tokens with random kid values, 4.6.5 rejected exactly the 1,871 whose header contained - or _, and none of the others.
PR creation is limited to collaborators, so the fix is on a branch: https://github.com/colinhacks/zod/compare/main...Prafyl:zod:fix/jwt-base64url-header
It maps the header back to padded base64 the same way util.base64urlToUint8Array does. The compiled path calls the same isValidJWT, so it needs no change, and the new test fails before the fix in both the zod and compile-mode projects. Costs about 40 B gzipped on schemas.ts; atob is forgiving about missing padding, so dropping the padding line halves that if you'd rather not carry it.
Source: colinhacks/zod