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