Blame 00382-cve-2015-20107.patch

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