Skip to content

Commit

Permalink
Merge pull request #1301 from yogeshojha/1202-bug-risk-of-leaking-the…
Browse files Browse the repository at this point in the history
…-scan-result-files

(Security) Fixes #1202 bug risk of leaking the scan result files
  • Loading branch information
yogeshojha authored Jul 5, 2024
2 parents 180010a + 2570004 commit ca8389b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
4 changes: 3 additions & 1 deletion config/nginx/rengine.conf
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ server {
alias /usr/src/app/staticfiles/;
}

location /media/ {
location /protected_media/ {
internal;
alias /usr/src/scan_results/;
autoindex off;
}

ssl_protocols TLSv1.2;
Expand Down
1 change: 1 addition & 0 deletions web/reNgine/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
DJANGO_CELERY_BEAT_TZ_AWARE = False

MEDIA_URL = '/media/'
MEDIA_ROOT = '/usr/src/scan_results/'
FILE_UPLOAD_MAX_MEMORY_SIZE = 100000000
FILE_UPLOAD_PERMISSIONS = 0o644
STATIC_URL = '/staticfiles/'
Expand Down
12 changes: 10 additions & 2 deletions web/reNgine/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from drf_yasg.views import get_schema_view
from rest_framework import permissions

from reNgine.views import serve_protected_media

schema_view = get_schema_view(
openapi.Info(
title="reNgine API",
Expand Down Expand Up @@ -53,5 +55,11 @@
include(
'api.urls',
'api')),
] + static(settings.MEDIA_URL, document_root=settings.RENGINE_RESULTS) + \
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
path(
'media/<path:path>',
serve_protected_media,
name='serve_protected_media'
),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
# ] + static(settings.MEDIA_URL, document_root=settings.RENGINE_RESULTS) + \

21 changes: 21 additions & 0 deletions web/reNgine/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
import mimetypes
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, Http404
from django.conf import settings

@login_required
def serve_protected_media(request, path):
file_path = os.path.join(settings.MEDIA_ROOT, path)
if os.path.isdir(file_path):
raise Http404("File not found")
if os.path.exists(file_path):
content_type, _ = mimetypes.guess_type(file_path)
response = HttpResponse()
# response['Content-Disposition'] = f'attachment; filename={os.path.basename(file_path)}'
response['Content-Type'] = content_type
response['X-Accel-Redirect'] = f'/protected_media/{path}'
return response
else:
raise Http404("File not found")

0 comments on commit ca8389b

Please sign in to comment.