pubnub / javascript

PubNub JavaScript SDK docs https://www.pubnub.com/docs/sdks/javascript
Other
553 stars 401 forks source link

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

Closed simform-manan-mankodi closed 10 months ago

simform-manan-mankodi commented 10 months ago

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

mohitpubnub commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

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. :+1:

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:

Thanks again!