#17281 closed Bug (fixed)
AdminErrorHandler silently fails if the log message contains newlines
Reported by: | Russell Keith-Magee | 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 |
Description
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)
Change History (12)
comment:1 by , 13 years ago
Owner: | changed from | to
---|
comment:2 by , 13 years ago
Cc: | added |
---|
by , 13 years ago
Attachment: | patch.diff added |
---|
comment:3 by , 13 years ago
Has patch: | set |
---|
I've followed the glencoates indications in order to create the patch.
comment:4 by , 13 years ago
Needs tests: | set |
---|---|
Owner: | changed from | to
Status: | new → assigned |
by , 13 years ago
Attachment: | django-17281.2.diff added |
---|
added tests, escape characters instead of truncate on newline
comment:5 by , 13 years ago
I checked and the problem exists in django 1.3.1. The supplied patch fixes it.
comment:6 by , 13 years ago
Needs tests: | unset |
---|
comment:7 by , 13 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
I've reviewed the patch and it looks very good.
comment:8 by , 13 years ago
Cc: | 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.