WordPress.org

Make WordPress Core

#46903 closed defect (bug) (duplicate)

Uploading .txt file fails due to 'fileinfo' extension thinking it's a file with MIME type 'text/html' instead of 'text/plain'

Reported by: renstillmann Owned by:
Milestone: Priority: normal
Severity: blocker Version: 5.1.1
Component: Upload Keywords: dev-feedback
Focuses: Cc:

Description

Hi,

I build a plugin which exports and imports settings and saves it to a .html file.
However the contents of this .txt file might or might not contain some HTML tags. This becomes an issue when it contains HTML tags when trying to upload the .txt file because in this case WordPress thinks the MIME type is 'text/html' while it really is 'text/plain'. WordPress throws an error stating that the file extension can't be uploaded due to security reasons. (understandable because it checks if 'text/html' == 'text/plain' which obviously isn't going to be the case. This info is returned by the 'finfo_file()' when extension 'fileinfo' is being used.

So now I thought it would be a good idea to just instead use the .html extension instead, but now the opposite is happening as soon as the .html file doesn't actually contain any html tags. Now wordpress thinks it's MIME type is 'text/plain' and again fails because due to the comparison of the .html file extension to the 'text/html' MIME type.

I did some debugging on this and found out about an array that was created for the same kind of problem with uploading other file types (csv, richtext, tsv, vtt).

In functions.php on line 2590 an array exists with " A few common file types are occasionally detected as text/plain; allow those", it is missing a value for the 'text/html' mime type. As soon as I add this I can upload the .html file (even when it doesn't contain any HTML tags) without problems.

Perhaps I am not aware of some other file extension that I really should be using?
The export/import file contains a serialized string with all settings. The settings may or may not contain HTML tags. It really doesn't care what file extension I use as long as it can be uploaded without issues to the Media library.

Hope someone can help me with this problem I am facing, thanks!

Change History (3)

#1 @tobifjellner
23 months ago

Temporary workaround could be to write some empty html comment in the file and disregard it afterwards, but you'll know the resulting type. :)

#2 @SergeyBiryukov
23 months ago

  • Component changed from General to Upload

#3 @joemcgill
23 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi @renstillmann,

Thanks for the detailed explanation of this issue. We’re currently tracking this in #40175. Please feel free to follow along with progress there.

Note: See TracTickets for help on using tickets.