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

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