<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rahul Nayak</title>
    <description>The latest articles on DEV Community by Rahul Nayak (@pragyes31).</description>
    <link>https://dev.to/pragyes31</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%2F324543%2Fd340c753-4a33-4a6d-a9fe-e9f50e65f891.JPG</url>
      <title>DEV Community: Rahul Nayak</title>
      <link>https://dev.to/pragyes31</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pragyes31"/>
    <language>en</language>
    <item>
      <title>How to merge JS objects with common keys</title>
      <dc:creator>Rahul Nayak</dc:creator>
      <pubDate>Wed, 16 Jun 2021 06:53:07 +0000</pubDate>
      <link>https://dev.to/pragyes31/how-to-merge-js-objects-with-common-keys-45i3</link>
      <guid>https://dev.to/pragyes31/how-to-merge-js-objects-with-common-keys-45i3</guid>
      <description>&lt;p&gt;While working on a project, I came across a situation where I had to merge 2 objects with common keys. In this post, I'll show you a simple solution to merge the objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem statement:
&lt;/h3&gt;

&lt;p&gt;We have 2 objects(&lt;code&gt;originalObject&lt;/code&gt; and &lt;code&gt;objectToMerge&lt;/code&gt;) and each object has a few keys which are common in both the objects. If the key is common, we want to add the values from both the objects and store the updated value in &lt;code&gt;originalObject&lt;/code&gt;. If it isn't, then we want to add the new key to the &lt;code&gt;originalObject&lt;/code&gt;.&lt;br&gt;
Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let originalObject = {
  key1:1,
  key2:2,
  key3:3,
  key4:4
}

let objectToMerge = {
  key2:5,
  key6:6,
  key3:-7
}

// Output after merging

originalObject = {
 key1: 1,
 key2: 7, // 2+5 = 7
 key3:-4, // 3+(-7) = -4
 key4: 4,
 key6: 6
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let originalObject = {
  key1:1,
  key2:2,
  key3:3,
  key4:4
}

let objectToMerge = {
  key2:5,
  key6:6,
  key3:-7
}

let keys = Object.keys(objectToMerge)

keys.forEach((key) =&amp;gt; {
if(originalObject.hasOwnProperty(key)) {
   originalObject[key] += objectToMerge[key]
}
else {
  originalObject[key] = objectToMerge[key]
}
})

console.log(originalObject)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code explanation&lt;/strong&gt;&lt;br&gt;
I used the &lt;code&gt;keys&lt;/code&gt; method on &lt;code&gt;originalObject&lt;/code&gt; object to extract all the keys name. &lt;br&gt;
Next we will loop through these keys and check if each key is present in the &lt;code&gt;objectToMerge&lt;/code&gt; object. &lt;/p&gt;

&lt;p&gt;If the key is present in both the objects, then we will add the values of the two and store it in &lt;code&gt;originalObject&lt;/code&gt;. If not, we will create a new key in the &lt;code&gt;originalObject&lt;/code&gt; object. And that's it!!&lt;/p&gt;

&lt;p&gt;Feel free to play around with the code present &lt;a href="https://codesandbox.io/s/confident-hooks-xkwgc"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A lot of libraries have this logic inbuilt as an utility function but I wanted a simple Vanilla JS solution hence this approach. I have found one such solution in Lodash. You can find it &lt;a href="https://lodash.com/docs/4.17.15#mergeWith"&gt;here&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Let me know if you solved this any other way. Would love to hear more ways to solve this problem. &lt;/p&gt;

&lt;p&gt;Until next post. ta-da!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Animate Button text using only CSS</title>
      <dc:creator>Rahul Nayak</dc:creator>
      <pubDate>Sun, 11 Apr 2021 19:33:16 +0000</pubDate>
      <link>https://dev.to/pragyes31/animate-button-text-using-only-css-3dan</link>
      <guid>https://dev.to/pragyes31/animate-button-text-using-only-css-3dan</guid>
      <description>&lt;p&gt;Hello, Hola and Namaste!&lt;/p&gt;

&lt;p&gt;Today we are going to achieve what you see 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1608963039573%2FbjdKJE7PT.gif" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1608963039573%2FbjdKJE7PT.gif" alt="deepin-screen-recorder_Select area_20201226113539.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically, we would animate the button text to smoothly slide up and down and show a different text when one hovers over it.&lt;/p&gt;

&lt;p&gt;Without further ado, let's dive in. &lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Add the html structure
&lt;/h3&gt;

&lt;p&gt;Needless to say that we would need two different text values for button. &lt;/p&gt;

&lt;p&gt;The primary text(&lt;code&gt;New Blog&lt;/code&gt;) would hold the current information while the hidden text(&lt;code&gt;Read it now!&lt;/code&gt;) would hold the value shown on button hover. &lt;/p&gt;

&lt;p&gt;Here is the  &lt;a href="https://codepen.io/pragyesh31/pen/PoGONVb" rel="noopener noreferrer"&gt;code&lt;/a&gt;  so far:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="btn blog-button"&amp;gt;
  &amp;lt;div class="primary text"&amp;gt;New Blog&amp;lt;/div&amp;gt;
  &amp;lt;div class="secondary text"&amp;gt;Read it now!&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
.btn {
  width: 200px;
  height: 50px;
  background-color: #0fabbc;
  border-radius: 25px;
  color:#fff;
  font-size:18px;
  font-weight:bold;
  font-family: "Lucida Console", "Courier New", monospace;
}

&lt;/code&gt;&lt;/pre&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1608911982787%2F-AJ5ceAhG.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1608911982787%2F-AJ5ceAhG.png" alt="Screenshot from 2020-12-25 21-28-52.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Initial state (No Hover)
&lt;/h3&gt;

&lt;p&gt;Right now both the text values are stacked against each other in the tiny button box. Let us use the &lt;code&gt;position&lt;/code&gt; property to separate them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.btn {
  width: 180px;
  height: 45px;
  background-color: #0d7377;
  border-radius: 25px;
  position: relative;
}

.text {
  width: 180px;
  height: 45px;
  position: absolute;
}

.primary {
  top: 0px;
}

.secondary {
  top: 80px;
}
&lt;/code&gt;&lt;/pre&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1608912040413%2FjaYo3JY9C.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1608912040413%2FjaYo3JY9C.png" alt="Screenshot from 2020-12-25 21-30-05.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you use &lt;code&gt;position: absolute;&lt;/code&gt;, it positions itself relative to the nearest positioned parent. If you look at the above code and the image, we gave the &lt;code&gt;.btn&lt;/code&gt; the position of relative and positioned the children divs absolute. &lt;/p&gt;

&lt;p&gt;So now whenever you try to change the position of the children elements(&lt;strong&gt;&lt;code&gt;.secondary and .primary&lt;/code&gt;&lt;/strong&gt;), they would move with respect to the &lt;code&gt;.btn&lt;/code&gt; div and not body.&lt;/p&gt;

&lt;p&gt;### Step 3: Hover state &lt;/p&gt;

&lt;p&gt;If you look at the code above, you'd notice that we have given the &lt;code&gt;.secondary&lt;/code&gt; class a top value of 80px. That's our button text to be displayed on hover. When one hover over the button, the top property should be 0 so it sits inside the button box. &lt;/p&gt;

&lt;p&gt;Similarly at this point, we want the existing text("New Blog") to clear space for the secondary text so we would move it up by setting the top to -80px. Let's see it in action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.btn:hover .primary {
  top:-80px
}

.btn:hover .secondary {
  top:0px
}
&lt;/code&gt;&lt;/pre&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1608909711163%2FaN-9ccwsF.gif" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1608909711163%2FaN-9ccwsF.gif" alt="deepin-screen-recorder_Select area_20201225205114.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's the complete &lt;a href="https://codepen.io/pragyesh31/pen/KKgyzvv" rel="noopener noreferrer"&gt;code&lt;/a&gt;  used so far for your reference. But wait, isn't something missing here?&lt;/p&gt;

&lt;p&gt;Ohh yeah, the transition doesn't look very smooth. Plus why the other text is still visible. Let's fix it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Sprinkle some animation here and there
&lt;/h3&gt;

&lt;p&gt;Right now, the sliding up and down behaviour is instant. Let's use the CSS &lt;code&gt;transition&lt;/code&gt; property to control the speed of this slide up/down behaviour. &lt;/p&gt;

&lt;p&gt;Additionally, time to get rid of the unwanted text. We only want to show whatever text is inside the button box with the &lt;code&gt;.btn&lt;/code&gt; class and so we would use &lt;code&gt;overflow:hidden&lt;/code&gt; to hide that text. Let's see it in action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.btn {
  overflow:hidden;
}

.text {
  transition: top 0.7s;
  -moz-transition: top 0.7s; // Firefox
  -webkit-transition: top 0.7s; // Safari and Chrome 
  -o-transition: top 0.7s; // Opera 
  -ms-transition: top 0.7s; // Explorer
}
&lt;/code&gt;&lt;/pre&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1608963039573%2FbjdKJE7PT.gif" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1608963039573%2FbjdKJE7PT.gif" alt="deepin-screen-recorder_Select area_20201226113539.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code snippet is simplified to only show the code relevant to this section. You can find the final code  &lt;a href="https://codepen.io/pragyesh31/pen/KKgyZPL" rel="noopener noreferrer"&gt;here&lt;/a&gt;  for all the css styling that I have used. &lt;/p&gt;

&lt;p&gt;This brings me to the end of this article. I enjoyed animating this button and writing about it. I hope this brings the same joy to you when you read and try it on your website.&lt;/p&gt;

&lt;p&gt;My comment section is open to your feedback. If you want to chat over twitter, you can find me  &lt;a href="https://twitter.com/rhlnyk" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Concluding this article with the usual request: Like, share and comment. It would mean a lot to me. 🙂🙂&lt;/p&gt;

&lt;p&gt;Until Next time!&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>animation</category>
    </item>
    <item>
      <title>How to secure your website against Cookies theft and Cross Site Scripting</title>
      <dc:creator>Rahul Nayak</dc:creator>
      <pubDate>Sat, 10 Apr 2021 03:56:28 +0000</pubDate>
      <link>https://dev.to/pragyes31/how-to-secure-your-website-against-cookies-theft-and-cross-site-scripting-1644</link>
      <guid>https://dev.to/pragyes31/how-to-secure-your-website-against-cookies-theft-and-cross-site-scripting-1644</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;30% of web applications are vulnerable to XSS.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://www.acunetix.com/white-papers/acunetix-web-application-vulnerability-report-2019/"&gt;Acunetix vulnerability report 2019&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Our dependency on the Internet has increased multifold over the last decade. Today we use internet for anything and everything from buying products off the e-commerce platforms to transferring money across boundaries and much more. &lt;/p&gt;

&lt;p&gt;Needless to say, as Developers, it becomes important to save customers from online frauds. One security breach incident can impact your website's brand and reputation.&lt;/p&gt;

&lt;p&gt;In this article, we will learn about Cookies theft and Cross Site Scripting(XSS). Post that, we will learn how to secure our websites and user data against these attacks.&lt;/p&gt;

&lt;p&gt;Now, before we find ways to prevent cookies theft, let's understand what Cookies are and how they are used.  &lt;/p&gt;




&lt;h2&gt;
  
  
  What are Cookies?
&lt;/h2&gt;

&lt;p&gt;Cookies are a small piece of data that your computer stores when you visit a website. It is used to store your interactions with the website that you are on. Some of the primary uses are listed below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tracking your browsing history to serve targeted ads.&lt;/li&gt;
&lt;li&gt;Persistent login credentials&lt;/li&gt;
&lt;li&gt;Persistent shopping cart items in e-commerce sites&lt;/li&gt;
&lt;li&gt;Track unique visits on websites&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By now you must have got an idea about the gravity of the situation in case someone steals them. At the same time, you must be thinking why to save the my personal data in cookies at all if it is prone to thefts?&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use Cookies?
&lt;/h2&gt;

&lt;p&gt;Let's say you visit orderPizza.com to order a Pizza. The site asks you to login to your account and stores the login credentials in the cookies. &lt;/p&gt;

&lt;p&gt;When you navigate to another page on the website, for example, orderPizza.com/pineapple, the website will check the cookies to see if the user login information is stored in the computer. &lt;br&gt;
If it is, you will not have to re-authenticate yourself when you navigate to different parts of the website. Pretty convenient, right!&lt;/p&gt;
&lt;h2&gt;
  
  
  What is Cookies theft?
&lt;/h2&gt;

&lt;p&gt;As the name suggests, cookies theft is when a hacker gets hold of your personal cookies. &lt;/p&gt;

&lt;p&gt;In our example, you stored your login information for orderPizza.com website. &lt;br&gt;
Once they steal the cookies, they can load it in their browser and impersonate you. &lt;/p&gt;

&lt;p&gt;They can then login to orderPizza.com as you, use your credit card details to order as many pizza as they like to their address. &lt;/p&gt;

&lt;p&gt;While this example may look harmless, imagine if it was not orderPizza.com but your internet banking site! &lt;/p&gt;

&lt;p&gt;Cross Site scripting(XSS) is one of the most common way to steal cookies of your computer and yes, we are going to talk about it next.&lt;/p&gt;


&lt;h1&gt;
  
  
  How Cross Site scripting(XSS) is used to steal cookies
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Cross site scripting(XSS)&lt;/strong&gt; is a web security vulnerability which allows the hackers to execute malicious code inside user's browser. &lt;/p&gt;

&lt;p&gt;It bypasses the "same origin policy" by injecting the code into the server through &lt;strong&gt;user input fields&lt;/strong&gt;. Once the code is in the server and requested by the user, the browser is tricked into assuming that this malicious code is coming from the trusted web server of orderPizza.com when in reality, it is not.&lt;/p&gt;

&lt;p&gt;According to The Open Web Application Security Project® (&lt;a href="https://owasp.org/"&gt;OWASP&lt;/a&gt;) which is a non profit organization dedicated to web application security, XSS  is among the top 10 critical attacks on the internet. &lt;/p&gt;

&lt;p&gt;Let's see XSS in action to understand it better.&lt;/p&gt;

&lt;p&gt;Say you can add reviews for each type of Pizza on orderPizza.com website. Here's how the attack will unfold:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The attacker will slide a malicious code through the 'add reviews' input text field which would look something like this.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Pizza is &amp;lt;script&amp;gt;alert("you are hacked!")&amp;lt;/script&amp;gt; delicious
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;The server assuming that it is a review, will save it in the database and will serve it upon request.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let latestReview = document.getElementById('latest-review')
latestReview.innerHTML = Pizza is &amp;lt;script&amp;gt;alert("you are hacked!")&amp;lt;/script&amp;gt; delicious
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Another user when requests the reviews page, the web server will serve all the user reviews including the malicious one. This will activate the code inside the &lt;code&gt;script&lt;/code&gt; tag&lt;/li&gt;
&lt;li&gt;Once the code is activated, it can do whatever it wants. Based on what's inside the &lt;code&gt;script&lt;/code&gt; tags, it can steal cookies which may have your login credentials to the website.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the attacker obtains your cookies, they can load those cookies to their browser and pose themselves as you to carry out fraudulent activities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; HTML5 doesn't allow to execute &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag inserted with innerHTML. However the hackers have found a way to bypass this. See the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img src='wrongLocation.jpg' onError='alert("You're hacked")'&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;innerHTML will allow the above code to run and this way the hackers can infiltrate and steal data. &lt;/p&gt;




&lt;h2&gt;
  
  
  How to prevent cookies theft and XSS attacks
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Use secure https connection
&lt;/h3&gt;

&lt;p&gt;When a user opens a website, a web connection is established between user's browser and your web server. Data continues to flow to and from your web server for as long as the connection is open.&lt;/p&gt;

&lt;p&gt;If you use the &lt;code&gt;http&lt;/code&gt; connection for data transfer, the data is prone to theft because using http, data is transferred in plain text format. So if a hacker intercepts that data, they can read and use it for their advantage. This process of intercepting data is called &lt;strong&gt;packet sniffing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;On the other hand, &lt;code&gt;https&lt;/code&gt; is a &lt;strong&gt;secure http&lt;/strong&gt; connection which encrypts the data before sending it. So even if it is intercepted along the way, the hacker won't be able to decrypt it and make sense of the data. &lt;/p&gt;

&lt;h3&gt;
  
  
  2. Implement Content Security Policy(CSP)
&lt;/h3&gt;

&lt;p&gt;According to MDN:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Content Security Policy (CSP) is an added layer of security that helps to detect and mitigate certain types of attacks, including Cross Site Scripting (XSS) and data injection attacks. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;CSP allows you to create a set of rules that would control what browsers can do with your website. For example, you can direct the browser to run scripts from a specific location. &lt;br&gt;
This will block all the scripts with malicious intent and mitigate the XSS attacks to a great extent.&lt;/p&gt;

&lt;p&gt;As a server administrator, you can configure the policy by adding the Content Security Policy HTTP header to your website. &lt;/p&gt;

&lt;p&gt;Let's see a few examples to understand the rules a little better:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allow all scripts from only the origin website&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Content-Security-Policy: script-src 'self'&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allow all scripts from the origin and trsutedSite.com domain&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Content-Security-Policy: script-src 'self' https://trsutedSite.com&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There are other directives like &lt;code&gt;image-src&lt;/code&gt; or &lt;code&gt;style-src&lt;/code&gt; which specify permitted sources for loading images and css stylesheets respectively.&lt;/p&gt;

&lt;p&gt;For an in-depth understanding of the CSP, you can visit the MDN page &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP"&gt;here&lt;/a&gt; which explains it in great detail.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. Use textContent instead of innerHTML
&lt;/h3&gt;

&lt;p&gt;Let's look at the malicious code again which was added as a review to 'orderPizza.com'&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let latestReview = document.getElementById('latest-review')
latestReview.innerHTML = Pizza is &amp;lt;img src='wrongLocation.jpg' onError='alert("You're hacked")'&amp;gt; delicious!!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the hacker hits 'submit review' button, the review will be stored in the database and served on the screen as the new review. &lt;br&gt;
Now if you use &lt;code&gt;innerHTML&lt;/code&gt; to output the review, the user input will be parsed as HTML and for the above example you will get an alert box. &lt;/p&gt;

&lt;p&gt;Instead if you use &lt;code&gt;textContent&lt;/code&gt;, the user input will be parsed as a plain string and not HTML so the new review will be added with this content:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"Pizza is &amp;lt;img src='wrongLocation.jpg' onError='alert("You're hacked")'&amp;gt; delicious!!"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And that's how you secure your website! 💪💪💪&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Invest in Web Security tools
&lt;/h3&gt;

&lt;p&gt;Web security is a huge undertaking so if you do not feel very confident about managing it on your own, it is always a good idea to invest in a good web security tool that can protect your website against incoming attacks. &lt;/p&gt;

&lt;h3&gt;
  
  
  5. Use modern frameworks and update them regularly
&lt;/h3&gt;

&lt;p&gt;Using frameworks is an important part of development process now and rightly so. It offers an organized approach to writing and maintaining code, enhances application performance, gives out of the box functionality and the list goes on.&lt;/p&gt;

&lt;p&gt;But it doesn't stop there. Modern frameworks like React, Angular for front end web development also offer rich security measures which prevents the websites from malicious attacks to a great extent. &lt;/p&gt;

&lt;p&gt;They come with a built in mechanism to detect XSS attacks for example and sanitize data before it is stored in server. The implementation and security features may differ between frameworks but they do the job just fine.&lt;/p&gt;

&lt;p&gt;That's why it becomes important that you use well supported frameworks during the development process and make sure to update them on a periodic basis. The teams developing these frameworks update the packages on a regular basis to find loopholes or backdoor entries and build stronger security , among other things.&lt;/p&gt;




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

&lt;p&gt;Cross Site scripting(XSS) attacks are major web security vulnerabilities that bypasses the same origin policy rule to inject malicious code into your website. These attacks poses a major risk to user's personal information, authentication details which is generally stored in browser/computer cookies.&lt;/p&gt;

&lt;p&gt;Using https secure connection, having a strong Content Security Policy in place alongside other measures can mitigate these risks and make your website more secure than ever.&lt;/p&gt;




&lt;p&gt;And this brings me to the end of the article. &lt;/p&gt;

&lt;p&gt;I hope this article helps strengthen your website security further. As always my comment section is open for feedbacks. Keep them coming. 🤗🤗&lt;br&gt;
I can be found on &lt;a href="https://in.linkedin.com/in/nayak-rahul"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/RhlNyk"&gt;Twitter&lt;/a&gt; if you want to connect. 🙂&lt;/p&gt;

&lt;p&gt;Until next time! ✌️✌️&lt;/p&gt;

&lt;p&gt;Photo credit goes to &lt;a href="https://unsplash.com/@flyd2069?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;FLY:D&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/security?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>security</category>
      <category>javascript</category>
      <category>html</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Front-End-Snippets E01: Is window and document object the same?</title>
      <dc:creator>Rahul Nayak</dc:creator>
      <pubDate>Sat, 28 Mar 2020 18:43:17 +0000</pubDate>
      <link>https://dev.to/pragyes31/front-end-snippets-e01-is-window-and-document-object-the-same-4jni</link>
      <guid>https://dev.to/pragyes31/front-end-snippets-e01-is-window-and-document-object-the-same-4jni</guid>
      <description>&lt;p&gt;For a very long time, the most basic and important Front end web development concepts eluded me and scared me away to a point where I started to skip these concepts and hide my head in the sand. All these concepts came to haunt me later on when I went to web developer interviews. The interviews were an eye-opener and I realized there is so much that I do not know yet.&lt;/p&gt;

&lt;p&gt;And this brings me and you to this first post of a larger series where I would share snippets of concepts used in Front end development process. These snippets are short, concise posts that will hopefully get you started with some of the most confusing parts of Front end development.&lt;/p&gt;

&lt;p&gt;Starting this series with this week’s topic: &lt;strong&gt;Is window and document object the same? If not, how are they different?&lt;/strong&gt;&lt;br&gt;
Let’s dive in.&lt;/p&gt;
&lt;h1&gt;
  
  
  Window Object
&lt;/h1&gt;

&lt;p&gt;Window is basically an object which is created when you run JavaScript in a browser.&lt;br&gt;
But what does this window object represent? It represents the current window of the browser. If you open 10 tabs, each tab would have its own window object.&lt;/p&gt;

&lt;p&gt;Now let’s see it in action. Open your developer console, type window and hit enter. You’d get something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, parent: Window, …}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expand the object and you’d find a lot of familiar functions that you use on a more frequent basis like setTimeout or setInterval. The window object shares all of its properties globally which means you do not need to type window.setTimeout to use it. Simply calling setTimeout with the required arguments would do the job.&lt;/p&gt;

&lt;p&gt;Now, this window object does not belong to JavaScript, it belongs to the browser so it would contain all the web APIs that you can leverage. Navigation, setTimeout, setInterval, history, audio, video to name a few.&lt;/p&gt;

&lt;h1&gt;
  
  
  Document
&lt;/h1&gt;

&lt;p&gt;The Document object is one of the properties of a window object. The Document is what renders on the screen. If you crack open the document object, you’d find the entire Html tree that you see on the webpage in the current window.&lt;/p&gt;

&lt;p&gt;Open the console again and run window.document and you can see the entire Html tree for the webpage you are on. Here is a screenshot from the dev.to homepage:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fapJTwCp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/pragyes31/images-repo/master/window_document_dev_to.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fapJTwCp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/pragyes31/images-repo/master/window_document_dev_to.png" alt="output of window.document command in browser console" title="output of window.document command in browser console"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty neat stuff right! And this is all I have for this post. Tune in for the upcoming posts.&lt;/p&gt;

&lt;p&gt;Before I go, shower some love if the post helped you in any way, share the feedback in the comments section and if there are any web development related concepts that should be demystified, let me know and I’ll write about it in the future.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
