Problem
The FileDownloadController serves files for Drupal's private file system. It issues a Symfony BinaryFileResponse when a file is allowed to be retrieved. During preparation of this response, it utilizes Symfony's MIME type guesser to set the Contenty-Type header. By default, Symfony adds a couple of guessers, but as part of prehandling the response in DrupalKernel, Drupal registers it's own ExtensionMimeTypeGuesser that, like the name suggests, guesses the MIME type based on the extension.
Prior to Symfony 4.3, the MIME guesser was included as part of the Http Foundation component. As part of Symfony 4.3, a new dedicated MIME component was added and the old pieces in Http Foundation were deprecated. So now the problem: at this point, the BinaryFileResponse was also moved to using the new MIME component for guessing, but Drupal is still registering it's extension-based guesser with the deprecated MIME guesser mechanism. As a result, Drupal's guesser is no longer being invoked when private files are downloaded and you can get some weird results from the default Symfony guessers that cause a CSS file to be classified as an assembly file (for instance).
Steps to reproduce
- Add a CSS file with a rule like
.globl-item { display: none; }to the private file system. - Download the file and notice the content type is
text/x-asminstead of the expectedtext/css
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | interdiff-2-4.txt | 4.31 KB | kevin.dutra |
| #4 | 3262305-4.patch | 5.24 KB | kevin.dutra |
Comments
Comment #2
kevin.dutra commentedHere's the basic gist of the fix.
Comment #4
kevin.dutra commentedAnd here's round 2, with adjustments to accommodate tests. :)
Comment #5
kevin.dutra commentedOops, forgot the interdiff. Better late than never!
Comment #8
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
This could use an issue summary update using the default template. Mainly proposed solution and remaining tasks.
Comment #9
scambler commentedI was getting this error:
Refused to execute script from '...' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.This was on a .js file using the private file system. After applying patch #4, this is now getting the correct mime type and working as expected.
Drupal version: 9.5.3
Thanks @kevin.dutra
Comment #11
mohit_aghera commentedI came across this issue while doing bugsmash triage.
It seems, this is already fixed as part of #3172550: Register Drupal's mime type guesser the Symfony MimeTypes service.
Please re-open if you notice further issues.