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.
Currently the content type/file extension is extracted from the data url provided by the user, rather than determined by the system based on the actual decoded content. This is not ideal since the user could maliciously provide the wrong type, or because the user's system may unwittingly provide an incorrect or generic type.
This PR adds an additional library to determine the actual MIME type and extension from the decoded content, and only falls back to the user-provided value if it is unable to be determined.
A specific example- using FileReader.readAsDataURL client-side on an OTF file on a Mac returns a generic application/octet-stream type, rather than a font type. So then when the data url string is passed to an uploader with a
content_type_whitelist
only allowing font types, it would incorrectly deny the file due to the invalid user-provided content type value. This problem may be part of issue #40.Even in the gem's own specs, the adapter_spec and base64_string_io_spec tests were using a PNG image incorrectly labeled as a JPEG image, as well as several other incorrect fixture types. I updated the specs to use a small actual file for each previously tested file type, which are now correctly determined by the content.
Also included is a fix to prefer the last-added MIME::Type, which should be the user's customized type, if one has been provided. I added a MIME::Type override to the test setup to simulate a user changing a preferred file extension, so that is properly tested now as well.
Current workarounds for the main issue include skipping CarrierWave's validations and using another file validator to determine the type after the file is parsed by the uploader, but preferably it would be handled inside the gem so that CarrierWave's built-in whitelists can be used.