paint-brush
How To Build A Secure Mobile Application With React Nativeby@AmyraSheldon
2,994 reads
2,994 reads

How To Build A Secure Mobile Application With React Native

by Amyra SheldonDecember 28th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

React Native is growing as a preferred technology for mobile app development due to its unmatched ease in application development for multiple platforms. Application security is something that can't be taken lightly as users can be indulged in using sensitive data over the app. Having immaculate provisions for security is critical in each React Native application. In React Native, Cordova, NativeScript, codes don't compile in native bytecode, which greatly exposes the codes to users. There are some strategies that we can use to put a lock on our code box to keep your intellectual property safe.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail

Coins Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - How To Build A Secure Mobile Application With React Native
Amyra Sheldon HackerNoon profile picture

React Native is growing as a preferred technology for mobile app development due to its unmatched ease in application development for multiple platforms. Though, for both businesses and developers, app security is a major concern. Therefore. ‘Building A Secure Mobile App With React Native’ is a frequent search on Google. 

Indeed, application security is something that can't be taken lightly as users can be indulged in using sensitive data over the app such as bank details, corporate secrets and other information. Thus, having immaculate provisions for security is critical in each React Native application. 

Thus, I have decided to provide you with a clear picture of React Native app security. So, stay hooked till the end of this article. 

React Native App 

Before learning about hard-core security aspects, you need to understand the React Native application architecture that every mobile application development company appreciates.  Actually, React Native is a solution for cross-platform application development enabling the creation of native apps using React. Where react, we refer to JavaScript or TypeScript. 

It means, the scripting language is the major programming language in reach native application where native application executes JS code in a separate block with native or custom JS engine. Native JavaScript engines leverage the source code from .jsbundle, and custom JS works as per its own behaviours. 

Image source 

In this architecture, A Bridge of communication develops between JavaScript engine and native components of the application, at the rise of any event (within native part of the application) these bridges work at serialized messages, data batches. These messages and batches later pass asynchronously to the JavaScript engine. The similar process takes place when an event rises from the JS engine. In short, it is a vice versa process. 

Moreover, when you strive to build a secure mobile app with React Native, you need to take an in-depth insight into every aspect of security one by one. Moreover, understanding of iOS and Android native platforms, in terms of JS engine and the connection Bride, is also critical. 

So, let's glean insight into aspects of security in native apps.

Source Code Security 

Many React Native developers come from web development backgrounds as the technology is based on HTML, CSS and JavaScript. But! Many developers don't know that issues like intellectual properties may arise for codes. To avert this issue and vulnerabilities, a little provision we can do.  

When it comes to the inherent security of codes, end-clients barely inherit any security and codes are exposed to them in a readable format. In React Native, Cordova, NativeScript, codes don't compile in native bytecode, which greatly exposes the codes to users. 

Moreover, there is no dearth of decompiling code tools to unravel your secret code. However, there are some strategies that we can use to put a lock on our code box to keep your intellectual property safe. 

Popular strategies for source code security are Minification & Obfuscation, Jscrambler. 

  • Minification and Obfuscation
  • It is a powerful method for code security that makes your application code unreadable to human eyes. Uglify is a popular obfuscation library that can convert multi-line source code to the simplest form and is unreadable to users.

  • Jscrambler
  • Jscrambler service enables advanced JavaScript obfuscation and protection; it transforms your JScode into a form that has protection against reverse-engineering. This service is based on automated static analysis attacks and provides a number of security layers for code protection, including: 

Image source 

Polymorphic JS & HTML5 obfuscation.

Code locks for the domain, browser, time frame and even OS. 

Anti-tampering & debug proofing layer 

Use of Jscrambler in Reach Native Apps provides higher degree obfuscated code. 

  • Hermes 
  • Hermes was unravelled in React Native 0.60.1 by Facebook, as a new JS Engine for mobile apps. Now, you can use it only with Android. In react native 0.60.4, you can use it by setting enableHermes flag in build.gradle. 

Hermes provides precompiling of JS code to bytecode which makes the entry file unreadable. Rather this strategy isn't as reliable as others just because the bytecode can be decompiled via hbcdump, which provides easy to read code. 

Tough Hermes infuses a certain complexity to entry file code, which has nothing to do with code tampering and theft.  

Server Connection Security 

APIs are the major facilitator of connection between back end server and mobile apps. Moreover, you must know that mobile applications don't protect network traffic. For this, the usage of SSL/TLS during authentication is prevalent. This all leads to a critical inconsistency that exposes data to various risks and SessionID to interception. 

In such circumstances, transport security can help though its implementation needs to be observed diligently. To fix issues, you need to detect basic flaws and examine the phone's network traffic. Moreover, inspecting design of the application and network configuration should be done.

Server connection failure and hacks can turn out very costly and pose loss of both time and data. Thus, having server connection provision like SSL certificate and other server certificates are essential. When you opt for mobile application development services, you learn that third party plugin and security certificates can double your app data safety. Altogether, the option for cleartext is also available, but it's not recommended. 

Storage Security

Storage security is most important as malicious people trigger great harm to data stored in the storage. Users have easy access to the mobile's file system and information stored in data storage of the device. This way, data of applications are easily accessible to users. This open data access is highly vulnerable. Organizations need to ensure that a malicious user or malware don't trigger any harm to app data. 

Thus, the use of access_token and refresh_token is very popular. Altogether, there are many other keys we have to persist the device to leverage application. Though these facilities are vulnerable. So, what is the best solution for storage security?

Don't stress your mind as a keychain on iOS devices and SharedPreferences/Keystore for Android are termed as the best solutions. React Native has a Keychain library that stores the key-value pair in the storage, which ensures the security of storage. Here take a glance at how we can use Keychain to secure storage.

const encrypt = (text: string) => {
  const textBytes = aes.utils.utf8.toBytes(text);
  const aesCtr = new aes.ModeOfOperation.ctr(aes256key);
  const encryptedBytes = aesCtr.encrypt(textBytes);
  const encryptedHex = aes.utils.hex.fromBytes(encryptedBytes);
  return encryptedHex;
};
Encrypting SecureStorage item
let aes256key: ?Array<number>;
sha256([brand, deviceId, ...otherIds].join('')).then(
  (hash: string) => {
    aes256key = Array.from(
      Array.from(hash)
        .map((c, i) => (i % 2 ? c : null))
        .filter(Boolean)
        .map(c => c.charCodeAt(0))
    );
  }
);

Creation of a AES256 key

export async function setItem(key: string, value: string) {
  try {
    return Keychain.setGenericPassword(key, encrypt(value), {
      service: `${BUNDLE_ID}.${key}`,
    });
  } catch (error) {
    Logger.warn(error.message);
    return null;
  }
}

To ensure safety, we can infuse a unique key of 256-bit AES with the stored data; this unique key will be generated every time when the application will launch. In this way, if someone accesses the device memory, the data will appear in hashes. It means real data will be hidden. This security provision is known as SQLCipher 

Apart from this, Realm is another popular technique to React Native App security. It is much more swift than SQLite (SQLCipher). It leverages the AES256 algorithm and verifies encrypted Realm via SHA2 HMAC hash. 

Here are some plugins you can use for local storage security of React Native application : 

  1. RN KeyChain: It enables access to Keychain for iOS, Key store for Android application, and conceal. Supports biometric authentication, and storage of username & Pass.
  2. React Native Sensitive Info: Similar to RN KeyChain and based on Keychain and shared preferences for data storage. It allows you to store multiple key-value parts. 
  3. RN Security Storage: Similar to React Native Sensitive Info and provide multiple key-value pair storage via Keystore and Keychain 


JavaScript Vulnerabilities You Shouldn't Miss Out. 

Plenty of JavaScript usage in React Native application development also requires security checks as issues like XSS attack can cause harm to application and user data. XXS attacks are typical when browser-based, but react native risks are low. 

Moreover, protection against XSS attacks is adequate for React Native applications. However, the usage of APIs in JS code and eval() function enable developers to steal data from local storage.

Image source 

The eval-based injection is a commonly used function for stealing data from local storage. This stealing triggers when a malicious string passes for dynamic eval. Altogether, Linters and stack code analyzers can be used to detect such malicious functions. 

What Else Must You Know?

React Native developers work on "Learn Once and Use Anywhere" coding approach. This results in native codes rewritten for React Native. Owing to this, some required features doesn't present in React Native SDK is likely to get implemented in application modules. 

This is a simple instance of dependency. Except these, JS-based development comes up with dozens of dependencies both unearthed and previously encountered. To handle such infrastructure, you need to put rigorous efforts into continual scanning, bug detecting, fixing security loops and upgrading. 

You should also know that numbers or dependencies expose the application to supply chain attacks. Thus, having a secure SDC and automated security test for React Native apps are important. 

Moreover, the time when you get involved in React Native app security testing; you may find that there is no dearth of instance, when you may feel that security can be compromised. Here are the ways you can avert dependencies and indulge in continual project development :

  1. Learn about security problems and issues your application poses, their scope and risk. There are chances the vulnerable code can be not useful within the project, and you can remove or replace. If you can't get a complete picture of risk, it is better to seek an expert's assistance. 
  2. Jot down the issue and share with the team (ensure proper documentation), update or replace dependency on predefined time. 
  3. Figure out the degree of dependency eradication urgency with your security team and work on the update. 
  4.  Ensure to set time for the team to deal with risks. 

Another matter of concern with security that dependencies are interconnected and one change can cause other potential security breaches. Therefore, developers sometimes choose to procrastinate dependencies. 

Tools for automating the updates are also available like Dependabot though manual changes are required to be performed by developers. 

Wrapping UP 

React Native enables you to integrate various security plugins to ensure that every leaky space can be fixed within the application. Besides, React Native includes a variety of features, APIs, and libraries that consolidate data and application security at several points.

(Disclaimer: The author of this story works at ValueCoders which provides React Native app Development Services )