moodle/moodle · error · WebAuthnException
invalid nonce in JWS payload
Error message
invalid nonce in JWS payload
What it means
This error is thrown during validateAttestation() when the 'nonce' field in the SafetyNet JWS payload does not equal the Base64-encoded SHA-256 hash of (authenticatorData binary . clientDataHash). The nonce binds the SafetyNet response to this exact WebAuthn ceremony; a mismatch means the response was replayed, generated for a different request, or the clientDataHash passed in is wrong. This check implements the SafetyNet verification protocol step 2.
Solutions
- Re-run registration so the device generates a fresh SafetyNet response whose nonce matches the current authenticatorData and clientDataHash.
- Verify what you pass to validate(): it must be the raw SHA-256 hash bytes of clientDataJSON (as given by the library's authData/clientDataHash helpers), not the JSON itself or a base64 string.
- Check the client does not cache or reuse the SafetyNet response between attempts; each ceremony needs a new one.
- Base64-encode hash('SHA256', authDataBinary . clientDataHash, true) yourself and compare with payload nonce to identify which side differs.
Example fix
// before: re-hashing or mis-typing the client data on the server
$webauthn->processGet($data, null);
$res->validate(hash('sha256', $clientDataJSON)); // wrong layer
// after: let the library derive clientDataHash from the received response
$res->validate($res->getClientDataHash()); // matches nonce computed on device Defensive patterns
Strategy: try-catch
Validate before calling
<?php
$expected = base64_encode(hash('SHA256', $authenticatorDataBinary . $clientDataHash, true));
// decode payload segment and compare before calling validate():
function nonceMatches(string $response, string $clientDataHash, string $authDataBinary): bool {
$parts = explode('.', $response);
$p = json_decode(base64_decode(strtr($parts[1], '-_', '+/')));
return isset($p->nonce) && $p->nonce === base64_encode(hash('SHA256', $authDataBinary . $clientDataHash, true));
} Type guard
function hasNonce(?stdClass $payload): bool { return $payload !== null && isset($payload->nonce) && is_string($payload->nonce) && $payload->nonce !== ''; } Try / catch
try {
$res->validate($clientDataHash);
} catch (WebAuthnException $e) {
if (str_contains($e->getMessage(), 'invalid nonce')) {
// reject credential: response bound to a different ceremony (replay or wrong hash)
return false;
}
throw $e;
} Prevention
- Always use the library-computed clientDataHash (raw SHA-256 bytes of clientDataJSON) — never pass the JSON itself or a base64 string.
- Never reuse a SafetyNet response across ceremonies; each registration needs a fresh nonce-bound response.
- Store and reject replayed responses by tracking nonce values server-side.
- Log expected vs received nonce on failure to quickly identify which side computed it differently.
When it happens
Trigger: Calling AuthenticatorAttestationResponse->validate() (or AndroidSafetyNet::validateAttestation) with a clientDataHash that differs from the one the device used, registering with a stale/replayed SafetyNet response whose nonce was computed for a previous authenticatorData, or the nonce field missing/empty in the payload.
Common situations: Passing the wrong hash buffer type to validate() (e.g. hashing clientDataJSON again instead of using the pre-computed clientDataHash, or passing a base64 string instead of raw bytes); nonce cached across retries when authenticatorData changes (e.g. signCount differs); clock/client bugs reusing an old SafetyNet response; non-standard authenticators emitting nonce computed over different byte order.
Understand the failure class
Background: Checksum mismatch errors: "checksum verification failed", "digest mismatch", "expected vs actual checksum" — what they mean and how to fix them — this error's family across 41 libraries.
Related errors
- ByteBuffer: cannot generate random bytes
- ByteBuffer: Invalid offset
- ByteBuffer: Invalid offset or length
- certInfo:extraData not hash of attToBeSigned
- certInfo not found
AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14).
Data as JSON: /api/errors/4e3d10e0c72289eb.
Report an issue: GitHub.
Appendix: source
Thrown at public/lib/webauthn/src/Attestation/Format/AndroidSafetyNet.php:104
/*
* returns the key certificate in PEM format
* @return string
*/
public function getCertificatePem() {
return $this->_createCertificatePem($this->_x5c);
}
/**
* @param string $clientDataHash
*/
public function validateAttestation($clientDataHash) {
$publicKey = \openssl_pkey_get_public($this->getCertificatePem());
// Verify that the nonce in the response is identical to the Base64 encoding
// of the SHA-256 hash of the concatenation of authenticatorData and clientDataHash.
if (empty($this->_payload->nonce) || $this->_payload->nonce !== \base64_encode(\hash('SHA256', $this->_authenticatorData->getBinary() . $clientDataHash, true))) {
throw new WebAuthnException('invalid nonce in JWS payload', WebAuthnException::INVALID_DATA);
}
// Verify that attestationCert is issued to the hostname "attest.android.com"
$certInfo = \openssl_x509_parse($this->getCertificatePem());
if (!\is_array($certInfo) || ($certInfo['subject']['CN'] ?? '') !== 'attest.android.com') {
throw new WebAuthnException('invalid certificate CN in JWS (' . ($certInfo['subject']['CN'] ?? '-'). ')', WebAuthnException::INVALID_DATA);
}
// Verify that the basicIntegrity attribute in the payload of response is true.
if (empty($this->_payload->basicIntegrity)) {
throw new WebAuthnException('invalid basicIntegrity in payload', WebAuthnException::INVALID_DATA);
}
// check certificate
return \openssl_verify($this->_signedValue, $this->_signature, $publicKey, OPENSSL_ALGO_SHA256) === 1;
}
View on GitHub (pinned to 48ea8c3322)