Annotation of pkgsrc/lang/python27/patches/patch-Lib_mailcap.py, revision 1.1

1.1     ! gutterid    1: $NetBSD$
        !             2: 
        !             3: Fix CVE-2015-20107: Make mailcap refuse to match unsafe filenames/types/params
        !             4: 
        !             5: Via Fedora:
        !             6: https://src.fedoraproject.org/rpms/python2.7/raw/a9b12e85bd4d3280e07bc3bfa72a9f2b674cb4ff/f/00382-cve-2015-20107.patch
        !             7: 
        !             8: --- Lib/mailcap.py.orig        2020-04-19 21:13:39.000000000 +0000
        !             9: +++ Lib/mailcap.py
        !            10: @@ -1,9 +1,18 @@
        !            11:  """Mailcap file handling.  See RFC 1524."""
        !            12:  
        !            13:  import os
        !            14: +import warnings
        !            15: +import re
        !            16:  
        !            17:  __all__ = ["getcaps","findmatch"]
        !            18:  
        !            19: +
        !            20: +_find_unsafe = re.compile(r'[^\xa1-\xff\w@+=:,./-]').search
        !            21: +
        !            22: +class UnsafeMailcapInput(Warning):
        !            23: +    """Warning raised when refusing unsafe input"""
        !            24: +
        !            25: +
        !            26:  # Part 1: top-level interface.
        !            27:  
        !            28:  def getcaps():
        !            29: @@ -144,15 +153,22 @@ def findmatch(caps, MIMEtype, key='view'
        !            30:      entry to use.
        !            31:  
        !            32:      """
        !            33: +    if _find_unsafe(filename):
        !            34: +        msg = "Refusing to use mailcap with filename %r. Use a safe temporary filename." % (filename,)
        !            35: +        warnings.warn(msg, UnsafeMailcapInput)
        !            36: +        return None, None
        !            37:      entries = lookup(caps, MIMEtype, key)
        !            38:      # XXX This code should somehow check for the needsterminal flag.
        !            39:      for e in entries:
        !            40:          if 'test' in e:
        !            41:              test = subst(e['test'], filename, plist)
        !            42: +            if test is None:
        !            43: +                continue
        !            44:              if test and os.system(test) != 0:
        !            45:                  continue
        !            46:          command = subst(e[key], MIMEtype, filename, plist)
        !            47: -        return command, e
        !            48: +        if command is not None:
        !            49: +            return command, e
        !            50:      return None, None
        !            51:  
        !            52:  def lookup(caps, MIMEtype, key=None):
        !            53: @@ -184,6 +200,10 @@ def subst(field, MIMEtype, filename, pli
        !            54:              elif c == 's':
        !            55:                  res = res + filename
        !            56:              elif c == 't':
        !            57: +                if _find_unsafe(MIMEtype):
        !            58: +                    msg = "Refusing to substitute MIME type %r into a shell command." % (MIMEtype,)
        !            59: +                    warnings.warn(msg, UnsafeMailcapInput)
        !            60: +                    return None
        !            61:                  res = res + MIMEtype
        !            62:              elif c == '{':
        !            63:                  start = i
        !            64: @@ -191,7 +211,12 @@ def subst(field, MIMEtype, filename, pli
        !            65:                      i = i+1
        !            66:                  name = field[start:i]
        !            67:                  i = i+1
        !            68: -                res = res + findparam(name, plist)
        !            69: +                param = findparam(name, plist)
        !            70: +                if _find_unsafe(param):
        !            71: +                    msg = "Refusing to substitute parameter %r (%s) into a shell command" % (param, name)
        !            72: +                    warnings.warn(msg, UnsafeMailcapInput)
        !            73: +                    return None
        !            74: +                res = res + param
        !            75:              # XXX To do:
        !            76:              # %n == number of parts if type is multipart/*
        !            77:              # %F == list of alternating type and filename for parts

CVSweb <webmaster@jp.NetBSD.org>