<?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: David Adi Nugroho</title>
    <description>The latest articles on Forem by David Adi Nugroho (@lakuapik).</description>
    <link>https://forem.com/lakuapik</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%2F91339%2F1236026d-a5bf-4174-b80e-e00720b2df11.png</url>
      <title>Forem: David Adi Nugroho</title>
      <link>https://forem.com/lakuapik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lakuapik"/>
    <language>en</language>
    <item>
      <title>Don’t make your .git folder publicly accessible, hacker can steal your source code</title>
      <dc:creator>David Adi Nugroho</dc:creator>
      <pubDate>Wed, 15 Apr 2020 12:04:23 +0000</pubDate>
      <link>https://forem.com/lakuapik/don-t-make-your-git-folder-publicly-accessible-hacker-can-steal-your-source-code-41nh</link>
      <guid>https://forem.com/lakuapik/don-t-make-your-git-folder-publicly-accessible-hacker-can-steal-your-source-code-41nh</guid>
      <description>&lt;p&gt;Have you ever realized the danger of a publicly accessible .git folder?&lt;/p&gt;

&lt;p&gt;There are some developers who deploy their app to production using pure &lt;strong&gt;git clone&lt;/strong&gt; method. They clone their app repository from gitlab/github/bitbucket directly to a web-root facing folder on server like /var/www/app/. That makes the .git folder exist in /var/www/app/.git.&lt;/p&gt;

&lt;p&gt;If you don’t have proper permission to that .git folder, it will be accessible to the public. Like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jmzrGMYt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/996/1%2AjOflET0r-jSjdEoRGV1MOA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jmzrGMYt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/996/1%2AjOflET0r-jSjdEoRGV1MOA.png" width="498" height="566"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And then hacker can download your .git folder using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;wget &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; &lt;span class="nt"&gt;-np&lt;/span&gt; &lt;span class="nt"&gt;-R&lt;/span&gt; &lt;span class="s2"&gt;"index.html*"&lt;/span&gt; http://example.com/.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After downloaded, it’s just an empty folder with .git folder on it. It also has all commit history.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PWBHt-Um--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1268/1%2AxWZYs1xTHuNW12RqsV7JhQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PWBHt-Um--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1268/1%2AxWZYs1xTHuNW12RqsV7JhQ.png" width="634" height="150"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hacker can reset to latest commit to restore the source code files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git reset &lt;span class="nt"&gt;--hard&lt;/span&gt; HEAD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2fZT0Y8B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1136/1%2ASxfmb1fbf6aS0z0NEc8RqQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2fZT0Y8B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1136/1%2ASxfmb1fbf6aS0z0NEc8RqQ.png" width="568" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And boom, your source code is leaked!&lt;/p&gt;




&lt;h1&gt;
  
  
  Here is how to mitigate it:
&lt;/h1&gt;

&lt;p&gt;Add/edit your .htaccess file to make the .git folder hidden&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RewriteEngine on  
RewriteRule .*\.git/.* - [404]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why use 404 not found instead of 403 forbidden?&lt;br&gt;&lt;br&gt;
Hacker doesn’t even know if the .git folder exists, it’s 404. But if it’s 403 forbidden, hacker knows there is .git folder, only not accessible.&lt;/p&gt;




&lt;p&gt;Hope it helps.&lt;br&gt;
Salam.&lt;/p&gt;

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