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

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