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

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