Skip to content

Commit d001118

Browse files
authored
* Fix CVE-2015-20107 Implemented fix from python/cpython#91993 * Update regex Update regex to be Python 2 compatible.
1 parent aa0526d commit d001118

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Src/StdLib/Lib/mailcap.py

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

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

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

9+
_find_unsafe = re.compile(r'[^\w@%+=:,./-]').search
10+
11+
class UnsafeMailcapInput(Warning):
12+
"""Warning raised when refusing unsafe input"""
13+
714
# Part 1: top-level interface.
815

916
def getcaps():
@@ -149,10 +156,13 @@ def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]):
149156
for e in entries:
150157
if 'test' in e:
151158
test = subst(e['test'], filename, plist)
159+
if test is None:
160+
continue
152161
if test and os.system(test) != 0:
153162
continue
154163
command = subst(e[key], MIMEtype, filename, plist)
155-
return command, e
164+
if command is not None:
165+
return command, e
156166
return None, None
157167

158168
def lookup(caps, MIMEtype, key=None):
@@ -184,14 +194,23 @@ def subst(field, MIMEtype, filename, plist=[]):
184194
elif c == 's':
185195
res = res + filename
186196
elif c == 't':
197+
if _find_unsafe(MIMEtype):
198+
msg = "Refusing to substitute MIME type %r into a shell command." % (MIMEtype,)
199+
warnings.warn(msg, UnsafeMailcapInput)
200+
return None
187201
res = res + MIMEtype
188202
elif c == '{':
189203
start = i
190204
while i < n and field[i] != '}':
191205
i = i+1
192206
name = field[start:i]
193207
i = i+1
194-
res = res + findparam(name, plist)
208+
param = findparam(name, plist)
209+
if _find_unsafe(param):
210+
msg = "Refusing to substitute parameter %r (%s) into a shell command" % (param, name)
211+
warnings.warn(msg, UnsafeMailcapInput)
212+
return None
213+
res = res + param
195214
# XXX To do:
196215
# %n == number of parts if type is multipart/*
197216
# %F == list of alternating type and filename for parts

0 commit comments

Comments
 (0)