<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sriram</title>
    <description>The latest articles on DEV Community by Sriram (@sriram2520).</description>
    <link>https://dev.to/sriram2520</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F468059%2F795f2db6-1c7c-4313-add1-9470181a6961.jpeg</url>
      <title>DEV Community: Sriram</title>
      <link>https://dev.to/sriram2520</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sriram2520"/>
    <language>en</language>
    <item>
      <title>Debugging PubNub's encryption hole</title>
      <dc:creator>Sriram</dc:creator>
      <pubDate>Mon, 13 Sep 2021 08:34:43 +0000</pubDate>
      <link>https://dev.to/sriram2520/pubnub-s-encryption-hole-4ao1</link>
      <guid>https://dev.to/sriram2520/pubnub-s-encryption-hole-4ao1</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
I work as a Lead Product Engineer with &lt;a href="https://www.minicorphq.com/"&gt;MiniCorp&lt;/a&gt;, where we work towards bringing our customer's ideas to life. In the recent past we have seen a surge in the number of customers requiring in-app chat within their product.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In-App Chat&lt;/strong&gt;&lt;br&gt;
This is the age of SaaS / API, where there are ton of options for each niche. These products provide a complete package which would take months for us to build from scratch. In our search for In-app chat, 2 products stood out the most. &lt;a href="https://www.talkjs.com/"&gt;TalkJS&lt;/a&gt; &amp;amp; &lt;a href="https://www.pubnub.com/"&gt;PubNub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We use both these products depending on the client's requirements. While TalkJS doesn't provide a free version, PubNub offers a limited free version. While in the long run TalkJS's price model is easy to maintain, for clients who prefer to launch their MVP, PubNub's free version makes more sense.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PubNub's Encryption Claim&lt;/strong&gt;&lt;br&gt;
One of our recent clients had the requirement that all chat messages &amp;amp; attachments should be end-to-end encrypted and no unencrypted data should be stored on the server. We went ahead and started building the product with PubNub as they &lt;a href="https://www.pubnub.com/docs/chat/features/encryption"&gt;claim&lt;/a&gt; to have built-in encryption support&lt;/p&gt;

&lt;p&gt;Here's the screenshot of PubNub docs showing file encryption via the SDK&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WKzOCJ4B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sriram-dev-images.s3.ap-south-1.amazonaws.com/pubnub_ss.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WKzOCJ4B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sriram-dev-images.s3.ap-south-1.amazonaws.com/pubnub_ss.png" alt="Screenshot from PubNub docs at the time of writing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation&lt;/strong&gt;&lt;br&gt;
We implemented the chat on our Ruby on Rails app with Javascript SDK. Everything worked as expected and we kick started our next phase of development which was building react native app.&lt;/p&gt;

&lt;p&gt;PubNub provides a neat &lt;a href="https://www.pubnub.com/docs/sdks/javascript/react"&gt;react SDK&lt;/a&gt; which is a wrapper on their Javascript SDK. As usual we were testing our implementation with plain text messages and everything worked as expected. The entire chat functionality was built &amp;amp; everyone was happy until we noticed a key detail.&lt;/p&gt;

&lt;p&gt;The files uploaded were stored on PubNub's servers and we were assuming that all files were encrypted. To our rude shock, when we used the &lt;code&gt;getFileUrl&lt;/code&gt; method and opened the link on a tab, we saw the unencrypted version of the file (hosted on PubNub's S3 account)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analysis&lt;/strong&gt;&lt;br&gt;
We were so sure that we did some mistake, because files uploaded through the web version were getting encrypted properly. We were unsuccessful in our attempts and contacted the PubNub support. The support assured us that other clients were infact using the SDK and encryption was working for them.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hoVVCIIE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sriram-dev-images.s3.ap-south-1.amazonaws.com/pubnub_ss_0.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hoVVCIIE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sriram-dev-images.s3.ap-south-1.amazonaws.com/pubnub_ss_0.JPG" alt="enter image description here"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since we couldn't find any issue with our code, I decided to dig into PubNub's SDK source code. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Digging into sendFile method&lt;/strong&gt;&lt;br&gt;
We started with the public method that was mentioned in the document.&lt;br&gt;
&lt;a href="https://github.com/pubnub/javascript/blob/master/src/core/endpoints/file_upload/send_file.js#L64"&gt;https://github.com/pubnub/javascript/blob/master/src/core/endpoints/file_upload/send_file.js#L64&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (PubNubFile.supportsEncryptFile &amp;amp;&amp;amp; (cipherKey ?? config.cipherKey)) {
  file = await cryptography.encryptFile(cipherKey ?? config.cipherKey, file, PubNubFile);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The 1st clue was looking at the &lt;code&gt;PubNubFile.supportsEncryptFile&lt;/code&gt; boolean. It was a surprise seeing such a variable. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. &lt;code&gt;supportsEncryptFile&lt;/code&gt; boolean&lt;/strong&gt;&lt;br&gt;
I did a &lt;a href="https://github.com/pubnub/javascript/search?q=supportsEncryptFile"&gt;quick search&lt;/a&gt; of this variable name within the SDK repository. This threw an interesting finding that the variable was &lt;em&gt;true&lt;/em&gt; for &lt;a href="https://github.com/pubnub/javascript/blob/master/src/file/modules/web.js#L25"&gt;web&lt;/a&gt; &amp;amp; &lt;a href="https://github.com/pubnub/javascript/blob/master/src/file/modules/node.js#L26"&gt;node&lt;/a&gt;, but &lt;em&gt;false&lt;/em&gt; for &lt;a href="https://github.com/pubnub/javascript/blob/787b95e8cff2a2fff4e32e7f512f0cd7aa7d7b4c/src/file/modules/react-native.js#L30"&gt;react-native&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static  supportsEncryptFile  =  false;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;3. Contacting support&lt;/strong&gt;&lt;br&gt;
 With this interesting finding, I reached out to PubNub support. And the response was that I needed to encrypt the file with the JS utility method before using the sendFile method. &lt;br&gt;
 &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RYtbWsXn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sriram-dev-images.s3.ap-south-1.amazonaws.com/pubnub_ss_1.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RYtbWsXn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sriram-dev-images.s3.ap-south-1.amazonaws.com/pubnub_ss_1.JPG" alt="PubNub support 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wSxLu2GJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sriram-dev-images.s3.ap-south-1.amazonaws.com/pubnub_ss_2.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wSxLu2GJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sriram-dev-images.s3.ap-south-1.amazonaws.com/pubnub_ss_2.JPG" alt="PubNub support 2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This was unexpected &amp;amp; nowhere mentioned in their docs. As we saw in the above snippet, &lt;code&gt;sendFile&lt;/code&gt; method internally calls the &lt;code&gt;encryptFile&lt;/code&gt; method if &lt;code&gt;cipherText&lt;/code&gt; is provided. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Implementing encryptFile&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inspite of the above, I decided to give it a try. When I called the utility method directly, I ended up getting the following error&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;TypeError: undefined is not an object (evaluating 'cryptography.encryptFile')&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;5. Understanding the &lt;code&gt;encryptFile&lt;/code&gt; method&lt;/strong&gt;&lt;br&gt;
While I assumed that manually encrypting the file would work, this was another surprise to see that the method could not be found. So I decided to dig deeper into the &lt;code&gt;encryptFile&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/pubnub/javascript/blob/master/src/core/pubnub-common.js#L379"&gt;https://github.com/pubnub/javascript/blob/master/src/core/pubnub-common.js#L379&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.encryptFile  =  (key,  file)  =&amp;gt;  cryptography.encryptFile(key,  file,  this.File);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://github.com/pubnub/javascript/blob/master/src/core/pubnub-common.js#L358"&gt;https://github.com/pubnub/javascript/blob/master/src/core/pubnub-common.js#L358&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const  cryptography  =  setup.cryptography;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here I noticed that the encryptFile method is a nice interface wrapper on the cryptography module. The cryptography module itself is an attribute to the setup, which is the constructor arugument.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cryptography&lt;/code&gt; variable is initialized on each module constructor depending on the platform&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Web&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://github.com/pubnub/javascript/blob/master/src/web/index.js#L101"&gt;https://github.com/pubnub/javascript/blob/master/src/web/index.js#L101&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setup.cryptography  =  new  WebCryptography();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Node&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://github.com/pubnub/javascript/blob/master/src/node/index.js#L23"&gt;https://github.com/pubnub/javascript/blob/master/src/node/index.js#L23&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setup.cryptography  =  new  NodeCryptography();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And, this was not initialized on the react-native module&lt;br&gt;
&lt;a href="https://github.com/pubnub/javascript/blob/88027bb862608796a6648f175e88c6b7bb6030f3/src/react_native/index.js"&gt;https://github.com/pubnub/javascript/blob/88027bb862608796a6648f175e88c6b7bb6030f3/src/react_native/index.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
As I mentioned before, working with 3rd party tools really fast tracks the development time &amp;amp; enables us to go-to-market sooner than if we built everything. But the trade-off is not having control when something doesn't work as expected. &lt;/p&gt;

&lt;p&gt;Luckily PubNub has an open source repository which allowed us to dive deeper into this issue. If we had known the lack of encryption support for react native SDK, we could have taken a different call. &lt;/p&gt;

&lt;p&gt;We have written to PubNub's support with all our above findings and hoping for a fix soon. I will update the post as &amp;amp; when I hear more updates from PubNub team.&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>security</category>
    </item>
  </channel>
</rss>
