{"record":{"id":"4e3d10e0c72289eb","repo":"moodle/moodle","slug":"invalid-nonce-in-jws-payload","errorCode":null,"errorMessage":"invalid nonce in JWS payload","messagePattern":"invalid nonce in JWS payload","errorType":"exception","errorClass":"WebAuthnException","httpStatus":null,"severity":"error","filePath":"public/lib/webauthn/src/Attestation/Format/AndroidSafetyNet.php","lineNumber":104,"sourceCode":"\n    /*\n     * returns the key certificate in PEM format\n     * @return string\n     */\n    public function getCertificatePem() {\n        return $this->_createCertificatePem($this->_x5c);\n    }\n\n    /**\n     * @param string $clientDataHash\n     */\n    public function validateAttestation($clientDataHash) {\n        $publicKey = \\openssl_pkey_get_public($this->getCertificatePem());\n\n        // Verify that the nonce in the response is identical to the Base64 encoding\n        // of the SHA-256 hash of the concatenation of authenticatorData and clientDataHash.\n        if (empty($this->_payload->nonce) || $this->_payload->nonce !== \\base64_encode(\\hash('SHA256', $this->_authenticatorData->getBinary() . $clientDataHash, true))) {\n            throw new WebAuthnException('invalid nonce in JWS payload', WebAuthnException::INVALID_DATA);\n        }\n\n        // Verify that attestationCert is issued to the hostname \"attest.android.com\"\n        $certInfo = \\openssl_x509_parse($this->getCertificatePem());\n        if (!\\is_array($certInfo) || ($certInfo['subject']['CN'] ?? '') !== 'attest.android.com') {\n            throw new WebAuthnException('invalid certificate CN in JWS (' . ($certInfo['subject']['CN'] ?? '-'). ')', WebAuthnException::INVALID_DATA);\n        }\n\n        // Verify that the basicIntegrity attribute in the payload of response is true.\n        if (empty($this->_payload->basicIntegrity)) {\n            throw new WebAuthnException('invalid basicIntegrity in payload', WebAuthnException::INVALID_DATA);\n        }\n\n        // check certificate\n        return \\openssl_verify($this->_signedValue, $this->_signature, $publicKey, OPENSSL_ALGO_SHA256) === 1;\n    }\n\n","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/webauthn/src/Attestation/Format/AndroidSafetyNet.php#L86-L122","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before: re-hashing or mis-typing the client data on the server\n$webauthn->processGet($data, null);\n$res->validate(hash('sha256', $clientDataJSON)); // wrong layer\n// after: let the library derive clientDataHash from the received response\n$res->validate($res->getClientDataHash()); // matches nonce computed on device","handlingStrategy":"try-catch","validationCode":"<?php\n$expected = base64_encode(hash('SHA256', $authenticatorDataBinary . $clientDataHash, true));\n// decode payload segment and compare before calling validate():\nfunction nonceMatches(string $response, string $clientDataHash, string $authDataBinary): bool {\n    $parts = explode('.', $response);\n    $p = json_decode(base64_decode(strtr($parts[1], '-_', '+/')));\n    return isset($p->nonce) && $p->nonce === base64_encode(hash('SHA256', $authDataBinary . $clientDataHash, true));\n}","typeGuard":"function hasNonce(?stdClass $payload): bool { return $payload !== null && isset($payload->nonce) && is_string($payload->nonce) && $payload->nonce !== ''; }","tryCatchPattern":"try {\n    $res->validate($clientDataHash);\n} catch (WebAuthnException $e) {\n    if (str_contains($e->getMessage(), 'invalid nonce')) {\n        // reject credential: response bound to a different ceremony (replay or wrong hash)\n        return false;\n    }\n    throw $e;\n}","preventionTips":["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."],"tags":["webauthn","php","safetynet","nonce","attestation"],"backgroundTag":"checksum-mismatch","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}