112

I have started using git for deployment of websites for testing. How do I prevent apache from serving the .git directory contents?

I tried

<Directorymatch "^/.*/\.svn/">
Order deny,allow
Deny from all
</Directorymatch>

with no success.

I know that I can create a .htaccess file in each .git directory and deny access, but I wanted something I could put into the main config file that makes this global across all websites.

2
  • 3
    Once you've prevented apache from serving the directory you may also need to hide the .git directory with "IndexIgnore .git" if you have Indexes enabled on your directory.
    – Ryan
    Jul 21, 2015 at 18:17
  • 1
    Hey, consider changing the accepted answer. Bennett's seems more appropriate. Thanks.
    – Gray
    Aug 7, 2023 at 21:00

12 Answers 12

200

This has the same effect as many of the other answers but is much simpler:

RedirectMatch 404 /\.git

This can go into .htaccess or your server config file. It hides any file or directory whose name begins with .git (e.g. a .git directory or .gitignore file) by returning a 404. So not only are the contents of your Git repo hidden, its very existence is hidden too.

13
  • 4
    I really like this solution. It's simple and elegant.
    – Shoan
    Mar 8, 2014 at 9:02
  • 5
    Putting this in the root htdocs directory does a global job, too.
    – jor
    Feb 27, 2015 at 14:19
  • 7
    Love this option the best as well. Seems to me that it is more secure to return a 404 for requests like /.git or /.gitignore so that the fact that git is even being used can't be determined from the outside.
    – Ezra Free
    Sep 6, 2015 at 2:54
  • 3
    Be aware that with if you have directory listings enabled, the .git folders will still be visible, but you'll get the 404 when you try to access them.
    – Andy Madge
    Feb 4, 2016 at 15:21
  • 2
    @shashwat you can't access the .git directory or anything in it, including .git/config. All such attempts will give a 404 error. Oct 31, 2017 at 22:46
82

It's not working because you have 'svn' instead of 'git' in the rule. All you have to do is to replace the 'svn' with 'git'.

<Directorymatch "^/.*/\.git/">
  Order 'deny,allow'
  Deny from all
</Directorymatch>
7
  • 2
    When I create a .htaccess containing only your code, I get the error: "<DirectoryMatch not allowed here"
    – Shoan
    Jul 27, 2010 at 19:03
  • 2
    It has to be in the Apache conf. See: httpd.apache.org/docs/1.3/mod/core.html#directorymatch
    – sinping
    Jul 28, 2010 at 12:45
  • 4
    The simplest regex is <DirectoryMatch /\.git/>
    – Bachsau
    Apr 5, 2017 at 18:00
  • Check this for perfect solution magento.stackexchange.com/questions/202840/… Nov 23, 2017 at 5:25
  • 1
    1st, thanks to singping/OP. Note that in Apache 2.4 the "Order,deny" and next line have been replaced by "Require all denied". Also, many installations the file called "Apache conf" above is named "httpd.conf" --- singping's usage was just a casual statement, so don't search for that literal name (should probably go without saying, but you never know how people might read it). Sep 14, 2018 at 21:44
18

If you're on a shared hosting service and don't have access to apache.conf, you can still do it in your .htaccess file, like this:

RewriteEngine On
RewriteRule "^(.*/)?\.git/" - [F,L]
2
  • thanks this worked for me in a shared hosting situation where the top answer didn't
    – Plato
    Oct 1, 2014 at 23:34
  • I appended this rewrite rule to site's own config and it worked.
    – nurettin
    Feb 9, 2023 at 11:36
16

If you don't use .htaccess files but instead want to use /etc/apache2/httpd.conf (or whatever your server's master conf file is) to hide both .git directories and .gitignore files, you can use the following. I found the answer above for master conf setting did not hide the gitignore file.

# do not allow .git version control files to be issued
<Directorymatch "^/.*/\.git+/">
  Order deny,allow
  Deny from all
</Directorymatch>
<Files ~ "^\.git">
    Order allow,deny
    Deny from all 
</Files>
1
  • 2
    Dosn't block the www.example.com/.git/config file in Apache httpd 2.4.27.
    – ilhan
    Sep 18, 2017 at 12:09
8
### never deliver .git folders, .gitIgnore
RewriteRule ^(.*/)?\.git+ - [R=404,L]

# 2nd line of defense (if no mod_rewrite)
RedirectMatch 404 ^(.*/)?\.git+

This works in .htaccess, no http.conf access required. Include this as the first of rewrite rules. Prepend Rewrite On if needed.

From a security angle, I prefer a bogus 404 over an 403, more informative to the attacker. Comment one of the two out, to ensure, the other works for you, too.

Btw, good changes are, your lithmus test for the two are:

http://localhost/.gitignore
http://localhost/.git/HEAD
5
  • Why have both rules? The simpler RedirectMatch suffices on its own. (Also, the regexes don't seem quite right -- why the plus on the end?) Aug 1, 2013 at 10:20
  • Personal Paranoia / doubled security. If RewriteEngine happens to get turned off (central config changes, poor team communication, unlucky server "update",... you name it :-) The + is obsolete or should be a $, good point! (no time for testing, sorry.)
    – Frank N
    Aug 2, 2013 at 12:24
  • If you're worried that RewriteEngine might be off, just put RewriteEngine On before your RewriteRule. But anyhow it is tautological and redundant because the simpler RedirectMatch suffices on its own. Though even that could be simplified. Basically I am recommending my answer instead. :) Aug 3, 2013 at 4:51
  • +1 for the litmus test.
    – user172409
    Jul 15, 2014 at 14:58
  • Why a + at the end of the rewrite rule? Wouldn't that mean one or more instances of the letter t? Doesn't make sense to me, but this answer isn't the only place I've seen this exact wording of that RewriteRule; hoping you can clarify.
    – Wildcard
    Aug 25, 2023 at 0:38
6

To protect both the .git directory as well as other files such as .gitignore and .gitmodules using .htaccess, use:

RewriteEngine On
RewriteRule ^(.*/)?\.git+ - [F,L]
ErrorDocument 403 "Access Forbidden"
2
  • 4
    Does work for me, however the trailing ErrorDocument has no impact. From a security angle, I'd fancy a bogus 404 over an informative 403 to the attacker...
    – Frank N
    May 26, 2013 at 10:01
  • 2
    This is a bad idea, because it discloses information to hackers. A 403 means it's there, a 404 means it's not. Every fact on a server's setup is usefull to a hacker. I'd consider revising this.
    – GerardJP
    Dec 10, 2014 at 10:25
4

I always add the following line into vhost template

RedirectMatch 404 /\\.(svn|git|hg|bzr|cvs)(/|$)

Just to be sure that no one can access VCS specific data. Works perfect.

2

Assuming your webserver is using a different user than the one you use to access the .git repository, you could disable the execute bit for others on the .git directory.

This should work with other webservers and doesn't rely on performance-consuming .htaccess files.

1
  • Wise & Simple solution
    – mahyard
    Aug 11, 2023 at 13:41
2

For those looking to simply deny all "hidden" files and directories on a Linux distribution (generally all files beginning with a "."), here's what works on Apache 2.4 when placed in server conf context:

<FilesMatch "^\.(.*)$">
    Require all denied
</FilesMatch>
<DirectoryMatch "/\.(.*)">
    Require all denied
</DirectoryMatch>

And here's the older Apache 2.2 style (same regex, just different auth directives):

<FilesMatch "^\.(.*)$">
    Order deny,allow
    Deny from all
</FilesMatch>
<DirectoryMatch "/\.(.*)">
    Order deny,allow
    Deny from all
</DirectoryMatch>

Then you don't have to worry about .git or .svn specifically. That would also match things like .htaccess and .htpasswd inherently.

Personally, I like issuing 403s for such requests instead of 404s, but you could easily use a RewriteRule instead of auth denial, like so:

<FilesMatch "^\.(.*)$">
    RewriteRule "^(.*)$" - [R=404,L]
</FilesMatch>
<DirectoryMatch "/\.(.*)">
    RewriteRule "^(.*)$" - [R=404,L]
</DirectoryMatch>
1

This is a little late but my answer is a slightly different so I thought I would add it. This must go in the httpd.conf file. The <Files "*"> nested inside the <Directory> tag will block all files in the directory.

# GitHub Directory
<Directory /var/www/html/yoursite/.git>
   Order Deny,Allow
   Deny from all
   <Files "*">
     Order Deny,Allow
     Deny from all
   </Files>
</Directory>
# GitHub files
<Files .gitignore>
  order Deny,Allow
  Deny from all
</Files>
0

You probably want to deny serving .gitignore as well.

Files starting with a dot are hidden in linux.

Therefore, just 404 anything that begins with a dot:

RedirectMatch 404 /\.

0

Ubuntu's apache2 package has a security.conf file that suggests

<DirectoryMatch "/\.svn">
   Require all denied
</DirectoryMatch>

(or change .svn to .git to block access to git, or use both 🤷)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .