Vulnerability: Improper Authentication in react-adal This affects versions of react-adal < 0.5.1. It is possible for a specially crafted JWT token and request URL can cause the nonce, session and refresh values to be incorrectly validated, causing the application to treat an attacker-generated JWT token as authentic. The logical defect is caused by how the nonce, session and refresh values are stored in the browser local storage or session storage. Each key is automatically appended by ||. When the received nonce and session keys are generated, the list of values is stored in the browser storage, separated by ||, with || always appended to the end of the list. Since || will always be the last 2 characters of the stored values, an empty string ("") will always be in the list of the valid values. Therefore, if an empty session parameter is provided in the callback URL, and a specially-crafted JWT token contains an nonce value of "" (empty string), then adal.js will consider the JWT token as authentic.
Vulnerability ID: GHSA-7mpx-vg3c-cmr4
CWE: CWE-287
Severity score: 8.2
Project: https://github.com/salvoravida/react-adal
Published: 2021-04-13
File: src/adal.js
Commit message: ADAL.js update
Commit author: Kris Hardy

Vulnerable version (before fix):
 var AuthenticationContext = (function () {         this._openedWindows = [];         this._requestType = this.REQUEST_TYPE.LOGIN;         window._adalInstance = this;         this._storageSupport = {             localStorage: null,             sessionStorage: null         };          // validate before constructor assignments         if (config.displayCall && typeof config.displayCall !== 'function') { var AuthenticationContext = (function () {      * Clears cache items.      */     AuthenticationContext.prototype.clearCache = function () {         this._user = null;         this._saveItem(this.CONSTANTS.STORAGE.LOGIN_REQUEST, '');         this._saveItem(this.CONSTANTS.STORAGE.ANGULAR_LOGIN_REQUEST, '');         this._saveItem(this.CONSTANTS.STORAGE.SESSION_STATE, ''); var AuthenticationContext = (function () {      */     AuthenticationContext.prototype.logOut = function () {         this.clearCache();         var urlNavigate;          if (this.config.logOutUri) { var AuthenticationContext = (function () {      * @ignore      */     AuthenticationContext.prototype._addHintParameters = function (urlNavigate) {          //If you don't use prompt=none, then if the session does not exist, there will be a failure.         //If sid is sent alongside domain or login hints, there will be a failure since request is ambiguous.         //If sid is sent with a prompt value other than none or attempt_none, there will be a failure since the request is ambiguous.  var AuthenticationContext = (function () {         if (requestNonce) {             requestNonce = requestNonce.split(this.CONSTANTS.CACHE_DELIMETER);             for (var i = 0; i < requestNonce.length; i++) {                 if (requestNonce[i] === user.profile.nonce) {                     return true;                 }             } var AuthenticationContext = (function () {         if (loginStates) {             loginStates = loginStates.split(this.CONSTANTS.CACHE_DELIMETER);             for (var i = 0; i < loginStates.length; i++) {                 if (loginStates[i] === requestInfo.stateResponse) {                     requestInfo.requestType = this.REQUEST_TYPE.LOGIN;                     requestInfo.stateMatch = true;                     return true; var AuthenticationContext = (function () {         if (acquireTokenStates) {             acquireTokenStates = acquireTokenStates.split(this.CONSTANTS.CACHE_DELIMETER);             for (var i = 0; i < acquireTokenStates.length; i++) {                 if (acquireTokenStates[i] === requestInfo.stateResponse) {                     requestInfo.requestType = this.REQUEST_TYPE.RENEW_TOKEN;                     requestInfo.stateMatch = true;                     return true; var AuthenticationContext = (function () {                             this._user = null;                         } else {                             this._saveItem(this.CONSTANTS.STORAGE.IDTOKEN, requestInfo.parameters[this.CONSTANTS.ID_TOKEN]);                             // Save idtoken as access token for app itself                             var idTokenResource = this.config.loginResource ? this.config.loginResource : this.config.clientId;                              if (!this._hasResource(idTokenResource)) {                                 keys = this._getItem(this.CONSTANTS.STORAGE.TOKEN_KEYS) || '';                                 this._saveItem(this.CONSTANTS.STORAGE.TOKEN_KEYS, keys + idTokenResource + this.CONSTANTS.RESOURCE_DELIMETER);                             }                              this._saveItem(this.CONSTANTS.STORAGE.ACCESS_TOKEN_KEY + idTokenResource, requestInfo.parameters[this.CONSTANTS.ID_TOKEN]);                             this._saveItem(this.CONSTANTS.STORAGE.EXPIRATION_KEY + idTokenResource, this._user.profile.exp);                         }                     }                     else { var AuthenticationContext = (function () {                 ifr.setAttribute('aria-hidden', 'true');                 ifr.style.visibility = 'hidden';                 ifr.style.position = 'absolute';                 ifr.style.width = ifr.style.height = ifr.style.borderWidth = '0px';                  adalFrame = document.getElementsByTagName('body')[0].appendChild(ifr);             } var AuthenticationContext = (function () {     };      /**      * Returns true if the browser supports given storage type      * @ignore      */     AuthenticationContext.prototype._supportsStorage = function(storageType) {         if (!(storageType in this._storageSupport)) {             return false;         }          if (this._storageSupport[storageType] !== null) {             return this._storageSupport[storageType];         }          try {             if (!(storageType in window) || window[storageType] === null) {                 throw new Error();             }             var testKey = '__storageTest__';             window[storageType].setItem(testKey, 'A');             if (window[storageType].getItem(testKey) !== 'A') {                 throw new Error();             }             window[storageType].removeItem(testKey);             if (window[storageType].getItem(testKey)) {                 throw new Error();             }             this._storageSupport[storageType] = true;         } catch (e) {             this._storageSupport[storageType] = false;         }         return this._storageSupport[storageType];     }      /**      * Returns true if browser supports localStorage, false otherwise.      * @ignore      */     AuthenticationContext.prototype._supportsLocalStorage = function () {                 return this._supportsStorage('localStorage');     };      /**      * Returns true if browser supports sessionStorage, false otherwise.      * @ignore      */     AuthenticationContext.prototype._supportsSessionStorage = function () {         return this._supportsStorage('sessionStorage');     };      /** var AuthenticationContext = (function () {      return AuthenticationContext;  }());\ No newline at end of file

Fixed version (after fix):
 var AuthenticationContext = (function () {         this._openedWindows = [];         this._requestType = this.REQUEST_TYPE.LOGIN;         window._adalInstance = this;          // validate before constructor assignments         if (config.displayCall && typeof config.displayCall !== 'function') { var AuthenticationContext = (function () {      * Clears cache items.      */     AuthenticationContext.prototype.clearCache = function () {         this._saveItem(this.CONSTANTS.STORAGE.LOGIN_REQUEST, '');         this._saveItem(this.CONSTANTS.STORAGE.ANGULAR_LOGIN_REQUEST, '');         this._saveItem(this.CONSTANTS.STORAGE.SESSION_STATE, ''); var AuthenticationContext = (function () {      */     AuthenticationContext.prototype.logOut = function () {         this.clearCache();         this._user = null;         var urlNavigate;          if (this.config.logOutUri) { var AuthenticationContext = (function () {      * @ignore      */     AuthenticationContext.prototype._addHintParameters = function (urlNavigate) {         //If you don�t use prompt=none, then if the session does not exist, there will be a failure.         //If sid is sent alongside domain or login hints, there will be a failure since request is ambiguous.         //If sid is sent with a prompt value other than none or attempt_none, there will be a failure since the request is ambiguous.  var AuthenticationContext = (function () {         if (requestNonce) {             requestNonce = requestNonce.split(this.CONSTANTS.CACHE_DELIMETER);             for (var i = 0; i < requestNonce.length; i++) {                 if (requestNonce[i] && requestNonce[i] === user.profile.nonce) {                     return true;                 }             } var AuthenticationContext = (function () {         if (loginStates) {             loginStates = loginStates.split(this.CONSTANTS.CACHE_DELIMETER);             for (var i = 0; i < loginStates.length; i++) {                 if (loginStates[i] && loginStates[i] === requestInfo.stateResponse) {                     requestInfo.requestType = this.REQUEST_TYPE.LOGIN;                     requestInfo.stateMatch = true;                     return true; var AuthenticationContext = (function () {         if (acquireTokenStates) {             acquireTokenStates = acquireTokenStates.split(this.CONSTANTS.CACHE_DELIMETER);             for (var i = 0; i < acquireTokenStates.length; i++) {                 if (acquireTokenStates[i] && acquireTokenStates[i] === requestInfo.stateResponse) {                     requestInfo.requestType = this.REQUEST_TYPE.RENEW_TOKEN;                     requestInfo.stateMatch = true;                     return true; var AuthenticationContext = (function () {                             this._user = null;                         } else {                             this._saveItem(this.CONSTANTS.STORAGE.IDTOKEN, requestInfo.parameters[this.CONSTANTS.ID_TOKEN]);                              // Save idtoken as access token for app itself                             resource = this.config.loginResource ? this.config.loginResource : this.config.clientId;                              if (!this._hasResource(resource)) {                                 keys = this._getItem(this.CONSTANTS.STORAGE.TOKEN_KEYS) || '';                                 this._saveItem(this.CONSTANTS.STORAGE.TOKEN_KEYS, keys + resource + this.CONSTANTS.RESOURCE_DELIMETER);                             }                              this._saveItem(this.CONSTANTS.STORAGE.ACCESS_TOKEN_KEY + resource, requestInfo.parameters[this.CONSTANTS.ID_TOKEN]);                             this._saveItem(this.CONSTANTS.STORAGE.EXPIRATION_KEY + resource, this._user.profile.exp);                         }                     }                     else { var AuthenticationContext = (function () {                 ifr.setAttribute('aria-hidden', 'true');                 ifr.style.visibility = 'hidden';                 ifr.style.position = 'absolute';                 ifr.style.width = ifr.style.height = ifr.borderWidth = '0px';                  adalFrame = document.getElementsByTagName('body')[0].appendChild(ifr);             } var AuthenticationContext = (function () {     };      /**      * Returns true if browser supports localStorage, false otherwise.      * @ignore      */     AuthenticationContext.prototype._supportsLocalStorage = function () {         try {             if (!window.localStorage) return false; // Test availability             window.localStorage.setItem('storageTest', 'A'); // Try write             if (window.localStorage.getItem('storageTest') != 'A') return false; // Test read/write             window.localStorage.removeItem('storageTest'); // Try delete             if (window.localStorage.getItem('storageTest')) return false; // Test delete             return true; // Success         } catch (e) {             return false;         }     };      /**      * Returns true if browser supports sessionStorage, false otherwise.      * @ignore      */     AuthenticationContext.prototype._supportsSessionStorage = function () {         try {             if (!window.sessionStorage) return false; // Test availability             window.sessionStorage.setItem('storageTest', 'A'); // Try write             if (window.sessionStorage.getItem('storageTest') != 'A') return false; // Test read/write             window.sessionStorage.removeItem('storageTest'); // Try delete             if (window.sessionStorage.getItem('storageTest')) return false; // Test delete             return true; // Success         } catch (e) {             return false;         }     };      /** var AuthenticationContext = (function () {      return AuthenticationContext; \ No newline at end of file }());

Patch:
patch: @@ -135,10 +135,6 @@ var AuthenticationContext = (function () {
         this._openedWindows = [];
         this._requestType = this.REQUEST_TYPE.LOGIN;
         window._adalInstance = this;
-        this._storageSupport = {
-            localStorage: null,
-            sessionStorage: null
-        };
 
         // validate before constructor assignments
         if (config.displayCall && typeof config.displayCall !== 'function') {
@@ -813,7 +809,6 @@ var AuthenticationContext = (function () {
      * Clears cache items.
      */
     AuthenticationContext.prototype.clearCache = function () {
-        this._user = null;
         this._saveItem(this.CONSTANTS.STORAGE.LOGIN_REQUEST, '');
         this._saveItem(this.CONSTANTS.STORAGE.ANGULAR_LOGIN_REQUEST, '');
         this._saveItem(this.CONSTANTS.STORAGE.SESSION_STATE, '');
@@ -860,6 +855,7 @@ var AuthenticationContext = (function () {
      */
     AuthenticationContext.prototype.logOut = function () {
         this.clearCache();
+        this._user = null;
         var urlNavigate;
 
         if (this.config.logOutUri) {
@@ -928,8 +924,7 @@ var AuthenticationContext = (function () {
      * @ignore
      */
     AuthenticationContext.prototype._addHintParameters = function (urlNavigate) {
-
-        //If you don't use prompt=none, then if the session does not exist, there will be a failure.
+        //If you don�t use prompt=none, then if the session does not exist, there will be a failure.
         //If sid is sent alongside domain or login hints, there will be a failure since request is ambiguous.
         //If sid is sent with a prompt value other than none or attempt_none, there will be a failure since the request is ambiguous.
 
@@ -1103,7 +1098,7 @@ var AuthenticationContext = (function () {
         if (requestNonce) {
             requestNonce = requestNonce.split(this.CONSTANTS.CACHE_DELIMETER);
             for (var i = 0; i < requestNonce.length; i++) {
-                if (requestNonce[i] === user.profile.nonce) {
+                if (requestNonce[i] && requestNonce[i] === user.profile.nonce) {
                     return true;
                 }
             }
@@ -1122,7 +1117,7 @@ var AuthenticationContext = (function () {
         if (loginStates) {
             loginStates = loginStates.split(this.CONSTANTS.CACHE_DELIMETER);
             for (var i = 0; i < loginStates.length; i++) {
-                if (loginStates[i] === requestInfo.stateResponse) {
+                if (loginStates[i] && loginStates[i] === requestInfo.stateResponse) {
                     requestInfo.requestType = this.REQUEST_TYPE.LOGIN;
                     requestInfo.stateMatch = true;
                     return true;
@@ -1135,7 +1130,7 @@ var AuthenticationContext = (function () {
         if (acquireTokenStates) {
             acquireTokenStates = acquireTokenStates.split(this.CONSTANTS.CACHE_DELIMETER);
             for (var i = 0; i < acquireTokenStates.length; i++) {
-                if (acquireTokenStates[i] === requestInfo.stateResponse) {
+                if (acquireTokenStates[i] && acquireTokenStates[i] === requestInfo.stateResponse) {
                     requestInfo.requestType = this.REQUEST_TYPE.RENEW_TOKEN;
                     requestInfo.stateMatch = true;
                     return true;
@@ -1218,16 +1213,17 @@ var AuthenticationContext = (function () {
                             this._user = null;
                         } else {
                             this._saveItem(this.CONSTANTS.STORAGE.IDTOKEN, requestInfo.parameters[this.CONSTANTS.ID_TOKEN]);
+
                             // Save idtoken as access token for app itself
-                            var idTokenResource = this.config.loginResource ? this.config.loginResource : this.config.clientId;
+                            resource = this.config.loginResource ? this.config.loginResource : this.config.clientId;
 
-                            if (!this._hasResource(idTokenResource)) {
+                            if (!this._hasResource(resource)) {
                                 keys = this._getItem(this.CONSTANTS.STORAGE.TOKEN_KEYS) || '';
-                                this._saveItem(this.CONSTANTS.STORAGE.TOKEN_KEYS, keys + idTokenResource + this.CONSTANTS.RESOURCE_DELIMETER);
+                                this._saveItem(this.CONSTANTS.STORAGE.TOKEN_KEYS, keys + resource + this.CONSTANTS.RESOURCE_DELIMETER);
                             }
 
-                            this._saveItem(this.CONSTANTS.STORAGE.ACCESS_TOKEN_KEY + idTokenResource, requestInfo.parameters[this.CONSTANTS.ID_TOKEN]);
-                            this._saveItem(this.CONSTANTS.STORAGE.EXPIRATION_KEY + idTokenResource, this._user.profile.exp);
+                            this._saveItem(this.CONSTANTS.STORAGE.ACCESS_TOKEN_KEY + resource, requestInfo.parameters[this.CONSTANTS.ID_TOKEN]);
+                            this._saveItem(this.CONSTANTS.STORAGE.EXPIRATION_KEY + resource, this._user.profile.exp);
                         }
                     }
                     else {
@@ -1689,7 +1685,7 @@ var AuthenticationContext = (function () {
                 ifr.setAttribute('aria-hidden', 'true');
                 ifr.style.visibility = 'hidden';
                 ifr.style.position = 'absolute';
-                ifr.style.width = ifr.style.height = ifr.style.borderWidth = '0px';
+                ifr.style.width = ifr.style.height = ifr.borderWidth = '0px';
 
                 adalFrame = document.getElementsByTagName('body')[0].appendChild(ifr);
             }
@@ -1764,52 +1760,37 @@ var AuthenticationContext = (function () {
     };
 
     /**
-     * Returns true if the browser supports given storage type
+     * Returns true if browser supports localStorage, false otherwise.
      * @ignore
      */
-    AuthenticationContext.prototype._supportsStorage = function(storageType) {
-        if (!(storageType in this._storageSupport)) {
-            return false;
-        }
-
-        if (this._storageSupport[storageType] !== null) {
-            return this._storageSupport[storageType];
-        }
-
+    AuthenticationContext.prototype._supportsLocalStorage = function () {
         try {
-            if (!(storageType in window) || window[storageType] === null) {
-                throw new Error();
-            }
-            var testKey = '__storageTest__';
-            window[storageType].setItem(testKey, 'A');
-            if (window[storageType].getItem(testKey) !== 'A') {
-                throw new Error();
-            }
-            window[storageType].removeItem(testKey);
-            if (window[storageType].getItem(testKey)) {
-                throw new Error();
-            }
-            this._storageSupport[storageType] = true;
+            if (!window.localStorage) return false; // Test availability
+            window.localStorage.setItem('storageTest', 'A'); // Try write
+            if (window.localStorage.getItem('storageTest') != 'A') return false; // Test read/write
+            window.localStorage.removeItem('storageTest'); // Try delete
+            if (window.localStorage.getItem('storageTest')) return false; // Test delete
+            return true; // Success
         } catch (e) {
-            this._storageSupport[storageType] = false;
+            return false;
         }
-        return this._storageSupport[storageType];
-    }
-
-    /**
-     * Returns true if browser supports localStorage, false otherwise.
-     * @ignore
-     */
-    AuthenticationContext.prototype._supportsLocalStorage = function () {        
-        return this._supportsStorage('localStorage');
     };
 
     /**
      * Returns true if browser supports sessionStorage, false otherwise.
      * @ignore
      */
     AuthenticationContext.prototype._supportsSessionStorage = function () {
-        return this._supportsStorage('sessionStorage');
+        try {
+            if (!window.sessionStorage) return false; // Test availability
+            window.sessionStorage.setItem('storageTest', 'A'); // Try write
+            if (window.sessionStorage.getItem('storageTest') != 'A') return false; // Test read/write
+            window.sessionStorage.removeItem('storageTest'); // Try delete
+            if (window.sessionStorage.getItem('storageTest')) return false; // Test delete
+            return true; // Success
+        } catch (e) {
+            return false;
+        }
     };
 
     /**
@@ -1955,4 +1936,4 @@ var AuthenticationContext = (function () {
 
     return AuthenticationContext;
 
-}());
\ No newline at end of file
+}());
path: src/adal.js