<?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: Pooya Parsa</title>
    <description>The latest articles on DEV Community by Pooya Parsa (@pi0).</description>
    <link>https://dev.to/pi0</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%2F131672%2F29313675-0665-40e3-a1d0-3300e6c32f2f.jpeg</url>
      <title>DEV Community: Pooya Parsa</title>
      <link>https://dev.to/pi0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pi0"/>
    <language>en</language>
    <item>
      <title>Why I dislike GitHub security alerts? (and how to disable them)</title>
      <dc:creator>Pooya Parsa</dc:creator>
      <pubDate>Thu, 06 Jun 2019 18:32:29 +0000</pubDate>
      <link>https://dev.to/pi0/why-i-dislike-github-security-alerts-and-how-to-disable-them-1cg2</link>
      <guid>https://dev.to/pi0/why-i-dislike-github-security-alerts-and-how-to-disable-them-1cg2</guid>
      <description>&lt;p&gt;You may have heard about &lt;a href="https://help.github.com/en/articles/about-security-alerts-for-vulnerable-dependencies" rel="noopener noreferrer"&gt;Github Security Alerts&lt;/a&gt; and received some for your projects (if you are an open source maintainer, probably a LOT of them!)&lt;/p&gt;

&lt;p&gt;Well, that sounds so promising, doesn't it? But the truth is that most the cases are false-positive, non-effective or a security alert doesn't provide any solution! In this article, I will try to explain about reasonings why GitHub security is not good protection and a workaround to avoid spams in your email.&lt;/p&gt;

&lt;h2&gt;
  
  
  For &lt;a href="https://npmjs.com" rel="noopener noreferrer"&gt;npm&lt;/a&gt; package maintainers
&lt;/h2&gt;

&lt;p&gt;Package dependencies are usually specified with a caret (&lt;code&gt;^&lt;/code&gt;) or tile (&lt;code&gt;~&lt;/code&gt;) range (learn more about &lt;a href="https://docs.npmjs.com/misc/semver#ranges" rel="noopener noreferrer"&gt;semver&lt;/a&gt;). This means even if you don't explicitly upgrade to a patch or minor version of a dependency, fresh installs of your package receive the patch of the dependencies so you shouldn't worry.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dependabot.com" rel="noopener noreferrer"&gt;dependabot&lt;/a&gt; (which is acquired by Microsoft/GitHub and now enabled by default on all repositories) tries to just update lock-file in the repository. This doesn't fix anything for your package users as lock file is &lt;strong&gt;not&lt;/strong&gt; published with your package and will not be used by package managers when someone installs your package. So what's the benefit? Probably nothing! unless dependency is harmless itself by execution (for example running malicious code or leaking tokens) which in this cases npm security team will take fast action and remove tarball from the registry, before any disclosure. Also, many security alerts are usually related to &lt;code&gt;devDependencies&lt;/code&gt; which are totally irrelevant as these dependencies are probably used for your local environment and not affecting end-users if there is a bug into them. (at least is not a &lt;em&gt;security&lt;/em&gt; problem!)&lt;/p&gt;

&lt;h2&gt;
  
  
  For end-projects
&lt;/h2&gt;

&lt;p&gt;If you have a GitHub project that powers a public website or an API, it really matters that you receive security patches ASAP and deploy them. But personally, still don't feel good about having GitHub eyes on &lt;strong&gt;all of my projects&lt;/strong&gt; by default:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Many of my personal GitHub repositories are temporary projects or examples and are not going to be updated. A regex DDOS is irrelevant for them and it is just annoying about GitHub that frequently requests me to update their dependencies. &lt;/li&gt;
&lt;li&gt;Many of security alerts are false-positive or related to &lt;code&gt;devDependencies&lt;/code&gt;. Like a potential bug in a jest dependency. Certainly, an attacker does not write a test that runs in CI to break it and it is not really a security alert.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I wish it could be enabled on only projects that I need, not everything! and also there were some options specifying behavior. I don't want to advertise but I had a much better experience and customizability by using &lt;a href="https://renovatebot.com" rel="noopener noreferrer"&gt;Renovate Bot&lt;/a&gt;. Another option is &lt;a href="https://snyk.io" rel="noopener noreferrer"&gt;snyk&lt;/a&gt; which regularly checks your project against known security alerts and more importantly, it provides automated patches for high-impact vulnerabilities (not just alerting) &lt;/p&gt;

&lt;h2&gt;
  
  
  Disabling GitHub Notifications
&lt;/h2&gt;

&lt;p&gt;Go to the &lt;a href="https://github.com/settings/notifications" rel="noopener noreferrer"&gt;notifications&lt;/a&gt; section of your GitHub profile and change preferences according to your needs:&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%2Fuser-images.githubusercontent.com%2F5158436%2F59031446-f76bc900-8878-11e9-83e0-6baeaf85f445.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%2Fuser-images.githubusercontent.com%2F5158436%2F59031446-f76bc900-8878-11e9-83e0-6baeaf85f445.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Skipping email alerts from inbox (Gmail)
&lt;/h2&gt;

&lt;p&gt;Even by disabling notifications you will still receive LOTS of emails from GitHub security. You can label them to skip the inbox and go to a specific category or archive by default.&lt;/p&gt;

&lt;p&gt;First, open &lt;a href="https://mail.google.com" rel="noopener noreferrer"&gt;Gmail&lt;/a&gt; and search for &lt;strong&gt;&lt;code&gt;to:(Security alert &amp;lt;security_alert@noreply.github.com&amp;gt;)&lt;/code&gt;&lt;/strong&gt;. Using the dropdown button right to the search box, open more options: &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%2Fuser-images.githubusercontent.com%2F5158436%2F59031186-39483f80-8878-11e9-8ffe-db0ebcb8a477.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%2Fuser-images.githubusercontent.com%2F5158436%2F59031186-39483f80-8878-11e9-8ffe-db0ebcb8a477.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then click on &lt;code&gt;Create filter&lt;/code&gt; to create a filter and configure it according to your preferences:&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%2Fuser-images.githubusercontent.com%2F5158436%2F59031277-76143680-8878-11e9-8b8c-d6ee38572950.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%2Fuser-images.githubusercontent.com%2F5158436%2F59031277-76143680-8878-11e9-8b8c-d6ee38572950.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Disable automated pull-requests
&lt;/h2&gt;

&lt;p&gt;Unfortunately, automated pull-requests are enabled by default for all of your repositories but the good news is that you can still disable them one by one by going to "Security" tab of each repository and selecting "Off: Automated security fixes" from the dropdown:&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%2Fuser-images.githubusercontent.com%2F5158436%2F59056328-2c920e80-88ad-11e9-9629-59520d986963.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%2Fuser-images.githubusercontent.com%2F5158436%2F59056328-2c920e80-88ad-11e9-9629-59520d986963.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Never ignore security for your project. If you are the author of a popular npm package or critical website. In this article, I tried to explain why (current) GitHub security is not probably the best tool and provide a way to give back &lt;strong&gt;freedom&lt;/strong&gt; choosing tooling for security checks.&lt;/p&gt;

&lt;p&gt;GitHub platform is powering thousands of opensource projects and for sure there is a good intention about new integrated security checks. But it could be better, allowing more customization and considering the facts about semver versioning and also the freedom to opt-in for security alerts. &lt;strong&gt;Having lots of irrelevant security alerts makes it harder to observe real security bugs.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>github</category>
      <category>opensource</category>
      <category>npm</category>
      <category>node</category>
    </item>
    <item>
      <title>Node.js fork is not what you think!</title>
      <dc:creator>Pooya Parsa</dc:creator>
      <pubDate>Thu, 24 Jan 2019 23:44:07 +0000</pubDate>
      <link>https://dev.to/pi0/nodejs-fork-is-not-what-you-think-of-37ko</link>
      <guid>https://dev.to/pi0/nodejs-fork-is-not-what-you-think-of-37ko</guid>
      <description>&lt;p&gt;Today I realized that in Node.js, neither &lt;code&gt;cluster.fork&lt;/code&gt; or &lt;code&gt;child_process.fork&lt;/code&gt; act like something you expect in a C environment. Actually, it is shortly mentioned in &lt;a href="https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options"&gt;docs&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Unlike the &lt;a href="http://man7.org/linux/man-pages/man2/fork.2.htm"&gt;fork(2)&lt;/a&gt; POSIX system call, &lt;code&gt;child_process.fork()&lt;/code&gt; does not clone the current process.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;child_process.fork()&lt;/code&gt; method is a special case of &lt;code&gt;child_process.spawn()&lt;/code&gt; used specifically to spawn new Node.js processes. Like &lt;code&gt;child_process.spawn()&lt;/code&gt;, a &lt;code&gt;ChildProcess&lt;/code&gt; object is returned. The returned &lt;code&gt;ChildProcess&lt;/code&gt; will have an additional communication channel built-in that allows messages to be passed back and forth between the parent and child.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  So what it means?
&lt;/h2&gt;

&lt;p&gt;Taking a simple C code that forks 5 processes:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;sys/types.h&amp;gt;
#include &amp;lt;unistd.h&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Shared Code&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&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="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fork&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="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;pid&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="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Worker %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&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="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="mi"&gt;0&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;Compiling and running this code gives us a result like this:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Shared
Worker 1
Worker 2
Worker 3
Worker 4
Worker 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What operating system does under the hoods, is when we call &lt;a href="http://man7.org/linux/man-pages/man2/fork.2.htm"&gt;fork()&lt;/a&gt;, it &lt;strong&gt;copies&lt;/strong&gt; entire process state to a new one with a new PID. Return value in the worker process is always &lt;code&gt;0&lt;/code&gt; so we have a way to find-out if &lt;strong&gt;rest of the code&lt;/strong&gt; is running in forked process or master. (&lt;em&gt;Thanks to @littlefox comment🧡&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;The important point is that forked process &lt;strong&gt;continues&lt;/strong&gt; from where &lt;code&gt;fork()&lt;/code&gt; was called. Not from the beginning so &lt;code&gt;Shared&lt;/code&gt; is printed once.&lt;/p&gt;

&lt;p&gt;Running a similar code in Node.js:&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isMaster&lt;/span&gt; &lt;span class="p"&gt;}&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="s1"&gt;'cluster'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Shared'&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;isMaster&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Fork workers.&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="mi"&gt;5&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;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;fork&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;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Worker&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Worker`&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 output is amazingly different:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Shared
Shared
Worker
Shared
Worker
Shared
Worker
Shared
Worker
Shared
Worker
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The point is that each time a worker forked, it started with a fresh V8 instance. This is not a behavior that it's name tells. Fork in Node.js is actually doing exec/spawn which causes shared code running each time.&lt;/p&gt;

&lt;h2&gt;
  
  
  OK. So let's move &lt;code&gt;console.log('Shared')&lt;/code&gt; into &lt;code&gt;if (isMaster)&lt;/code&gt; :P
&lt;/h2&gt;

&lt;p&gt;Well. Yes. You are right. That's the solution. But just for this example case!&lt;/p&gt;

&lt;p&gt;In a real-world application that needs a cluster, we don't immediately fork workers. We may want to set up our web framework, parse CLI args and require a couple of libs and files. All of this steps has to be repeated on &lt;strong&gt;each&lt;/strong&gt; worker that may introduce lots of unnecessary overhead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Solution
&lt;/h2&gt;

&lt;p&gt;Now that we know what exactly &lt;code&gt;cluster.fork&lt;/code&gt; does under the hood, we can split our worker logic into a seperate &lt;code&gt;worker.js&lt;/code&gt; file and change default value of &lt;code&gt;exec&lt;/code&gt; which is &lt;code&gt;process.argv[1]&lt;/code&gt; to &lt;code&gt;worker.js&lt;/code&gt; :) This is possible by calling &lt;a href="https://nodejs.org/api/cluster.html#cluster_cluster_setupmaster_settings"&gt;&lt;code&gt;cluster.setupMaster()&lt;/code&gt;&lt;/a&gt; on master process.&lt;/p&gt;

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