Email on [mention] in comments

I just tested the notification feature when somebody mentions you with the @username syntax. The notification itself worked but there was no email upon posting the comment. In the logfiles it told me that there is a BadHeader Error in the /rhodecode/lib/rcmail/message.py file. I looked it up and this function checks for linefeed/carriagereturns in the mail headers and throws an error if there is one. So I monkeypatched it with some simple stripping of linefeeds and carriagereturns and it works.

Is this a bug or did I do something wrong? Did you experience this too?

We never seen such issue, can you post the original error, and your fix ?

Here is the log from the original error.

[03/May/2017:11:56:47 +0200] GNCRN <726>    127.0.0.1       rqt:6.435311 200 3396 "POST:/Converter/changeset/ce4b9a3b0e7826725d41fa58d8269d4293f17b17/comment " usr:- "stuff"
2017-05-03 12:01:50.889 INFO  [rhodecode.lib.base] IP: <ip> User: <AuthUser('id:2[<username>] ip:<ip> auth:True')> accessed /Converter/changeset/ce4b9a3b0e7826725d41fa58d8269d4293f17b17/comment [changeset.comment]
2017-05-03 12:01:51.010 INFO  [rhodecode.lib.auth] user <AuthUser('id:2[<username>] ip:<ip> auth:True')> authenticating with:RHODECODE_AUTH IS authenticated on func ChangesetController:comment
2017-05-03 12:01:51.659 INFO  [root] MAIL SEND TO: [u'<validemailaddress>']
2017-05-03 12:01:51.682 ERROR [rhodecode.lib.celerylib.tasks] Mail sending failed
Traceback (most recent call last):
  File "/opt/rhodecode/store/9q264m389m52myrgjanjivaab4192bva-python2.7-rhodecode-enterprise-ce-4.7.2/lib/python2.7/site-packages/rhodecode/lib/celerylib/tasks.py", line 100, in send_email
    m.send(recipients, subject, body, html_body)
  File "/opt/rhodecode/store/9q264m389m52myrgjanjivaab4192bva-python2.7-rhodecode-enterprise-ce-4.7.2/lib/python2.7/site-packages/rhodecode/lib/rcmail/smtp_mailer.py", line 84, in send
    raw_msg = msg.to_message()
  File "/opt/rhodecode/store/9q264m389m52myrgjanjivaab4192bva-python2.7-rhodecode-enterprise-ce-4.7.2/lib/python2.7/site-packages/rhodecode/lib/rcmail/message.py", line 87, in to_message
    self.validate()
  File "/opt/rhodecode/store/9q264m389m52myrgjanjivaab4192bva-python2.7-rhodecode-enterprise-ce-4.7.2/lib/python2.7/site-packages/rhodecode/lib/rcmail/message.py", line 147, in validate
    if self.is_bad_headers():
BadHeaders
2017-05-03 12:01:51.684 INFO  [rhodecode.lib.utils] Logging action:`user_commented_revision:ce4b9a3b0e7826725d41fa58d8269d4293f17b17` on repo:`<Repository('9:Converter')>` by user:<User('id:2:<username>')> ip:<ip>

Then I printed out what caused the error with a custom exception(Notice the linefeed):

Traceback (most recent call last):
  File "/opt/rhodecode/store/9q264m389m52myrgjanjivaab4192bva-python2.7-rhodecode-enterprise-ce-4.7.2/lib/python2.7/site-packages/rhodecode/lib/celerylib/tasks.py", line 100, in send_email
    m.send(recipients, subject, body, html_body)
  File "/opt/rhodecode/store/9q264m389m52myrgjanjivaab4192bva-python2.7-rhodecode-enterprise-ce-4.7.2/lib/python2.7/site-packages/rhodecode/lib/rcmail/smtp_mailer.py", line 84, in send
    raw_msg = msg.to_message()
  File "/opt/rhodecode/store/9q264m389m52myrgjanjivaab4192bva-python2.7-rhodecode-enterprise-ce-4.7.2/lib/python2.7/site-packages/rhodecode/lib/rcmail/message.py", line 87, in to_message
    self.validate()
  File "/opt/rhodecode/store/9q264m389m52myrgjanjivaab4192bva-python2.7-rhodecode-enterprise-ce-4.7.2/lib/python2.7/site-packages/rhodecode/lib/rcmail/message.py", line 147, in validate
    if self.is_bad_headers():
  File "/opt/rhodecode/store/9q264m389m52myrgjanjivaab4192bva-python2.7-rhodecode-enterprise-ce-4.7.2/lib/python2.7/site-packages/rhodecode/lib/rcmail/message.py", line 129, in is_bad_headers
    raise Exception("Bad Header:" + val + "%%%")
Exception: Bad Header:[RhodeCode] [mention] 
    <username> (RhodeCode Admin) left note on commit `r1:ce4b9a3b0e78` in the Converter repository%%%

And the obvious final fix for this was:

def is_bad_headers(self):
	"""
	Checks for bad headers i.e. newlines in subject, sender or recipients.
	"""

	headers = [self.subject, self.sender]
	headers += list(self.send_to)
	headers += self.extra_headers.values()

	for index, val in enumerate(headers):
		headers[index] = val.strip()

	return False
	#for val in headers:
	#    for c in '\r\n':
	#        if c in val:
	#            return True
	#return False

But then again… How did the linefeed get there :confused:?

We confirmed it’s caused by an error in two email templates. We managed to found this problem, fix it, and add some regression tests. We’ll add this and few other fixes to a next bugfix release in a couple of days.

Thanks for reporting this !

Nice :relaxed:, thanks for the fast response and fix.