+54
-4
lines changedFilter options
+54
-4
lines changed Original file line number Diff line number Diff line change
@@ -60,6 +60,18 @@ standard. However, mailcap files are supported on most Unix systems.
60
60
use) to determine whether or not the mailcap line applies. :func:`findmatch`
61
61
will automatically check such conditions and skip the entry if the check fails.
62
62
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.
63
75
64
76
.. function:: getcaps()
65
77
Original file line number Diff line number Diff line change
@@ -2338,3 +2338,11 @@ line flag, or :mod:`sys` APIs. See the :ref:`integer string conversion
2338
2338
length limitation <int_max_str_digits>` documentation. The default limit
2339
2339
is 4300 digits in string form.
2340
2340
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`.)
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
2
2
3
3
import os
4
4
import warnings
5
+
import re
5
6
6
7
__all__ = ["getcaps","findmatch"]
7
8
@@ -13,6 +14,11 @@ def lineno_sort_key(entry):
13
14
else:
14
15
return 1, 0
15
16
17
+
_find_unsafe = re.compile(r'[^\xa1-\U0010FFFF\w@+=:,./-]').search
18
+
19
+
class UnsafeMailcapInput(Warning):
20
+
"""Warning raised when refusing unsafe input"""
21
+
16
22
17
23
# Part 1: top-level interface.
18
24
@@ -165,15 +171,22 @@ def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]):
165
171
entry to use.
166
172
167
173
"""
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
168
178
entries = lookup(caps, MIMEtype, key)
169
179
# XXX This code should somehow check for the needsterminal flag.
170
180
for e in entries:
171
181
if 'test' in e:
172
182
test = subst(e['test'], filename, plist)
183
+
if test is None:
184
+
continue
173
185
if test and os.system(test) != 0:
174
186
continue
175
187
command = subst(e[key], MIMEtype, filename, plist)
176
-
return command, e
188
+
if command is not None:
189
+
return command, e
177
190
return None, None
178
191
179
192
def lookup(caps, MIMEtype, key=None):
@@ -206,14 +219,23 @@ def subst(field, MIMEtype, filename, plist=[]):
206
219
elif c == 's':
207
220
res = res + filename
208
221
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
209
226
res = res + MIMEtype
210
227
elif c == '{':
211
228
start = i
212
229
while i < n and field[i] != '}':
213
230
i = i+1
214
231
name = field[start:i]
215
232
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
217
239
# XXX To do:
218
240
# %n == number of parts if type is multipart/*
219
241
# %F == list of alternating type and filename for parts
Original file line number Diff line number Diff line change
@@ -123,7 +123,8 @@ def test_subst(self):
123
123
(["", "audio/*", "foo.txt"], ""),
124
124
(["echo foo", "audio/*", "foo.txt"], "echo foo"),
125
125
(["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"),
127
128
(["echo \\%t", "audio/*", "foo.txt"], "echo %t"),
128
129
(["echo foo", "audio/*", "foo.txt", plist], "echo foo"),
129
130
(["echo %{total}", "audio/*", "foo.txt", plist], "echo 3")
@@ -207,7 +208,10 @@ def test_findmatch(self):
207
208
('"An audio fragment"', audio_basic_entry)),
208
209
([c, "audio/*"],
209
210
{"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)),
211
215
([c, "message/external-body"],
212
216
{"plist": plist},
213
217
("showexternal /dev/null default john python.org /tmp foo bar", message_entry))
Original file line number Diff line number Diff 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).
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4