Repository navigation
Regression on initialization and new non-deterministic behavior issue in mimetypes #93417
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jun 1, 2022 Thanks for pinging me. I'll share what I know and can remember.
The original bug regarded the non-deterministic behavior of the guess_extension method/function. It would return different answers upon different executions of the python interpreter and import of the mimetypes module. I suggest reading through all of https://bugs.python.org/issue4963 as it is very informative as to the discussion and approaches to a solution that were kicked around.
The change you've fingered is related to this comment by me in that issue:
- I modified the init() function to check the files argument as r.david.murray suggested. If it is supplied, then the existing database is used and the files are added to it. If it is not supplied, then the module reinitializes from scratch. I'll update the documentation to reflect this if the commit passes muster.
That comment is referring to R. David Murray's comment in that issue: https://bugs.python.org/issue4963#msg108934
It's worth reading that comment carefully and in its entirety to understand how the change to init() tried to walk a very fine line.
I think this current issue is a result of that analysis and my fix related to it missing the use case where files is definitely not meant to be additive - it should be the only thing loaded.
In terms of a workaround, I think the easiest thing that should work is:
import mimetypes mimetypes.knownfiles = [] mimetypes.init(files=...)Perhaps a longer-term fix would be to add a boolean argument to init() that controls whether or not knownfiles are used additively.
Just my $0.02.
Reacted by Aaron Deadman- added a commit that references this issue
on Jun 1, 2022 Thanks for your detailed comment @davidkhess and thanks for opening the report @pombredanne.
From R. David Murray's analysis in the original issue:
It's not so clear what the behavior should be when you pass init one or more files. It is possible, even highly probable, that there is code out there that depends on the fact that doing so is additive.
I'm not clear exactly what was meant by "additive", in this context. However, an examination of the
init()code before the change would suggest that this was not always the case, as previously a call toinit()passing in a list of files used only the files supplied, and used the knownfiles as a default if no files were specified. A caller would have to explicitly pass the known files in like this:mimetypes.init(files=['my_mimetype_file'] + mimetypes.knownfiles).I believe his analysis was perhaps referring to calling
MimeTypes()a second time would not reinitialise the global db, as theinitedflag prevents that from happening, but not a call tomimetypes.init()explicitly.Regardless of the validity of this point of analysis, the fact remains that the current behaviour of mimetypes.init (since Python 3.7) is to load from the knownfiles as well as the files specified, and so Hyrum's Law would suggest that to put the behavior back as it was will inconvenience someone.
Perhaps as a first step we could update the docstring to clearly mark that the behaviour changed between Python 3.6 and 3.7, so people that must maintain supporting for multiple versions can be aware of the difference.
Reacted by David K. Hess- added3.11only security fixesonly security fixes3.10 (EOL)end of lifeend of life3.12only security fixesonly security fixes
on Jun 1, 2022 Facing this issue when investigate [CVE-2024-3220] Default mimetype known files writeable on Windows
The workaround to ignore the knownfiles in the email is wrong:
To work-around this issue a user can call mimetypes.init() with an empty
list (“[]”) on Windows platforms to avoid using the default list of known
file locations.So I create #141393 to update the doc.
- added3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesand removed3.11only security fixesonly security fixes3.10 (EOL)end of lifeend of life3.12only security fixesonly security fixes
on Jun 1, 2026 #150733 adds the
use_system_defaultstomimetypes.init()andMimeTypes(). It controls whther to load MIME information fromknownfilesfiles and Windows registry.It is a draft for now, I'll add documentation after merging other documentation PR.
Reacted by Hang
Bug report
Mimetypes should NOT include defaults when initialized with file(s)
This is a follow up from https://bugs.python.org/issue4963 that led to this merged PR by @davidkhess #3062
I reported the issue in https://bugs.python.org/issue4963#msg384730 :
See also this thread 9fc720e#r45794801
@pombredanne
pombredanne on Jan 9, 2021 Contributor
@adeadman
adeadman 2 hours ago
@pombredanne
pombredanne 1 hour ago Contributor
And @adeadman also posted in #3062 (comment)
Linked PRs
mimetypes.init()doc to match the code #141393