Blame SOURCES/00382-cve-2015-20107.patch

3a1cd0
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
3a1cd0
From: Petr Viktorin <encukou@gmail.com>
3a1cd0
Date: Fri, 3 Jun 2022 11:43:35 +0200
3a1cd0
Subject: [PATCH] 00382: CVE-2015-20107
3a1cd0
3a1cd0
Make mailcap refuse to match unsafe filenames/types/params (GH-91993)
3a1cd0
3a1cd0
Upstream: https://github.com/python/cpython/issues/68966
3a1cd0
3a1cd0
Tracker bug: https://bugzilla.redhat.com/show_bug.cgi?id=2075390
3a1cd0
---
3a1cd0
 Doc/library/mailcap.rst                       | 12 +++++++++
3a1cd0
 Lib/mailcap.py                                | 26 +++++++++++++++++--
3a1cd0
 Lib/test/test_mailcap.py                      |  8 ++++--
3a1cd0
 ...2-04-27-18-25-30.gh-issue-68966.gjS8zs.rst |  4 +++
3a1cd0
 4 files changed, 46 insertions(+), 4 deletions(-)
3a1cd0
 create mode 100644 Misc/NEWS.d/next/Security/2022-04-27-18-25-30.gh-issue-68966.gjS8zs.rst
3a1cd0
3a1cd0
diff --git a/Doc/library/mailcap.rst b/Doc/library/mailcap.rst
3a1cd0
index a22b5b9c9e..7aa3380fec 100644
3a1cd0
--- a/Doc/library/mailcap.rst
3a1cd0
+++ b/Doc/library/mailcap.rst
3a1cd0
@@ -60,6 +60,18 @@ standard.  However, mailcap files are supported on most Unix systems.
3a1cd0
    use) to determine whether or not the mailcap line applies.  :func:`findmatch`
3a1cd0
    will automatically check such conditions and skip the entry if the check fails.
3a1cd0
 
3a1cd0
+   .. versionchanged:: 3.11
3a1cd0
+
3a1cd0
+      To prevent security issues with shell metacharacters (symbols that have
3a1cd0
+      special effects in a shell command line), ``findmatch`` will refuse
3a1cd0
+      to inject ASCII characters other than alphanumerics and ``@+=:,./-_``
3a1cd0
+      into the returned command line.
3a1cd0
+
3a1cd0
+      If a disallowed character appears in *filename*, ``findmatch`` will always
3a1cd0
+      return ``(None, None)`` as if no entry was found.
3a1cd0
+      If such a character appears elsewhere (a value in *plist* or in *MIMEtype*),
3a1cd0
+      ``findmatch`` will ignore all mailcap entries which use that value.
3a1cd0
+      A :mod:`warning <warnings>` will be raised in either case.
3a1cd0
 
3a1cd0
 .. function:: getcaps()
3a1cd0
 
3a1cd0
diff --git a/Lib/mailcap.py b/Lib/mailcap.py
3a1cd0
index ae416a8e9f..444c6408b5 100644
3a1cd0
--- a/Lib/mailcap.py
3a1cd0
+++ b/Lib/mailcap.py
3a1cd0
@@ -2,6 +2,7 @@
3a1cd0
 
3a1cd0
 import os
3a1cd0
 import warnings
3a1cd0
+import re
3a1cd0
 
3a1cd0
 __all__ = ["getcaps","findmatch"]
3a1cd0
 
3a1cd0
@@ -13,6 +14,11 @@ def lineno_sort_key(entry):
3a1cd0
     else:
3a1cd0
         return 1, 0
3a1cd0
 
3a1cd0
+_find_unsafe = re.compile(r'[^\xa1-\U0010FFFF\w@+=:,./-]').search
3a1cd0
+
3a1cd0
+class UnsafeMailcapInput(Warning):
3a1cd0
+    """Warning raised when refusing unsafe input"""
3a1cd0
+
3a1cd0
 
3a1cd0
 # Part 1: top-level interface.
3a1cd0
 
3a1cd0
@@ -165,15 +171,22 @@ def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]):
3a1cd0
     entry to use.
3a1cd0
 
3a1cd0
     """
3a1cd0
+    if _find_unsafe(filename):
3a1cd0
+        msg = "Refusing to use mailcap with filename %r. Use a safe temporary filename." % (filename,)
3a1cd0
+        warnings.warn(msg, UnsafeMailcapInput)
3a1cd0
+        return None, None
3a1cd0
     entries = lookup(caps, MIMEtype, key)
3a1cd0
     # XXX This code should somehow check for the needsterminal flag.
3a1cd0
     for e in entries:
3a1cd0
         if 'test' in e:
3a1cd0
             test = subst(e['test'], filename, plist)
3a1cd0
+            if test is None:
3a1cd0
+                continue
3a1cd0
             if test and os.system(test) != 0:
3a1cd0
                 continue
3a1cd0
         command = subst(e[key], MIMEtype, filename, plist)
3a1cd0
-        return command, e
3a1cd0
+        if command is not None:
3a1cd0
+            return command, e
3a1cd0
     return None, None
3a1cd0
 
3a1cd0
 def lookup(caps, MIMEtype, key=None):
3a1cd0
@@ -206,6 +219,10 @@ def subst(field, MIMEtype, filename, plist=[]):
3a1cd0
             elif c == 's':
3a1cd0
                 res = res + filename
3a1cd0
             elif c == 't':
3a1cd0
+                if _find_unsafe(MIMEtype):
3a1cd0
+                    msg = "Refusing to substitute MIME type %r into a shell command." % (MIMEtype,)
3a1cd0
+                    warnings.warn(msg, UnsafeMailcapInput)
3a1cd0
+                    return None
3a1cd0
                 res = res + MIMEtype
3a1cd0
             elif c == '{':
3a1cd0
                 start = i
3a1cd0
@@ -213,7 +230,12 @@ def subst(field, MIMEtype, filename, plist=[]):
3a1cd0
                     i = i+1
3a1cd0
                 name = field[start:i]
3a1cd0
                 i = i+1
3a1cd0
-                res = res + findparam(name, plist)
3a1cd0
+                param = findparam(name, plist)
3a1cd0
+                if _find_unsafe(param):
3a1cd0
+                    msg = "Refusing to substitute parameter %r (%s) into a shell command" % (param, name)
3a1cd0
+                    warnings.warn(msg, UnsafeMailcapInput)
3a1cd0
+                    return None
3a1cd0
+                res = res + param
3a1cd0
             # XXX To do:
3a1cd0
             # %n == number of parts if type is multipart/*
3a1cd0
             # %F == list of alternating type and filename for parts
3a1cd0
diff --git a/Lib/test/test_mailcap.py b/Lib/test/test_mailcap.py
3a1cd0
index c08423c670..920283d9a2 100644
3a1cd0
--- a/Lib/test/test_mailcap.py
3a1cd0
+++ b/Lib/test/test_mailcap.py
3a1cd0
@@ -121,7 +121,8 @@ class HelperFunctionTest(unittest.TestCase):
3a1cd0
             (["", "audio/*", "foo.txt"], ""),
3a1cd0
             (["echo foo", "audio/*", "foo.txt"], "echo foo"),
3a1cd0
             (["echo %s", "audio/*", "foo.txt"], "echo foo.txt"),
3a1cd0
-            (["echo %t", "audio/*", "foo.txt"], "echo audio/*"),
3a1cd0
+            (["echo %t", "audio/*", "foo.txt"], None),
3a1cd0
+            (["echo %t", "audio/wav", "foo.txt"], "echo audio/wav"),
3a1cd0
             (["echo \\%t", "audio/*", "foo.txt"], "echo %t"),
3a1cd0
             (["echo foo", "audio/*", "foo.txt", plist], "echo foo"),
3a1cd0
             (["echo %{total}", "audio/*", "foo.txt", plist], "echo 3")
3a1cd0
@@ -205,7 +206,10 @@ class FindmatchTest(unittest.TestCase):
3a1cd0
              ('"An audio fragment"', audio_basic_entry)),
3a1cd0
             ([c, "audio/*"],
3a1cd0
              {"filename": fname},
3a1cd0
-             ("/usr/local/bin/showaudio audio/*", audio_entry)),
3a1cd0
+             (None, None)),
3a1cd0
+            ([c, "audio/wav"],
3a1cd0
+             {"filename": fname},
3a1cd0
+             ("/usr/local/bin/showaudio audio/wav", audio_entry)),
3a1cd0
             ([c, "message/external-body"],
3a1cd0
              {"plist": plist},
3a1cd0
              ("showexternal /dev/null default john python.org     /tmp foo bar", message_entry))
3a1cd0
diff --git a/Misc/NEWS.d/next/Security/2022-04-27-18-25-30.gh-issue-68966.gjS8zs.rst b/Misc/NEWS.d/next/Security/2022-04-27-18-25-30.gh-issue-68966.gjS8zs.rst
3a1cd0
new file mode 100644
3a1cd0
index 0000000000..da81a1f699
3a1cd0
--- /dev/null
3a1cd0
+++ b/Misc/NEWS.d/next/Security/2022-04-27-18-25-30.gh-issue-68966.gjS8zs.rst
3a1cd0
@@ -0,0 +1,4 @@
3a1cd0
+The deprecated mailcap module now refuses to inject unsafe text (filenames,
3a1cd0
+MIME types, parameters) into shell commands. Instead of using such text, it
3a1cd0
+will warn and act as if a match was not found (or for test commands, as if
3a1cd0
+the test failed).