Skip to content

Commit f9f4e69

Browse files
committed
Prevent URL parameter enumeration of thumbnails
Add a whitelist of allowed thumbnail dimensions and check against it when serving thumbnails as to prevent malicious enumeration of thumbnails.
1 parent b0c692a commit f9f4e69

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

django/thunderstore/frontend/views.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from urllib.parse import urlparse
44

55
from django.conf import settings
6+
from django.core.exceptions import PermissionDenied
67
from django.core.files.storage import default_storage
78
from django.http import FileResponse, HttpResponseNotFound, HttpResponseRedirect
89
from django.shortcuts import render
@@ -57,6 +58,9 @@ def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
5758
return context
5859

5960

61+
ALLOWED_DIMENSIONS = (64, 128, 256, 360, 480)
62+
63+
6064
class ThumbnailServeView(View):
6165
def get(self, request, *args, **kwargs):
6266
asset_path = self.kwargs.get("path")
@@ -67,6 +71,11 @@ def get(self, request, *args, **kwargs):
6771
except (ValueError, TypeError):
6872
width, height = 0, 0
6973

74+
# This is a check to prevent potential malicious enumeration of
75+
# thumbnail sizes to flood the storage / server.
76+
if width not in ALLOWED_DIMENSIONS or height not in ALLOWED_DIMENSIONS:
77+
raise PermissionDenied()
78+
7079
max_age = 300 # 5 minutes
7180

7281
if not asset_path or width <= 0 or height <= 0:

0 commit comments

Comments
 (0)