-
Notifications
You must be signed in to change notification settings - Fork 623
Fix file upload security loopholes #14
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
682db04
Add file upload validation
gaya3-zipstack 7317bb2
Code formatting
gaya3-zipstack c78eba1
Code formatting
gaya3-zipstack 8ccfd49
Merge branch 'main' into fix/file_upload_security
gaya3-zipstack bbbfa11
Change values to constants
gaya3-zipstack 0646539
Merge branch 'fix/file_upload_security' of https://github.com/Zipstac…
gaya3-zipstack 25b1fc4
Merge remote-tracking branch 'origin' into fix/file_upload_security
gaya3-zipstack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,83 @@ | ||
| import magic | ||
| from os.path import splitext | ||
|
|
||
| from django.core.exceptions import ValidationError | ||
| from django.template.defaultfilters import filesizeformat | ||
| from django.utils.translation import gettext_lazy as _ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gaya3-zipstack are these imports used? Did you run it against pre-commit |
||
| from django.utils.translation import ngettext_lazy | ||
|
|
||
|
|
||
| class FileValidator(object): | ||
| """ | ||
| Validator for files, checking the size, extension and mimetype. | ||
|
|
||
| Initialization parameters: | ||
| allowed_extensions: iterable with allowed file extensions | ||
| ie. ('txt', 'doc') | ||
| allowed_mimetypes: iterable with allowed mimetypes | ||
| ie. ('image/png', ) | ||
| min_size: minimum number of bytes allowed | ||
| ie. 100 | ||
| max_size: maximum number of bytes allowed | ||
| ie. 24*1024*1024 for 24 MB | ||
|
|
||
| """ | ||
|
|
||
| extension_message = _("Extension '%(extension)s' not allowed. " | ||
| "Allowed extensions are: '%(allowed_extensions)s.'") | ||
| mime_message = _("MIME type '%(mimetype)s' is not valid. " | ||
| "Allowed types are: %(allowed_mimetypes)s.") | ||
| min_size_message = _('The current file %(size)s, which is too small. ' | ||
| 'The minumum file size is %(allowed_size)s.') | ||
| max_size_message = _('The current file %(size)s, which is too large. ' | ||
| 'The maximum file size is %(allowed_size)s.') | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| self.allowed_extensions = kwargs.pop('allowed_extensions', None) | ||
| self.allowed_mimetypes = kwargs.pop('allowed_mimetypes', None) | ||
| self.min_size = kwargs.pop('min_size', 0) | ||
| self.max_size = kwargs.pop('max_size', None) | ||
|
|
||
| def __call__(self, value): | ||
| """ | ||
| Check the extension, content type and file size for each file | ||
| """ | ||
| for file in value: | ||
| # Check the extension | ||
| ext = splitext(file.name)[1][1:].lower() | ||
| if self.allowed_extensions and not ext in self.allowed_extensions: | ||
| message = self.extension_message % { | ||
| 'extension' : ext, | ||
| 'allowed_extensions': ', '.join(self.allowed_extensions) | ||
| } | ||
|
|
||
| raise ValidationError(message) | ||
|
|
||
| # Check the content type | ||
| mimetype = magic.from_buffer(file.read(2048), mime=True) | ||
| if (self.allowed_mimetypes and | ||
| not mimetype in self.allowed_mimetypes): | ||
| message = self.mime_message % { | ||
| 'mimetype': mimetype, | ||
| 'allowed_mimetypes': ', '.join(self.allowed_mimetypes) | ||
| } | ||
|
|
||
| raise ValidationError(message) | ||
|
|
||
| # Check the file size | ||
| filesize = len(file) | ||
| if self.max_size and filesize > self.max_size: | ||
| message = self.max_size_message % { | ||
| 'size': filesizeformat(filesize), | ||
| 'allowed_size': filesizeformat(self.max_size) | ||
| } | ||
|
|
||
| raise ValidationError(message) | ||
|
|
||
| elif filesize < self.min_size: | ||
| message = self.min_size_message % { | ||
| 'size': filesizeformat(filesize), | ||
| 'allowed_size': filesizeformat(self.min_size) | ||
| } | ||
|
|
||
| raise ValidationError(message) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gaya3-zipstack will this impact file upload for workflows / API deployment as well? We'll have other file types to allow in that case