Skip to content

gh-75223: Reject undotted extensions in mimetypes.add_type()#2895

Open
OddBloke wants to merge 16 commits intopython:mainfrom
OddBloke:fix-for-31040
Open

gh-75223: Reject undotted extensions in mimetypes.add_type()#2895
OddBloke wants to merge 16 commits intopython:mainfrom
OddBloke:fix-for-31040

Conversation

@OddBloke
Copy link
Copy Markdown
Contributor

@OddBloke OddBloke commented Jul 26, 2017

Extensions that don't start with a dot will never be found by
methods/functions that act on the registry, so we should stop users from
mistakenly adding them.

https://bugs.python.org/issue31040

@OddBloke
Copy link
Copy Markdown
Contributor Author

I've opened this now to get feedback on whether this is an appropriate change to make. Does it need a warning first, before it becomes the default behaviour?

(At a minimum, I would expect to need to add to documentation.)

@JulienPalard
Copy link
Copy Markdown
Member

JulienPalard commented Sep 10, 2019

As said in https://bugs.python.org/issue31040, please use startswith instead of find.

There is one case where it works without a dot:

>>> mimetypes.add_type('empty', '')
>>> mimetypes.guess_type('')
('empty', None)

And someone may have used it to distinguish between emptyness and type not found. What I propose:

Emit a warning in case the ext is empty, else ValueError in case the type does not start with a dot, to ensure we're not breaking any code.

Comment thread Lib/mimetypes.py Outdated
@bedevere-bot
Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@OddBloke OddBloke requested a review from a team as a code owner September 10, 2019 12:45
Extensions that don't start with a dot will never be found by
methods/functions that act on the registry, so we should stop users from
mistakenly adding them.

The one exception is the empty string extension.  This could be in use
to detect absent extensions, so instead of raising a ValueError we emit
a warning.
@OddBloke
Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

@bedevere-bot
Copy link
Copy Markdown

Thanks for making the requested changes!

@JulienPalard: please review the changes made to this pull request.

Comment thread Lib/mimetypes.py Outdated
@bedevere-bot
Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be put in the comfy chair!

@github-actions

This comment was marked as outdated.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Feb 20, 2022
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Jul 29, 2022
@github-actions

This comment was marked as outdated.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Aug 30, 2022
@arhadthedev arhadthedev changed the title bpo-31040: Reject undotted extensions in mimetypes.add_type gh-75223: Reject undotted extensions in mimetypes.add_type Apr 15, 2023
@arhadthedev arhadthedev added topic-email stdlib Standard Library Python modules in the Lib/ directory labels Apr 15, 2023
@hugovk hugovk removed their request for review November 16, 2024 13:13
@github-actions

This comment was marked as outdated.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Dec 21, 2024
@encukou
Copy link
Copy Markdown
Member

encukou commented Jan 3, 2025

IMO, this is a backwards-incompatible change and should be deprecated with a warning, and documented. (For example, if add_type is fed from a data file, this PR can change a relatively harmless data-entry error into a hard failure. Or someone could use types_map directly.)

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Jan 3, 2025
@hugovk hugovk marked this pull request as draft January 8, 2025 15:12
@hugovk
Copy link
Copy Markdown
Member

hugovk commented Jan 8, 2025

Thanks, please see #128638 to deprecate it first, and I've marked this as a draft until it's ready for merge.

@github-actions

This comment was marked as outdated.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Feb 7, 2025
@StanFromIreland StanFromIreland removed the stale Stale PR or inactive for long period of time. label May 9, 2026
@StanFromIreland
Copy link
Copy Markdown
Member

Thanks, please see #128638 to deprecate it first, and I've marked this as a draft until it's ready for merge.

It was deprecated and scheduled for 3.16, so it's time!

@StanFromIreland StanFromIreland self-assigned this May 9, 2026
@read-the-docs-community
Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #32618039 | 📁 Comparing c491311 against main (7241f27)

  🔍 Preview build  

3 files changed
± library/mimetypes.html
± whatsnew/3.16.html
± whatsnew/changelog.html

@StanFromIreland StanFromIreland marked this pull request as ready for review May 9, 2026 18:09
@StanFromIreland StanFromIreland requested a review from AA-Turner as a code owner May 9, 2026 18:09
@StanFromIreland StanFromIreland changed the title gh-75223: Reject undotted extensions in mimetypes.add_type gh-75223: Reject undotted extensions in mimetypes.add_type() May 9, 2026
@StanFromIreland StanFromIreland removed their assignment May 9, 2026
Comment thread Doc/library/mimetypes.rst

.. function:: add_type(type, ext, strict=True)

Add a mapping from the MIME type *type* to the extension *ext*. When the
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should have the same "Valid extensions start with a '.' or are empty." note IMO, but it wasn't updated before?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stdlib Standard Library Python modules in the Lib/ directory topic-email

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants