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

Handle suspicious file operations and return a 404. #11596

Merged
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
12 changes: 8 additions & 4 deletions kolibri/utils/kolibri_whitenoise.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from wsgiref.headers import Headers

from django.contrib.staticfiles import finders
from django.core.exceptions import SuspiciousFileOperation
from django.core.files.storage import FileSystemStorage
from django.utils._os import safe_join
from six.moves.urllib.parse import parse_qs
Expand Down Expand Up @@ -294,10 +295,13 @@ def find_and_cache_dynamic_file(self, url, remote_baseurl):
return self.files.get(url)

def get_dynamic_path(self, url):
if self.static_prefix is not None and url.startswith(self.static_prefix):
return finders.find(url[len(self.static_prefix) :])
if self.dynamic_check is not None and self.dynamic_check.match(url):
return self.dynamic_finder.find(url)
try:
if self.static_prefix is not None and url.startswith(self.static_prefix):
return finders.find(url[len(self.static_prefix) :])
if self.dynamic_check is not None and self.dynamic_check.match(url):
return self.dynamic_finder.find(url)
except SuspiciousFileOperation:
pass

def candidate_paths_for_url(self, url):
paths = super(DynamicWhiteNoise, self).candidate_paths_for_url(url)
Expand Down
22 changes: 22 additions & 0 deletions kolibri/utils/tests/test_kolibri_whitenoise.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from kolibri.utils.kolibri_whitenoise import DynamicWhiteNoise
from kolibri.utils.kolibri_whitenoise import FileFinder
from kolibri.utils.kolibri_whitenoise import NOT_FOUND


def test_file_finder():
Expand Down Expand Up @@ -86,3 +87,24 @@ def test_dynamic_whitenoise():
os.remove(tempdir22tempfilepath)
os.removedirs(tempdir11)
os.removedirs(tempdir12)


def test_dynamic_whitenoise_suspicious_file():
tempdir11 = tempfile.mkdtemp()
tempdir12 = tempfile.mkdtemp()
prefix1 = "/test"
dynamic_whitenoise = DynamicWhiteNoise(
MagicMock(),
dynamic_locations=[
(prefix1, tempdir11),
(prefix1, tempdir12),
],
)
assert (
dynamic_whitenoise.find_and_cache_dynamic_file(
prefix1 + "/" + tempdir11 + "../../../leet_haxx0r.js", None
)
is not NOT_FOUND
)
os.removedirs(tempdir11)
os.removedirs(tempdir12)