DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Ultimate Guide to React Dashboards Part 1: Overview and Analytics
  • The Technology Stack Needed To Build a Web3 Application
  • How to Build a Full-Stack App With Next.js, Prisma, Postgres, and Fastify
  • Apache Aries: Helping Enterprise Developers Build OSGi Apps

Trending

  • Agentic AI for Automated Application Security and Vulnerability Management
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Ethical AI in Agile
  • AI's Dilemma: When to Retrain and When to Unlearn?
  1. DZone
  2. Coding
  3. JavaScript
  4. Protecting Your React.js Source Code With Jscrambler

Protecting Your React.js Source Code With Jscrambler

Source code protection is recommended by the OWASP and NIST as a way to prevent reverse-engineering and tampering attacks. Here's how to protect the source code of React.js apps.

By 
Pedro Fortuna user avatar
Pedro Fortuna
·
Nov. 10, 20 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
15.6K Views

Join the DZone community and get the full member experience.

Join For Free

React.js is one of the most popular JavaScript libraries. The 2019 "State of JavaScript" survey puts React as the front-end framework of choice, with 72% of responders stating that they have used it and would use again.

With its elegant programming style, rich package ecosystem and good documentation, React has found its way into powering the applications of large enterprises. Specifically, the developer survey found that 18% of responders who are using React work for companies with over 1000 employees.

As we know, the very nature of JavaScript means it can't be encrypted and can easily be accessed on the client-side or even tampered with.

Because React powers enterprise-grade applications, it requires an enterprise-grade security solution such as Jscrambler.

This tutorial will explain how to integrate Jscrambler seamlessly into React's build process in just a few minutes. You'll learn how to protect your React source code with the most advanced polymorphic obfuscation techniques, along with code locks and self-defensive capabilities.

Pre-Requisites

Only two things are needed to properly integrate Jscrambler into the React build process: creating a React app and configuring Jscrambler. We will highlight both below.

How to Create a React Application

For the purposes of this tutorial, we will be using a create-react-app boilerplate app. To get started, we will need to install it using npm:

Shell
xxxxxxxxxx
1
 
1
npm i -g create-react-app


This will download create-react-app and install it globally with all the required dependencies for the latest React version.

Now, we're ready to create our boilerplate app, which we'll use as the basis for this tutorial. Start by creating this new app with the following command:

Shell
xxxxxxxxxx
1
 
1
create-react-app react-jscrambler-boilerplate


After the installation finishes, we can run our newly created boilerplate app:

Shell
xxxxxxxxxx
1
 
1
cd react-jscrambler-boilerplate
2
npm start


Our new React app will run on development mode and appear at localhost:3000. Check if everything is in place before moving to the next step. For further help on getting started with create-react-app, see the official documentation.

The base project structure of our React application is as follows:

Plain Text
xxxxxxxxxx
1
12
 
1
react-jscrambler-boilerplate/
2
|-- package-lock.json
3
|-- package.json
4
|-- yarn.lock
5
|-- build/
6
| |-- static/
7
| | |-- css/
8
| | |-- js/
9
| | |-- media/
10
|-- node_modules/
11
|-- public/
12
|-- src/
  • package.json contains all the configurations which are related to npm such as dependencies, version and scripts.

  • The src directory features all the source code of the application. The sources are then built and packed into the build directory. This is where our protected HTML and JavaScript files will be placed after the build.

How to Configure Jscrambler

If you haven't created a Jscrambler account yet, be sure to do so before moving forward.

All of Jscrambler's configuration will reside inside a single file: .jscramblerrc. As such, we will need to create this file to specify which transformations we wish to use.

The quickest way to achieve this is via the Jscrambler Web App. Once there, create a new app. Now, in the Application Modes tab, select the Language Specifications and application type. Next, select the transformations you want (check the Templates and Fine-Tuning tabs). In this tutorial, we'll be selecting the Obfuscation template. If you need help with these steps, please refer to our guide.

Now, we simply have to download a JSON file with all this configuration, which will be used only for quickly getting the required settings.

quick settings

Now, let's create a new file named .jscramblerrc on the React project’s root folder. Open the jscrambler.json file we just downloaded and copy all its contents to the .jscramblerrc file. After that, we just have to add two new sections to .jscramblerrc, which are filesSrc and filesDest (see below). Your final .jscramblerrc file should look like this:

JSON
xxxxxxxxxx
1
69
 
1
{
2
 "keys": {
3
   "accessKey": <ACCESS_KEY_HERE>,
4
   "secretKey": <SECRET_KEY_HERE>
5
 },
6
 "applicationId": <APP_ID_HERE>,
7
 "filesSrc": [
8
   "./build/**/*.html",
9
   "./build/**/*.js"
10
 ],
11
 "filesDest": "./",
12
 "params": [
13
   {
14
     "name": "whitespaceRemoval"
15
   },
16
   {
17
     "name": "identifiersRenaming",
18
     "options": {
19
       "mode": "SAFEST"
20
     }
21
   },
22
   {
23
     "name": "dotToBracketNotation"
24
   },
25
   {
26
     "name": "deadCodeInjection"
27
   },
28
   {
29
     "name": "stringConcealing"
30
   },
31
   {
32
     "name": "functionReordering"
33
   },
34
   {
35
     "options": {
36
       "freq": 1,
37
       "features": [
38
         "opaqueFunctions"
39
       ]
40
     },
41
     "name": "functionOutlining"
42
   },
43
   {
44
     "name": "propertyKeysObfuscation"
45
   },
46
   {
47
     "name": "regexObfuscation"
48
   },
49
   {
50
     "name": "booleanToAnything"
51
   }
52
 ],
53
 "areSubscribersOrdered": false,
54
 "applicationTypes": {
55
   "webBrowserApp": true,
56
   "desktopApp": false,
57
   "serverApp": false,
58
   "hybridMobileApp": false,
59
   "javascriptNativeApp": false,
60
   "html5GameApp": false
61
 },
62
 "languageSpecifications": {
63
   "es5": true,
64
   "es6": false,
65
   "es7": false
66
 },
67
 "useRecommendedOrder": true,
68
 "jscramblerVersion": "6.<X>"
69
}


Because we got this information directly via the Jscrambler Web App, our accessKey, secretKey and applicationId fields are already filled. If you wish to retrieve them manually, refer to our guide.

It's important to note that the params section specifies the transformations that will be used to protect your React app. These can be hand-picked by you, by selecting them in the Web App or setting them manually. You can find documentation on all the available transformations here.

You can also change filesSrc to match the files you need/want to protect. For our example — and all React apps — we recommend protecting the .html and .js files. Certainly, with a better understanding of the project, you may identify what’s critical and essential protecting.

By using filesDest: './', the files we send to protect will be overwritten by their protected version.

Integrating Jscrambler in the Build Process

Using the CLI is likely the most common way of generating your build. We will use our boilerplate app to showcase how to integrate Jscrambler into the build process.

The first step of our integration with Jscrambler is installing the Jscrambler API Client. Simply run:

Shell
xxxxxxxxxx
1
 
1
npm i jscrambler --save-dev


To integrate Jscrambler in our application's build process via the CLI, we need to create a CLI hook in the scripts section of package.json. The section should look like this:

JSON
xxxxxxxxxx
1
 
1
"scripts": {
2
  "start": "react-scripts start",
3
  "build": "react-scripts build && jscrambler",
4
  "test": "react-scripts test",
5
  "eject": "react-scripts eject"
6
},


The specific "build": "react-scripts build && jscrambler" hook will trigger the jscrambler command after the build process is finished.

In order for this command to be executable, we need to make sure that the .jscramblerrc file that we created before is in our project's root folder.

We are now ready to protect our code and build our application via the CLI:

Shell
xxxxxxxxxx
1
 
1
npm run build


This will create the protected production files on build/static/.

And you're done! Now all your HTML and JavaScript files are protected with Jscrambler against code theft and reverse-engineering. Remember that you can always fine-tune your protections to manage eventual performance hits. If that's the case, be sure to follow our tutorial.

Note: If you have ejected your project, you can also protect the files using the Jscrambler webpack plugin.

Testing the Protected React App

As a final step, let's check if the app is running successfully with the newly-protected source code. Start by installing the required dependencies:

Shell
xxxxxxxxxx
1
 
1
npm i -g serve


Next, let's simply deploy the app build files to a local development server:

Shell
xxxxxxxxxx
1
 
1
serve -s build


Now, as you should be able to see on the terminal, you can run this server on two ports. One which is publicly available, and another which is specific to your machine.

Open the provided URL and your app will start in the browser.

You can now check what your protected files look like. This can be achieved simply by opening the browser's debugger and opening the files from the "Sources" tab. The protected code should look like this:

final output

Conclusion

There are no doubts that React is a crowd favorite, from individual developers to large enterprises.

If you're building React applications which have sensitive logic, want to prevent reverse-engineering, licensing violations, and tampering, a security solution such as Jscrambler is a must.

Integrating Jscrambler into React's build process is simple and enables protecting your code with the most sophisticated polymorphic obfuscation, code locks, and self-defensive capabilities.

app React (JavaScript library) application Build (game engine)

Published at DZone with permission of . See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Ultimate Guide to React Dashboards Part 1: Overview and Analytics
  • The Technology Stack Needed To Build a Web3 Application
  • How to Build a Full-Stack App With Next.js, Prisma, Postgres, and Fastify
  • Apache Aries: Helping Enterprise Developers Build OSGi Apps

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!