Skip to content

Commit b1809d4

Browse files
committed
1 parent 476ebae commit b1809d4

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
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

0 commit comments

Comments
 (0)