amazon-archives / amazon-cognito-identity-js

Amazon Cognito Identity SDK for JavaScript
Other
986 stars 452 forks source link

Race condition in AuthenticationHelper with React Native #585

Closed maxdavidson closed 6 years ago

maxdavidson commented 6 years ago

In c359661350c98fd0d25ec8f3cd5fd494c5a167d7, some functions were made asynchronous with the introduction of native bindings in React Native. generateHashDevice in AuthenticationHelper.js was given a callback parameter because of its use of modPow, but the callback is not used in CognitoUser.js

https://github.com/aws/amazon-cognito-identity-js/blob/c359661350c98fd0d25ec8f3cd5fd494c5a167d7/src/AuthenticationHelper.js#L137-L156

https://github.com/aws/amazon-cognito-identity-js/blob/c359661350c98fd0d25ec8f3cd5fd494c5a167d7/src/AuthenticationHelper.js#L126-L128

https://github.com/aws/amazon-cognito-identity-js/blob/c359661350c98fd0d25ec8f3cd5fd494c5a167d7/src/CognitoUser.js#L363-L374

This happens to work in the browser, since the callback is called synchronously. However, this will create an error in RN, because authenticationHelper.verifierDevices is accessed before it's assigned, and util.Buffer will be instantiated with undefined.

itrestian commented 6 years ago

React Native support was just released so we would have to look into it. You mentioned this doesn't happen in the browser, correct? Also if you disable device tracking in the console will it work?

maxdavidson commented 6 years ago

Turns out it doesn't work in the browser, because of the missing third callback parameter to authenticationHelper.generateHashDevice. It's the same problem causing #586 and #588.

itrestian commented 6 years ago

K, so in the browser we can add a check for the existence of callback.

maxdavidson commented 6 years ago

Or just add the callback so it works in as intended in the both the browser and RN

itrestian commented 6 years ago

Pull request with fix has been merged.

dahudson88 commented 6 years ago

Beautiful!