<?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: Marcin Hoppe</title>
    <description>The latest articles on DEV Community by Marcin Hoppe (@marcin_hoppe).</description>
    <link>https://dev.to/marcin_hoppe</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%2F421380%2F1fcd2047-b43e-4d63-a3f5-75a65ede3369.jpg</url>
      <title>DEV Community: Marcin Hoppe</title>
      <link>https://dev.to/marcin_hoppe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marcin_hoppe"/>
    <language>en</language>
    <item>
      <title>Abusing the Type System</title>
      <dc:creator>Marcin Hoppe</dc:creator>
      <pubDate>Tue, 15 Sep 2020 07:14:39 +0000</pubDate>
      <link>https://dev.to/marcin_hoppe/abusing-the-type-system-5cjn</link>
      <guid>https://dev.to/marcin_hoppe/abusing-the-type-system-5cjn</guid>
      <description>&lt;p&gt;I learned to write computer programs before JavaScript was created. The languages used in schools back then were mainly C and Pascal. They taught me that each variable has a specific type, such as integer or string, and that this type determines the operations that can be performed on a variable.&lt;/p&gt;

&lt;p&gt;JavaScript is a bit different.&lt;/p&gt;

&lt;h1&gt;
  
  
  Types
&lt;/h1&gt;

&lt;p&gt;JavaScript also has types. Variables can refer to numbers, strings, Boolean values, objects, symbols, and special values such as &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dynamic typing
&lt;/h2&gt;

&lt;p&gt;Unlike C and Pascal, JavaScript variables can hold values of different types throughout their lifetime. A variable can be a number in one execution scenario and a string in another one. This makes it difficult to analyze how the program works just by reading its source code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Weak typing
&lt;/h2&gt;

&lt;p&gt;Operators work on values. For example, the &lt;code&gt;+&lt;/code&gt; operator adds two numbers together or concatenates two strings. In C and Pascal, you cannot add a number to a string. This operation is undefined, and you need to convert one of the variables to a different type.&lt;/p&gt;

&lt;p&gt;JavaScript will do its best to convert the operands implicitly, often in surprising ways.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparing objects of different types
&lt;/h2&gt;

&lt;p&gt;JavaScript has two comparison operators:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strict comparison&lt;/strong&gt; (&lt;code&gt;===&lt;/code&gt;) compares both the value and the type. If the compared values have different types, it will return &lt;code&gt;false&lt;/code&gt;. This is what we would intuitively expect from a comparison operator.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Loose comparison&lt;/strong&gt; (&lt;code&gt;==&lt;/code&gt;) tries to automatically convert the operands to a common type to make the comparison possible. The rules of the conversions are complex and can be confusing for newcomers. Who would expect that the special value &lt;code&gt;null&lt;/code&gt; can be equal to another special value &lt;code&gt;undefined&lt;/code&gt;?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both dynamic and weak typing allow JavaScript programs to be very flexible and succinct, but they may also lead to security problems.&lt;/p&gt;

&lt;h1&gt;
  
  
  Searching based on dynamic criteria
&lt;/h1&gt;

&lt;p&gt;The dynamic nature of JavaScript makes it possible to implement algorithms that work on different types of data, including objects with different properties.&lt;/p&gt;

&lt;p&gt;Let’s try to implement an HTTP endpoint that allows searching for objects in an array based on an arbitrary field and value and see how the type system can help us make the code as generic as possible. This will help us reuse it for different types of objects, and different types of search fields.&lt;/p&gt;

&lt;p&gt;Our sample will use the &lt;a href="https://expressjs.com/"&gt;Express framework&lt;/a&gt; to deal with details of handling HTTP requests but you don’t need to know Express in-depth to understand the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Search example
&lt;/h2&gt;

&lt;p&gt;In our example, we will search the array of objects representing users. The search parameters will be passed as query string parameters. The callers will pass an object property name in the &lt;code&gt;field&lt;/code&gt; parameter, and the search value in the &lt;code&gt;value&lt;/code&gt; parameter. This way one endpoint can support multiple different search criteria.&lt;/p&gt;

&lt;p&gt;The sample HTTP request and response could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET /profile?field=email&amp;amp;value=joe%40wiredbraincoffee.com HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Accept: */*
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 120
Connection: keep-alive

[{"email":"joe@wiredbraincoffee.com","password":"coldbrew","address":"1235 Wired Brain Blvd\r\nAwesome City, MM 55555"}]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Handler
&lt;/h2&gt;

&lt;p&gt;The HTTP handler code is quite generic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;readProfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Get search params&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;field&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getParams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;field&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="c1"&gt;// Find user(s)&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;field&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;First, we import the &lt;code&gt;users&lt;/code&gt; array from a separate module. The &lt;code&gt;readProfile&lt;/code&gt; function implements the search algorithm and conforms to Express conventions of taking the HTTP request and response objects as parameters.&lt;/p&gt;

&lt;p&gt;Here is where the fun begins: we grab the values of &lt;code&gt;field&lt;/code&gt; and &lt;code&gt;value&lt;/code&gt; query string parameters and use those values to search the &lt;code&gt;users&lt;/code&gt; array to find objects that have the property stored in the &lt;code&gt;field&lt;/code&gt; variable with the value equal to the value variable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Utility functions
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;readProfile&lt;/code&gt; implementation looks simple, but the bulk of the work happens in the &lt;code&gt;filter&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Return items where a field has specific value&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;field&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;field&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;filter&lt;/code&gt; function iterates over each element of the array and uses the bracket notation to retrieve the object property by name. The algorithm uses the loose comparison operator to compare object property value to the search criteria provided by the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Retrieve array of parameters from the query string&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getParams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;qs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;qs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
            &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;qs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
            &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;getParams&lt;/code&gt; function streamlines retrieval of search parameters from the query string. It takes an array of parameter names as an argument and iterates over it. For each parameter, it checks if it is present in the query string and adds it to the results array. If the requested parameter is not in the query string, it adds &lt;code&gt;null&lt;/code&gt; instead. &lt;code&gt;null&lt;/code&gt; is a special JavaScript value used to denote missing data.&lt;/p&gt;

&lt;p&gt;The resulting code is short and can easily be reused to implement search over other data sets, and based on criteria provided by the caller at runtime.&lt;/p&gt;

&lt;p&gt;It also has a security flaw.&lt;/p&gt;

&lt;h1&gt;
  
  
  Abusing loose comparison
&lt;/h1&gt;

&lt;p&gt;One of the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#:~:text=Loose%20equality%20using%20%3D%3D,exactly%20as%20%3D%3D%3D%20performs%20it."&gt;surprising rules&lt;/a&gt; that the loose comparison operator uses to compare values of different types is the one that says that &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt; are equal, while the strict comparison algorithm treats those two values as different.&lt;br&gt;
Let’s take one more look at the comparison in the filter function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;field&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we were able to force one operand to be always &lt;code&gt;null&lt;/code&gt;, and the other to always be &lt;code&gt;undefined&lt;/code&gt;, the comparison would always return true. Our HTTP endpoint would return the entire content of the users array, disclosing sensitive information about all the users of our application.&lt;/p&gt;

&lt;p&gt;How can we do that?&lt;/p&gt;

&lt;h2&gt;
  
  
  Attack payload
&lt;/h2&gt;

&lt;p&gt;The right-hand side of the comparison is a value returned by the &lt;code&gt;getParams&lt;/code&gt; function. We can for this value to be &lt;code&gt;null&lt;/code&gt; by… omitting it from the query string altogether.&lt;/p&gt;

&lt;p&gt;Now we need a way to get the left-hand side to always return &lt;code&gt;undefined&lt;/code&gt;. &lt;code&gt;undefined&lt;/code&gt; is a special value that JavaScript uses for variables and object properties that have not been written to. If the field variable referred to a property that does not exist, the entire left-hand side of the comparison would always return &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We do not always know what properties exist on objects. With a little bit of trial and error, it should not be difficult to find a value that is very unlikely to be a real property name.&lt;/p&gt;

&lt;p&gt;A successful attack could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET /profile?field=doesnotexist HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Accept: */*
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 364
Connection: keep-alive

[{"email":"janet@wiredbraincoffee.com","password":"coldbrew","address":"1234 Wired Brain Blvd\r\nAwesome City, MM 55555"},{"email":"joe@wiredbraincoffee.com","password":"coldbrew","address":"1235 Wired Brain Blvd\r\nAwesome City, MM 55555"},{"email":"michael@wiredbraincoffee.com","password":"coldbrew","address":"1236 Wired Brain Blvd\r\nAwesome City, MM 55555"}]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  The fix
&lt;/h1&gt;

&lt;p&gt;The root cause of the vulnerability is not difficult to fix. The &lt;code&gt;===&lt;/code&gt; operator will treat &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;null&lt;/code&gt; as different values. The comparison will always return false and the endpoint will not return any data from the &lt;code&gt;users&lt;/code&gt; array, as expected.&lt;/p&gt;

&lt;p&gt;This simple change fixed the vulnerability but there is more than we can do.&lt;/p&gt;

&lt;h2&gt;
  
  
  A more robust fix
&lt;/h2&gt;

&lt;p&gt;The vulnerability was exploitable because of the loose comparison and the fact that the attacker could omit the value parameter. Instead of returning an error, the readProfile function was executed with corrupt input data.&lt;br&gt;
A more complete fix uses the &lt;code&gt;===&lt;/code&gt; operator but also adds more strict input validation. Our endpoint should return HTTP 400 response code when query string parameters are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Missing&lt;/strong&gt;. Omitting a parameter can lead to unexpected code behavior. The dynamic and weak typing make our program work without errors, even if it does something we did not expect it to do.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Invalid&lt;/strong&gt;. We also need to validate if the values are within the expected range. In our example, we should do it for the &lt;code&gt;field&lt;/code&gt; parameter: we know what properties objects from the users array have, and there is no reason to allow other values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will leave adding this input validation logic as an exercise for… you, dear reader. Have fun!&lt;/p&gt;

&lt;h1&gt;
  
  
  What's next?
&lt;/h1&gt;

&lt;p&gt;The next post in this series will explain how using certain unsafe functions can allow attackers to execute their code within our applications.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>security</category>
    </item>
    <item>
      <title>How to Think About JavaScript Security</title>
      <dc:creator>Marcin Hoppe</dc:creator>
      <pubDate>Thu, 02 Jul 2020 13:31:38 +0000</pubDate>
      <link>https://dev.to/marcin_hoppe/how-to-think-about-javascript-security-1f54</link>
      <guid>https://dev.to/marcin_hoppe/how-to-think-about-javascript-security-1f54</guid>
      <description>&lt;p&gt;JavaScript has no security model. The runtime environments do. This post is a primer on how to think about JavaScript code security in Web browsers and Node.js.&lt;/p&gt;

&lt;h1&gt;
  
  
  How Browsers Execute JavaScript Code?
&lt;/h1&gt;

&lt;p&gt;JavaScript was created to add interactivity to HTML pages. Web browsers were the first runtime environment for JavaScript code.&lt;/p&gt;

&lt;p&gt;When the user visits a Web page, the browser downloads the HTML code of that page and parses it to create the Document Object Model (DOM). The HTML contains information about other assets that need to be downloaded to render the page to the user. This includes stylesheets (CSS), images, other documents to display in frames, and many more.&lt;/p&gt;

&lt;p&gt;The type of asset we are most interested in here is JavaScript code. It is also downloaded by the browser from locations referenced in the HTML.&lt;/p&gt;

&lt;h2&gt;
  
  
  Same-Origin Policy
&lt;/h2&gt;

&lt;p&gt;Users can simultaneously visit many pages in tabs or separate browser windows. JavaScript code downloaded from multiple different sites is executed in the same browser.&lt;/p&gt;

&lt;p&gt;One of those sites could be infected or operated by an attacker. Is this a risk? Could malicious code compromise the machine or steal data from other sites the user is browsing?&lt;/p&gt;

&lt;p&gt;Browsers protect against this. Each Web site executes JavaScript code in a sandbox. Code downloaded from one Web site cannot read or write data from another site. It also cannot call functions or methods across different sites.&lt;/p&gt;

&lt;p&gt;This is called the &lt;a href="https://web.dev/same-origin-policy/"&gt;Same-origin policy&lt;/a&gt; (SOP) and it is one of the most fundamental security policies on the Web.&lt;/p&gt;

&lt;h2&gt;
  
  
  Protecting Code Integrity
&lt;/h2&gt;

&lt;p&gt;Attackers could breach the SOP through the injection of malicious code at the network level, making the injected code appear as coming from the legitimate site. Browsers use the &lt;a href="https://web.dev/why-https-matters/"&gt;secure HTTPS protocol&lt;/a&gt; to ensure JavaScript code is downloaded from the legitimate server and that the code is not tampered with in transit.&lt;/p&gt;

&lt;p&gt;JavaScript is often distributed using Content Delivery Networks (CDN). Attackers capable of injecting content into the CDN could also compromise the SOP. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity"&gt;Subresource Integrity&lt;/a&gt; (SRI) provides an additional level of protection that allows HTML code to be cryptographically bound to JavaScript code to prevent this.&lt;/p&gt;

&lt;h1&gt;
  
  
  Sandboxing
&lt;/h1&gt;

&lt;p&gt;Sandboxing is difficult to implement. Browsers use isolation mechanisms provided by the hardware and the operating system. JavaScript code from different sites is executed in separate processes.&lt;/p&gt;

&lt;p&gt;The code in a sandbox is restricted in what it can do. It cannot directly access devices such as webcams or microphones. The filesystem and the local network are also not directly available.&lt;/p&gt;

&lt;p&gt;JavaScript can use those resources only through very limited APIs. This reduces the attack surface. It also allows the browser to always ask the user for explicit permission before uploading files, capturing the webcam, or listening to the user’s microphone.&lt;/p&gt;

&lt;h1&gt;
  
  
  Node.js vs Browsers
&lt;/h1&gt;

&lt;p&gt;Node.js is a runtime environment for JavaScript based on the V8 engine built for the Google Chrome browser. It allows JavaScript code to be executed outside of the browser, typically on servers.&lt;/p&gt;

&lt;p&gt;Node.js does not use the browser sandbox to run JavaScript. Security properties of both execution environments are different:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Origin&lt;/strong&gt;. Browsers download the code and Node.js loads the code from local files like other popular programming languages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Trust&lt;/strong&gt;. Browsers treat the code as untrusted and Node.js treats the code with full trust.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Permissions&lt;/strong&gt;. Browsers restrict capabilities the code has access to and Node.js grants all the privileges of the operating system account. This includes access to devices, files, and the local network.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Impact on Security
&lt;/h1&gt;

&lt;p&gt;The same JavaScript script or module can be executed in the browser or Node.js. Potential attacks may be different in both environments. The impact of successful exploits may be drastically different. It is very hard to reason about the security of JavaScript code without a specific execution environment in mind.&lt;/p&gt;

&lt;h2&gt;
  
  
  Browsers
&lt;/h2&gt;

&lt;p&gt;A successful attack on JavaScript code running in the browser impacts a single user. The impact is limited to what the sandbox, browser APIs, and the user’s explicit consent allow for.&lt;/p&gt;

&lt;p&gt;Compromised JavaScript script or module runs within the context of an authenticated session of the victim and it can perform actions on behalf of the user. In this scenario, the vulnerable code becomes an attack vector against Web applications the victim has legitimate access to.&lt;/p&gt;

&lt;h2&gt;
  
  
  Node.js
&lt;/h2&gt;

&lt;p&gt;A successful attack on Node.js programs may impact the entire server the program runs on. The attacker may get access to all the resources the operating system account has access to, potentially leading to a full compromise of the server.&lt;/p&gt;

&lt;h1&gt;
  
  
  What’s next?
&lt;/h1&gt;

&lt;p&gt;The next post in this series will demonstrate how the dynamic type system may lead to subtle security bugs.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>security</category>
      <category>node</category>
    </item>
    <item>
      <title>JavaScript Security Pitfalls</title>
      <dc:creator>Marcin Hoppe</dc:creator>
      <pubDate>Wed, 01 Jul 2020 09:51:47 +0000</pubDate>
      <link>https://dev.to/marcin_hoppe/javascript-security-pitfalls-29mk</link>
      <guid>https://dev.to/marcin_hoppe/javascript-security-pitfalls-29mk</guid>
      <description>&lt;h1&gt;
  
  
  Why bother with JavaScript security?
&lt;/h1&gt;

&lt;p&gt;The Web runs on JavaScript. If you are a software developer, chances are you are writing JavaScript. Even if you are not, you rely on tools and applications written in this popular language. You would not be reading this article without JavaScript.&lt;/p&gt;

&lt;p&gt;A lot has been written about &lt;a href="https://owasp.org/www-project-top-ten/"&gt;Web security&lt;/a&gt;. Many software engineers know what &lt;a href="https://owasp.org/www-project-top-ten/OWASP_Top_Ten_2017/Top_10-2017_A1-Injection"&gt;SQL injection&lt;/a&gt; is and can tell &lt;a href="https://owasp.org/www-project-top-ten/OWASP_Top_Ten_2017/Top_10-2017_A7-Cross-Site_Scripting_(XSS)"&gt;cross-site scripting&lt;/a&gt; (XSS) from &lt;a href="https://owasp.org/www-community/attacks/csrf"&gt;cross-site request forgery&lt;/a&gt; (CSRF). And yet, security issues that are unique to JavaScript remain unknown to many developers.&lt;/p&gt;

&lt;p&gt;Unfortunately, this does not mean that those vulnerabilities cannot be exploited by attackers seeking fame, fortune, or revenge. They certainly can.&lt;/p&gt;

&lt;h1&gt;
  
  
  A series is born
&lt;/h1&gt;

&lt;p&gt;The goal of this blog post series is to help you become a better JavaScript developer. I will help you to build a strong mental model of the most prevalent vulnerabilities that plague JavaScript code. Follow along to learn how to write secure and robust code that prevents them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Here be dragons
&lt;/h2&gt;

&lt;p&gt;JavaScript is a bit of an odd animal in the programming language menagerie. Rapid development and massive popularity gave us language features and coding patterns that may easily lead to exploitable security bugs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dynamic typing&lt;/strong&gt;. JavaScript variables can refer to objects of different types. A variable can refer to a number, a string, or an object, depending on the flow of control. When you look at the code, you do not always know the types of your variables. It may lead to unintentional information disclosure or other security issues.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dynamic code execution&lt;/strong&gt;. JavaScript programs can invoke the JavaScript engine at runtime. It sounds like a really powerful feature, and it is. Also, this is what attackers dream about: the ability to inject their code into your application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prototype pollution&lt;/strong&gt;. JavaScript has a pretty unusual inheritance mechanism. Instead of expressing static relationships between classes, the same goal is achieved by building dynamic relationships between objects. If attackers can modify the objects forming the prototype chain, they may alter the behavior of your code in unforeseen ways.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This series of posts will look into those problems in detail. It will also offer actionable guidance on how to find and avoid them.&lt;/p&gt;

&lt;h1&gt;
  
  
  What’s next?
&lt;/h1&gt;

&lt;p&gt;The next post in this series will explain the JavaScript security model in two most popular runtime environments: Web browsers and Node.js.&lt;/p&gt;

&lt;h1&gt;
  
  
  Video course
&lt;/h1&gt;

&lt;p&gt;I am also working on a video course JavaScript Security: Best Practices on &lt;a href="https://www.pluralsight.com/"&gt;Pluralsight&lt;/a&gt;. It will be a part of the &lt;a href="https://www.pluralsight.com/paths/javascript-core-language"&gt;JavaScript Core Language&lt;/a&gt; learning path. Learn more on the &lt;a href="https://marcinhoppe.com/courses/"&gt;Courses&lt;/a&gt; page on my website and subscribe to the newsletter to get regular updates about the progress.&lt;/p&gt;

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