<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Frankie on Medium]]></title>
        <description><![CDATA[Stories by Frankie on Medium]]></description>
        <link>https://medium.com/@frankie95?source=rss-a7208dec0e8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*tVsokWtR6DiwAyZMcWUv0A.jpeg</url>
            <title>Stories by Frankie on Medium</title>
            <link>https://medium.com/@frankie95?source=rss-a7208dec0e8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 21:40:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@frankie95/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Use normalize() to duel with non-English string in javascript]]></title>
            <link>https://frankie95.medium.com/use-normalize-to-duel-with-non-english-string-in-javascript-b483effa1f0f?source=rss-a7208dec0e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/b483effa1f0f</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[unicode]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[string]]></category>
            <category><![CDATA[js]]></category>
            <dc:creator><![CDATA[Frankie]]></dc:creator>
            <pubDate>Mon, 10 Jan 2022 15:14:26 GMT</pubDate>
            <atom:updated>2022-01-10T15:14:26.116Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YicvikrSZDI4MNlG" /><figcaption>Photo by <a href="https://unsplash.com/@jstrippa?utm_source=medium&amp;utm_medium=referral">James Harrison</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>For a international website, we may need to handle different languages, in some cases, the user may need to input in their language, we cannot control what they type, they may maliciously or accidentally input something that we do not expect.</p><p>One famous example:</p><pre>const name1 = &#39;\u0041\u006d\u00e9\u006c\u0069\u0065&#39;;<br>const name2 = &#39;\u0041\u006d\u0065\u0301\u006c\u0069\u0065&#39;;</pre><pre>console.log(`${name1}, ${name2}`);<br>// expected output: &quot;Amélie, Amélie&quot;<br>console.log(name1 === name2);<br>// expected output: false<br>console.log(name1.length === name2.length);<br>// expected output: false</pre><p>We cannot see any differences by our eyes, they look the same, but they are different, very weird.</p><p>Here is the reason:</p><blockquote>Unicode assigns a unique numerical value, called a <em>code point</em>, to each character. For example, the code point for &quot;A&quot; is given as U+0041. However, sometimes more than one code point, or sequence of code points, can represent the same abstract character — the character &quot;ñ&quot; for example can be represented by either of:</blockquote><blockquote>The single code point U+00F1.</blockquote><blockquote>The code point for &quot;n&quot; (U+006E) followed by the code point for the combining tilde (U+0303).</blockquote><p><strong>How to fix it?</strong></p><p>By using normalize() , it will returns the Unicode Normalization Form of the string.</p><pre>const name1NFC = name1.normalize(&#39;NFC&#39;);<br>const name2NFC = name2.normalize(&#39;NFC&#39;);</pre><pre>console.log(`${name1NFC}, ${name2NFC}`);<br>// expected output: &quot;Amélie, Amélie&quot;<br>console.log(name1NFC === name2NFC);<br>// expected output: true<br>console.log(name1NFC.length === name2NFC.length);<br>// expected output: true</pre><p><strong>Why this is important?</strong></p><p>You can see from the above example, the length of two identical string can have two different length, it will fail the length check.</p><p>Also, imagine a user register a username which has been taken, user can play this trick to circumvent the checking.</p><p>Therefore, we should normalize the string before we do any checking.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b483effa1f0f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Install latest GO in AWS Cloud9 (Ubuntu Server 18.04 LTS)]]></title>
            <link>https://towardsaws.com/install-latest-go-in-aws-cloud9-ubuntu-server-18-04-lts-f179877896d7?source=rss-a7208dec0e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/f179877896d7</guid>
            <category><![CDATA[aws-ec2]]></category>
            <category><![CDATA[golang-tutorial]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Frankie]]></dc:creator>
            <pubDate>Mon, 24 May 2021 14:47:09 GMT</pubDate>
            <atom:updated>2021-05-26T00:52:34.148Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WZ6PBAhdVxYJalNc" /><figcaption>Photo by <a href="https://unsplash.com/@florianolv?utm_source=medium&amp;utm_medium=referral">Florian Olivo</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When we create an AWS Cloud9 environment, it comes with <strong>GO</strong>. However, if we want to use the GIN framework, <strong>GO version 1.13+ is required</strong>. When we choose <strong>Ubuntu Server 18.04 LTS</strong> in AWS Cloud9, the GO version is too old. Hence, we need to install the latest<strong> GO</strong> manually.</p><p><em>Type </em><em>go version to check the current GO version.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/405/1*-L3drOPxfFDpHby-d2SzuQ.png" /></figure><p><strong>Step 1</strong>: Remove the existing golang</p><pre>sudo apt-get purge golang*</pre><p><strong>Step 2: </strong>Download the latest GO zip file(you can find the latest version <a href="https://golang.org/dl/">here</a>.) The latest version now is 1.16.4, you might need to change the highlighted link in the future.</p><pre>wget <strong>https://storage.googleapis.com/golang/go1.16.4.linux-amd64.tar.gz</strong></pre><p><strong>Step 3: </strong>Unzip it and move to /usr/local. (You might need to change the highlighted file name to match the file downloaded from step2.)</p><pre>sudo tar -C /usr/local -xzf ./<strong>go1.16.4.linux-amd64.tar.gz</strong>              </pre><p><strong>Step 4: </strong>Remove the zip file as we do not need it anymore. (You might need to change the highlighted file name to match the file downloaded from step2.)</p><pre>rm ./<strong>go1.16.4.linux-amd64.tar.gz</strong>                                       </pre><p><strong>Step 5:</strong> Add path of GO to PATH variable</p><pre>sudo nano ~/.profile</pre><p>In the .profile add /usr/local/go/bin to PATH just like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*xe_8p5vaLWOhAd-6JwQXxw.png" /></figure><p><strong>Step 6: </strong>Refresh the PATH variable as we changed it.</p><pre>source ~/.bash_profile</pre><p>Now we can see the GO version has been updated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/319/1*m5Hb0IMdiUH_89F5C2YJAg.png" /></figure><p>However, we cannot run sudo go.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/308/1*vzcZOvWQQRXwvwsILVLn_Q.png" /></figure><p>This is because when we run sudo, it will read the config file at /etc/sudoers, but we have not yet added the GO path in this file. Run command below to edit /etc/sudoers.</p><pre>sudo nano /etc/sudoer</pre><p>Add /usr/local/go/bin/ to secure_path.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*sVWy4dHc5cgVJ58vtx92Rg.png" /></figure><p>Run sudo go again. This time should be fine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*CIJhPfqY21E8dZvEC3UItg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f179877896d7" width="1" height="1" alt=""><hr><p><a href="https://towardsaws.com/install-latest-go-in-aws-cloud9-ubuntu-server-18-04-lts-f179877896d7">Install latest GO in AWS Cloud9 (Ubuntu Server 18.04 LTS)</a> was originally published in <a href="https://towardsaws.com/">Towards AWS</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Set alias command in powershell to make your life easier]]></title>
            <link>https://frankie95.medium.com/set-alias-command-in-powershell-to-make-your-life-easier-61de600c18d2?source=rss-a7208dec0e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/61de600c18d2</guid>
            <category><![CDATA[command-line]]></category>
            <category><![CDATA[shell]]></category>
            <category><![CDATA[vscode]]></category>
            <category><![CDATA[powershell]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Frankie]]></dc:creator>
            <pubDate>Wed, 02 Dec 2020 13:26:24 GMT</pubDate>
            <atom:updated>2020-12-02T13:26:24.312Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QtRJoZsbsAQcQLQa" /><figcaption>Photo by <a href="https://unsplash.com/@ilyapavlov?utm_source=medium&amp;utm_medium=referral">Ilya Pavlov</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When we are using VS Code for development, it also comes with powershell as the default terminal.</p><p>Sometimes we have to run commands in the terminal, and the commands maybe long and easily make typo, for example:</p><p>kubectl</p><p>kudectl</p><p>You have to spend some amount of time to figure it out.</p><p>Also, setting alias command can save your time from typing a long command to just a few characters.</p><h3><strong>Create permanent PowerShell Aliases</strong></h3><p>First, open VS Code and type below command on the terminal.</p><pre>Get-Variable profile | Format-List</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*mTjeW06ld1Fm_HhzpvGIiw.png" /></figure><p>Value shows the path of your powershell profile. If you open fire explorer and follow the path, you may find out that the folder is not exist. If that is the case, we need to create the folder WindowsPowerShell and the file Microsoft.PowerShell_profile.ps1 .</p><p>After that, it’s time for us to set some alias commands, use editor (e.g. notepad) to open Microsoft.PowerShell_profile.ps1</p><p>Create new alias command by inputting Set-Alias &lt;name&gt; &lt;value&gt;.</p><p>For example: Set-Alias k kubectl.</p><p>Then save the file and reopen VS Code. Input k on the terminal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZDlm0QLIctIbcC_inzGv6A.png" /></figure><p>It is equal to the command kubectl.</p><p>However, Set-Alias are just that — aliases for command names — not command names plus arguments. For example, we cannot set alias like this:</p><p>kubectl describe pod [pod name]</p><p>Set-Alias kdp kubectl describe pod</p><p>In this case, we need to use function :</p><pre>function &lt;alias name&gt;{<br>&lt;command plus arguments&gt; $args<br>}</pre><p>open Microsoft.PowerShell_profile.ps1 and add:</p><pre>Set-Alias k kubectl</pre><pre>function kdp {<br> k describe pod $args<br>}</pre><p>Save and reopen VS Code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*XGMuNwrg4jVicWxU-jaEaA.png" /></figure><p>Now, when we input kdp posts it is equivalence to</p><p>kubectl describe pod posts</p><p>You should always make your own list of alias commands to save time and get rid of typo.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=61de600c18d2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[encodeURI & encodeURIComponent You should know the differences]]></title>
            <link>https://frankie95.medium.com/encodeuri-encodeuricomponent-you-should-know-the-differences-a58426b9f91a?source=rss-a7208dec0e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/a58426b9f91a</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[Frankie]]></dc:creator>
            <pubDate>Tue, 13 Oct 2020 14:56:08 GMT</pubDate>
            <atom:updated>2020-10-13T14:56:08.552Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*y-QaRsXgFznDsyX6" /><figcaption>Photo by <a href="https://unsplash.com/@coolmilo?utm_source=medium&amp;utm_medium=referral">camilo jimenez</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>At the past, you may have heard about escape() function encodes a string to make a string portable, so it can be transmitted across any network to any computer that supports ASCII characters. However, it was deprecated in JavaScript version 1.5. Now, we should use encodeURI() or encodeURIComponent() instead.</p><p>The main difference of these two functions is the encoding range.</p><p>encodeURI will <strong>not </strong>encode ASCII alphabet, number and ~!@#$&amp;*()=:/,;?+.-_&#39;</p><p>encodeURIComponent will <strong>not </strong>encode ASCII alphabet, number and ~!*().-_&#39;</p><p>Range of encoding: encodeURIComponent &gt; encodeURI</p><p>For example:</p><pre>encodeURI(&quot;https://abc-def.com/index.php?name=David Beckham&quot;)<br>//&quot;https://abc-def.com/index.php?name=David%20Beckham&quot;</pre><pre>encodeURIComponent(&quot;https://abc-def.com/index.php?name=David Beckham&quot;)<br>//&quot;https%3A%2F%2Fabc-def.com%2Findex.php%3Fname%3DDavid%20Beckham&quot;</pre><p>We can see that the result of encodeURI is still a valid URI, but the result of encodeURIComponent is not, because it will encode the symbol :/.</p><p>When to use encodeURI?</p><p>encodeURI is to process the entire URI. It accepts the protocol, host, port and only encode the path and query.</p><p>When to use encodeURIComponent?</p><p>encodeURIComponent is to process part of the URI, e.g. query strings.</p><p>For encodeURIyou can retrieve the original data by using decodeURI and for encodeURIComponent you can use decodeURIComponent.</p><p>For example:</p><pre>decodeURI(&quot;https://abc-def.com/index.php?name=David%20Beckham&quot;)<br>//&quot;https://abc-def.com/index.php?name=David Beckham&quot;</pre><pre>decodeURIComponent(&quot;https%3A%2F%2Fabc-def.com%2Findex.php%3Fname%3DDavid%20Beckham&quot;)<br>//&quot;https://abc-def.com/index.php?name=David Beckham&quot;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a58426b9f91a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Small SVG Pitfall in React]]></title>
            <link>https://medium.com/swlh/a-small-svg-pitfall-in-react-39cf09a3f66c?source=rss-a7208dec0e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/39cf09a3f66c</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[svg]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[Frankie]]></dc:creator>
            <pubDate>Tue, 28 Jul 2020 15:16:32 GMT</pubDate>
            <atom:updated>2020-08-17T18:07:32.602Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sl_qflLQbaHBngh-" /><figcaption>Photo by <a href="https://unsplash.com/@cgower?utm_source=medium&amp;utm_medium=referral">Christopher Gower</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When we want to use SVG image in React, we may import it like this:</p><pre>import {ReactComponent as Logo} from &quot;./logo.svg&quot;;</pre><p>In my project, the logo.svg is a X for user to click to delete the item in the list.</p><p>I added an onClick function to it as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14867e196c51a75795c4b55ad8aef36c/href">https://medium.com/media/14867e196c51a75795c4b55ad8aef36c/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FLcK6QrMAhqNpVRiuO3FOA.gif" /></figure><p>When we click on the X, the item is undefined. Hence, we can not delete it correctly. However, when we click on the space outside X, but within that square area, it can retrieve the item correctly from the data-item.</p><p>The crux of the issue is that we are using event.target which is referencing the actual target that received the click. In this case, the &lt;path&gt; tag. The data-item is on&lt;svg&gt; tag. Therefor, we cannot get the correct data.</p><p>It will only happen on SVG graphic as it is a vector graphic defined by a group of paths, boxes, circles, text etc. So, when we clicked on a SVG graphic, we may actually clicked on the paths, boxes, circles, text etc. but not the &lt;svg&gt; itself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*siR0eeMxswtab2wkTS-3AA.gif" /></figure><h3>Solution:</h3><p>To solve it, we should use event.currentTarget, which is always the object listening for the event. It always refers to the element to which the event handler has been attached(which is &lt;svg&gt; tag in this case as we added the onClick event handler to it).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39cf09a3f66c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/a-small-svg-pitfall-in-react-39cf09a3f66c">A Small SVG Pitfall in React</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Make a lucky wheel to solve your lunch dilemma by using react and react-spring]]></title>
            <link>https://frankie95.medium.com/make-a-lucky-wheel-to-solve-your-lunch-dilemma-by-using-react-and-react-spring-29b611a5aaed?source=rss-a7208dec0e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/29b611a5aaed</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[svg-animation]]></category>
            <category><![CDATA[react-spring]]></category>
            <category><![CDATA[svg]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[Frankie]]></dc:creator>
            <pubDate>Sun, 19 Jul 2020 09:57:52 GMT</pubDate>
            <atom:updated>2020-07-19T09:57:52.745Z</atom:updated>
            <content:encoded><![CDATA[<p>I’m a Libra. It is hard for me to make a decision especially at lunch time. Does it resonates to you? So I decided to make a lucky wheel to help me to choose a restaurant at lunch time. It will rotate base on your pressing time.</p><p>This is what we going to make:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*KnELgKiDMB-Vjl4sTYPwYw.gif" /></figure><p><strong>Step 1</strong>: Start with create-react-app and install react-spring, run the command below:</p><p>npx create-react-app lucky-wheel</p><p>npm i react-spring --save</p><p><strong>Step 2:</strong> Make a lucky wheel in App.js.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a638b2621abaad6bbb730d26ee0b4bb5/href">https://medium.com/media/a638b2621abaad6bbb730d26ee0b4bb5/href</a></iframe><p>We make a view box with height and width are 500px. So that we can easily calculate the center of circle is x:250 y:250 , svg is like a <strong>coordinate system with origin at top left.</strong></p><p>renderItems is a function that calculate the coordinate from the center(x:250,y:250) to the edge of big circle, so that we can draw a red line to separate each region equally and place the text between two red lines. It seemed deceptively simple, we need to convert polar coordinate to Cartesian coordinate and deal with radian and degree.</p><p>With the OFFSET value, we can make sure each time the starting point of the wheel is different. You can refresh the page and see it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*De2xTVxJTXYZkD9wpnyxrw.gif" /></figure><p><strong>Step 3: </strong>Create a pressing area, with an animation of charging the power, the color change from hotpink to red.</p><p>In App.css:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/217cb2e4144f6d5c482316f4ddb4bdc6/href">https://medium.com/media/217cb2e4144f6d5c482316f4ddb4bdc6/href</a></iframe><p>In App.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4096d52293317bdc1a438aa3252d62fc/href">https://medium.com/media/4096d52293317bdc1a438aa3252d62fc/href</a></iframe><p>We use react-spring library here, we give the initial state: width: &#39;0%&#39;, backgroundColor: &#39;hotpink&#39; and final state:width: &#39;100%&#39;, backgroundColor: &#39;red&#39; to set function, it will automatically generate the in-between value, addanimated.(html Tag) to the region that should have the animation and access the value by props from useSpring.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*Hm3ToLe89ym_HaETsG06fA.gif" /></figure><p><strong>Final Step: </strong>Pass the value to luck wheel and make it rotate.</p><p>In App.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f205ddf5eb69638d7010af76f232478/href">https://medium.com/media/1f205ddf5eb69638d7010af76f232478/href</a></iframe><p>We add a power and setPower to store the power from user, and pass the setPower to PressButton, the width in PressButton is equal topower (100 is max power and 0 is min power), we will pass the width to setPower when mouse up, once the power changed it will trigger the set function in App and make the wheel spin. Also, there is a accumulator(acc) to store the total power(total rotate degrees) and a map function that map 0 to 100 to a range of rotate degree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*KnELgKiDMB-Vjl4sTYPwYw.gif" /></figure><p>You can try it <a href="https://frankie95.github.io/lucky-wheel/">here</a>.</p><p>Full code is in <a href="https://github.com/frankie95/lucky-wheel">here</a>.</p><p>Thanks for watching. See you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29b611a5aaed" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Demo CSRF attack and Double-Submit Cookie defense with Express.js]]></title>
            <link>https://medium.com/swlh/demo-csrf-attack-and-double-submit-cookie-defense-with-express-js-16a99c0dfc8c?source=rss-a7208dec0e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/16a99c0dfc8c</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[web-security]]></category>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[csrf]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Frankie]]></dc:creator>
            <pubDate>Sun, 07 Jun 2020 11:29:50 GMT</pubDate>
            <atom:updated>2020-06-09T12:30:27.767Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>What is CSRF?</blockquote><blockquote>Cross-site request forgery (also known as CSRF) is a web security vulnerability that allows an attacker to induce users to perform actions that they do not intend to perform. It allows an attacker to partly circumvent the same origin policy, which is designed to prevent different websites from interfering with each other.</blockquote><p>Still don’t understand? Never mind, let’s see a demo.</p><h3>CSRF attack</h3><p>Create a file called index.js, copy and paste the code below. (Remember to install express, body-parser, cookie-parser.)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba315818350e8fef452f4dd6b6686804/href">https://medium.com/media/ba315818350e8fef452f4dd6b6686804/href</a></iframe><p>In index.html, we just make a simple form for user to input the account name that he/she wants to delete.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53dd5e59bf531569d53da602f0652066/href">https://medium.com/media/53dd5e59bf531569d53da602f0652066/href</a></iframe><p>Now we can run it by node index.js .</p><p>In index.js we created a web server on port 3000 and one route &quot;/&quot;, when we go to 127.0.0.1:3000/ , it will add a cookie named status and value is logged in to simulate user logged in. Then user can submit a form data to &quot;/&quot; to delete the account, we will check weather the account is logged in by checking the cookie, if the user is logged in, then it will perform the delete action, in this demo, we will simulate the delete action by console log the user input.</p><p>In web browser:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*dvGRBcZtrksDTaHlriXt_A.gif" /></figure><p>In terminal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/471/1*lwet8Ftnjw3kmgD5WZBj7A.jpeg" /></figure><p>Everything works fine. But… what if we make a HTTP request from other origin? Open a new tab and go to other website, in the browser console input below code to make a HTTP request to the express server.</p><pre>var xmlhttp = new XMLHttpRequest();<br>xmlhttp.open(&quot;POST&quot;, &quot;<a href="http://127.0.0.1:3000/">http://127.0.0.1:3000/</a>&quot;);<br>xmlhttp.setRequestHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);<br>xmlhttp.withCredentials = true;<br>xmlhttp.send(&quot;delete=Tom&#39;s account&quot;);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1wOqUKTnc2OOt3EC8TBtMw.jpeg" /></figure><p>It will show a error:</p><pre>Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at <a href="http://127.0.0.1:3000/">http://127.0.0.1:3000/</a>. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).</pre><p>Wow! Blocked. But in the terminal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*Bo6gvUS0NuKTaT89jFUGBQ.jpeg" /></figure><p>Actually, it blocked the response, not request. Therefore the server still received the request and performed the delete action. Imagine that if the above HTTP request java-script code is in a malicious web site, when client browses the malicious web site, the java-script code will run and perform actions that they do not intend to perform and they will not aware, since the <strong>cookie</strong> will be automatically sent with the rogue request. This is called CSRF attack.</p><h3>Double-Submit Cookie</h3><p>This is a method that client side set a random cookie value and submit it in the form, server side will compare the form value and the cookie value, if they are the same, then it will perform the action. Since malicious web sites cannot edit the cookie as they are not in the same origin. They have to guess the value, once the value is long enough, it is hard to crack.</p><p>Let’s implement it.</p><p>In index.js , we added the criteria of checking the _crsf value in line 18.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10de5796fc6a3f7a88829938ba31d564/href">https://medium.com/media/10de5796fc6a3f7a88829938ba31d564/href</a></iframe><p>In index.html, we added some java-script code that run on the client side to set the cookie _csrf to some random string and added a hidden field to contain the value of _csrf.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/519ec79b5329f29c18a08fb49be5293f/href">https://medium.com/media/519ec79b5329f29c18a08fb49be5293f/href</a></iframe><p>Restart the server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*ha7RuzzgpIUu0QgZcBg25Q.gif" /></figure><p>In terminal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/673/1*zpRS13irfwlYzN29JsA0AA.jpeg" /></figure><p>Let’s make HTTP request from other origin.</p><pre>var xmlhttp = new XMLHttpRequest();<br>xmlhttp.open(&quot;POST&quot;, &quot;<a href="http://127.0.0.1:3000/">http://127.0.0.1:3000/</a>&quot;);<br>xmlhttp.setRequestHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);<br>xmlhttp.withCredentials = true;<br>xmlhttp.send(&quot;delete=Tom&#39;s account&quot;);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1wOqUKTnc2OOt3EC8TBtMw.jpeg" /></figure><p>This time the terminal shows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*Q8YmAClKqHWhgt2V6Qht-w.jpeg" /></figure><p>Yeah!! It works!</p><p>Full code is in <a href="https://github.com/frankie95/csrf-test">here</a>.</p><p>Be care, this method is not invincible, cookie fixation can be done through Exploiting subdomains and Man-in-the-middle HTTP connections, read more:</p><p><a href="https://owasp.org/www-chapter-london/assets/slides/David_Johansson-Double_Defeat_of_Double-Submit_Cookie.pdf">https://owasp.org/www-chapter-london/assets/slides/David_Johansson-Double_Defeat_of_Double-Submit_Cookie.pdf</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=16a99c0dfc8c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/demo-csrf-attack-and-double-submit-cookie-defense-with-express-js-16a99c0dfc8c">Demo CSRF attack and Double-Submit Cookie defense with Express.js</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build a Multilingual Website in React using react-intl v4 in 2020 (1)]]></title>
            <link>https://frankie95.medium.com/build-a-multilingual-website-in-react-using-react-intl-v4-in-2020-b051035aae09?source=rss-a7208dec0e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/b051035aae09</guid>
            <category><![CDATA[react-intl]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Frankie]]></dc:creator>
            <pubDate>Mon, 01 Jun 2020 17:40:36 GMT</pubDate>
            <atom:updated>2020-06-01T17:44:34.170Z</atom:updated>
            <content:encoded><![CDATA[<p>I found that most of the resources about react-intl are outdated and the <a href="https://formatjs.io/docs/react-intl">docs</a> of react-intl is quite ambiguous. Therefore, I will demo some components and functions of react-intl after some trial and error to help other peoples who want to build a multilingual website.</p><p>We use create-react-app in this article. Let’s make a new project called multilingual-website :</p><p>npx create-react-app multilingual-website</p><p>First of all, we need to install the package by running command :</p><pre>npm i react-intl --save<br>npm install @material-ui/core</pre><p><em>We are using </em><em>material-ui for the ui component library.</em></p><p>Then, we can import IntlProvider component from react-intl and wrap</p><p>the react app with &lt;IntlProvider&gt; to enable internationalization.</p><p>In App.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/555d3eda3f50788a71e78cbd3488af2a/href">https://medium.com/media/555d3eda3f50788a71e78cbd3488af2a/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u925U0LnaCns6V2ralQLAg.jpeg" /><figcaption>run npm start and you will see this</figcaption></figure><p>In IntlProvider we can set some properties:</p><p>locale : we need to tell IntlProvider which locale is using.</p><p>messages : object of translated strings in different languages.</p><p>To look up for the user’s locale, we can use navigator.language and it will return user’s preferred locale.</p><p>In this demo, I restricted the locale should be zh or en.</p><p>At this moment, you click the button and nothing happened. We have to add the translated strings to the message and add function to the Button for switching languages. Then, we replace the plain text with react-intl component to retrieve the string for the corresponding language.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e125fde1c4da31c1d9a47aeb0f0b32f4/href">https://medium.com/media/e125fde1c4da31c1d9a47aeb0f0b32f4/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C3DdHij_hHfKUk4uqhQDYw.gif" /></figure><p>We use useState hook to store the locale and use Button to change it. Also, we created two versions of translated strings in const messages ,in &lt;IntlProvider/&gt; we give messages props with correct locale messages object. Then, pass the key of that string as id in &lt;FormattedMessage/&gt;.</p><p>Now, if we want to add a input field :</p><p>&lt;TextField id=”standard-basic” label=”username” /&gt;</p><p>we want the label show the correct text based on the locale, we should use injectIntl function, it will inject intl as props, then we can get the massages by calling intl.formatMessage({id:&quot;XXXXXXX&quot;}) :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0209077cf180441c177a2489ee3b1473/href">https://medium.com/media/0209077cf180441c177a2489ee3b1473/href</a></iframe><p>We can also make some variables in message and inject the value based on the user interaction, e.g. greeting, we add &#39;homepage.greeting&#39;: &#39;Hello {name}&#39; to the messages, we can now inject value to {name}. See the example code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4bb3f40139f7deaddda04a94b5f32d25/href">https://medium.com/media/4bb3f40139f7deaddda04a94b5f32d25/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J-NEZsuXLj760P_V7pXTCA.gif" /></figure><p>Tips: Remember to check the Safari browser, you may need to import extra Polyfill to make it work fine in Safari.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b051035aae09" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[(2)basic usage of useFormik hook with React Form]]></title>
            <link>https://frankie95.medium.com/basic-usage-of-useformik-hook-with-react-form-2-c8f44e272051?source=rss-a7208dec0e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/c8f44e272051</guid>
            <category><![CDATA[useformik]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react-forms]]></category>
            <category><![CDATA[formik]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Frankie]]></dc:creator>
            <pubDate>Fri, 15 May 2020 07:18:01 GMT</pubDate>
            <atom:updated>2020-05-30T11:42:37.116Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This is part 2 of the tutorial. Please read </em><a href="https://medium.com/@frankie95/basic-usage-of-useformik-hook-with-react-form-ca97c95e138c"><em>basic usage of useFormik hook with React Form(1)</em></a><em> first.</em></p><p>Let’s continue with the code in last tutorial.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4305f0159b42d3ce0fe9c1fc71536bf6/href">https://medium.com/media/4305f0159b42d3ce0fe9c1fc71536bf6/href</a></iframe><p>Now, we add a new validation for field lname, and see what will happen.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef69d6160eb917f519f151f7e5e0af3c/href">https://medium.com/media/ef69d6160eb917f519f151f7e5e0af3c/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/341/1*Do_JmrAVWqMQWmjvtIuKWw.gif" /></figure><p>When we just type in first name, the error of last name appeared. This is not what we want, we should check whether the input box has been touched, if the user has not yet touched the input box, we should not show the error.</p><p>In useFormik you can just add onBlur={formik.handleBlur} to the input field, then get the value by formik.touched.(input field name), it will be true if user has touched that input field. You can see the code below to understand how to use it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a6023651a6c56c3a7d86357aee8938a/href">https://medium.com/media/5a6023651a6c56c3a7d86357aee8938a/href</a></iframe><p>We need to add the name, onChange, value, onBlur, it becomes a little bit cumbersome, with formik,we can simply use one magic code: {...formik.getFieldProps(‘field name’)}, it will return 4 properties.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*cus_ane-HJpjLTkPhQVdwA.jpeg" /></figure><p>Below is the complete code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba1c0a9b70a40bd233b8b5ecff40ce80/href">https://medium.com/media/ba1c0a9b70a40bd233b8b5ecff40ce80/href</a></iframe><p>Be careful, when we work with radio input type, the position of {...formik.getFieldProps(‘field name’)}is <strong>important</strong>. If we place it after value=&quot;male&quot; in this example, since we do not set initial value,{...formik.getFieldProps(‘field name’)} will return value:&quot;&quot; and it will override our value=&quot;male&quot;, then the form will not work.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8f44e272051" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java Serializable interface for beginner]]></title>
            <link>https://frankie95.medium.com/java-serializable-interface-for-beginner-1b831924c3f4?source=rss-a7208dec0e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b831924c3f4</guid>
            <category><![CDATA[serializable]]></category>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[serialization]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[deserialization]]></category>
            <dc:creator><![CDATA[Frankie]]></dc:creator>
            <pubDate>Mon, 11 May 2020 17:46:42 GMT</pubDate>
            <atom:updated>2020-05-11T17:48:30.164Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable. — from Java docs.</blockquote><p>Quite vague. In a nutshell, sometime we want to share the state of a class instance,then we will store the class variables to e.g. a file, then restore it back to a class object, in this moment, we will use the Serializable interface.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iueIQxFQcDj2zf56" /><figcaption>Photo by <a href="https://unsplash.com/@markusspiske?utm_source=medium&amp;utm_medium=referral">Markus Spiske</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Let’s create a class object to understand it.</p><p>This is a Person object implements Serializable which has AGE, name, gender and bankNumber.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1090e17871139e8ded8a854c4f3ab3b/href">https://medium.com/media/f1090e17871139e8ded8a854c4f3ab3b/href</a></iframe><p>Then, we create the method to store the state of object instance and restore back to the object. From the code below, we are trying to store the state of object to D drive named person.txt. Then, we try to read person.txt and get back the state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83ff5d250abdde7a09d5ced4a1803572/href">https://medium.com/media/83ff5d250abdde7a09d5ced4a1803572/href</a></iframe><p>You can open the person.txt from the file explorer. There are some meaningless string in the file since the data stored in binary format and not for human read.</p><p>The out put is:</p><pre>serialize<br>deserialize<br>Person{name=&#39;billy&#39;, gender=&#39;male&#39;, bankNumber=&#39;<strong>null</strong>&#39;, AGE=&#39;20&#39;}</pre><p>We can find out that the bankNumber is null. Why?</p><p><strong>All variable(no matter </strong><strong>public or </strong><strong>private ) marked with </strong><strong>transient will not store the state.</strong></p><p>We find that the static variable AGE is not null, does it mean that we can store static variable? The answer is no and we will prove it.</p><p>Let’s comment out serializePerson(); and change the AGE value to “10”.</p><pre>in class Person</pre><pre>private static String AGE = &quot;10&quot;;</pre><pre>in public static void main</pre><pre>//serializePerson();<br>Person person = deserializePerson();<br>System.out.println(person.toString());</pre><p>Run the program again. As we already have person.txt file, the program will restore the value base on it, so, if the file stored static variable AGE&#39;s value, it should be 20 instead of 10.</p><p>The output is:</p><pre>deserialize<br>Person{name=&#39;null&#39;, gender=&#39;male&#39;, bankNumber=&#39;null&#39;, AGE=&#39;<strong>10</strong>&#39;}</pre><p>Now, we can conclude that Serializable will ignore the static variable and transient variable.</p><p>Now, consider a situation, we want to add a new variable address to Person. After that, we try to restore the data from person.txt.</p><pre>in class Person</pre><pre>private String address;</pre><pre>in public static void main</pre><pre>//serializePerson();<br>Person person = deserializePerson();<br>System.out.println(person.toString());</pre><p>An error occurred. It said the serialVersionUID has changed.</p><pre>Exception in thread &quot;main&quot; java.io.InvalidClassException: test.Person; local class incompatible: stream classdesc <strong>serialVersionUID</strong> = -5617285546907292941, local class <strong>serialVersionUID</strong> = 7380129921541072321<br>    at java.io.ObjectStreamClass.initNonProxy(Unknown Source)<br>    at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)<br>    at java.io.ObjectInputStream.readClassDesc(Unknown Source)<br>    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)<br>    at java.io.ObjectInputStream.readObject0(Unknown Source)<br>    at java.io.ObjectInputStream.readObject(Unknown Source)<br>    at java.io.ObjectInputStream.readObject(Unknown Source)<br>    at test.main.deserializePerson(main.java:33)<br>    at test.main.main(main.java:15)</pre><p>what is serialVersionUID?</p><blockquote>The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender&#39;s class, then deserialization will result in an <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/InvalidClassException.html">InvalidClassException</a>. A serializable class can declare its own serialVersionUID explicitly by declaring a field named serialVersionUID that must be static, final, and of type long.</blockquote><p>Since we do not explicitly declare serialVersionUID in Person. Java will automatically generate one for us. We added address, Java will generated another serialVersionUID which does not match the old one in person.txt.</p><p>Let’s add a serialVersionUID to Person.</p><pre>in class Person</pre><pre>private static final long serialVersionUID = 1L;</pre><pre>in public static void main</pre><pre>serializePerson();<br>Person person = deserializePerson();<br>System.out.println(person.toString());</pre><p>It will now generate a new person.txt with our declared serialVersionUID. You can add some new variables and try to restore the data from person.txt. Everything should work fine, of cause, for the newly added variables, person.txt does not have the data, so it should be default data.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b831924c3f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>