Skip to content

Commit 2e458f0

Browse files
authored
* Fix CVE-2015-20107 * Fix tests
1 parent 699824b commit 2e458f0

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

Src/StdLib/Lib/mailcap.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
"""Mailcap file handling. See RFC 1524."""
22

33
import os
4+
import warnings
5+
import re
46

57
__all__ = ["getcaps","findmatch"]
68

9+
# https://github.com/IronLanguages/ironpython3/issues/1274
10+
# _find_unsafe = re.compile(r'[^\xa1-\U0010FFFF\w@+=:,./-]').search
11+
_find_unsafe = re.compile(r'[^\w@+=:,./-]').search
12+
13+
class UnsafeMailcapInput(Warning):
14+
"""Warning raised when refusing unsafe input"""
15+
716
# Part 1: top-level interface.
817

918
def getcaps():
@@ -144,15 +153,22 @@ def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]):
144153
entry to use.
145154
146155
"""
156+
if _find_unsafe(filename):
157+
msg = "Refusing to use mailcap with filename %r. Use a safe temporary filename." % (filename,)
158+
warnings.warn(msg, UnsafeMailcapInput)
159+
return None, None
147160
entries = lookup(caps, MIMEtype, key)
148161
# XXX This code should somehow check for the needsterminal flag.
149162
for e in entries:
150163
if 'test' in e:
151164
test = subst(e['test'], filename, plist)
165+
if test is None:
166+
continue
152167
if test and os.system(test) != 0:
153168
continue
154169
command = subst(e[key], MIMEtype, filename, plist)
155-
return command, e
170+
if command is not None:
171+
return command, e
156172
return None, None
157173

158174
def lookup(caps, MIMEtype, key=None):
@@ -184,14 +200,23 @@ def subst(field, MIMEtype, filename, plist=[]):
184200
elif c == 's':
185201
res = res + filename
186202
elif c == 't':
203+
if _find_unsafe(MIMEtype):
204+
msg = "Refusing to substitute MIME type %r into a shell command." % (MIMEtype,)
205+
warnings.warn(msg, UnsafeMailcapInput)
206+
return None
187207
res = res + MIMEtype
188208
elif c == '{':
189209
start = i
190210
while i < n and field[i] != '}':
191211
i = i+1
192212
name = field[start:i]
193213
i = i+1
194-
res = res + findparam(name, plist)
214+
param = findparam(name, plist)
215+
if _find_unsafe(param):
216+
msg = "Refusing to substitute parameter %r (%s) into a shell command" % (param, name)
217+
warnings.warn(msg, UnsafeMailcapInput)
218+
return None
219+
res = res + param
195220
# XXX To do:
196221
# %n == number of parts if type is multipart/*
197222
# %F == list of alternating type and filename for parts

Src/StdLib/Lib/test/test_mailcap.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ def test_subst(self):
101101
(["", "audio/*", "foo.txt"], ""),
102102
(["echo foo", "audio/*", "foo.txt"], "echo foo"),
103103
(["echo %s", "audio/*", "foo.txt"], "echo foo.txt"),
104-
(["echo %t", "audio/*", "foo.txt"], "echo audio/*"),
105-
(["echo \%t", "audio/*", "foo.txt"], "echo %t"),
104+
(["echo %t", "audio/*", "foo.txt"], None),
105+
(["echo %t", "audio/wav", "foo.txt"], "echo audio/wav"),
106+
(["echo \\%t", "audio/*", "foo.txt"], "echo %t"),
106107
(["echo foo", "audio/*", "foo.txt", plist], "echo foo"),
107108
(["echo %{total}", "audio/*", "foo.txt", plist], "echo 3")
108109
]
@@ -183,7 +184,10 @@ def test_findmatch(self):
183184
('"An audio fragment"', audio_basic_entry)),
184185
([c, "audio/*"],
185186
{"filename": fname},
186-
("/usr/local/bin/showaudio audio/*", audio_entry)),
187+
(None, None)),
188+
([c, "audio/wav"],
189+
{"filename": fname},
190+
("/usr/local/bin/showaudio audio/wav", audio_entry)),
187191
([c, "message/external-body"],
188192
{"plist": plist},
189193
("showexternal /dev/null default john python.org /tmp foo bar", message_entry))

0 commit comments

Comments
 (0)