-
Notifications
You must be signed in to change notification settings - Fork 1
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
LTD-5812-file-upload-validation #2322
base: dev
Are you sure you want to change the base?
Conversation
c982fd3
to
3cb5cb1
Compare
6aee124
to
b2c0a27
Compare
@@ -62,7 +62,10 @@ def receive_data_chunk(self, raw_data, start): | |||
mime = magic.from_buffer(raw_data, mime=True) | |||
if mime not in self.ACCEPTED_FILE_UPLOAD_MIME_TYPES: | |||
self.abort() | |||
raise UnacceptableMimeTypeError(f"Unsupported file type: {mime}") | |||
# Raise StopUpload signals the django multipartparser to exhaust the stream and stops the upload | |||
exception = UnacceptableMimeTypeError(f"Unsupported file type: {mime}") |
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.
SkipUpload halts the upload which is good practice as it prevents non supported file types being uploaded to S3.
As a result the calling form will not receive the file and sees an empty FileInput.
raise UnacceptableMimeTypeError(f"Unsupported file type: {mime}") | ||
# Raise StopUpload signals the django multipartparser to exhaust the stream and stops the upload | ||
exception = UnacceptableMimeTypeError(f"Unsupported file type: {mime}") | ||
logger.error(exception) |
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.
We now log the error since the django multipartparser will swallow this exception.
b2c0a27
to
ee9e36a
Compare
https://uktrade.atlassian.net/browse/LTD-5812
Current changes --
The file handler SafeS3FileUploadHandler now raises an exception that inherits from StopUpload this forces to halt the upload.
https://github.com/django/django/blob/64b1ac7292c72d3551b2ad70b2a78c8fe4af3249/django/http/multipartparser.py#L674
The form which requested the file will receive no file in the post request. Hence the following are supported
Previously there was a mismatch between 1 and 2. Also in Scenario 1 the usee would see a 500 error resulting in generic Service error.
Since the fileupload hander Halts the upload operation there appears to be no easy route to signal this to the view.
Checking Form data will be blank since no file has been upload , alternatively I tired intercepting in the middleware
request.FILES is always empty due to the file upload being halted. Have also tried form validation methods however the clean method for the field is empty as no file has been uploaded.