10

According to Django document: "it was common to place static assets in MEDIA_ROOT along with user-uploaded files, and serve them both at MEDIA_URL. "

Does that mean everyone could access other people's uploaded files? Isn't it unsafe?

Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
yejinxin
  • 586
  • 1
  • 3
  • 13

2 Answers2

6

Yes

A clever user can possibly guess the path to media files belonging to other users.

Django was born in the news publishing business where this was not of concern: the admin is based in the concept of trusted users like writers and editors belonging to the same organization.

Mitigation

Requiring authentication

Not my first choice but you can make the webserver authenticate against Django's user database:

WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com

WSGIProcessGroup %{GLOBAL}
WSGIApplicationGroup %{GLOBAL}

<Location "/media/private-user-content/">
    AuthType Basic
    AuthName "Top Secret"
    Require valid-user
    AuthBasicProvider wsgi
    WSGIAuthUserScript /path/to/mysite.com/mysite/wsgi.py
</Location>

The accepted answer recommends serving sensitive files from an authenticated Django view. It is OK for low traffic apps but for larger projects it carries a performance hit not every site can afford.

Serving from Cloud Storage Services

Large projects should be using some cloud storage backend for both performance and cost considerations. If your project is already hosted at some of the big 3 (AWS, GCP, Azure) check Django Storages. For example, if you are using the S3 backend, you can turn "query parameter authentication" for generated URLs and voila, problem gone. This has some advantages:

  • it is transparent to developers
  • enterprise-grade performance
  • lower cost of storage and network
  • probably the most secure option

Obfuscating the path

For small projects where you are serving media and application from the same webserver you can make very hard for nosy users to find media files not belonging to them:

1) disable the web server "auto index" in the MEDIA_ROOT folder. For apache, it is like:

<Directory /path/to/application/media/root>
   Options -Indexes
</Directory>

Without indexes, in order to access files belonging to other people you will have to guess the exact file name.

2) make the file path hard to guess using a crypto hash in the "upload_to" parameter from FileFields:

def hard_to_guess(instance, filename):
    salt = 'six random words for hidden salt'
    hash =  hashlib.md5(instance.user.username + salt)
    return '/'.join(['content', hash, filename])

...

class SomeModel(models.Model):
    ...
    user = models.ForeignKey(User)        
    content = models.FileField(upload_to=hard_to_guess)
    ...

This solution has no performance hit because media files are still served directly from the webserver.

Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
4

To answer your question: yes, this would allow everyone to access everybody's uploaded files. And yes, this is a security risk.

As a general rule, sensitive files should never be served directly from the filesystem. As another rule, all files should be considered sensitive unless explicitly marked otherwise.

The origin of the MEDIA_ROOT and MEDIA_URL settings probably lie in Django's history as a publishing platform. After all, your editors probably won't mind if the pictures they add to articles can easily be found. But then again, pictures accompanying an article are usually non-sensitive.

To expand on your question: sensitive files should always be placed in a directory that is not directly accessible by the web server. Requesting those files should only be done through a view class or function, which can do some sensible access checking before serving the file.

Also, do not rely on obfuscation for sensitive files. For example, let's use Paulo's example (see other answer) to obfuscate photo albums. Now my pictures are stored like MEDIA_URL/A8FEB0993BED/P100001.JPG. If I share this link with someone else, they can easily try URLs like MEDIA_URL/A8FEB0993BED/P710032.JPG, basically allowing them to brute-force my entire photo album.

publysher
  • 11,214
  • 1
  • 23
  • 28
  • So, this has something to do with django's background. Serving user-uploaded files directly is ok for news-oriented sites, but that's not general case, right? – yejinxin Sep 08 '12 at 05:37
  • That is basically correct. Whenever you are in a situation where 'user-uploaded' means 'uploaded by an editor', you're save. Other examples are blogs, review sites, brand websites, etc. – publysher Sep 09 '12 at 08:05
  • If you add a second hash for the filename using the same technique, you make the file path virtually impossible to bruteforce. This should be good enough for most web applications. – Paulo Scardine Sep 09 '12 at 14:16
  • That's true. However, once such a link has become publicly known it's no longer secure. If you only serve your files through a custom view, you can do your authorization on every request. – publysher Sep 10 '12 at 07:29