Backported the fix from [5581] (unicode-branch) to trunk. Fixes a method

resolution order problem when setting email headers.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5582 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-07-01 06:32:34 +00:00
parent 3a892122f2
commit 1b1379a182
1 changed files with 10 additions and 9 deletions

View File

@ -62,22 +62,23 @@ def make_msgid(idstring=None):
class BadHeaderError(ValueError): class BadHeaderError(ValueError):
pass pass
class SafeHeaderMixin(object): class SafeMIMEText(MIMEText):
def __setitem__(self, name, val): def __setitem__(self, name, val):
"Forbids multi-line headers, to prevent header injection." "Forbids multi-line headers, to prevent header injection."
if '\n' in val or '\r' in val: if '\n' in val or '\r' in val:
raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name) raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name)
if name == "Subject": if name == "Subject":
val = Header(val, settings.DEFAULT_CHARSET) val = Header(val, settings.DEFAULT_CHARSET)
# Note: using super() here is safe; any __setitem__ overrides must use MIMEText.__setitem__(self, name, val)
# the same argument signature.
super(SafeHeaderMixin, self).__setitem__(name, val)
class SafeMIMEText(MIMEText, SafeHeaderMixin): class SafeMIMEMultipart(MIMEMultipart):
pass def __setitem__(self, name, val):
"Forbids multi-line headers, to prevent header injection."
class SafeMIMEMultipart(MIMEMultipart, SafeHeaderMixin): if '\n' in val or '\r' in val:
pass raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name)
if name == "Subject":
val = Header(val, settings.DEFAULT_CHARSET)
MIMEMultipart.__setitem__(self, name, val)
class SMTPConnection(object): class SMTPConnection(object):
""" """