Skip to content

Commit 133504f

Browse files
jo-carterag-TJNIIroute443
authored
Fixed ID token nonce claim validation (#104)
The validateIdToken function previously did not correctly validate the nonce claim in the ID Token due to improper handling of session state. The newSession variable, intended to indicate a new authentication session, was not reliably set, causing nonce validation to be skipped in all cases. --------- Co-authored-by: Tom Noonan II <t.noonanii@f5.com> Co-authored-by: Ivan Ovchinnikov <33402471+route443@users.noreply.github.com>
1 parent afa8f4c commit 133504f

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

openid_connect.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
*
44
* Copyright (C) 2020 Nginx, Inc.
55
*/
6-
var newSession = false; // Used by oidcAuth() and validateIdToken()
7-
86
export default {auth, codeExchange, validateIdToken, logout};
97

108
function retryOriginalRequest(r) {
@@ -32,8 +30,6 @@ function auth(r, afterSyncCheck) {
3230
}
3331

3432
if (!r.variables.refresh_token || r.variables.refresh_token == "-") {
35-
newSession = true;
36-
3733
// Check we have all necessary configuration variables (referenced only by njs)
3834
var oidcConfigurables = ["authz_endpoint", "scopes", "hmac_key", "cookie_flags"];
3935
var missingConfig = [];
@@ -241,10 +237,9 @@ function validateIdToken(r) {
241237
validToken = false;
242238
}
243239

244-
// If we receive a nonce in the ID Token then we will use the auth_nonce cookies
245-
// to check that the JWT can be validated as being directly related to the
246-
// original request by this client. This mitigates against token replay attacks.
247-
if (newSession) {
240+
// According to OIDC Core 1.0 Section 2:
241+
// "If present in the ID Token, Clients MUST verify that the nonce Claim Value is equal to the value of the nonce parameter sent in the Authentication Request."
242+
if (r.variables.jwt_claim_nonce) {
248243
var client_nonce_hash = "";
249244
if (r.variables.cookie_auth_nonce) {
250245
var c = require('crypto');
@@ -255,6 +250,9 @@ function validateIdToken(r) {
255250
r.error("OIDC ID Token validation error: nonce from token (" + r.variables.jwt_claim_nonce + ") does not match client (" + client_nonce_hash + ")");
256251
validToken = false;
257252
}
253+
} else if (!r.variables.refresh_token || r.variables.refresh_token == "-") {
254+
r.error("OIDC ID Token validation error: missing nonce claim in ID Token during initial authentication.");
255+
validToken = false;
258256
}
259257

260258
if (validToken) {

0 commit comments

Comments
 (0)