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

gh-68966: Make mailcap refuse to match unsafe filenames/types/params #91993

Merged
merged 4 commits into from
Jun 3, 2022

Conversation

encukou
Copy link
Member

@encukou encukou commented Apr 27, 2022

Here is a possible fix for CVE-2015-20107 for cases where mailcap needs to continue working, at least when following best practices: refuse to inject text into commands unless deemed safe by a list of allowed characters.

  • add docs

if _find_unsafe(filename):
msg = "Refusing to use mailcap with filename %r. Use a safe temporary filename." % (filename,)
warnings.warn(msg, UnsafeMailcapInput)
return None, None
Copy link
Member

Choose a reason for hiding this comment

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

Usually when a warning is displayed, the function still returns its regular result. For example, calling a deprecated function emits a DeprecationWarning, but the function is executed. If we go this way (reject filenames which look unsafe), I suggest to raise an exception instead.

Copy link
Member Author

@encukou encukou Apr 28, 2022

Choose a reason for hiding this comment

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

In this case, IMO since the caller should always do whatever it does on None result -- fall back to some other mechanism, or ask the user -- returning None is less disruptive.

Lib/mailcap.py Outdated
@@ -19,6 +20,11 @@ def lineno_sort_key(entry):
else:
return 1, 0

_find_unsafe = re.compile(r'[^\xa1-\U0010FFFF\w@%+=:,./-]').search
Copy link
Member

Choose a reason for hiding this comment

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

is % safe? DOS derived shells use that for variable expansion IIRC.

Copy link
Member Author

Choose a reason for hiding this comment

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

Mailcap implements the “Implementation Details for UNIX” appendix of RFC-1524 (which is what specifies using % for the placeholders), and that says:

(Because of differences in shells and the implementation and behavior
of the same shell from one system to another, it is specified that
the command line be intended as input to the Bourne shell, i.e., that
it is implicitly preceded by "/bin/sh -c " on the command line.)

So I'd say it's safe for Python to allow %. But it also won't hurt much to disallow it, so I'll do that.

jpuhlman pushed a commit to MontaVista-OpenSourceTechnology/poky that referenced this pull request May 18, 2022
…cond argument

Source: python/cpython#91993
MR: 117402
Type: Security Fix
Disposition: Backport from python/cpython@c3e7f13
ChangeID: 7118c5678a340cfcad95b0e5fb116b8919e389b8
Description:
          CVE-2015-20107
          python(mailcap): findmatch() function does not sanitise the second argument.

Signed-off-by: Hitendra Prajapati <hprajapati@mvista.com>
Signed-off-by: Jeremy A. Puhlman <jpuhlman@mvista.com>
@encukou encukou marked this pull request as ready for review May 26, 2022 15:14
@encukou encukou requested a review from a team as a code owner May 26, 2022 15:14
@encukou encukou added the needs backport to 3.11 only security fixes label May 26, 2022
@frenzymadness
Copy link
Contributor

I think it's good and it seems to work well. I like the regex.

@encukou encukou merged commit b9509ba into python:main Jun 3, 2022
@miss-islington
Copy link
Contributor

Thanks @encukou for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

@encukou encukou deleted the mailcap-refusal branch June 3, 2022 09:43
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 3, 2022
…arams (pythonGH-91993)

(cherry picked from commit b9509ba)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Jun 3, 2022
@bedevere-bot
Copy link

GH-93458 is a backport of this pull request to the 3.11 branch.

miss-islington added a commit that referenced this pull request Jun 3, 2022
…params (GH-91993) (GH-93458)

(cherry picked from commit b9509ba)


Co-authored-by: Petr Viktorin <encukou@gmail.com>

Automerge-Triggered-By: GH:encukou
jpuhlman pushed a commit to MontaVista-OpenSourceTechnology/poky that referenced this pull request Jun 6, 2022
…cond argument

Source: python/cpython#91993
MR: 117410
Type: Security Fix
Disposition: Backport from python/cpython@c3e7f13
ChangeID: 6101cf28d6a5288fe07c654df016c3f5810c705a
Description:
          CVE-2015-20107 python(mailcap): findmatch() function does not sanitise the second argument.

Signed-off-by: Hitendra Prajapati <hprajapati@mvista.com>
Signed-off-by: Jeremy A. Puhlman <jpuhlman@mvista.com>
@encukou encukou added 3.10 only security fixes needs backport to 3.10 only security fixes and removed 3.10 only security fixes labels Jun 6, 2022
@miss-islington
Copy link
Contributor

Thanks @encukou for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-93543 is a backport of this pull request to the 3.10 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label Jun 6, 2022
@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Oct 11, 2022
@bedevere-bot
Copy link

GH-98192 is a backport of this pull request to the 3.8 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 11, 2022
…arams (pythonGH-91993)

(cherry picked from commit b9509ba)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 11, 2022
…arams (pythonGH-91993)

(cherry picked from commit b9509ba)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
@miss-islington
Copy link
Contributor

Thanks @encukou for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 11, 2022
…arams (pythonGH-91993)

(cherry picked from commit b9509ba)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
@ambv
Copy link
Contributor

ambv commented Oct 11, 2022

#98191 is the 3.7 backport. Bedevere somehow missed the comment here.

ambv pushed a commit that referenced this pull request Oct 11, 2022
…arams (GH-91993) (GH-98191)

gh-68966: Make mailcap refuse to match unsafe filenames/types/params (GH-91993)
(cherry picked from commit b9509ba)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
ambv pushed a commit that referenced this pull request Oct 11, 2022
…arams (GH-91993) (#98190)

gh-68966: Make mailcap refuse to match unsafe filenames/types/params (GH-91993)
(cherry picked from commit b9509ba)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
ambv added a commit that referenced this pull request Oct 11, 2022
…arams (GH-91993) (#98192)

gh-68966: Make mailcap refuse to match unsafe filenames/types/params (GH-91993)
(cherry picked from commit b9509ba)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
hroncok pushed a commit to fedora-python/cpython that referenced this pull request Oct 12, 2022
Make mailcap refuse to match unsafe filenames/types/params (pythonGH-91993)

Upstream: python#68966

Tracker bug: https://bugzilla.redhat.com/show_bug.cgi?id=2075390
hroncok pushed a commit to fedora-python/cpython that referenced this pull request Oct 12, 2022
Make mailcap refuse to match unsafe filenames/types/params (pythonGH-91993)

Upstream: python#68966

Tracker bug: https://bugzilla.redhat.com/show_bug.cgi?id=2075390
hroncok pushed a commit to fedora-python/cpython that referenced this pull request Oct 12, 2022
Make mailcap refuse to match unsafe filenames/types/params (pythonGH-91993)

Upstream: python#68966

Tracker bug: https://bugzilla.redhat.com/show_bug.cgi?id=2075390
slozier pushed a commit to IronLanguages/ironpython2 that referenced this pull request Jan 13, 2023
* Fix CVE-2015-20107

Implemented fix from python/cpython#91993

* Update regex

Update regex to be Python 2 compatible.
hroncok pushed a commit to fedora-python/cpython that referenced this pull request Oct 6, 2023
00382 #
Make mailcap refuse to match unsafe filenames/types/params (pythonGH-91993)

Upstream: python#68966

Tracker bug: https://bugzilla.redhat.com/show_bug.cgi?id=2075390

Backported from python3.
stratakis pushed a commit to stratakis/cpython that referenced this pull request Mar 11, 2024
Make mailcap refuse to match unsafe filenames/types/params (pythonGH-91993)

Upstream: python#68966

Tracker bug: https://bugzilla.redhat.com/show_bug.cgi?id=2075390
stratakis pushed a commit to stratakis/cpython that referenced this pull request Mar 11, 2024
Make mailcap refuse to match unsafe filenames/types/params (pythonGH-91993)

Upstream: python#68966

Tracker bug: https://bugzilla.redhat.com/show_bug.cgi?id=2075390
stratakis pushed a commit to stratakis/cpython that referenced this pull request Mar 20, 2024
Make mailcap refuse to match unsafe filenames/types/params (pythonGH-91993)

Upstream: python#68966

Tracker bug: https://bugzilla.redhat.com/show_bug.cgi?id=2075390
stratakis pushed a commit to stratakis/cpython that referenced this pull request Mar 20, 2024
Make mailcap refuse to match unsafe filenames/types/params (pythonGH-91993)

Upstream: python#68966

Tracker bug: https://bugzilla.redhat.com/show_bug.cgi?id=2075390
stratakis pushed a commit to stratakis/cpython that referenced this pull request Mar 20, 2024
Make mailcap refuse to match unsafe filenames/types/params (pythonGH-91993)

Upstream: python#68966

Tracker bug: https://bugzilla.redhat.com/show_bug.cgi?id=2075390
stratakis pushed a commit to stratakis/cpython that referenced this pull request Mar 20, 2024
Make mailcap refuse to match unsafe filenames/types/params (pythonGH-91993)

Upstream: python#68966

Tracker bug: https://bugzilla.redhat.com/show_bug.cgi?id=2075390
stratakis pushed a commit to stratakis/cpython that referenced this pull request Mar 25, 2024
Make mailcap refuse to match unsafe filenames/types/params (pythonGH-91993)

Upstream: python#68966

Tracker bug: https://bugzilla.redhat.com/show_bug.cgi?id=2075390
hroncok pushed a commit to fedora-python/cpython that referenced this pull request Mar 26, 2024
Make mailcap refuse to match unsafe filenames/types/params (pythonGH-91993)

Upstream: python#68966

Tracker bug: https://bugzilla.redhat.com/show_bug.cgi?id=2075390
mcepl pushed a commit to openSUSE-Python/cpython that referenced this pull request Apr 2, 2024
Make mailcap refuse to match unsafe filenames/types/params (pythonGH-91993)

Upstream: python#68966

Tracker bug: https://bugzilla.redhat.com/show_bug.cgi?id=2075390
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants