Ticket #5887: mail.diff
File mail.diff, 1.9 KB (added by , 17 years ago) |
---|
-
django/core/mail.py
67 67 class BadHeaderError(ValueError): 68 68 pass 69 69 70 class SafeMIMEText(MIMEText): 71 def __setitem__(self, name, val): 70 def forbid_multi_line_headers(name, val): 72 71 "Forbids multi-line headers, to prevent header injection." 73 72 if '\n' in val or '\r' in val: 74 73 raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name) … … 84 83 val = ', '.join(result) 85 84 else: 86 85 val = Header(force_unicode(val), settings.DEFAULT_CHARSET) 86 return (name, val) 87 88 class SafeMIMEText(MIMEText): 89 def __setitem__(self, name, val): 90 name, val = forbid_multi_line_headers(name, val) 87 91 MIMEText.__setitem__(self, name, val) 88 92 89 93 class SafeMIMEMultipart(MIMEMultipart): 90 94 def __setitem__(self, name, val): 91 "Forbids multi-line headers, to prevent header injection." 92 if '\n' in val or '\r' in val: 93 raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name) 94 try: 95 val = force_unicode(val).encode('ascii') 96 except UnicodeEncodeError: 97 if name.lower() in ('to', 'from', 'cc'): 98 result = [] 99 for item in val.split(', '): 100 nm, addr = parseaddr(item) 101 nm = str(Header(nm, settings.DEFAULT_CHARSET)) 102 result.append(formataddr((nm, str(addr)))) 103 val = ', '.join(result) 104 else: 105 val = Header(force_unicode(val), settings.DEFAULT_CHARSET) 95 name, val = forbid_multi_line_headers(name, val) 106 96 MIMEMultipart.__setitem__(self, name, val) 107 97 108 98 class SMTPConnection(object):