views:

202

answers:

3

My website is a mercurial repository with multiple subrepositories. I need to make sure I'm denying access to all files in every .hg directory on the server.

For example, I have http://example.com/.hg/ and http://example.com/subrepo1/.hg/

I've added the following to .htaccess:

<Files ~ "^\.(hg|ht)">
    Order allow,deny
    Deny from all
</Files>

This is a good start, as it denies access to files beginning with .hg and .ht, but it doesn't deny access to the files inside .hg directories, so if someone types, for instance http://example.com/.hg/branch, the branch file will be displayed in their browser.

What do I need to do in order to make sure these files are not displayed to the user? I'd like to send either a 403 or a 404 back to the browser if someone tries to access a file inside any .hg directory on my server.

This question is also relevant for anyone whose website is a subversion / svn repository.

+4  A: 

If you don't have to use mod_rewrite, then you can just do this:

RedirectMatch 404 /\\.hg(/|$)

(Full disclosure: answer adapted for Mercurial from this question about doing the same thing for Subversion).

Dominic Rodger
I get a 500 error when I try anything with Directory, DirectoryMatch, Location or LocationMatch, so I'm thinking it's not allowed inside .htaccess. The RedirectMatch works like a charm, however. Thanks!
Tex
@Tex - I'll edit that out - it appears that the OP from the SVN question had trouble getting `DirectoryMatch` to work too.
Dominic Rodger
My understanding is that `Directory`, `DirectoryMatch`, `Location` and `LocationMatch` are not allowed in .htaccess .
Tex
@Tex - that looks right to me, based on http://httpd.apache.org/docs/2.0/mod/core.html#directorymatch, which says they're available in server config files (e.g. `httpd.conf`) and in `VirtualHost` containers in server config files, they are not allowed in `.htaccess` files.
Dominic Rodger
A: 

You always have the possibility to place .htaccess files in these folders and deny all access within the Folder.

I'm assuming you want a solution, where you don't have to put the .htaccess files in every folder and subfolder?

Try the following (assuming you have ssh access to the webserver):

  • Change into the DocumentRoot of your site
  • create a .htaccess file

Content:

Order deny, allow
Deny from all
  • Execute the following command:

    find . -type d -name .hg -exec cp ./.htaccess {} \;

  • Afterwards delete the .htaccess file from your document root again

Aurril
Hi, I like this solution, as well, but it has one drawback - the files inside the .hg directories are not versioned, and I'm pretty sure they are not included in a new clone of a repository. This means I'd have to perform this step again if I ever start over with a new clone, since the .hg/.htaccess files won't be automatically included in the clone, if I'm not mistaken.
Tex
Yes I think you are right. On a new clone it wouldn't be included.
Aurril
A: 

This is much less of a concern if you just keep the repositories outside of your DocumentRoot altogether. You're probably using hgweb or hgwebdir, which don't require the files be inside the DocumentRoot, so don't do it. Put them in /home/hg/repos or something and configure your hgwebdir.conf to look there.

The only reason to have the repos inside the DocumentRoot would be enable the static-http URL form for mercurial, but it's very slow and hgweb is always preferred when it's possible.

Ry4an