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

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