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

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