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

Fix to verify pdf file type and limit pdf size #1785

Merged

Conversation

amitupreti
Copy link
Contributor

  1. Added a utility function to verify if the uploaded file is a pdf file or not.

    Currently users are able to upload any file type by changing the file extension in the training form.
    This commit adds a utility function to verify if the uploaded file is a pdf file or not.

  2. Update test to work with the training pdf validation The new change makes sure that the file uploaded is a valid pdf and is readable. Our previous test was using django core SimpleUploadedFile to create a plain/txt file and uploading that which is not a valid pdf(even if we change the content type). So the solution to this would be to either create pdf on the fly during the test or use a static valid pdf file.

    I chose the later option, because creating pdf on the fly would require us to add a new dependency to the project which might introduce a security risk and need more discussion.

  3. Added a file size limit for pdf Also, earlier there was no limit on the file size, i added a limit of 1MB Default(but customizable from .env) for the pdf file For the file size validation function, i added it in the user.validators instead of adding it somewhere else like physionet.utility because, i thought we might to implement different sizes for different applications, so i didnot try to make it generic.

1. Added a utility function to verify if the uploaded file is a pdf file or not.

    Currently users are able to upload any file type by changing the file extension in the training form.
    This commit adds a utility function to verify if the uploaded file is a pdf file or not.

2. Update test to work with the training pdf validation The new change makes sure that the file uploaded is a valid pdf and is readable. Our previous test was using django core SimpleUploadedFile to create a plain/txt file and uploading that which is not a valid pdf(even if we change the content type). So the solution to this would be to either create pdf on the fly during the test or use a static valid pdf file.

    I chose the later option, because creating pdf on the fly would require us to add a new dependency to the project which might introduce a security risk and need more discussion.

3. Added a file size limit for pdf Also, earlier there was no limit on the file size, i added a limit of 1MB Default(but customizable from .env) for the pdf file For the file size validation function, i added it in the user.validators instead of adding it somewhere else like physionet.utility because, i thought we might to implement different sizes for different applications, so i didnot try to make it generic.
physionet-django/physionet/settings/base.py Outdated Show resolved Hide resolved
physionet-django/physionet/settings/base.py Outdated Show resolved Hide resolved
physionet-django/user/validators.py Outdated Show resolved Hide resolved
physionet-django/user/validators.py Outdated Show resolved Hide resolved
1. In previous commit, i had forgotten to change the variable name for the size upload limit(we wanted to use the file size limit in KB, i had made changes for that, but forgot to make change in the function)
2. Also there was an mistake where the size limit has hardcoded as 5MB, so we updated the code to show the actual limit as per .env setup in KB
@amitupreti amitupreti force-pushed the fix/arbitary_file_upload_in_training_v3 branch from b072d1b to 2b0b8d4 Compare January 6, 2023 15:45
…e forget to set the size limit on env file
@tompollard
Copy link
Member

Thanks @amitupreti, looks good to me!

@tompollard tompollard merged commit 03ba322 into MIT-LCP:dev Jan 6, 2023
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.

None yet

2 participants