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

Checking MIME types and file extension #7083

Merged
merged 9 commits into from
Jun 15, 2024

Conversation

AinaMerch
Copy link
Contributor

@AinaMerch AinaMerch commented May 28, 2024

Proposed Changes

When a student submits a file, use the Marcel gem (https:/rails/marcel) in the back-end to guess the mimetype of the uploaded file, and compare against file extension (note that Marcel can also tell the expected mimetype based on file extension). If there's a mismatch, flash a warning message to the student, but allow the file to be submitted as normal.

In doing this, the main use case, checking .docx to .pdf works, but .txt to .pdf doesn't flash a warning. A test case that fails is added to show that issue.
...

Screenshots of your changes (if applicable)
Associated [documentation repository](https:/MarkUsProject/Wiki) pull request (if applicable)

Type of Change

(Write an X or a brief description next to the type or types that best describe your changes.)

Type Applies?
🚨 Breaking change (fix or feature that would cause existing functionality to change)
New feature (non-breaking change that adds functionality) X
🐛 Bug fix (non-breaking change that fixes an issue)
🎨 User interface change (change to user interface; provide screenshots)
♻️ Refactoring (internal change to codebase, without changing functionality)
🚦 Test update (change that only adds or modifies tests) X
📦 Dependency update (change that updates a dependency)
🔧 Internal (change that only affects developers or continuous integration)

Checklist

(Complete each of the following items for your pull request. Indicate that you have completed an item by changing the [ ] into a [x] in the raw text, or by clicking on the checkbox in the rendered description on GitHub.)

Before opening your pull request:

  • I have performed a self-review of my changes.
    • Check that all changed files included in this pull request are intentional changes.
    • Check that all changes are relevant to the purpose of this pull request, as described above.
  • I have added tests for my changes, if applicable.
    • This is required for all bug fixes and new features.

After opening your pull request:

  • I have updated the project Changelog (this is required for all changes).
  • I have verified that the pre-commit.ci checks have passed.
  • I have verified that the CI tests have passed.
  • I have reviewed the test coverage changes reported by Coveralls.
  • I have requested a review from a project maintainer.

Questions and Comments

(Include any questions or comments you have regarding your changes.)

@coveralls
Copy link
Collaborator

coveralls commented May 31, 2024

Pull Request Test Coverage Report for Build 9443854722

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 46 of 46 (100.0%) changed or added relevant lines in 2 files are covered.
  • 30 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.04%) to 91.385%

Files with Coverage Reduction New Missed Lines %
app/lib/git_repository.rb 1 59.3%
app/assets/javascripts/Components/Result/notebook_viewer.jsx 7 0.0%
app/assets/javascripts/Components/Helpers/range_selector.js 22 0.0%
Totals Coverage Status
Change from base Build 9231200649: -0.04%
Covered Lines: 40048
Relevant Lines: 43144

💛 - Coveralls

Copy link
Contributor

@david-yz-liu david-yz-liu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AinaMerch make sure to update the changelog.

app/controllers/submissions_controller.rb Show resolved Hide resolved
expected_mime_type = Marcel::MimeType.for extension: file_extension

if content_type != expected_mime_type && content_type != 'application/octet-stream'
flash_message(:warning, "The uploaded file doesn't match its file extension (#{file_extension}).")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Internationalize this string

expect(@student).to have_accepted_grouping_for(@assignment.id)

# Check MIME type for a correct file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should create new tests rather than add to existing tests. Define different unit tests for each test input.

@AinaMerch AinaMerch changed the title Checking MIME types and file extension - Failing Tests Checking MIME types and file extension Jun 9, 2024
@david-yz-liu
Copy link
Contributor

@AinaMerch looks like the tests failed, please take a look at that

params: { course_id: course.id, assignment_id: @assignment.id, new_files: [valid_file] }
expect(response).to have_http_status :ok

# update_files action assert assign to various instance variables.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to include the checks for "assigns" here or in the test below, they aren't relevant to this particular test.

expect(@student).to have_accepted_grouping_for(@assignment.id)

invalid_file = fixture_file_upload('docx_file.docx', 'application/pdf')
allow(Marcel::MimeType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using mocking, create a second test file: take a word document and rename it so that its extension is .pdf. Make sure the file name itself communicates that this is what has occurred, so that there's no confusion later. This creates a more authentic test than just mocking alone.

params: { course_id: course.id, assignment_id: @assignment.id, new_files: [invalid_file] }

expect(response).to have_http_status :ok
sample_warning_message = "The uploaded file doesn't match its file extension (#{file_extension})."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use string literals, use the internationalized string you defined

@@ -0,0 +1 @@
abcd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this file is not being used in testing that's fine, but in this case you should remove it

@@ -35,6 +35,7 @@ en:
empty_file_warning: "%{file_name} is empty"
external_submit_only: MarkUs is only accepting external submits
file_conflicts: 'Your changes have not been made. The following file conflicts were detected:'
file_extension_mismatch: The uploaded file doesn't match its file extension %{extension}.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reword to "The contents of the uploaded file do not match the file extension %{extension}".

@david-yz-liu david-yz-liu merged commit 463c860 into MarkUsProject:master Jun 15, 2024
6 checks passed
@david-yz-liu david-yz-liu added this to the v2.4.12 milestone Jun 25, 2024
donny-wong pushed a commit to donny-wong/Markus that referenced this pull request Jul 2, 2024
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.

3 participants