Skip to content

Commit fae93ab

Browse files
[3.11] gh-68966: Make mailcap refuse to match unsafe filenames/types/params (GH-91993) (GH-93458)
(cherry picked from commit b9509ba) Co-authored-by: Petr Viktorin <encukou@gmail.com> Automerge-Triggered-By: GH:encukou
1 parent 71fae64 commit fae93ab

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
@@ -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.11
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

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

@@ -19,6 +20,11 @@ def lineno_sort_key(entry):
1920
else:
2021
return 1, 0
2122

23+
_find_unsafe = re.compile(r'[^\xa1-\U0010FFFF\w@+=:,./-]').search
24+
25+
class UnsafeMailcapInput(Warning):
26+
"""Warning raised when refusing unsafe input"""
27+
2228

2329
# Part 1: top-level interface.
2430

@@ -171,15 +177,22 @@ def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]):
171177
entry to use.
172178
173179
"""
180+
if _find_unsafe(filename):
181+
msg = "Refusing to use mailcap with filename %r. Use a safe temporary filename." % (filename,)
182+
warnings.warn(msg, UnsafeMailcapInput)
183+
return None, None
174184
entries = lookup(caps, MIMEtype, key)
175185
# XXX This code should somehow check for the needsterminal flag.
176186
for e in entries:
177187
if 'test' in e:
178188
test = subst(e['test'], filename, plist)
189+
if test is None:
190+
continue
179191
if test and os.system(test) != 0:
180192
continue
181193
command = subst(e[key], MIMEtype, filename, plist)
182-
return command, e
194+
if command is not None:
195+
return command, e
183196
return None, None
184197

185198
def lookup(caps, MIMEtype, key=None):
@@ -212,14 +225,23 @@ def subst(field, MIMEtype, filename, plist=[]):
212225
elif c == 's':
213226
res = res + filename
214227
elif c == 't':
228+
if _find_unsafe(MIMEtype):
229+
msg = "Refusing to substitute MIME type %r into a shell command." % (MIMEtype,)
230+
warnings.warn(msg, UnsafeMailcapInput)
231+
return None
215232
res = res + MIMEtype
216233
elif c == '{':
217234
start = i
218235
while i < n and field[i] != '}':
219236
i = i+1
220237
name = field[start:i]
221238
i = i+1
222-
res = res + findparam(name, plist)
239+
param = findparam(name, plist)
240+
if _find_unsafe(param):
241+
msg = "Refusing to substitute parameter %r (%s) into a shell command" % (param, name)
242+
warnings.warn(msg, UnsafeMailcapInput)
243+
return None
244+
res = res + param
223245
# XXX To do:
224246
# %n == number of parts if type is multipart/*
225247
# %F == list of alternating type and filename for parts

Lib/test/test_mailcap.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ def test_subst(self):
128128
(["", "audio/*", "foo.txt"], ""),
129129
(["echo foo", "audio/*", "foo.txt"], "echo foo"),
130130
(["echo %s", "audio/*", "foo.txt"], "echo foo.txt"),
131-
(["echo %t", "audio/*", "foo.txt"], "echo audio/*"),
131+
(["echo %t", "audio/*", "foo.txt"], None),
132+
(["echo %t", "audio/wav", "foo.txt"], "echo audio/wav"),
132133
(["echo \\%t", "audio/*", "foo.txt"], "echo %t"),
133134
(["echo foo", "audio/*", "foo.txt", plist], "echo foo"),
134135
(["echo %{total}", "audio/*", "foo.txt", plist], "echo 3")
@@ -212,7 +213,10 @@ def test_findmatch(self):
212213
('"An audio fragment"', audio_basic_entry)),
213214
([c, "audio/*"],
214215
{"filename": fname},
215-
("/usr/local/bin/showaudio audio/*", audio_entry)),
216+
(None, None)),
217+
([c, "audio/wav"],
218+
{"filename": fname},
219+
("/usr/local/bin/showaudio audio/wav", audio_entry)),
216220
([c, "message/external-body"],
217221
{"plist": plist},
218222
("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)