Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decrypt method of legacyCryptoModule throws an error for unencrypted string values with "_" #353

Closed
simform-manan-mankodi opened this issue Jan 2, 2024 · 3 comments

Comments

@simform-manan-mankodi
Copy link

Hello,

We've recently updated our Pubnub SDK from version 4.33.1 to the latest 7.4.5 as the first step to use 256-bit encryption for better security, though the 256-bit encryption is not yet enabled. So far we have followed the steps mentioned in the guide here.

Due to a certain behavior change in the decrypt method, our web application is broken. We have a listener on the React web app to receive messages - which can be encrypted or unencrypted messages. For unencrypted messages/values, earlier the pubnubInstance.decrypt() method used to return null, but the new cryptoModule.decrypt() method throws an error whenever a value with character _ is passed to it. Returns null as expected for a string value without _.

pubnub_error

Would it be possible to fix this and achieve the old behavior of returning null as expected? Is there any other way to handle both encrypted & unencrypted messages on the web app listener without trying to decrypt the unencrypted messages, thus avoiding this issue?

Thanks.

Working snippet with v4.33.1:

pubnub_4_33_1_working

Snippet having the issue with v7.4.5:

punub_latest_error

@simform-manan-mankodi simform-manan-mankodi changed the title decrypt method of legacyCryptoModule throws an error for unencrypted messages decrypt method of legacyCryptoModule throws an error for unencrypted string values with "_" Jan 2, 2024
@mohitpubnub
Copy link
Contributor

Initialising PubNub instance along with cryptoModule(legacyCryptoModule or aesCbcCryptoModule) won't break the listener in case of mixture of encrypted/unencrypted messages. pubnub initialisation with cryptoModule example snippet is here

In the description code snippet, Message is being decrypted manually. In case of possible unencrypted message, we need to catch the error and return null. If we want to return null instead original unencrypted message

Reason: cryptoModules are able to decrypted both 128 and 256 bit encrypted messages for backward compatibility. So now decrypt method throws Error with error message about failure reason instead returning null. So that user of native decrypt method can handle various scenarios based on failure reason.

@mohitpubnub
Copy link
Contributor

Also,

Would it be possible to fix this and achieve the old behavior of returning null as expected?

one possible solution is:
If PubNub instance is initialised with cryptoModule and if subscription received unencrypted message then
listener with receive message as it's original form and message.error will contain error message.
you can implement a quick logic to return null from there if there is any failure.

or try catch-error with manual decryption if that is compatible with other app logic!

@simform-manan-mankodi
Copy link
Author

Initialising PubNub instance along with cryptoModule(legacyCryptoModule or aesCbcCryptoModule) won't break the listener in case of mixture of encrypted/unencrypted messages. pubnub initialisation with cryptoModule example snippet is here

In the description code snippet, Message is being decrypted manually. In case of possible unencrypted message, we need to catch the error and return null. If we want to return null instead original unencrypted message

Reason: cryptoModules are able to decrypted both 128 and 256 bit encrypted messages for backward compatibility. So now decrypt method throws Error with error message about failure reason instead returning null. So that user of native decrypt method can handle various scenarios based on failure reason.

Thank you so much for the quick response. We need to use manual decryption for now so will catch & handle the error accordingly. 👍

Also would like to request a couple of updates in the guide here in the section If you are not specifying the cipher key as part of your PubNub configuration but encrypting messages as needed:

  • About mentioning the error handling change for the manual decryption method.
  • About updating the statement adding the CryptoModule to the SDKs was a non-breaking change in the best feasible manner, as this small issue might be a breaking change for some users of JS SDK.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants