Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to protect media folder #1476

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ RUN set -eux; \
libmagic1 \
libcairo2 \
libpango1.0-0 \
libpcre3 \
libpcre3-dev \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deze packages zijn nodig om uwsgi met PCRE ondersteuning te bouwen. Dit is nodig voor de X-Sendfile emulation.

libpq-dev \
gcc \
graphviz \
Expand Down
23 changes: 23 additions & 0 deletions app/signals/apps/media/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Protected media
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering this is a separate app, maybe it would be worth noting that it needs to be added to the SIGNALS_APPS in app/signals/settings.py?


This app provides the possibility to protect the media folder. To use this functionality in production, make sure to configure the PROTECTED_FILE_SYSTEM_STORAGE setting.

Then specific the following uWSGI settings to protect the media folder:

```bash
uwsgi \
--master \
--http=0.0.0.0:8000 \
--module=signals.wsgi:application \
--static-map=/signals/static=./app/static \
--static-safe=./app/media \
--offload-threads=2 \
--collect-header="X-Sendfile X_SENDFILE" \
--response-route-if-not="empty:${X_SENDFILE} static:${X_SENDFILE}" \
--buffer-size=32768 \
--die-on-term \
--processes=4 \
--threads=2
```

The relevant settings are `plugins`, `offload-threads`, `collect-header` and `response-route-if-not`. For more information see the [X-Sendfile emulation snippet of the uWSGI documentation](https://uwsgi-docs.readthedocs.io/en/latest/Snippets.html#x-sendfile-emulation).
2 changes: 2 additions & 0 deletions app/signals/apps/media/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# SPDX-License-Identifier: MPL-2.0
# Copyright (C) 2024 Delta10 B.V.
8 changes: 8 additions & 0 deletions app/signals/apps/media/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: MPL-2.0
# Copyright (C) 2024 Delta10 B.V.
from django.apps import AppConfig


class MediaConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'media'
24 changes: 24 additions & 0 deletions app/signals/apps/media/storages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-License-Identifier: MPL-2.0
# Copyright (C) 2024 Delta10 B.V.
from urllib.parse import urljoin

from django.core import signing
from django.core.files.storage import FileSystemStorage
from django.utils.encoding import filepath_to_uri

signer = signing.TimestampSigner()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to make this more secure, would it be a good idea to pass a salt to the signer?
By default the salt is set like this: https://github.com/django/django/blob/5b4d949d7ca118e70985ffc53f8191b766591c12/django/core/signing.py#L188C1-L191C10

It doesn't seem like a huge deal considering the secret is also used, so as long as that is unique we should still receive acceptable signatures. But better safe than sorry I suppose.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SHA256 is used by default as the hashing algo here. We might want to explicitly specify it to prevent any issues of it changing in the future. That is if we deem SHA256 sufficient for this use case.
One of it's advantages is that it's fast, however that is also a disadvantage as it would also be possible to relatively quickly produce things like rainbow tables.
Considering we might not need lightning fast speeds here, we could perhaps choose an even more secure algorithm here?



class ProtectedFileSystemStorage(FileSystemStorage):
def url(self, name):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please use type hints for the arguments and the return type?

if self.base_url is None:
raise ValueError('This file is not accessible via a URL.')

url = filepath_to_uri(name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we converting a file path or a file name here? If it's a path, please rename the argument.

if url is not None:
url = url.lstrip('/')

signature = signer.sign(url).split(':')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that if the same file is requested multiple times within the same second the signature that is produced will be exactly the same. Not sure if that is acceptable?


full_path = urljoin(self.base_url, url)
return full_path + f'?t={signature[1]}&s={signature[2]}'
46 changes: 46 additions & 0 deletions app/signals/apps/media/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# SPDX-License-Identifier: MPL-2.0
# Copyright (C) 2024 Delta10 B.V.
from unittest.mock import patch

from django.http import HttpResponse
from django.test import TestCase, override_settings

from signals.apps.media.storages import ProtectedFileSystemStorage


@override_settings(PROTECTED_FILE_SYSTEM_STORAGE=True)
class DownloadFileTestCase(TestCase):
def setUp(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use type hints for the methods here. It should be as easy as adding -> None: to each of them.

self.storage = ProtectedFileSystemStorage(base_url='http://localhost:8000/signals/media/')

def test_missing_signature(self):
# Test with missing 't' or 's' parameter
response = self.client.get('/signals/media/test.txt')
self.assertEqual(response.status_code, 401)
self.assertEqual(response.content, b'No signature provided')

def test_bad_signature(self):
# Test with an invalid signature
response = self.client.get('/signals/media/test.txt?t=some_time&s=some_signature')
self.assertEqual(response.status_code, 401)
self.assertEqual(response.content, b'Bad signature')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no test case for expired signatures?

@override_settings(DEBUG=True)
def test_debug_mode_file_serving(self):
# Test serving the file in DEBUG mode
with patch('signals.apps.media.views.serve') as mock_serve:
mock_serve.return_value = HttpResponse('File content')
response = self.client.get(self.storage.url('test.txt'))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'File content')
mock_serve.assert_called_once()

@override_settings(DEBUG=False)
def test_production_mode_file_serving(self):
# Test serving the file in production mode
with patch('signals.apps.media.views.mimetypes.guess_type') as mock_mimetype:
mock_mimetype.return_value = 'text/plain', None
response = self.client.get(self.storage.url('test.txt'))
self.assertEqual(response.status_code, 200)
self.assertIn('test.txt', response['X-Sendfile'])
self.assertEqual(response['Content-Type'], 'text/plain')
9 changes: 9 additions & 0 deletions app/signals/apps/media/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: MPL-2.0
# Copyright (C) 2024 Delta10 B.V.
from django.urls import re_path

from . import views

urlpatterns = [
re_path(r'^(?P<path>.*)$', views.download_file, name='download_file'),
]
45 changes: 45 additions & 0 deletions app/signals/apps/media/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# SPDX-License-Identifier: MPL-2.0
# Copyright (C) 2024 Delta10 B.V.
import mimetypes
import os
from datetime import timedelta

from django.conf import settings
from django.core import signing
from django.http import HttpResponse
from django.views.static import serve

signer = signing.TimestampSigner()


def download_file(request, path):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use type hints for the arguments and return type here.

t = request.GET.get('t')
s = request.GET.get('s')

if not t or not s:
return HttpResponse('No signature provided', status=401)

try:
signer.unsign(f'{path}:{t}:{s}', max_age=timedelta(hours=1))
except signing.SignatureExpired:
return HttpResponse('Signature expired', status=401)
except signing.BadSignature:
return HttpResponse('Bad signature', status=401)

if settings.DEBUG:
response = serve(request, path, document_root=settings.MEDIA_ROOT, show_indexes=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little nitpicky, but this function is already using early returns, why not just return here as well instead of having the else block?

else:
mimetype, encoding = mimetypes.guess_type(path)

response = HttpResponse()

if mimetype:
response['Content-Type'] = mimetype
if encoding:
response['Content-Encoding'] = encoding

response['X-Sendfile'] = os.path.join(
settings.MEDIA_ROOT, path
).encode('utf8')

return response
8 changes: 7 additions & 1 deletion app/signals/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,16 @@ def is_super_user(user) -> bool:
MEDIA_URL: str = '/signals/media/'
MEDIA_ROOT: str = os.path.join(os.path.dirname(BASE_DIR), 'media')

DEFAULT_FILE_STORAGE: str = 'django.core.files.storage.FileSystemStorage'

PROTECTED_FILE_SYSTEM_STORAGE: bool = os.getenv('PROTECTED_FILE_SYSTEM_STORAGE', False) in TRUE_VALUES
if PROTECTED_FILE_SYSTEM_STORAGE:
DEFAULT_FILE_STORAGE = 'signals.apps.media.storages.ProtectedFileSystemStorage'

AZURE_STORAGE_ENABLED: bool = os.getenv('AZURE_STORAGE_ENABLED', False) in TRUE_VALUES
if AZURE_STORAGE_ENABLED:
# Azure Settings
DEFAULT_FILE_STORAGE: str = 'storages.backends.azure_storage.AzureStorage'
DEFAULT_FILE_STORAGE = 'storages.backends.azure_storage.AzureStorage'

AZURE_ACCOUNT_NAME: str | None = os.getenv('AZURE_STORAGE_ACCOUNT_NAME')
AZURE_ACCOUNT_KEY: str | None = os.getenv('AZURE_STORAGE_ACCOUNT_KEY')
Expand Down
10 changes: 4 additions & 6 deletions app/signals/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
path('signals/', BaseSignalsAPIRootView.as_view()),
path('signals/', include('signals.apps.api.urls')),

# The media folder is routed with X-Sendfile when DEBUG=False and
# with the Django static helper when DEBUG=True
path('signals/media/', include('signals.apps.media.urls')),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens here if the app is not configured to be loaded in the settings?


# The Django admin
path('signals/admin/', admin.site.urls),
re_path(r'^signals/markdownx/', include('markdownx.urls')),
Expand All @@ -27,12 +31,6 @@
path('signals/sigmax/', include('signals.apps.sigmax.urls')),
]

if settings.DEBUG:
from django.conf.urls.static import static

media_root = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += media_root

if settings.OIDC_RP_CLIENT_ID:
urlpatterns += [
path('signals/oidc/login_failure/', TemplateView.as_view(template_name='admin/oidc/login_failure.html')),
Expand Down
Loading