DEV Community

Mind vs Drive
Mind vs Drive

Posted on

WebAuthn "challenge mismatch"? Decode clientDataJSON and compare bytes, not strings

You've wired up passkeys, the browser prompt works, the authenticator signs, and then your server rejects the assertion with "challenge mismatch". The challenge you stored and the challenge the browser sent look almost the same. Nine times out of ten the bug isn't cryptography at all. It's the difference between Base64 and Base64URL.

This post walks through one illustrative login assertion byte by byte: what is inside clientDataJSON, why a plain string comparison fails, and how to read the 37-byte authenticatorData without a WebAuthn library.

What the browser actually sends

After navigator.credentials.get(), the response contains (among others) two ArrayBuffers: clientDataJSON and authenticatorData. Most front-ends ship them to the server as Base64URL strings, because that's what libraries like SimpleWebAuthn and the PublicKeyCredential.toJSON() method produce.

clientDataJSON is literally UTF-8 JSON. The WebAuthn Level 3 spec (W3C Recommendation, 25 August 2026, w3.org/TR/webauthn-3, checked 25 Nov 2026) defines the challenge member like this: it "contains the base64url encoding of the challenge provided by the Relying Party."

That one sentence is the whole bug.

The worked example (illustrative values)

Say the server generated these 32 random bytes as the challenge (hex):

161cd826db9c861b0d27277c7ba01a6a50c1db665d5bf9711e99fed7acc941bb
Enter fullscreen mode Exit fullscreen mode

Encoded two ways:

Encoding Value
Standard Base64 FhzYJtuchhsNJyd8e6AaalDB22ZdW/lxHpn+16zJQbs=
Base64URL (no padding) FhzYJtuchhsNJyd8e6AaalDB22ZdW_lxHpn-16zJQbs

Same 32 bytes. The strings differ in three places: / becomes _, + becomes -, and the trailing = is dropped. If your backend stored the standard form (for example via Node's buffer.toString('base64') or Python's base64.b64encode) and later does stored === clientData.challenge, it fails. With a 32-byte challenge it fails every single time: 32 bytes always encode to 43 characters plus one =, so the padding alone guarantees a mismatch.

And if someone "fixed" it by trimming the =, the bug becomes intermittent: the comparison then fails only when the random bytes happen to produce a + or /.

Step 1: decode clientDataJSON

Here is the clientDataJSON the browser would send for this challenge, Base64URL-encoded (176 characters, no padding):

eyJ0eXBlIjoid2ViYXV0aG4uZ2V0IiwiY2hhbGxlbmdlIjoiRmh6WUp0dWNoaHNOSnlkOGU2QWFhbERCMjJaZFdfbHhIcG4tMTZ6SlFicyIsIm9yaWdpbiI6Imh0dHBzOi8vZXhhbXBsZS5jb20iLCJjcm9zc09yaWdpbiI6ZmFsc2V9
Enter fullscreen mode Exit fullscreen mode

Decoded as UTF-8 it's 132 bytes:

{"type":"webauthn.get","challenge":"FhzYJtuchhsNJyd8e6AaalDB22ZdW_lxHpn-16zJQbs","origin":"https://example.com","crossOrigin":false}
Enter fullscreen mode Exit fullscreen mode

Three things to check immediately:

  1. type is webauthn.get for login (webauthn.create for registration). Mixing up the two endpoints gives a confusing error.
  2. origin must match exactly what you expect, scheme and port included. http://localhost:3000 is not http://localhost:5173.
  3. challenge is Base64URL. Decode it to bytes before you compare.

The real browser JSON may contain extra members (for example topOrigin, or an extra key that Chrome adds on purpose to catch servers that parse it with string templates). Always parse it as JSON. Don't regex it, and don't compare the whole string.

Step 2: compare bytes, not strings

The fix is boring and reliable: decode both sides to bytes.

// Node 16+
const stored = Buffer.from(session.challengeB64, 'base64');          // standard form
const sent   = Buffer.from(clientData.challenge, 'base64url');       // from the browser
const ok = stored.length === sent.length && crypto.timingSafeEqual(stored, sent);
Enter fullscreen mode Exit fullscreen mode
import base64, hmac

def b64url_decode(s: str) -> bytes:
    return base64.urlsafe_b64decode(s + "=" * (-len(s) % 4))

ok = hmac.compare_digest(base64.b64decode(stored_b64), b64url_decode(client["challenge"]))
Enter fullscreen mode Exit fullscreen mode

Running that on the example: the string comparison returns False, the byte comparison returns True. Better still, store the challenge as raw bytes (or as Base64URL from the start) so only one encoding exists in your codebase.

Step 3: read authenticatorData by hand

For an assertion without extensions, authenticatorData is exactly 37 bytes:

Bytes Field
0–31 rpIdHash: SHA-256 of the RP ID
32 flags
33–36 signCount, big-endian uint32

Our example (Base64URL, 50 characters):

o3mm9u6vuaVeN4wRgDTidR5oL6ufLTCrE9ISVYbOGUcdAAAAAA
Enter fullscreen mode Exit fullscreen mode

Decoded to hex:

A3 79 A6 F6 EE AF B9 A5 5E 37 8C 11 80 34 E2 75 1E 68 2F AB 9F 2D 30 AB 13 D2 12 55 86 CE 19 47 1D 00 00 00 00
Enter fullscreen mode Exit fullscreen mode
  • The first 32 bytes A379A6F6…CE1947 are SHA-256 of example.com. If yours don't match sha256(rpId), you've registered under a different RP ID (a classic when moving from localhost to a staging domain).
  • Flags 0x1D = 0001 1101 in binary: bit 0 UP (user present) = 1, bit 2 UV (user verified) = 1, bit 3 BE (backup eligible) = 1, bit 4 BS (backed up) = 1, bit 6 AT = 0, bit 7 ED = 0. That's the pattern you'd expect from a synced passkey that did a biometric or PIN check.
  • signCount = 00 00 00 00 = 0. Many synced passkeys always report 0, so treat "counter didn't increase" as a problem only if the stored counter was non-zero.

For registration, authenticatorData is longer (AT = 1, followed by attested credential data) and sits inside the CBOR attestationObject. A Base64 decoder will show you the hex, but you'll need a CBOR parser to read the structure.

Doing the decode in a browser tool

If you just want to look at a captured value during debugging, an online decoder is quicker than writing a script. With ours:

  1. Open the decoder, keep Decode Base64 selected and paste the clientDataJSON string. Base64URL characters (-, _) and missing padding are accepted as they are.
  2. Leave Character Set on UTF-8 to see the JSON; switch it to Hexadecimal for authenticatorData to get the spaced hex bytes shown above.
  3. To turn a hex challenge back into Base64URL, switch to Encode to Base64, set Character Set to Hexadecimal and enable URL-Safe Format.

It runs in your browser, but treat captured production assertions like any other credential material and prefer test accounts.

Checklist

  • clientDataJSON.challenge is Base64URL: decode, then compare bytes in constant time.
  • Parse clientDataJSON as JSON and check type and origin exactly.
  • authenticatorData[0..31] must equal sha256(rpId); byte 32 holds the UP/UV/BE/BS flags.
  • A signCount of 0 is normal for many passkeys.

All byte values in this post are illustrative and were generated for the example. They aren't from a real credential.

Disclosure: this tool is from Mind vs Drive, the team behind this account. https://base64decodetool.com/decode/

Top comments (0)