<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Angel M</title>
    <description>The latest articles on Forem by Angel M (@angelmm).</description>
    <link>https://forem.com/angelmm</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%2F197952%2F9af59d38-a2e9-4028-9812-161c3f1672e1.jpg</url>
      <title>Forem: Angel M</title>
      <link>https://forem.com/angelmm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/angelmm"/>
    <language>en</language>
    <item>
      <title>Lazy initial states in React</title>
      <dc:creator>Angel M</dc:creator>
      <pubDate>Sun, 17 Jul 2022 20:13:42 +0000</pubDate>
      <link>https://forem.com/angelmm/lazy-initial-states-in-react-1ca4</link>
      <guid>https://forem.com/angelmm/lazy-initial-states-in-react-1ca4</guid>
      <description>&lt;p&gt;One of the most important aspects about performance in React applications is how your components &lt;em&gt;react to changes&lt;/em&gt;. After introducing &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;hooks&lt;/a&gt; in &lt;a href="https://reactjs.org/blog/2019/02/06/react-v16.8.0.html"&gt;2019&lt;/a&gt;, the definition of components using functions became the new norm.&lt;/p&gt;

&lt;p&gt;They came with an interesting side effect: &lt;strong&gt;the entire function is executed any time React detects a potential change in your component&lt;/strong&gt;. Before, components defined with classes only executed certain methods like the lifecycle ones (&lt;code&gt;componentDidMount&lt;/code&gt;, etc) and the well known &lt;code&gt;render&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;To manage it, React added the amazing &lt;code&gt;useEffect&lt;/code&gt; &lt;a href="https://reactjs.org/docs/hooks-effect.html"&gt;hook&lt;/a&gt;. However, it's important to keep in mind that functions executes all the code inside when they are called.&lt;/p&gt;

&lt;h2&gt;
  
  
  Initialize a state in React
&lt;/h2&gt;

&lt;p&gt;You can initialize a state in React using the &lt;code&gt;useState&lt;/code&gt; &lt;a href="https://reactjs.org/docs/hooks-reference.html#usestate"&gt;hook&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&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;MyComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&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;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Increment the given counter&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;incrementCounter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;section&lt;/span&gt; &lt;span class="nx"&gt;aria&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Counter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;incrementCounter&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Increment&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;output&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/output&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/section&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&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;&lt;code&gt;MyComponent&lt;/code&gt; defines a new state to manage the current counter value. Following the previous statement, &lt;strong&gt;any time React detects a potential change, it calls &lt;code&gt;MyComponent&lt;/code&gt; function&lt;/strong&gt; and compares the result of the execution with the previous state of the application.&lt;/p&gt;

&lt;p&gt;Now, taking a deep look to this function, there are multiple calls and defintions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Call to &lt;code&gt;useState&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Define the &lt;code&gt;incrementCounter&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;Call JSX method under the hood&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Apart from that, there's a tiny detail that is usually forgotten. &lt;code&gt;0&lt;/code&gt; is also evaluated. So, &lt;strong&gt;what happens if you need to call a function to calculate the initial state value?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Lazy initial state
&lt;/h2&gt;

&lt;p&gt;Now, let's check the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;initState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./utils&lt;/span&gt;&lt;span class="dl"&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;MyComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&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;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initState&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, &lt;code&gt;useState&lt;/code&gt; doesn't receive a static value but a function result as parameter. &lt;strong&gt;Note that the &lt;code&gt;initState&lt;/code&gt; function is called any time React calls &lt;code&gt;MyComponent&lt;/code&gt;&lt;/strong&gt;. However, &lt;code&gt;useState&lt;/code&gt; only use the the result once. After its mounted, next executions of the component will discard the &lt;code&gt;initState&lt;/code&gt; result.&lt;/p&gt;

&lt;p&gt;Depending on the complexity of &lt;code&gt;initState&lt;/code&gt;, it may cause some performance issues in &lt;code&gt;MyComponent&lt;/code&gt; even after the first initialization. To avoid it, &lt;strong&gt;React allows you to pass a function that will be executed just once&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;initState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./utils&lt;/span&gt;&lt;span class="dl"&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;MyComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&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;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;initState&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This trick is called &lt;a href="https://reactjs.org/docs/hooks-reference.html#lazy-initial-state"&gt;&lt;em&gt;lazy state initialization&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  You don't need to be lazy by default
&lt;/h2&gt;

&lt;p&gt;Let's be fair. Fortunately, states are initialized with static values most of the times. Not all applications will benefit from this &lt;code&gt;useState&lt;/code&gt; feature. However, this is one of those difficult performance issues to detect and the solution is quite simple.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Just keep it in mind when you need to call a function to initialize a state&lt;/strong&gt;. And think it twice if it's a requirement because your component will still need to wait for the result when it's mounted.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;React Hooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/docs/hooks-reference.html#usestate"&gt;React useState Hook&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Hire ethical developers</title>
      <dc:creator>Angel M</dc:creator>
      <pubDate>Wed, 06 Jul 2022 17:47:30 +0000</pubDate>
      <link>https://forem.com/angelmm/hire-ethical-developers-1ani</link>
      <guid>https://forem.com/angelmm/hire-ethical-developers-1ani</guid>
      <description>&lt;p&gt;&lt;em&gt;Imagine you are running a small shop in your city. One day you catch a few people stealing something in your shop. After arguing, they ask to join your shop crew because they know some security points that could be improved.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is that the kind of people you would want to work with?&lt;/strong&gt; Probably not. They know that you're losing your money when they steal something. However, that's not a reason for them to stop doing it.&lt;/p&gt;

&lt;p&gt;What's happening in the developer's world?&lt;/p&gt;

&lt;h2&gt;
  
  
  Professional ethics
&lt;/h2&gt;

&lt;p&gt;Some time ago, I read &lt;a href="https://medium.com/@timotheejeannin/i-built-a-screenshot-api-and-some-guy-was-mining-cryptocurrencies-with-it-cd188dfae773"&gt;this article&lt;/a&gt; about a man that was using a screenshot API to earn money mining cryptocurrencies. The article talks about how the company managed the situation and stopped him.&lt;/p&gt;

&lt;p&gt;The conversation was respectful and finally, the man stopped minning crypto using that service. I don't want to discuss how the company managed the situation, but I'd like to highlight some parts of the conversation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;C: are we the first company you attacked?&lt;/p&gt;

&lt;p&gt;M: no..&lt;/p&gt;

&lt;p&gt;M: this is my hobby&lt;/p&gt;

&lt;p&gt;M: for fun and profit&lt;/p&gt;

&lt;p&gt;[...]&lt;/p&gt;

&lt;p&gt;M: i am sorry if you had any loss&lt;/p&gt;

&lt;p&gt;C: It's ok we haven't lost anything.&lt;/p&gt;

&lt;p&gt;[...]&lt;/p&gt;

&lt;p&gt;M: can i be a part of your team ?&lt;/p&gt;

&lt;p&gt;C: i'm sorry but we already have a developer in house.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We're at the same point as the shop example. &lt;strong&gt;A person without professional ethics that knows the damage he or she might cause to your company is asking to join your team&lt;/strong&gt;. He knows how to use your service illicitly, so he knows how to protect you.&lt;/p&gt;

&lt;p&gt;Based on the comments of the &lt;a href="https://medium.com/@timotheejeannin/i-built-a-screenshot-api-and-some-guy-was-mining-cryptocurrencies-with-it-cd188dfae773"&gt;article&lt;/a&gt;, it seems the company was wrong. But, were they really wrong ignoring his offering?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Ethics in general ought to be integrated in all your hiring decisions. &lt;strong&gt;Surround yourself with people that help you to grow as a professional, but even more importantly, as a person&lt;/strong&gt;. Please, don't reward bad people because they are good programmers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mentors and idols
&lt;/h2&gt;

&lt;p&gt;The same principle applies to your mentors and idols. Remember, ethics matter and are essential. If you know that your idols are good professionals but completely awful people, don't promote their work.&lt;/p&gt;

&lt;p&gt;You may think that it doesn't matter because their opinions are not relevant for their work. &lt;strong&gt;However, increasing their popularity has a dangerous consequence&lt;/strong&gt;. Their ideas will become more popular too.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please, create a better community by promoting good people, not just developers&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>hiring</category>
      <category>career</category>
      <category>community</category>
      <category>leadership</category>
    </item>
    <item>
      <title>Safe and predictable inline scripts</title>
      <dc:creator>Angel M</dc:creator>
      <pubDate>Mon, 04 Jul 2022 08:18:51 +0000</pubDate>
      <link>https://forem.com/angelmm/safe-and-predictable-inline-scripts-4d09</link>
      <guid>https://forem.com/angelmm/safe-and-predictable-inline-scripts-4d09</guid>
      <description>&lt;p&gt;In web security, there are many attack vectors that a malicious actor can use. One of the most common attack vectors is the &lt;em&gt;injection&lt;/em&gt;. There are many different types of injections. This article is focused on &lt;a href="https://cwe.mitre.org/data/definitions/79.html"&gt;CWE-79 Cross-site Scripting (XSS)&lt;/a&gt; injections.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cross-Site Scripting (XSS) attacks are a type of injection, in which malicious scripts are injected into otherwise benign and trusted websites - &lt;a href="https://owasp.org/www-community/attacks/xss/"&gt;OWASP&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;In other words, an XSS attack consists of running untrusted and malicious scripts in other user browsers&lt;/strong&gt;. With this attack, a malicious actor can get access to the environment to read the information in cookies and storage, and compromise the site behavior.&lt;/p&gt;

&lt;p&gt;To minimize the risk, there are different ways to ensure &lt;strong&gt;the browser only executes trusted resources in your sites&lt;/strong&gt;. Let's explore how to run inline scripts securely 👇.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content Security Policy (CSP)
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Content Security Policy (CSP) property allows you to teach the browser about the resources it should read and execute in your site&lt;/strong&gt;. It can prevent the execution of non-expected resources to minimize the risk.&lt;/p&gt;

&lt;p&gt;You can define a CSP policy in two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Returning the Content-Security-Policy HTTP header in the response
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  Content-Security-Policy: policy;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create a &lt;code&gt;&amp;lt;meta&amp;gt;&lt;/code&gt; tag
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;http-equiv=&lt;/span&gt;&lt;span class="s"&gt;"Content-Security-Policy"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"policy;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Policies
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Policies give you the tooling to only allow the execution of resources that you trust&lt;/strong&gt;. A CSP &lt;code&gt;policy&lt;/code&gt; can define:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A default behavior for the different resources&lt;/li&gt;
&lt;li&gt;Restrictions for specific resources&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, this policy ensures the browser only executes resources from the same domain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;default-src "self";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can allow other domains and specify any domain for certain resources like images:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;default-src "self" example.com; img-src *;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From there, you can start adding stricter policies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inline Scripts
&lt;/h2&gt;

&lt;p&gt;Inline scripts are wild and one of the most common injection vectors. An attacker may run arbitrary code on your site &lt;a href="https://owasp.org/www-community/attacks/xss/#description"&gt;using different approach&lt;/a&gt;. So, a good practice is to restrict inline scripts via CSP:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;default-src "self";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, sometimes inline scripts are required. For example, this site uses an inline script to load the site theme. Using the previous CSP policy would block this script. Inline scripts can be enabled using the &lt;code&gt;unsafe-inline&lt;/code&gt; value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;default-src "self"; script-src "self" "unsafe-inline";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you may guess, &lt;strong&gt;this is a risky option as it allows any inline script in your site&lt;/strong&gt;. To protect against unexpected inline scripts, CSP provides us with two tools to enable only trusted ones.&lt;/p&gt;

&lt;h3&gt;
  
  
  Nonces
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;In cryptography, a nonce (number once) is an arbitrary number that can be used just once in a cryptographic communication - &lt;a href="https://en.wikipedia.org/wiki/Cryptographic_nonce"&gt;Wikipedia&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Nonce strategy requires to &lt;strong&gt;generate a base64 random string on the server for every request&lt;/strong&gt;. It requires a server to generate the nonce dynamically. Once the nonce is created, you should:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Set the &lt;code&gt;nonce&lt;/code&gt; value in every trusted script
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;   &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;nonce=&lt;/span&gt;&lt;span class="s"&gt;"RANDOM_NONCE"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;my_trusted_script&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Configure the CSP the policy to allow script associated to the nonce:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   default-src "self"; script-src "self" "nonce-RANDOM_NONCE";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach is straightforward. However, it requires a server to generate a new random nonce on every request. If your site is static like this blog, let's check the next approach.&lt;/p&gt;

&lt;h3&gt;
  
  
  Script Hash
&lt;/h3&gt;

&lt;p&gt;The script hash strategy allows you to indicate the hash of the inline scripts of your site. &lt;strong&gt;The browser computes the hash of every inline script and compares it with the values provided in the CSP policy&lt;/strong&gt;. If the script matches the given hash, the script will be executed.&lt;/p&gt;

&lt;p&gt;To compute a script hash, you need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compute the &lt;code&gt;SHA-256&lt;/code&gt;, &lt;code&gt;SHA-384&lt;/code&gt; or &lt;code&gt;SHA-512&lt;/code&gt; hash of the script content. Note &lt;strong&gt;this includes every tab, space, and break line&lt;/strong&gt;. Always calculate the hash of the exact code that will be executed.&lt;/li&gt;
&lt;li&gt;Convert the given hash to &lt;code&gt;base64&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To make things simpler, I created &lt;a href="https://angel.kiwi/playground/csp-script-hash/"&gt;👉 a small tool 🔨&lt;/a&gt; so you only need to paste your code there.&lt;/p&gt;

&lt;p&gt;Once you have the hash, configure it as a trusted inline script in the CSP policy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;default-src "self"; script-src "self" "sha256-sytuQ9rGYPcMw/DRh3WEVO2EynM4II6TcLanpOZl+NA=";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach allows you to execute inline scripts in static sites safely. This is the approach I use on the site 😄.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.precisesecurity.com/articles/cross-site-scripting-xss-makes-nearly-40-of-all-cyber-attacks-in-2019/"&gt;Cross-Site Scripting (XSS) Makes Nearly 40% of All Cyber Attacks in 2019&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://owasp.org/Top10/A03_2021-Injection/"&gt;
OWASP Top 10:2021 - A03:2021 – Injection&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tripwire.com/state-of-security/featured/most-common-website-security-attacks-and-how-to-protect-yourself/"&gt;The 10 Most Common Website Security Attacks (and How to Protect Yourself)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://owasp.org/www-community/attacks/xss/"&gt;Cross Site Scripting (XSS)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP"&gt;Content Security Policy (CSP)&lt;/a&gt;, Mozilla MDN&lt;/li&gt;
&lt;li&gt;&lt;a href="https://content-security-policy.com/unsafe-inline/"&gt;Content Security Policy (CSP) Quick Reference Guide - Unsafe Inline&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src"&gt;CSP: script-src&lt;/a&gt;, Mozilla MDN&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>security</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
