[3.11] gh-68966: Make mailcap refuse to match unsafe filenames/types/params (GH-91993) (GH-93458)

(cherry picked from commit b9509ba7a9)


Co-authored-by: Petr Viktorin <encukou@gmail.com>

Automerge-Triggered-By: GH:encukou
This commit is contained in:
Miss Islington (bot) 2022-06-03 08:25:58 -07:00 committed by GitHub
parent 71fae642c0
commit fae93ab163
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 4 deletions

View File

@ -60,6 +60,18 @@ standard. However, mailcap files are supported on most Unix systems.
use) to determine whether or not the mailcap line applies. :func:`findmatch` use) to determine whether or not the mailcap line applies. :func:`findmatch`
will automatically check such conditions and skip the entry if the check fails. will automatically check such conditions and skip the entry if the check fails.
.. versionchanged:: 3.11
To prevent security issues with shell metacharacters (symbols that have
special effects in a shell command line), ``findmatch`` will refuse
to inject ASCII characters other than alphanumerics and ``@+=:,./-_``
into the returned command line.
If a disallowed character appears in *filename*, ``findmatch`` will always
return ``(None, None)`` as if no entry was found.
If such a character appears elsewhere (a value in *plist* or in *MIMEtype*),
``findmatch`` will ignore all mailcap entries which use that value.
A :mod:`warning <warnings>` will be raised in either case.
.. function:: getcaps() .. function:: getcaps()

View File

@ -2,6 +2,7 @@
import os import os
import warnings import warnings
import re
__all__ = ["getcaps","findmatch"] __all__ = ["getcaps","findmatch"]
@ -19,6 +20,11 @@ def lineno_sort_key(entry):
else: else:
return 1, 0 return 1, 0
_find_unsafe = re.compile(r'[^\xa1-\U0010FFFF\w@+=:,./-]').search
class UnsafeMailcapInput(Warning):
"""Warning raised when refusing unsafe input"""
# Part 1: top-level interface. # Part 1: top-level interface.
@ -171,14 +177,21 @@ def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]):
entry to use. entry to use.
""" """
if _find_unsafe(filename):
msg = "Refusing to use mailcap with filename %r. Use a safe temporary filename." % (filename,)
warnings.warn(msg, UnsafeMailcapInput)
return None, None
entries = lookup(caps, MIMEtype, key) entries = lookup(caps, MIMEtype, key)
# XXX This code should somehow check for the needsterminal flag. # XXX This code should somehow check for the needsterminal flag.
for e in entries: for e in entries:
if 'test' in e: if 'test' in e:
test = subst(e['test'], filename, plist) test = subst(e['test'], filename, plist)
if test is None:
continue
if test and os.system(test) != 0: if test and os.system(test) != 0:
continue continue
command = subst(e[key], MIMEtype, filename, plist) command = subst(e[key], MIMEtype, filename, plist)
if command is not None:
return command, e return command, e
return None, None return None, None
@ -212,6 +225,10 @@ def subst(field, MIMEtype, filename, plist=[]):
elif c == 's': elif c == 's':
res = res + filename res = res + filename
elif c == 't': elif c == 't':
if _find_unsafe(MIMEtype):
msg = "Refusing to substitute MIME type %r into a shell command." % (MIMEtype,)
warnings.warn(msg, UnsafeMailcapInput)
return None
res = res + MIMEtype res = res + MIMEtype
elif c == '{': elif c == '{':
start = i start = i
@ -219,7 +236,12 @@ def subst(field, MIMEtype, filename, plist=[]):
i = i+1 i = i+1
name = field[start:i] name = field[start:i]
i = i+1 i = i+1
res = res + findparam(name, plist) param = findparam(name, plist)
if _find_unsafe(param):
msg = "Refusing to substitute parameter %r (%s) into a shell command" % (param, name)
warnings.warn(msg, UnsafeMailcapInput)
return None
res = res + param
# XXX To do: # XXX To do:
# %n == number of parts if type is multipart/* # %n == number of parts if type is multipart/*
# %F == list of alternating type and filename for parts # %F == list of alternating type and filename for parts

View File

@ -128,7 +128,8 @@ class HelperFunctionTest(unittest.TestCase):
(["", "audio/*", "foo.txt"], ""), (["", "audio/*", "foo.txt"], ""),
(["echo foo", "audio/*", "foo.txt"], "echo foo"), (["echo foo", "audio/*", "foo.txt"], "echo foo"),
(["echo %s", "audio/*", "foo.txt"], "echo foo.txt"), (["echo %s", "audio/*", "foo.txt"], "echo foo.txt"),
(["echo %t", "audio/*", "foo.txt"], "echo audio/*"), (["echo %t", "audio/*", "foo.txt"], None),
(["echo %t", "audio/wav", "foo.txt"], "echo audio/wav"),
(["echo \\%t", "audio/*", "foo.txt"], "echo %t"), (["echo \\%t", "audio/*", "foo.txt"], "echo %t"),
(["echo foo", "audio/*", "foo.txt", plist], "echo foo"), (["echo foo", "audio/*", "foo.txt", plist], "echo foo"),
(["echo %{total}", "audio/*", "foo.txt", plist], "echo 3") (["echo %{total}", "audio/*", "foo.txt", plist], "echo 3")
@ -212,7 +213,10 @@ class FindmatchTest(unittest.TestCase):
('"An audio fragment"', audio_basic_entry)), ('"An audio fragment"', audio_basic_entry)),
([c, "audio/*"], ([c, "audio/*"],
{"filename": fname}, {"filename": fname},
("/usr/local/bin/showaudio audio/*", audio_entry)), (None, None)),
([c, "audio/wav"],
{"filename": fname},
("/usr/local/bin/showaudio audio/wav", audio_entry)),
([c, "message/external-body"], ([c, "message/external-body"],
{"plist": plist}, {"plist": plist},
("showexternal /dev/null default john python.org /tmp foo bar", message_entry)) ("showexternal /dev/null default john python.org /tmp foo bar", message_entry))

View File

@ -0,0 +1,4 @@
The deprecated mailcap module now refuses to inject unsafe text (filenames,
MIME types, parameters) into shell commands. Instead of using such text, it
will warn and act as if a match was not found (or for test commands, as if
the test failed).