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

LTD-5812-file-upload-validation #2322

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

depsiatwal
Copy link
Contributor

@depsiatwal depsiatwal commented Jan 21, 2025

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

  1. If the user uploads a file that not in ACCEPTED_FILE_UPLOAD_MIME_TYPES then the Upload handler stops the upload and the form receives no file data the user sees the default required message for the form.
  2. if the user updated a file that's in ACCEPTED_FILE_UPLOAD_MIME_TYPES but not in ACCEPTED_FILE_UPLOAD_EXTENSIONS then the user will see the error from the FileExtensionValidator "The file type is not supported. Upload a supported file type"

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.

@depsiatwal depsiatwal marked this pull request as draft January 21, 2025 09:24
@depsiatwal depsiatwal force-pushed the LTD-5812-file-upload-validation branch 3 times, most recently from c982fd3 to 3cb5cb1 Compare January 22, 2025 09:37
@depsiatwal depsiatwal marked this pull request as ready for review January 22, 2025 09:40
@depsiatwal depsiatwal force-pushed the LTD-5812-file-upload-validation branch 3 times, most recently from 6aee124 to b2c0a27 Compare January 22, 2025 19:02
@uktrade uktrade deleted a comment from kevincarrogan Jan 22, 2025
@uktrade uktrade deleted a comment from kevincarrogan Jan 22, 2025
@uktrade uktrade deleted a comment from kevincarrogan Jan 22, 2025
@depsiatwal depsiatwal marked this pull request as draft January 22, 2025 19:18
@@ -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}")
Copy link
Contributor Author

@depsiatwal depsiatwal Jan 22, 2025

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)
Copy link
Contributor Author

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.

@depsiatwal depsiatwal force-pushed the LTD-5812-file-upload-validation branch from b2c0a27 to ee9e36a Compare January 23, 2025 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant