Skip to content

Commit 399f168

Browse files
encukoumiss-islington
authored andcommitted
pythongh-68966: Make mailcap refuse to match unsafe filenames/types/params (pythonGH-91993)
(cherry picked from commit b9509ba) Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent 6d576a4 commit 399f168

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

Doc/library/mailcap.rst

+12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ standard. However, mailcap files are supported on most Unix systems.
5454
use) to determine whether or not the mailcap line applies. :func:`findmatch`
5555
will automatically check such conditions and skip the entry if the check fails.
5656

57+
.. versionchanged:: 3.11
58+
59+
To prevent security issues with shell metacharacters (symbols that have
60+
special effects in a shell command line), ``findmatch`` will refuse
61+
to inject ASCII characters other than alphanumerics and ``@+=:,./-_``
62+
into the returned command line.
63+
64+
If a disallowed character appears in *filename*, ``findmatch`` will always
65+
return ``(None, None)`` as if no entry was found.
66+
If such a character appears elsewhere (a value in *plist* or in *MIMEtype*),
67+
``findmatch`` will ignore all mailcap entries which use that value.
68+
A :mod:`warning <warnings>` will be raised in either case.
5769

5870
.. function:: getcaps()
5971

Lib/mailcap.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import warnings
5+
import re
56

67
__all__ = ["getcaps","findmatch"]
78

@@ -13,6 +14,11 @@ def lineno_sort_key(entry):
1314
else:
1415
return 1, 0
1516

17+
_find_unsafe = re.compile(r'[^\xa1-\U0010FFFF\w@+=:,./-]').search
18+
19+
class UnsafeMailcapInput(Warning):
20+
"""Warning raised when refusing unsafe input"""
21+
1622

1723
# Part 1: top-level interface.
1824

@@ -165,15 +171,22 @@ def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]):
165171
entry to use.
166172
167173
"""
174+
if _find_unsafe(filename):
175+
msg = "Refusing to use mailcap with filename %r. Use a safe temporary filename." % (filename,)
176+
warnings.warn(msg, UnsafeMailcapInput)
177+
return None, None
168178
entries = lookup(caps, MIMEtype, key)
169179
# XXX This code should somehow check for the needsterminal flag.
170180
for e in entries:
171181
if 'test' in e:
172182
test = subst(e['test'], filename, plist)
183+
if test is None:
184+
continue
173185
if test and os.system(test) != 0:
174186
continue
175187
command = subst(e[key], MIMEtype, filename, plist)
176-
return command, e
188+
if command is not None:
189+
return command, e
177190
return None, None
178191

179192
def lookup(caps, MIMEtype, key=None):
@@ -206,14 +219,23 @@ def subst(field, MIMEtype, filename, plist=[]):
206219
elif c == 's':
207220
res = res + filename
208221
elif c == 't':
222+
if _find_unsafe(MIMEtype):
223+
msg = "Refusing to substitute MIME type %r into a shell command." % (MIMEtype,)
224+
warnings.warn(msg, UnsafeMailcapInput)
225+
return None
209226
res = res + MIMEtype
210227
elif c == '{':
211228
start = i
212229
while i < n and field[i] != '}':
213230
i = i+1
214231
name = field[start:i]
215232
i = i+1
216-
res = res + findparam(name, plist)
233+
param = findparam(name, plist)
234+
if _find_unsafe(param):
235+
msg = "Refusing to substitute parameter %r (%s) into a shell command" % (param, name)
236+
warnings.warn(msg, UnsafeMailcapInput)
237+
return None
238+
res = res + param
217239
# XXX To do:
218240
# %n == number of parts if type is multipart/*
219241
# %F == list of alternating type and filename for parts

Lib/test/test_mailcap.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ def test_subst(self):
121121
(["", "audio/*", "foo.txt"], ""),
122122
(["echo foo", "audio/*", "foo.txt"], "echo foo"),
123123
(["echo %s", "audio/*", "foo.txt"], "echo foo.txt"),
124-
(["echo %t", "audio/*", "foo.txt"], "echo audio/*"),
124+
(["echo %t", "audio/*", "foo.txt"], None),
125+
(["echo %t", "audio/wav", "foo.txt"], "echo audio/wav"),
125126
(["echo \\%t", "audio/*", "foo.txt"], "echo %t"),
126127
(["echo foo", "audio/*", "foo.txt", plist], "echo foo"),
127128
(["echo %{total}", "audio/*", "foo.txt", plist], "echo 3")
@@ -205,7 +206,10 @@ def test_findmatch(self):
205206
('"An audio fragment"', audio_basic_entry)),
206207
([c, "audio/*"],
207208
{"filename": fname},
208-
("/usr/local/bin/showaudio audio/*", audio_entry)),
209+
(None, None)),
210+
([c, "audio/wav"],
211+
{"filename": fname},
212+
("/usr/local/bin/showaudio audio/wav", audio_entry)),
209213
([c, "message/external-body"],
210214
{"plist": plist},
211215
("showexternal /dev/null default john python.org /tmp foo bar", message_entry))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The deprecated mailcap module now refuses to inject unsafe text (filenames,
2+
MIME types, parameters) into shell commands. Instead of using such text, it
3+
will warn and act as if a match was not found (or for test commands, as if
4+
the test failed).

0 commit comments

Comments
 (0)