Limits

Know how much work one token can make your server do, what happens when it asks for more, and when to raise a bound.

A token comes from anyone who can reach your server, and some tokens ask for a lot of work. A JSON JWS can carry dozens of signatures, a JWE dozens of recipients, a JWK Set dozens of keys to try, and a password-based JWE names how many rounds of key stretching it wants. Each one multiplies the work of a single request. unjwt bounds each of them per call and fails closed: past a bound, the call throws before doing the work, instead of trying harder.

#Before you start

  • Limits are set per call, in the limits option of verify, decrypt and their multi variants. Each call takes only the limits that apply to it.
  • Every value is a positive integer. 0, 1.5 or a string throws ERR_INVALID_OPTION.
  • DEFAULT_JOSE_LIMITS, from unjwt, unjwt/jws and unjwt/jwe, holds the defaults below.
  • unjwt sets no limit on a token's length. Your server's limits on header and body size are the bound there.

#The limits

LimitDefaultCountsTaken byPast it
signatures16Entries in a JSON JWS's signaturesverifyMulti, verifyMultiAllERR_JWS_INVALID, before any signature is checked
recipients16Entries in a JSON JWE's recipientsdecryptMultiERR_JWE_INVALID, before any recipient is tried
keyCandidates8Keys of a JWK Set left to try for one token, signature or recipient, after filtering by kid, alg and useverify, decrypt and the multi variants, sessionsERR_JWK_KEY_NOT_FOUND, before the first key is tried; on a multi call of several entries, as that entry's failure in cause. A session whose JWK Set key holds a key without kid that would pass it throws ERR_SESSION_CONFIG_INVALID when it is defined
keyAttempts16Key trials one call spends, summed over every signature or recipientverifyMulti, verifyMultiAll, decryptMultiERR_JWK_KEY_NOT_FOUND, and the call stops: verifyMultiAll too
pbes2Iterations2,400,000Password-stretching rounds one call spends, summed over every password-based attemptdecrypt, decryptMultiERR_JWE_INVALID; on decryptMulti with several recipients, as that recipient's failure in cause

The defaults fit ordinary traffic: a kid in every token narrows a JWK Set to one key, 16 key attempts cover one key tried against each of 16 entries, and 2,400,000 rounds are four password-based recipients at the 600,000 rounds encrypt uses.

#Raise a limit for a known source

Raise a limit for one call, and only as far as the source you trust needs. This partner publishes 12 keys and signs without a kid:

limits.ts
import type { JWKSet } from "unjwt/jwk";
import { verify, verifyMultiAll, type JWSGeneralSerialization } from "unjwt/jws";

/**
 * A partner publishes 12 public keys and signs without a `kid`, so every
 * token has to be tried against all of them. The default of 8 refuses that
 * set; this reader raises the bound to exactly what the partner publishes.
 */
export async function readPartnerToken(token: string, partnerKeys: JWKSet) {
  const { payload } = await verify(token, partnerKeys, {
    algorithms: ["ES256"],
    issuer: "https://partner.example.com",
    limits: { keyCandidates: 12 },
  });
  return payload;
}
  • Pinned to the source: 12 is what the partner publishes. If their set grows, the call fails with ERR_JWK_KEY_NOT_FOUND and you find out, instead of trying ever more keys per request.
  • The better fix: ask the issuer to put a kid in the header. With one, the set narrows to a single key and the limit never matters.

#Raise the limits of a multi call

A JSON JWS signed by many parties needs room for every signature and for one key trial per signature:

limits.ts
/**
 * A release signed by up to 24 maintainers, accepted once 3 signatures verify.
 * Every signature carries its maintainer's `kid`, so each costs one key trial.
 */
export async function isApproved(release: JWSGeneralSerialization, maintainers: JWKSet) {
  const outcomes = await verifyMultiAll(release, () => maintainers, {
    algorithms: ["Ed25519"],
    limits: { signatures: 24, keyAttempts: 24 },
  });
  return outcomes.filter((outcome) => outcome.verified).length >= 3;
}

signatures: 24 lets 24 entries in; keyAttempts: 24 pays for one trial each, which holds because every signature names its maintainer's kid. Without a kid, each signature would try every key, and 24 signatures against 24 keys would need 576 attempts: that's the work the budget exists to refuse.

#Fixed bounds

A few bounds come from the specs or from the way a token is stored, and aren't part of limits:

BoundValuePast it
Rounds named by one password-based JWE (p2c) when decrypting1,000 to 600,000ERR_JWE_INVALID. Change the range with minIterations / maxIterations on decrypt.
Rounds you ask encrypt for1,000 to 600,000ERR_INVALID_OPTION
apu / apv in an ECDH-ES header1024 bytes eachERR_JWE_INVALID
Cookie chunks per session16ERR_SESSION_INVALID from update(), or from use() when a roll issues the token

A session token longer than one cookie holds is split across several, 4000 characters each unless cookie.chunkMaxLength says otherwise. A token that needs more than 16 is refused when you update the session, or when a roll reissues it inside use(), so keep session data small: an id and a few flags, not a profile. A request whose cookie claims more than 16 chunks reads as having no session. Configure a session covers the cookie options.

#Decide whether to raise one

A limit that fires on real traffic means your tokens, keys or senders do more than the default expects. Before you raise it:

  • Narrow first: a kid in the header, or strictSignerMatch / strictRecipientMatch on a multi call, cuts the work at the source.
  • Raise per source: pass the raised limits on the call that reads that issuer's tokens, not on every call.
  • Raise to a number you can name: the size of the key set you publish, the number of signers a release has. Then a token that asks for more still fails.

#When it fails

SituationWhat happens
More signatures or recipients than the limitERR_JWS_INVALID / ERR_JWE_INVALID, before any is read.
A JWK Set leaves more keys to try than keyCandidatesERR_JWK_KEY_NOT_FOUND, saying to narrow the keys with a kid.
A multi call runs out of keyAttemptsERR_JWK_KEY_NOT_FOUND, and no further entry is tried.
Password-based rounds out of range, or past pbes2IterationsERR_JWE_INVALID. On decryptMulti, that recipient fails with it, and the call throws ERR_JWE_DECRYPTION_FAILED when no other recipient opens, or ERR_JWE_INVALID itself when it is the only one.
A limit that isn't a positive integer, or limits that isn't an objectERR_INVALID_OPTION.
A session token needs more than 16 cookie chunksERR_SESSION_INVALID, from update(), or from use() when a roll issues the token.

Handle errors lists every code and what to answer.

#Going further