# Securing private file area

**URL:** <https://www.ruby-forum.com/t/securing-private-file-area/106460>\
**Category:** Rails\
**Created:** [June 10, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/securing-private-file-area/106460 "2007-06-10T21:16:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 10, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/securing-private-file-area/106460/1 "2007-06-10T21:16:01Z")

</div>

I’m building an application for a company that has thousands of clients,  
and  
for each client there will a set of documents that are private to that  
client. Rails makes it easy, of course, to show the list of documents on  
a  
per-client basis, using acts\_as\_authenticated and using current\_user as  
part  
of the find any time a document list is displayed.

The part for which I’m looking for the best solution is access to the  
documents themselves. My simple implementation has the Rails view  
showing a  
series of links to the files (which are word, excel, and powerpoint  
documents). The problem is that Rails isn’t involved at all in the  
actual  
document delivery, since these static files are served directly from  
Apache.  
So anyone who gets, or guesses, the URL can access these files without  
authentication.

I’ve come up with three possible solutions and would appreciate hearing  
what  
others have done, and whether there are holes that I’m missing, or  
another  
approach I’ve missed.

1. Security by obscurity. Use long random strings for the directory and  
file  
names. This is probably secure enough in practice, but certainly isn’t  
secure in theory and doesn’t feel quite right.

2. Deliver the static files via Rails. Put the documents in a directory  
tree  
outside of public and use a Rails action to read the file and deliver it  
to  
the browser. This seems like it would be secure, and doesn’t require any  
Apache configuration, but it would dramatically increase the server load  
when these files are viewed. I’m not sure whether that would be a real  
issue  
or not.

3. Referrer-based blocking in Apache. Set the Apache config to prohibit  
access to the documents directories if the referrer is not the Rails  
view  
that renders the list of files from the database. This seems like  
potentially the best solution but I’m wondering if there’s any security  
hole  
here that I’m missing. One weakness is that some people seem to have  
their  
web browser set to not provide a referrer, and those people wouldn’t be  
able  
to access the documents.

I’d appreciate any suggestions.

Michael S.  
[www.mslater.com](http://www.mslater.com)

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 10, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/securing-private-file-area/106460/2 "2007-06-10T22:20:12Z")

</div>

Hi~

On Jun 10, 2007, at 12:14 PM, Michael S. wrote:

> and powerpoint documents). The problem is that Rails isn’t involved  
> certainly isn’t secure in theory and doesn’t feel quite right.  
> Yeah this is not a satisfactory solution.

> 1. Deliver the static files via Rails. Put the documents in a  
> directory tree outside of public and use a Rails action to read the  
> file and deliver it to the browser. This seems like it would be  
> secure, and doesn’t require any Apache configuration, but it would  
> dramatically increase the server load when these files are viewed.  
> I’m not sure whether that would be a real issue or not.

You don’t want to do this by reading the whole file in inside rails  
and then streaming it out, that will skyrocket your memory consumption.

> 1. Referrer-based blocking in Apache. Set the Apache config to  
> prohibit access to the documents directories if the referrer is not  
> the Rails view that renders the list of files from the database.  
> This seems like potentially the best solution but I’m wondering if  
> there’s any security hole here that I’m missing. One weakness is  
> that some people seem to have their web browser set to not provide  
> a referrer, and those people wouldn’t be able to access the documents.

This won’t work in all situation so it’s not a viable option either.

> I’d appreciate any suggestions.

A few ways to skin this cat. The most efficient way is to use X-  
Sendfile header with lighttpd or the X-Accell-Redirect feature of  
nginx. I recommend nginx. THe way this works is that a request for a  
file goes to your rails app so you can authenticate the request, but  
all your rails action has to do is set a response header with the  
path to a file somehwere outside of your publiuc/ dir. THen the front  
end nginx server will see that header and serve the file directly.

Here’s a bit more info:  
[http://blog.kovyrin.net/2006/11/01/nginx-x-accel-redirect-php-rails/](http://blog.kovyrin.net/2006/11/01/nginx-x-accel-redirect-php-rails/)

Or you can use a gem called mongrel\_secure\_download . This is a  
custom mongrel handler that allows for the same type of thing but all  
in the same mongrel process. So this doesn’t require interaction with  
a front end webserver, but it still lets mongrel serve the files  
fairly fast by streaming them and not reading the whole thing into  
memory at once.

Good luck!

Cheers-

– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 10, 2007, 10:38pm UTC](https://www.ruby-forum.com/t/securing-private-file-area/106460/3 "2007-06-10T22:38:15Z")

</div>

You could do (and don’t quote me on this as I haven’t used it):

def fileserve  
file = Client.file.find(:first, …)  
respond\_to |format|  
format.zip {  
# send file here, not a link but a real file  
}  
end  
end

Sorry for my obscurity in the answer, don’t know how else to explain it.

That should work though I reckon. Never used respond\_to for anything  
but rails files though.

Cheers,  
Zach I.  
â†’ Blog – [http://www.zachinglis.com](http://www.zachinglis.com)  
â†’ Company – [http://www.lt3media.com](http://www.lt3media.com)  
â†’ Portfolio – [http://portfolio.zachinglis.com](http://portfolio.zachinglis.com)

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 12, 2007, 4:48am UTC](https://www.ruby-forum.com/t/securing-private-file-area/106460/4 "2007-06-12T04:48:25Z")

</div>

I’d definitely use x\_sendfile per Ezra. He neglects to mention that  
Apache also supports this, via mod\_xsendfile:

[http://tn123.ath.cx/mod\_xsendfile/](http://tn123.ath.cx/mod_xsendfile/)

I use this for exactly the reasons you describe.

Note, however, that you must be running Apache2, and your Apache must be  
compiled to support DSOs (I believe all Apache2 standard installs do  
support this). This further implies that you must control the server,  
which it seems you do.

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 11, 2007, 7:52am UTC](https://www.ruby-forum.com/t/securing-private-file-area/106460/5 "2007-06-11T07:52:55Z")

</div>

On 6/10/07, Zach I. // LT3media [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> Sorry for my obscurity in the answer, don’t know how else to explain it.
> 
> That should work though I reckon. Never used respond\_to for anything but  
> rails files though.

That’ll lead to high memory usage, depending on the sizes of the  
files. In general, I’d stay away from send\_data or the send\_file  
methods. I’ve seen mongrel sizes over 1GB from serving files up to  
200MB. I switched it to a heavily modified mongrel\_secure\_download  
handler and all was right in the world.

So in general, do as Ezra said 🙂

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 12, 2007, 6:20am UTC](https://www.ruby-forum.com/t/securing-private-file-area/106460/6 "2007-06-12T06:20:31Z")

</div>

Thanks everyone for your suggestions. I’m running at Rails Machine and  
would like to stick with their standard stack, which includes Apache  
2.2, so it sounds like mod\_xsendfile is the way to go.

Michael  
[www.mslater.com](http://www.mslater.com)

On Jun 11, 7:48 pm, Brian A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 12, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/securing-private-file-area/106460/7 "2007-06-12T14:36:22Z")

</div>

Forgot to mention that there’s a plug-in to make life easier:

[http://john.guen.in/rdoc/x\_send\_file/](http://john.guen.in/rdoc/x_send_file/)
