<?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: KamilPabinS</title>
    <description>The latest articles on DEV Community by KamilPabinS (@kamilpabins).</description>
    <link>https://dev.to/kamilpabins</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%2F555432%2F781fb60a-ac16-4ad9-95bb-423bef8432f2.png</url>
      <title>DEV Community: KamilPabinS</title>
      <link>https://dev.to/kamilpabins</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kamilpabins"/>
    <language>en</language>
    <item>
      <title>Introduction to Cross-site scripting (XSS)</title>
      <dc:creator>KamilPabinS</dc:creator>
      <pubDate>Thu, 07 Jan 2021 12:36:11 +0000</pubDate>
      <link>https://dev.to/sonalake/introduction-to-cross-site-scripting-xss-5a14</link>
      <guid>https://dev.to/sonalake/introduction-to-cross-site-scripting-xss-5a14</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;As of 2020, many might think that Cross-site Scripting (XSS) vulnerabilities are well explored and that the vast majority of them are addressed by frameworks we use. However, numerous examples from this year alone show XSS remains an issue, with possibly the most famous XSS vulnerability of the year being &lt;a href="https://www.youtube.com/watch?v=aCexqB9qi70"&gt;&lt;strong&gt;found in Google Sheets&lt;/strong&gt;&lt;/a&gt;. In this article, I will take you through the basics of Cross-site Scripting (XSS), focusing on the most common types of XSS attacks and the various ways applications are exposed.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;That’s &lt;del&gt;not&lt;/del&gt; my code!&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;XSS is probably one of the most known web application vulnerabilities. At a high-level, it involves injecting malicious code into a 3rd party web page. That malicious code will later be executed when the page is visited by users. Going a little deeper, XSS vulnerabilities can be divided into three main families, each with its own way of compromising an application. Even though cases in each group share similarities, each is unique in its own way. Let’s walk through each family of vulnerability by showing a simple example, and discussing how you can protect your application against such attacks.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Reflected XSS&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A reflected XSS is a specific type of XSS whose malicious script bounces off of another website to the victim’s browser. It is passed in the query, typically, in the URL. It makes exploitation as easy as tricking a user to click on a link.&lt;/p&gt;

&lt;p&gt;In 2017 a hacker managed to implement reflected XSS in Google Docs Awarded him a bounty for his trouble!&lt;/p&gt;

&lt;p&gt;To simulate how the hacker did this, let’s imagine we have an online shop, and one of the features is search for products by phrase. This search bar might look like one shown in Figure 1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9cqRULFE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-1.-Online-shop-search-bar.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9cqRULFE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-1.-Online-shop-search-bar.jpg" alt="Online shop search bar" width="725" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Figure 1. Online shop search bar&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When we search for specific phrases, we may not get any results (Figure 2). Notice that the product_name is part of the URL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c9GbdjoC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-2.-Searching-for-product-in-online-shop.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c9GbdjoC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-2.-Searching-for-product-in-online-shop.png" alt="Searching for product in online shop" width="880" height="103"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Figure 2. Searching for product in online shop&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Next, let’s try searching for a phrase with a HTML tag, for example &lt;strong&gt;&lt;b&gt;&lt;/b&gt;&lt;/strong&gt;. This time, when we get a response, everything after our search phrase is in bold! This might indicate that the application is vulnerable to XSS attacks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q3pJED_S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-3.-Injecting-b-tag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q3pJED_S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-3.-Injecting-b-tag.png" alt="Injecting b tag" width="880" height="102"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Figure 3. Injecting b tag&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let’s do one last test, this time we inject JavaScript code &lt;code&gt;&amp;lt;script&amp;gt;alert(1)&amp;lt;script/&amp;gt;&lt;/code&gt;. This time when we hit enter, alert popup shows. This confirms that this page is vulnerable to the type of XSS attack known as reflected XSS. What we searched for was &lt;em&gt;reflected&lt;/em&gt; by the server (see Figure 4).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tqMI9jLq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-4.-Successful-injection-of-script-tag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tqMI9jLq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-4.-Successful-injection-of-script-tag.png" alt="Successful injection of script tag" width="880" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Figure 4. Successful injection of script tag&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Attackers are not limited to HTTP GET, they can also inject code inside tag event methods or element class names.&lt;/p&gt;

&lt;p&gt;So how do you defend against this kind of attack? Simple, you must URL encode all user input. These are what’s known as HTML entities and are replaced as follows:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;

&lt;thead&gt;

&lt;tr&gt;

&lt;th&gt;HTML entity&lt;/th&gt;

&lt;th&gt;replacement&lt;/th&gt;

&lt;/tr&gt;

&lt;/thead&gt;

&lt;tbody&gt;

&lt;tr&gt;

&lt;td&gt;“&lt;/td&gt;

&lt;td&gt;

&amp;amp;quot

&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td&gt;‘&lt;/td&gt;

&lt;td&gt;

&amp;amp; #39

&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td&gt;&amp;lt;&lt;/td&gt;

&lt;td&gt;

&amp;amp;lt

&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td&gt;&amp;gt;&lt;/td&gt;

&lt;td&gt;

&amp;amp;gt

&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td&gt;&amp;amp;&lt;/td&gt;

&lt;td&gt;

&amp;amp;amp

&lt;/td&gt;

&lt;/tr&gt;

&lt;/tbody&gt;

&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Table 1. HTML entities and its replacements&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;What’s shown here is just the tip of the iceberg. For more detail, I encourage you to read the &lt;a href="https://sklep.sekurak.pl/product/view?id=1"&gt;&lt;strong&gt;book written by Securitum&lt;/strong&gt;&lt;/a&gt; (in Polish).&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Stored XSS&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This vulnerability was discovered in Twitter in 2018 and &lt;a href="https://www.youtube.com/watch?v=zv0kZKC6GAM"&gt;&lt;strong&gt;caused a massive tweet storm&lt;/strong&gt;&lt;/a&gt;. The text input field wasn’t secured against XSS resulting in the possibility of compromising the retweet button.&lt;/p&gt;

&lt;p&gt;In this example, let’s take the same page for searching for products, but this time the seller has an option to upload a description of a product, using HTML tags.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X9sDBCKy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-6.-Product-page.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X9sDBCKy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-6.-Product-page.png" alt="Product page" width="664" height="274"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Figure 5. Product page&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This allows the seller to use formatting tags so description looks more attractive to site visitors. This description is taken as raw input and stored inside a database, so every time a client visits the product page, it will be rendered.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7UunkdNC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-7.-Product-description-updated-with-b-tag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7UunkdNC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-7.-Product-description-updated-with-b-tag.png" alt="Product description updated with b tag" width="648" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Figure 6. Product description updated with b tag&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;But what happens when we inject a script from the previous example as part of the description? When a client visits such a site, the JavaScript code is executed and a popup is displayed. Furthermore, this code is executed in the context of the currently logged-in user!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kWQVyKf0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-8.-Injected-javascript-is-executed-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kWQVyKf0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-8.-Injected-javascript-is-executed-1.png" alt="Injected javascript is executed" width="880" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Figure 7. Injected JavaScript is executed&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XEglEMHp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-9.-After-javascript-execution-rest-of-the-page-is-rendered.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XEglEMHp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-9.-After-javascript-execution-rest-of-the-page-is-rendered.png" alt="After javascript execution rest of the page is rendered" width="668" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Figure 8. After JavaScript execution rest of the page is rendered&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This could lead to stealing authentication tokens, or in this case an attacker could inject code which automatically buys a product the same second you visit the page. This example is highly exaggerated, but this is just basics, in real-life applications, there could be input fields for names, or help questions for password recovery, which is vulnerable to XSS. So how do you avoid these attacks? All rules from Reflected XSS apply here. If you want to allow users to use some set of HTML tags, you could use &lt;em&gt;sanitizers&lt;/em&gt; such as DOMPurify which will automatically get rid of unwanted tags.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;DOM Based XSS&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The two types of XSS shown earlier were based on injecting new HTML elements into a page. There is a third way of exploiting XSS by using already existing javascript code.&lt;/p&gt;

&lt;p&gt;Let’s look at the online shop application as an example. We’ve modified the design and now offer an input box for adding a URL to an image that will be added somewhere in a page. Users provide a URL, click the preview button, and the image shows in the product description.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s6j3fkmG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-10.-Image-preview.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s6j3fkmG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-10.-Image-preview.png" alt="Image preview" width="880" height="583"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Figure 9. Image preview&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;What will happen if instead of URL we provide such string: &lt;code&gt;x onerror=alert(1)&lt;/code&gt;? When we submit our new URL the JavaScript code is executed! But how does this happen? When the user clicks the button, the application finds the div container and using its .innerHTML method adds an image tag with URL provided by the user. The renderer doesn’t find an image at the URL, so the onerror method of img is executed, thus invoking XSS code.&lt;/p&gt;

&lt;p&gt;This is one of the examples where JavaScript code can be evaluated without injecting any new HTML tags. Another example could be eval function or setTimeout function. The best protection is to not use any of these mechanisms, and if you really need to use one, consider using a sanitiser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nee4SsCY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-11.-XSS-injection-via-img-src-attribute.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nee4SsCY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sonalake.com/wp-content/uploads/2020/09/Pic-11.-XSS-injection-via-img-src-attribute.png" alt="XSS injection via img src attribute" width="880" height="184"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Figure 10. XSS injection via img src attribute&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;All three examples shown above are highly exaggerated, but its point was to show how each vulnerability works. If you are interested in software security, once again, I highly recommend the book written by Securitum. In the next section, I will describe my experience with software security and my thought process in exploiting vulnerabilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Summary&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Modern front-end frameworks lessen the risk of developing applications that are susceptible to XSS attacks. However, as you can see from  the &lt;strong&gt;&lt;a href="https://www.youtube.com/watch?v=lG7U3fuNw3A"&gt;Google&lt;/a&gt;&lt;/strong&gt; and  &lt;strong&gt;&lt;a href="https://www.youtube.com/watch?v=zv0kZKC6GAM"&gt;Twitter&lt;/a&gt;&lt;/strong&gt; stories mentioned above, the risk still exists. As developers, we must be aware of these problems waiting for us and to broaden our knowledge. I encourage you to read the book mentioned above and to engage with various online courses and challenges that will build to your understanding of cybersecurity.&lt;/p&gt;

</description>
      <category>security</category>
    </item>
  </channel>
</rss>
