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

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