Skip to content

Commit 96739bc

Browse files
miss-islingtonencukougpshead
authored
[3.10] gh-68966: Make mailcap refuse to match unsafe filenames/types/params (GH-91993) (GH-93543)
* gh-68966: Make mailcap refuse to match unsafe filenames/types/params (GH-91993) (cherry picked from commit b9509ba) * Add a What's New entry for 3.10.8. Co-authored-by: Petr Viktorin <[email protected]> Co-authored-by: Gregory P. Smith <[email protected]>
1 parent 7b6021b commit 96739bc

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

Doc/library/mailcap.rst

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

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

6476
.. function:: getcaps()
6577

Doc/whatsnew/3.10.rst

+8
Original file line numberDiff line numberDiff line change
@@ -2338,3 +2338,11 @@ line flag, or :mod:`sys` APIs. See the :ref:`integer string conversion
23382338
length limitation <int_max_str_digits>` documentation. The default limit
23392339
is 4300 digits in string form.
23402340
2341+
Notable security feature in 3.10.8
2342+
==================================
2343+
2344+
The deprecated :mod:`mailcap` module now refuses to inject unsafe text
2345+
(filenames, MIME types, parameters) into shell commands. Instead of using such
2346+
text, it will warn and act as if a match was not found (or for test commands,
2347+
as if the test failed).
2348+
(Contributed by Petr Viktorin in :gh:`98966`.)

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
@@ -123,7 +123,8 @@ def test_subst(self):
123123
(["", "audio/*", "foo.txt"], ""),
124124
(["echo foo", "audio/*", "foo.txt"], "echo foo"),
125125
(["echo %s", "audio/*", "foo.txt"], "echo foo.txt"),
126-
(["echo %t", "audio/*", "foo.txt"], "echo audio/*"),
126+
(["echo %t", "audio/*", "foo.txt"], None),
127+
(["echo %t", "audio/wav", "foo.txt"], "echo audio/wav"),
127128
(["echo \\%t", "audio/*", "foo.txt"], "echo %t"),
128129
(["echo foo", "audio/*", "foo.txt", plist], "echo foo"),
129130
(["echo %{total}", "audio/*", "foo.txt", plist], "echo 3")
@@ -207,7 +208,10 @@ def test_findmatch(self):
207208
('"An audio fragment"', audio_basic_entry)),
208209
([c, "audio/*"],
209210
{"filename": fname},
210-
("/usr/local/bin/showaudio audio/*", audio_entry)),
211+
(None, None)),
212+
([c, "audio/wav"],
213+
{"filename": fname},
214+
("/usr/local/bin/showaudio audio/wav", audio_entry)),
211215
([c, "message/external-body"],
212216
{"plist": plist},
213217
("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)