Opened 4 years ago

Closed 4 years ago

#17281 closed Bug (fixed)

AdminErrorHandler silently fails if the log message contains newlines

Reported by: russellm Owned by: marw85
Component: Core (Other) Version:
Severity: Normal Keywords:
Cc: glencoates, iacobcatalin@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no


If you have loggers configured with an django.utils.log.AdminEmailHandler, and you log a message that has a newline in it, the log message is handled by the logger, but is silently discarded.

For example if you sent the following:

logger.error('This is a\n test message')

The log message will be correctly written to any text-based log handlers or console log handlers, but the AdminEmailHandler silently fails.

This is because the subject of an email can't contain newlines.

Given that the AdminEmailHandler is intended as a mechanism for reporting serious errors, it's pretty bad that it can fail silently due to the contents of the message it's reporting. The "subject" of the log email should be cleansed of newlines before the call to mail_admins is made.

This cleansing could also be performed in the mail utilities themselves. However, I'm not convinced this is the right place; the general mail tools have "fail_silently" as an option, so it would be possible to handle this error in a better way. Failing is not an option during logging, so we should make sure that the messages to be sent are appropriately cleansed.

Attachments (2)

patch.diff (1.3 KB) - added by elbarto 4 years ago.
django-17281.2.diff (3.1 KB) - added by marw85 4 years ago.
added tests, escape characters instead of truncate on newline

Download all attachments as: .zip

Change History (11)

comment:1 Changed 4 years ago by elbarto

  • Owner changed from nobody to elbarto

comment:2 Changed 4 years ago by glencoates

  • Cc glencoates added

I would guess that in many cases the bit of an error message after the first newline is a stack trace, and that in that case you probably don't want all that stuff in the email subject (it will be in the body anyway). It would be good if AdminEmailHandler defined a make_subject( record ) method which strips everything after the first newline, but allows the user to easily override that behaviour in a custom handler class if they choose.

Changed 4 years ago by elbarto

comment:3 Changed 4 years ago by elbarto

  • Has patch set

I've followed the glencoates indications in order to create the patch.

comment:4 Changed 4 years ago by marw85

  • Needs tests set
  • Owner changed from elbarto to marw85
  • Status changed from new to assigned

Changed 4 years ago by marw85

added tests, escape characters instead of truncate on newline

comment:5 Changed 4 years ago by viciu

I checked and the problem exists in django 1.3.1. The supplied patch fixes it.

comment:6 Changed 4 years ago by marw85

  • Needs tests unset

comment:7 Changed 4 years ago by oinopion

  • Triage Stage changed from Accepted to Ready for checkin

I've reviewed the patch and it looks very good.

comment:8 Changed 4 years ago by cataliniacob

  • Cc iacobcatalin@… added

comment:9 Changed 4 years ago by julien

  • Resolution set to fixed
  • Status changed from assigned to closed

In [17501]:

Fixed #17281 -- Prevented AdminErrorHandler from silently failing if the log message contains newlines. Thanks to Russell Keith-Magee for the report and to Bartolome Sanchez Salado and Marcin Wróbel for the patch.

Note: See TracTickets for help on using tickets.
Back to Top