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

@vstinner vstinner Apr 28, 2022

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

@gpshead gpshead May 11, 2022

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

@encukou encukou May 26, 2022

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
@encukou encukou requested a review from a team as a code owner May 26, 2022
@frenzymadness
Copy link
Contributor

frenzymadness commented Jun 2, 2022

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
13 checks passed
@miss-islington
Copy link
Contributor

miss-islington commented Jun 3, 2022

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

@encukou encukou deleted the mailcap-refusal branch Jun 3, 2022
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
Copy link

bedevere-bot commented Jun 3, 2022

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>
@miss-islington
Copy link
Contributor

miss-islington commented Jun 6, 2022

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

@bedevere-bot
Copy link

bedevere-bot commented Jun 6, 2022

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

hroncok pushed a commit to hroncok/cpython that referenced this pull request Aug 22, 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 Sep 7, 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 Sep 7, 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 Sep 7, 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 Sep 7, 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
encukou added a commit that referenced this pull request Sep 20, 2022
…params (GH-91993) (GH-93543)

* gh-68966: Make mailcap refuse to match unsafe filenames/types/params (GH-91993)
(cherry picked from commit b9509ba)
* Add a What's New entry for 3.10.8.

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
@miss-islington
Copy link
Contributor

miss-islington commented Oct 11, 2022

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

@miss-islington
Copy link
Contributor

miss-islington commented Oct 11, 2022

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

@miss-islington
Copy link
Contributor

miss-islington commented Oct 11, 2022

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

@bedevere-bot
Copy link

bedevere-bot commented Oct 11, 2022

GH-98190 is a backport of this pull request to the 3.9 branch.

@bedevere-bot
Copy link

bedevere-bot commented Oct 11, 2022

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

miss-islington commented Oct 11, 2022

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
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.

None yet

8 participants