This repository has been archived by the owner on Aug 10, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make allowed file endings configurable
Fixes: #205
- Loading branch information
1 parent
f49afc2
commit 13e6a89
Showing
7 changed files
with
110 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from collections import OrderedDict | ||
|
||
from django.utils.safestring import mark_safe | ||
|
||
config = OrderedDict() | ||
config["ALLOWED_FILENAME_EXTENSIONS"] = (".java", mark_safe( | ||
"Filename extensions to be accepted in solution upload forms " | ||
"such as <code>.java, .c, .cpp, .h, .hpp</code>. " | ||
"Separate multiple filename extensions by commas." | ||
"Surrounding whitespace will be stripped." | ||
)) | ||
|
||
fieldsets = {"General settings": ["ALLOWED_FILENAME_EXTENSIONS"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from django.core.exceptions import ValidationError | ||
|
||
from constance import config | ||
|
||
|
||
def _get_allowed_filename_extensions(): | ||
""" | ||
Return all allowed filename extensions. | ||
""" | ||
allowed_filename_extensions = config.ALLOWED_FILENAME_EXTENSIONS | ||
if not allowed_filename_extensions: | ||
return None | ||
return [e.strip().lower() for e in allowed_filename_extensions.split(",")] | ||
|
||
|
||
def validate_filenames(upload): | ||
""" | ||
Verify that all uploaded files comply to the given naming constraints. | ||
""" | ||
allowed_filename_extensions = _get_allowed_filename_extensions() | ||
if not allowed_filename_extensions: | ||
raise ValidationError("Currently no filename extensions are accepted.") | ||
if not upload: | ||
raise ValidationError("No files were supplied.") | ||
for file_name, _ in upload.items(): | ||
if not file_name.lower().endswith(tuple(allowed_filename_extensions)): | ||
raise ValidationError( | ||
"One or more files contain disallowed filename extensions. " | ||
"(Allowed: {})".format(", ".join(allowed_filename_extensions)) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.test import TestCase | ||
|
||
from constance.test import override_config | ||
|
||
from inloop.solutions.validators import _get_allowed_filename_extensions, validate_filenames | ||
|
||
|
||
@override_config(ALLOWED_FILENAME_EXTENSIONS=".java , .b ,.cpp, \n.h,\r.py") | ||
class FileNameExtensionValidationTest(TestCase): | ||
def test_get_allowed_filename_extensions(self): | ||
filename_extensions = _get_allowed_filename_extensions() | ||
self.assertEqual([".java", ".b", ".cpp", ".h", ".py"], filename_extensions) | ||
|
||
def test_valid_uploads(self): | ||
uploads = { | ||
"HelloWorld.java": "public class HelloWorld {//...}", | ||
"HelloWorld.b": "+[-[<<[+[--->]-[<<<]]]>>>-]>-.---.>..>.<<<<-.<+.>>>>>.>.<<.<-.", | ||
"HelloWorld.cpp": "int main() {//...}", | ||
"HelloWorld.h": "", | ||
"HelloWorld.py": "print(\"Hello World\")" | ||
} | ||
try: | ||
validate_filenames(uploads) | ||
except ValidationError as e: | ||
self.fail("Filename validation should succeed on the " | ||
"given files. ({})".format(e.message)) | ||
|
||
def test_invalid_uploads(self): | ||
uploads = { | ||
"HelloWorld.kt": "class Test {}", | ||
} | ||
self.assertRaises(ValidationError, validate_filenames, uploads) | ||
|
||
def test_no_uploads(self): | ||
uploads = {} | ||
self.assertRaises(ValidationError, validate_filenames, uploads) | ||
|
||
|
||
@override_config(ALLOWED_FILENAME_EXTENSIONS="") | ||
class EmptyAllowedFileNameExtensionsValidationTest(TestCase): | ||
def test_get_allowed_filename_extensions(self): | ||
filename_extensions = _get_allowed_filename_extensions() | ||
self.assertFalse(filename_extensions) | ||
|
||
def test_uploads(self): | ||
uploads = { | ||
"HelloWorld.java": "public class HelloWorld {//...}", | ||
} | ||
self.assertRaises(ValidationError, validate_filenames, uploads) |