Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mimetypes.guess_all_extensions potentially mutates list #79655

Closed
rmccampbell7 mannequin opened this issue Dec 12, 2018 · 6 comments
Closed

mimetypes.guess_all_extensions potentially mutates list #79655

rmccampbell7 mannequin opened this issue Dec 12, 2018 · 6 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 bug and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@rmccampbell7
Copy link
Mannequin

rmccampbell7 mannequin commented Dec 12, 2018

BPO 35474
Nosy @ambv, @serhiy-storchaka, @miss-islington
PRs
  • bpo-35474: Fix mimetypes.guess_all_extensions() potentially mutating list #28286
  • [3.10] bpo-35474: Fix mimetypes.guess_all_extensions() potentially mutating list (GH-28286) #28289
  • [3.9] bpo-35474: Fix mimetypes.guess_all_extensions() potentially mutating list (GH-28286) #28290
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-09-13.17:25:10.378>
    created_at = <Date 2018-12-12.19:06:57.748>
    labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
    title = 'mimetypes.guess_all_extensions potentially mutates list'
    updated_at = <Date 2021-09-13.17:25:10.376>
    user = 'https://bugs.python.org/rmccampbell7'

    bugs.python.org fields:

    activity = <Date 2021-09-13.17:25:10.376>
    actor = 'lukasz.langa'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-09-13.17:25:10.378>
    closer = 'lukasz.langa'
    components = ['Library (Lib)']
    creation = <Date 2018-12-12.19:06:57.748>
    creator = 'rmccampbell7'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 35474
    keywords = ['patch']
    message_count = 6.0
    messages = ['331715', '401632', '401640', '401717', '401718', '401719']
    nosy_count = 4.0
    nosy_names = ['lukasz.langa', 'serhiy.storchaka', 'rmccampbell7', 'miss-islington']
    pr_nums = ['28286', '28289', '28290']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue35474'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @rmccampbell7
    Copy link
    Mannequin Author

    rmccampbell7 mannequin commented Dec 12, 2018

    The mimetypes.guess_all_extensions function is defined as:

    def guess_all_extensions(self, type, strict=True):
        type = type.lower()
        extensions = self.types_map_inv[True].get(type, [])
        if not strict:
            for ext in self.types_map_inv[False].get(type, []):
                if ext not in extensions:
                    extensions.append(ext)
        return extensions

    If any mime type exists in both the strict and non-strict types_map_inv and it is called with strict=False, then it will modify the strict list in-place which effects future calls even with strict=True. While this doesn't manifest as an error for me because the dictionaries are non-overlapping, it is a potential error; it is also vulnerable to people accidentally modifying the returned list. The list should be copied after the first lookup.

    @rmccampbell7 rmccampbell7 mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Dec 12, 2018
    @serhiy-storchaka
    Copy link
    Member

    Also, mutating the result of guess_all_extensions() mutated the internal state of the MimeTypes object and affected the following calls of guess_all_extensions().

    @serhiy-storchaka serhiy-storchaka added 3.9 only security fixes 3.10 only security fixes 3.11 bug and security fixes labels Sep 11, 2021
    @serhiy-storchaka
    Copy link
    Member

    New changeset 97ea18e by Serhiy Storchaka in branch 'main':
    bpo-35474: Fix mimetypes.guess_all_extensions() potentially mutating list (GH-28286)
    97ea18e

    @ambv
    Copy link
    Contributor

    ambv commented Sep 13, 2021

    New changeset 9cd8fb8 by Miss Islington (bot) in branch '3.9':
    bpo-35474: Fix mimetypes.guess_all_extensions() potentially mutating list (GH-28286) (GH-28290)
    9cd8fb8

    @ambv
    Copy link
    Contributor

    ambv commented Sep 13, 2021

    New changeset 06c26f4 by Miss Islington (bot) in branch '3.10':
    bpo-35474: Fix mimetypes.guess_all_extensions() potentially mutating list (GH-28286) (GH-28289)
    06c26f4

    @ambv
    Copy link
    Contributor

    ambv commented Sep 13, 2021

    Thanks, Serhiy! ✨ 🍰 ✨

    @ambv ambv closed this as completed Sep 13, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 bug and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants