Skip to content

Meringue Protected¤

This package includes functionality for organizing private files.

The main functionality is encapsulated in x_accel_redirect_view. It checks whether the user has access to view the file and serves it.

However, when working with nginx, you can enable the PROTECTED_SERVE_WITH_NGINX option. In this case, nginx itself will serve the file instead of Django, using the internal directive.

Usage¤

In the simplest scenario, you just need to enable view, and everything will work:

urlpatterns = [
    ...
    path(
        "protected/<int:cid>/<slug:field>/<slug:pk>",
        staff_member_required(x_accel_redirect_view),
        name="meringue-protected-file",
    ),
    ...
]

Nginx¤

Of course, when using nginx, files in the media folder need to be protected from access:

server {
    ...
    location /media/protected/ {
        return 404;
    }
    ...
}

However, for production, you can configure nginx to serve files instead of Django. To do this, you need to enable the PROTECTED_SERVE_WITH_NGINX option and configure nginx as follows:

server {
    ...
    location /media/protected/ {
        internal;
        alias /home/user/public/media/protected/;
    }
    ...
}

S3¤

Files in remote storages (for example, django-storages S3) are supported. When PROTECTED_SERVE_WITH_NGINX is off, the view, after checking access:

  • for an S3 storage with signed URLs (querystring_auth = True, the django-storages default) — redirects to a temporary signed URL of the file, so the file is downloaded from S3 directly. The URL makes S3 return the same Content-Disposition and Content-Type as the view;
  • for any other storage (including S3 with querystring_auth = False, where the URL would be public and permanent) — reads the file through the storage and serves it with Django.

Keep the bucket (or at least the protected folder) private, otherwise the files are available without the view.

S3 protection works differently

With local files and nginx, every download goes through the access check. With S3 the access is checked only when the signed URL is made: until the URL expires, anyone who has it can download the file (for example, if the user shares the link). Set a short lifetime for signed URLs, e.g. AWS_QUERYSTRING_EXPIRE = 60 (django-storages default is one hour): the URL is opened right after the redirect anyway.

PROTECTED_SERVE_WITH_NGINX does not work with S3 out of the box: an nginx internal location serves local files only. To use it, configure an internal nginx location that proxies to the bucket (proxy_pass) and give nginx access to the bucket. Usually it is simpler to turn the option off for S3 and use signed URLs.

ProtectedFileField and ProtectedImageField¤

To generate protected files, there are two fields available for the model - ProtectedFileField and ProtectedImageField.

class Foo(models.Model):
    file = ProtectedFileField(
        view_name="x_accel_redirect_view",
        host_name="sub",
        disposition="inline",
        nginx_location_getter=_test_getter,
    )
  • view_name - the name of the route handling the file request (mandatory field);
  • host_name - the host name for reversing to the view, when used in combination with django-hosts;
  • disposition - disposition for the Content-Disposition header;
  • nginx_location_getter - a method that returns the link through which the file will be served by nginx after redirection from the view. The default method is set in the parameter PROTECTED_NGINX_LOCATION_GETTER. The default method nginx_location_getter returns the original link to the file.

The original properties of the url fields have been changed to point to the view specified in the view_name attribute.

By default, the field saves the file in the protected directory.

Authors: Dmitry Dobrynin