diff options
author | Petr Viktorin <encukou@gmail.com> | 2022-06-03 09:43:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-03 09:43:35 (GMT) |
commit | b9509ba7a9c668b984dab876c7926fe1dc5aa0ba (patch) | |
tree | 87acb264c690572842b87aa4d21679dd26623b14 /Lib/mailcap.py | |
parent | 5a80e8580e2eb9eac4035d81439ed51523fcc4d2 (diff) | |
download | cpython-b9509ba7a9c668b984dab876c7926fe1dc5aa0ba.zip cpython-b9509ba7a9c668b984dab876c7926fe1dc5aa0ba.tar.gz cpython-b9509ba7a9c668b984dab876c7926fe1dc5aa0ba.tar.bz2 |
gh-68966: Make mailcap refuse to match unsafe filenames/types/params (GH-91993)
Diffstat (limited to 'Lib/mailcap.py')
-rw-r--r-- | Lib/mailcap.py | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/Lib/mailcap.py b/Lib/mailcap.py index 856b6a5..7278ea7 100644 --- a/Lib/mailcap.py +++ b/Lib/mailcap.py @@ -2,6 +2,7 @@ import os import warnings +import re __all__ = ["getcaps","findmatch"] @@ -19,6 +20,11 @@ def lineno_sort_key(entry): else: return 1, 0 +_find_unsafe = re.compile(r'[^\xa1-\U0010FFFF\w@+=:,./-]').search + +class UnsafeMailcapInput(Warning): + """Warning raised when refusing unsafe input""" + # Part 1: top-level interface. @@ -171,15 +177,22 @@ def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]): entry to use. """ + if _find_unsafe(filename): + msg = "Refusing to use mailcap with filename %r. Use a safe temporary filename." % (filename,) + warnings.warn(msg, UnsafeMailcapInput) + return None, None entries = lookup(caps, MIMEtype, key) # XXX This code should somehow check for the needsterminal flag. for e in entries: if 'test' in e: test = subst(e['test'], filename, plist) + if test is None: + continue if test and os.system(test) != 0: continue command = subst(e[key], MIMEtype, filename, plist) - return command, e + if command is not None: + return command, e return None, None def lookup(caps, MIMEtype, key=None): @@ -212,6 +225,10 @@ def subst(field, MIMEtype, filename, plist=[]): elif c == 's': res = res + filename elif c == 't': + if _find_unsafe(MIMEtype): + msg = "Refusing to substitute MIME type %r into a shell command." % (MIMEtype,) + warnings.warn(msg, UnsafeMailcapInput) + return None res = res + MIMEtype elif c == '{': start = i @@ -219,7 +236,12 @@ def subst(field, MIMEtype, filename, plist=[]): i = i+1 name = field[start:i] i = i+1 - res = res + findparam(name, plist) + param = findparam(name, plist) + if _find_unsafe(param): + msg = "Refusing to substitute parameter %r (%s) into a shell command" % (param, name) + warnings.warn(msg, UnsafeMailcapInput) + return None + res = res + param # XXX To do: # %n == number of parts if type is multipart/* # %F == list of alternating type and filename for parts |