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