<?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>Forem: Donghyuk (Jacob) Jang</title>
    <description>The latest articles on Forem by Donghyuk (Jacob) Jang (@donghyukjacobjang).</description>
    <link>https://forem.com/donghyukjacobjang</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%2F130769%2F711bca81-c3c8-4755-ba69-3be2711f8daf.jpeg</url>
      <title>Forem: Donghyuk (Jacob) Jang</title>
      <link>https://forem.com/donghyukjacobjang</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/donghyukjacobjang"/>
    <language>en</language>
    <item>
      <title>Cryptography git commits</title>
      <dc:creator>Donghyuk (Jacob) Jang</dc:creator>
      <pubDate>Sun, 18 Aug 2019 04:39:31 +0000</pubDate>
      <link>https://forem.com/jam3/cryptography-git-commits-c3h</link>
      <guid>https://forem.com/jam3/cryptography-git-commits-c3h</guid>
      <description>&lt;p&gt;-&lt;/p&gt;

&lt;p&gt;Git is an industry-standard version control tool used in IT fields, so it has a mandatory tool and a skill to learn for developers. &lt;/p&gt;

&lt;p&gt;During the development stage, developers make hundreds / thousands of commits. However, it is not difficult to find &lt;strong&gt;unverified commits&lt;/strong&gt;, although it would be related to &lt;strong&gt;security flaws&lt;/strong&gt; and potentially impacts the &lt;strong&gt;vulnerability&lt;/strong&gt; of projects.&lt;/p&gt;

&lt;p&gt;This article will mainly focus on &lt;strong&gt;the importance of the signing(verified) commits&lt;/strong&gt;. As well, there is one practice on how to pretend someone else in Git repositories.&lt;/p&gt;

&lt;p&gt;-&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Table of Contents&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Brief about SSH key&lt;/li&gt;
&lt;li&gt;GPG key&lt;/li&gt;
&lt;li&gt;An example of a security flaw without using GPG key&lt;/li&gt;
&lt;li&gt;How to setup GPG key&lt;/li&gt;
&lt;li&gt;How to create signing commits&lt;/li&gt;
&lt;li&gt;Final thoughts&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;-&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Brief about SSH&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ft112w61uke9qty2golnk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ft112w61uke9qty2golnk.png" alt="SSH-key added in Github"&gt;&lt;/a&gt;&lt;/p&gt;
SSH-key added in Github



&lt;p&gt;First of all, I would assume that you have already set up &lt;strong&gt;SSH-key&lt;/strong&gt; on your machine to authenticate to Github and have it registered with your Github account. If not, please read &lt;a href="https://help.github.com/en/articles/connecting-to-github-with-ssh" rel="noopener noreferrer"&gt;Connecting to GitHub with SSH&lt;/a&gt; and follow the steps. &lt;/p&gt;

&lt;p&gt;Setting up SSH-key means that you do not have to provide your Github username or password for all of your git activity. That is the main reason to set up an SSH-key. Does connecting to Github with an SSH-key mean all of your commits become secure? We will dig into this and talk more about the disadvantages of only using an SSH-key later in this article.&lt;/p&gt;

&lt;p&gt;-&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Then, what is GPG key?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgnupg.org%2Fshare%2Flogo-gnupg-light-purple-bg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgnupg.org%2Fshare%2Flogo-gnupg-light-purple-bg.png" alt="GnuPG logo"&gt;&lt;/a&gt;&lt;/p&gt;
GnuPG logo



&lt;p&gt;Gnu Privacy Guard(GPG) is a tool that allows users to integrate an additional security layer easily with other applications. In this case, it will be Git. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;GnuPG is a complete and free implementation of the OpenPGP standard as defined by RFC4880 (also known as PGP). GnuPG allows you to encrypt and sign your data and communications; it features a versatile key management system, along with access modules for all kinds of public key directories. GnuPG, also known as GPG, is a command line tool with features for easy integration with other applications. A wealth of frontend applications and libraries are available. GnuPG also provides support for S/MIME and Secure Shell (ssh).&lt;/p&gt;

&lt;p&gt;Since its introduction in 1997, GnuPG is Free Software (meaning that it respects your freedom). It can be freely used, modified and distributed under the terms of the GNU General Public License .&lt;/p&gt;

&lt;p&gt;The current version of GnuPG is 2.2.17. See the download page for other maintained versions.&lt;/p&gt;

&lt;p&gt;Gpg4win is a Windows version of GnuPG featuring a context menu tool, a crypto manager, and an Outlook plugin to send and receive standard PGP/MIME mails. The current version of Gpg4win is 3.1.10.&lt;/p&gt;
&lt;/blockquote&gt;
The GNU Privacy Guard



&lt;p&gt;-&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What could happen if you do not use GPG key?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ffb91psceqge48rtj287m.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ffb91psceqge48rtj287m.jpg"&gt;&lt;/a&gt;&lt;/p&gt;
a costume under surveillance



&lt;p&gt;One very common severe issue is that someone can set up other developers' Github username and email in their local machine, and create commits which will appear as "the someone else"' commits in the Git repository. Of course, this activity needs to meet a certain condition. The command below shows how to set up a different Github username and email within your terminal.&lt;/p&gt;

&lt;p&gt;-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// setup username and email
$ git config --global user.name "No GPG"
$ git config --global user.email no.gpg@email.com

// verity username and email
$ git config --global user.name
&amp;gt; No GPG
$ git config --global user.email
&amp;gt; no.gpg@email.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here I want to share with you an example of two different commits that I pushed in a test Git repository(&lt;a href="https://github.com/DonghyukJacobJang/signing-commits" rel="noopener noreferrer"&gt;signing commit&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;When I created this git repository, I made the initial push with a signed(verified) commit. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxatnsmsl2917tuz76eth.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxatnsmsl2917tuz76eth.png" alt="Screenshot of git commit history"&gt;&lt;/a&gt;&lt;/p&gt;
Screenshot of git commit history



&lt;p&gt;And then, I asked a friend of mine (&lt;a class="mentioned-user" href="https://dev.to/alemesa"&gt;@alemesa&lt;/a&gt;) to follow the practice above and gave him my Github username and email. (These credentials for signing in can be found with very little effort.) He created a commit and pushed to the &lt;code&gt;master&lt;/code&gt; branch. Check out the results below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fd404zd1zleurd6eq4j28.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fd404zd1zleurd6eq4j28.png" alt="Screenshot of git commit history"&gt;&lt;/a&gt;&lt;/p&gt;
Screenshot of git commit history



&lt;p&gt;The commit has appeared that it was created by me. However, there is no "verified" flag like the first initial commit. &lt;/p&gt;

&lt;p&gt;If &lt;code&gt;master&lt;/code&gt; branch is not protected like the testing Git repository, then anyone in the contributor list can push any code by pretending to be someone else. Please find details about how to protect branches &lt;a href="https://help.github.com/en/enterprise/2.15/admin/developer-workflow/configuring-protected-branches-and-required-status-checks" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;NOTE: If you want to test this activity, please send me a direct message that includes your Github username. I will add you into the collaborator list.&lt;/p&gt;

&lt;p&gt;-&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;How to setup GPG key&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The references will guide you on how to setup GPG key in your workstation, and how to add them into your Github account.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://help.github.com/en/articles/generating-a-new-gpg-key" rel="noopener noreferrer"&gt;Setup GPG key Github&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@timmywil/sign-your-commits-on-github-with-gpg-566f07762a43" rel="noopener noreferrer"&gt;Sign your commits on Github with GPG&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;-&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;How to create signing commit(s)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Add file(s) before creating a commit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// add a single file
$ git add [path file]

// add all file
$ git add .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The git command below is how to create a commit with or without your signature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// create commit with a message without signing
$ git commit -m "commit message"

// add all file that you want to commit
$ git commit -S -m "commit message"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once your signing commit(s) is created, the next step is to push to a git repo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// push the commit
$ git push 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The screenshot below is an example of git signing commit in the test git repository(&lt;a href="https://github.com/DonghyukJacobJang/signing-commits" rel="noopener noreferrer"&gt;signing commit&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fu2xhsjza2qwnstd04mwx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fu2xhsjza2qwnstd04mwx.png"&gt;&lt;/a&gt;&lt;/p&gt;
detail of signing commit



&lt;p&gt;-&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To sum this article up, using GPG-key will increase security and authenticity level by encrypting its data, and adding a person's signature. If you or your organization have considered these, the steps above will bring its advantages and ensure trust with your commit workflow. &lt;/p&gt;

&lt;p&gt;NOTE: this activity may not be suitable for all case. Please make sure using this for the right purpose.&lt;/p&gt;

&lt;p&gt;-&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;References&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://gnupg.org/" rel="noopener noreferrer"&gt;Gnu PG&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://help.github.com/en/articles/telling-git-about-your-signing-key" rel="noopener noreferrer"&gt;Telling Git about your signing key&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://softwareengineering.stackexchange.com/questions/212192/what-are-the-advantages-and-disadvantages-of-cryptographically-signing-commits-a" rel="noopener noreferrer"&gt;What are the advantages and disadvantages of cryptographically signing commits and tags in Git?&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://security.stackexchange.com/questions/120706/why-would-i-sign-my-git-commits-with-a-gpg-key-when-i-already-use-an-ssh-key-to" rel="noopener noreferrer"&gt;Why would I sign my git commits with a GPG key when I already use an SSH key to authenticate myself when I push?&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://help.github.com/en/articles/signing-commits" rel="noopener noreferrer"&gt;How to create signing commits&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://confluence.atlassian.com/sourcetreekb/setup-gpg-to-sign-commits-within-sourcetree-765397791.html" rel="noopener noreferrer"&gt;Setup gpg-key within SourceTree&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.gnupg.org/gph/en/manual.html" rel="noopener noreferrer"&gt;GnuPG manual&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;-&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Collaborators&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Alejandro Mesa (&lt;a class="mentioned-user" href="https://dev.to/alemesa"&gt;@alemesa&lt;/a&gt;) - &lt;a href="mailto:alejandro.suarez@jam3.com"&gt;alejandro.suarez@jam3.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Danny Paton - &lt;a href="mailto:danny.paton@jam3.com"&gt;danny.paton@jam3.com&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>git</category>
      <category>security</category>
      <category>sshkey</category>
      <category>gpgkey</category>
    </item>
    <item>
      <title>Managing .env variables for provisional builds with Create React App</title>
      <dc:creator>Donghyuk (Jacob) Jang</dc:creator>
      <pubDate>Sun, 07 Apr 2019 21:41:41 +0000</pubDate>
      <link>https://forem.com/jam3/managing-env-variables-for-provisional-builds-h37</link>
      <guid>https://forem.com/jam3/managing-env-variables-for-provisional-builds-h37</guid>
      <description>&lt;p&gt;When developing web applications by using Create React App, developers get &lt;code&gt;NODE_ENV=development&lt;/code&gt; on their local environment and &lt;code&gt;NODE_ENV=production&lt;/code&gt; on the production build by default. And, modifying &lt;code&gt;NODE_ENV&lt;/code&gt; is forbidden. According to the Create React App, this is an intentional setting to protect the &lt;code&gt;production&lt;/code&gt; environment from a mistake/accident deploying.&lt;/p&gt;

&lt;p&gt;You will be able to see scripts like below in &lt;code&gt;package.json&lt;/code&gt; after creating your web app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// package.json
scripts: {
  "start": "react-scripts start", // `NODE_ENV` is equal to `development`.
  "build": "react-scripts build", // `NODE_ENV` is equal to `production`.
  ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you create or already have &lt;code&gt;.env.development&lt;/code&gt; and &lt;code&gt;.env.production&lt;/code&gt; in the root of your project, these files will be used for running each script. &lt;code&gt;npm start&lt;/code&gt; will pick up &lt;code&gt;.env.development&lt;/code&gt;, and &lt;code&gt;npm build&lt;/code&gt; will use environment variables in &lt;code&gt;.env.production&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;-&lt;br&gt;
&lt;strong&gt;What if you want to setup &lt;code&gt;.env.staging&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This article will show you how to manage environment variables for provisional builds.&lt;/p&gt;

&lt;p&gt;Let's dive into that! Oh, if you do not have any experiences of CRA, please &lt;a href="https://facebook.github.io/create-react-app/docs/getting-started"&gt;Getting started&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Story&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Imagine that your project will have three separated provisional environments; &lt;code&gt;development&lt;/code&gt;, &lt;code&gt;staging&lt;/code&gt;, and &lt;code&gt;production&lt;/code&gt;. Each environment is using different API endpoints. In addition to that, the project may require a &lt;code&gt;REACT_APP_CUSTOM_NODE_ENV&lt;/code&gt;. This is because &lt;code&gt;NODE_ENV&lt;/code&gt; will always be &lt;code&gt;production&lt;/code&gt; for the build regardless.&lt;/p&gt;

&lt;p&gt;-&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Goal&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create &lt;code&gt;.env.development&lt;/code&gt;, &lt;code&gt;.env.staging&lt;/code&gt;, and &lt;code&gt;.env.production&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Configure environment viriables for each build.&lt;/li&gt;
&lt;li&gt;Modify scripts in &lt;code&gt;package.json&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;-&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Getting started&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Step 1.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Thankfully, &lt;code&gt;dotenv&lt;/code&gt; comes out of box. Let's create &lt;code&gt;.env&lt;/code&gt; files under the root folder to manage environment variables. The files are &lt;code&gt;.env&lt;/code&gt;, &lt;code&gt;.env.development&lt;/code&gt;, &lt;code&gt;.env.staging&lt;/code&gt;, and &lt;code&gt;.env.production&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.env&lt;/code&gt; - Keep all common/shared environment variable&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.env.development&lt;/code&gt; - Variables are used for the local development &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.env.staging&lt;/code&gt; - Variables are used for the staging build&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.env.production&lt;/code&gt; - Variables are used for the production build&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#.env
REACT_APP_DOC_TITLE = "Document title"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//.env.developement
REACT_APP_API_ENDPOINT = "https://development-api.endpoint.com/"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#.env.staging

# NODE_ENV will always be set to "production" for a build
# more details at https://create-react-app.dev/docs/deployment/#customizing-environment-variables-for-arbitrary-build-environments
REACT_APP_CUSTOM_NODE_ENV = "staging"
REACT_APP_API_ENDPOINT = "https://staging-api.endpoint.com/"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#.env.production
REACT_APP_API_ENDPOINT = "https://api.endpoint.com/"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;NOTE: The prefix &lt;code&gt;REACT_APP_&lt;/code&gt; is required when creating custom environment variables. &lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;.env.development&lt;/code&gt; and &lt;code&gt;.env.production&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;As a default behavior, those files will be served with no configuration. You do not even have to update scripts in &lt;code&gt;package.json&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;.env.staging&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;Here is the main focus of this post. To target &lt;code&gt;.env.staging&lt;/code&gt; file for the staging build, we need a library to achieve this.&lt;/p&gt;

&lt;p&gt;1- Let's install &lt;code&gt;env-cmd&lt;/code&gt;. This library will will help us on using/executing a selected environment file. &lt;a href="https://www.npmjs.com/package/env-cmd"&gt;See more detail&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// execute command below at the root of project
$ npm install env-cmd --save
or
$ yarn add env-cmd
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;2- Add a script in &lt;code&gt;package.json&lt;/code&gt; like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// package.json
scripts: {
  "start": "react-scripts start", // `NODE_ENV` is equal to `development`.
  "build": "react-scripts build", // `NODE_ENV` is equal to `production`.
  "build:staging": "env-cmd -f .env.staging react-scripts build", // `NODE_ENV` is equal to `production`.
  ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;3- Finally, test your &lt;code&gt;build:staging&lt;/code&gt; script. &lt;/p&gt;

&lt;p&gt;-&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The intention of this technique is to use different custom environment variables for many provisional environments without ejecting the default CRA configs. &lt;/p&gt;

&lt;p&gt;-&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Resources&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://facebook.github.io/create-react-app/docs/deployment#customizing-environment-variables-for-arbitrary-build-environments"&gt;Customizing Environment Variables for Arbitrary Build Environments&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables"&gt;Adding Custom Environment Viriables&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.npmjs.com/package/dotenv"&gt;dotenv&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Special Thanks&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://dev.to/foxbit19"&gt;@foxbit19&lt;/a&gt; - found &lt;code&gt;env-cmd&lt;/code&gt; version 8.x requries additional argument to link to the custom env file. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/1970smthin"&gt;@j6000&lt;/a&gt; - pointed out &lt;code&gt;NODE_ENV&lt;/code&gt; for the build will always be set to "production" regardless.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>dotenv</category>
      <category>reactappenv</category>
    </item>
    <item>
      <title>How to prevent XSS attacks when using dangerouslySetInnerHTML in React</title>
      <dc:creator>Donghyuk (Jacob) Jang</dc:creator>
      <pubDate>Mon, 04 Feb 2019 21:11:04 +0000</pubDate>
      <link>https://forem.com/jam3/how-to-prevent-xss-attacks-when-using-dangerouslysetinnerhtml-in-react-1464</link>
      <guid>https://forem.com/jam3/how-to-prevent-xss-attacks-when-using-dangerouslysetinnerhtml-in-react-1464</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft473h25wb25zgob55r5a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft473h25wb25zgob55r5a.png" alt="XSS hero image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article intends to show one of the techniques we use to mitigate cross-site scripting (XSS) attacks at &lt;a href="https://jam3.com" rel="noopener noreferrer"&gt;Jam3&lt;/a&gt;. These vulnerabilities may appear when &lt;code&gt;dangerouslySetInnerHTML&lt;/code&gt; is wrongly used, and our goal is to detect it ahead of time and to clean up untrusted values.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Dangerously Set innerHTML&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;This feature is designed to present and insert DOM formatted content data into the frontend. The use of the feature is a bad practice, especially when dealing with user inputs and dynamic data. You must consider its vulnerabilities in order to prevent &lt;a href="https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)" rel="noopener noreferrer"&gt;XSS attack&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"Easy" to make thing safe&lt;/strong&gt; is one of React philosophy. React is flexible and extendable which means that the bad practice can be turning into the best practice. Sanitizing props value is one obvious option and strongly recommended.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;XSS attacks&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Cross-site scripting (XSS) allows attackers(hackers) to inject malicious code into a website for other end-users. By doing this, attackers may have access to personal data, cookies, webcams, and even more. Read more about &lt;a href="https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)" rel="noopener noreferrer"&gt;Cross-site scripting&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Copy &lt;code&gt;https://placeimgxxx.com/320/320/any" onerror="alert('xss injection')&lt;/code&gt; and paste it in the input field in the xss injection example below:&lt;br&gt;
&lt;a href="https://codesandbox.io/s/k9vxk9ppyo?autoresize=1&amp;amp;moduleview=1" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodesandbox.io%2Fstatic%2Fimg%2Fplay-codesandbox.svg" alt="Edit k9vxk9ppyo"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Preventing XSS&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This issue is not restricted to React; to learn how to prevent it in your web development OWASP has a good &lt;a href="https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet" rel="noopener noreferrer"&gt;prevention cheat sheet&lt;/a&gt;. One approach to prevent XSS attacks is to sanitize data. It can be done either on the server-side or the client-side; in this article, we will focus on the client-side solution.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Preventing XSS with dangerouslyInnerSetHTML&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Sanitizing content in the frontend when using &lt;code&gt;dangerouslySetInnerHTML&lt;/code&gt; is always a good security practice, even with a trusted &lt;a href="https://en.wikipedia.org/wiki/Single_source_of_truth" rel="noopener noreferrer"&gt;source of truth&lt;/a&gt;. For example, another development team in charge of maintaining the project changes the source of truth without realizing how it could impact the site. A change like that may cause a critical XSS vulnerability.&lt;/p&gt;

&lt;p&gt;At Jam3 we avoid using &lt;code&gt;dangerouslySetInnerHTML&lt;/code&gt; whenever possible. When it's required, we &lt;strong&gt;always&lt;/strong&gt; apply sanitization security layers on both the back-end and front-end. In addition, we created an &lt;a href="https://eslint.org/" rel="noopener noreferrer"&gt;ESLint&lt;/a&gt; rule called &lt;code&gt;no-sanitizer-with-danger&lt;/code&gt; inside &lt;a href="https://github.com/Jam3/eslint-plugin-jam3" rel="noopener noreferrer"&gt;&lt;code&gt;eslint-plugin-jam3&lt;/code&gt;&lt;/a&gt; to detect improper use of &lt;code&gt;dangerouslySetInnerHTML&lt;/code&gt;.&lt;/p&gt;
&lt;h1&gt;
  
  
  &lt;strong&gt;ESLint rule&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;I assume that you are already familiar with ESLint. If not, &lt;a href="https://eslint.org/docs/user-guide/getting-started" rel="noopener noreferrer"&gt;Get Started&lt;/a&gt;.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

$ npm i eslint eslint-plugin-jam3 -save-dev


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Extend &lt;code&gt;pluginsin&lt;/code&gt; the .eslintrc config file by adding &lt;code&gt;jam3&lt;/code&gt;. You can omit the eslint-plugin- prefix. Then, configure the rules by adding &lt;code&gt;jam3/no-sanitizer-with-danger&lt;/code&gt; to the rules. Note: error level 2 is recommended. With this option, exit code will be 1. error level 1 will give warning alert, but does not affect exit code. 0 means to turn the rule off. The plugin will check that the content passed to &lt;code&gt;dangerouslySetInnerHTML&lt;/code&gt; is wrapped in this sanitizer function. The wrapper function name can be also be changed in the JSON file (&lt;code&gt;sanitizer&lt;/code&gt; is the default wrapper name).&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  &lt;strong&gt;How to use it&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Here is an &lt;strong&gt;unsafe&lt;/strong&gt; way of using dangerouslySetInnerHTML.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fddomsjut8oky9160bxyl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fddomsjut8oky9160bxyl.png" alt="dangerouslySetInnerHTML without sanitizer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the rule is enabled, your code editor will alert the lack of a sanitizer in &lt;code&gt;dangerouslySetInnerHTML&lt;/code&gt;. For the purpose of this article we use &lt;a href="https://github.com/cure53/DOMPurify" rel="noopener noreferrer"&gt;dompurify&lt;/a&gt;, you can find an extended list of available sanitizers at the end of the article.&lt;/p&gt;

&lt;p&gt;The sanitizer wrapper must have a name, for the purpose of this article we are creating &lt;code&gt;const sanitizer = dompurify.sanitize;&lt;/code&gt;. However, it is recommended to create a sanitization utility to abstract your chosen sanitizer.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h1&gt;
  
  
  &lt;strong&gt;Sanitizer libraries&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Our team has researched and tried many sanitizers and can recommend these 3 libraries.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/cure53/DOMPurify" rel="noopener noreferrer"&gt;&lt;strong&gt;dompurify&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strip out all dirty HTML and returns clean HTML data
npm Weekly download 50k+&lt;/li&gt;
&lt;li&gt;40 contributors &lt;/li&gt;
&lt;li&gt;Earned 2800+ GitHub ⭐️&lt;/li&gt;
&lt;li&gt;5.6kB MINIFIED + GZIPPED&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://github.com/leizongmin/js-xss" rel="noopener noreferrer"&gt;&lt;strong&gt;xss&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Escape HTML entity characters to prevent the attack which occurs to transform non-readable content for the end users&lt;/li&gt;
&lt;li&gt;npm weekly download 30k+&lt;/li&gt;
&lt;li&gt;18 contributors&lt;/li&gt;
&lt;li&gt;Earned 2500+ github ⭐️&lt;/li&gt;
&lt;li&gt;5.3kB MINIFIED + GZIPPED&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://github.com/yahoo/xss-filters" rel="noopener noreferrer"&gt;&lt;strong&gt;xss-filters&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Escape HTML entity characters to prevent the attack which occurs to transform non-readable content for the end users&lt;/li&gt;
&lt;li&gt;npm weekly download 30k+&lt;/li&gt;
&lt;li&gt;5 contributors&lt;/li&gt;
&lt;li&gt;Earned 900+ github ⭐️&lt;/li&gt;
&lt;li&gt;2.1kB MINIFIED + GZIPPED&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h1&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;To sum up, finding the most suitable sanitizer library for your project is very important for security. You might want to have a look at GitHub stars, npm download numbers, and maintenance routines. The use of &lt;code&gt;no-sanitizer-with-danger&lt;/code&gt; in &lt;code&gt;eslint-plugin-jam3&lt;/code&gt; will be a great choice to help ensure all data is being properly purified and gain confidence that your project will be safe from XSS vulnerabilities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; Please keep it in mind there is a performance disadvantage of sanitizing data in client-side. For example, sanitizing all data at once may slow down the initial load. To prevent this in large scale web applications, you can implement a "lazy-sanitizing" approach to sanitize on the fly.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Further reading and sources&lt;/strong&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;ESLint developer guide&lt;/li&gt;
&lt;li&gt;Creating an ESLint Plugin&lt;/li&gt;
&lt;li&gt;eslint-plugin-react&lt;/li&gt;
&lt;li&gt;eslint-plugin-jam3&lt;/li&gt;
&lt;li&gt;Cross-site scripting&lt;/li&gt;
&lt;li&gt;XSS attack cheat sheet&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Contributors&lt;/strong&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/DonghyukJacobJang" rel="noopener noreferrer"&gt;Donghyuk(Jacob) Jang&lt;/a&gt; / &lt;a href="//mailto:jacob.jang@jam3.com"&gt;jacob.jang@jam3.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/iranreyes" rel="noopener noreferrer"&gt;Iran Reyes&lt;/a&gt; / &lt;a href="//mailto:iran.reyes@jam3.com"&gt;iran.reyes@jam3.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Peter Altamirano / &lt;a href="mailto:peter.altamirano@jam3.com"&gt;peter.altamirano@jam3.com&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2u6hysdns7wagk9lsy3q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2u6hysdns7wagk9lsy3q.png" alt="Jam3 logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Jam3 is a design and experience agency that partners with forward-thinking brands from around the world. To learn more, visit us at &lt;a href="https://jam3.com" rel="noopener noreferrer"&gt;jam3.com&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Article by Donghyuk (Jacob) Jang&lt;/p&gt;

</description>
      <category>react</category>
      <category>xss</category>
      <category>dangerous</category>
      <category>eslint</category>
    </item>
  </channel>
</rss>
