From e08ee893d1e286d56dafb4b683024f2df97e245c Mon Sep 17 00:00:00 2001 From: Julien Castiaux Date: Wed, 21 Apr 2021 12:09:31 +0000 Subject: [PATCH] [FIX] mail: sanitize dots in generated mail alias Create a company whoose name ends with a dot like "Bidule Inc.", change the company of the user to that new company, head to the Accouning module and create a new journal of type purchase. Traceback because the generated mail alias for that journal uses the company name and that the local-part of an email cannot ends with a dot. From the RFC standpoint, the local-part of an email address (the part before the @, `john` in `"John Doe" `, cannot begins with, ends with or contain following dots. The email sanitizing function have been updated so it takes care of the above requirement. See also #61811 opw-2448692 closes odoo/odoo#69864 X-original-commit: f0e840ae6db387e1dfc99cc21e09d84eb126e7b9 Signed-off-by: Julien Castiaux --- addons/mail/models/mail_alias.py | 1 + addons/test_mail/tests/test_mail_gateway.py | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/addons/mail/models/mail_alias.py b/addons/mail/models/mail_alias.py index 9ab9722d9cb..d8aec7a9751 100644 --- a/addons/mail/models/mail_alias.py +++ b/addons/mail/models/mail_alias.py @@ -161,6 +161,7 @@ class Alias(models.Model): """ Cleans and sanitizes the alias name """ sanitized_name = remove_accents(name).lower().split('@')[0] sanitized_name = re.sub(r'[^\w+.]+', '-', sanitized_name) + sanitized_name = re.sub(r'^\.+|\.+$|\.+(?=\.)', '', sanitized_name) sanitized_name = sanitized_name.encode('ascii', errors='replace').decode() return sanitized_name diff --git a/addons/test_mail/tests/test_mail_gateway.py b/addons/test_mail/tests/test_mail_gateway.py index b76e5d9752b..f722cf236ed 100644 --- a/addons/test_mail/tests/test_mail_gateway.py +++ b/addons/test_mail/tests/test_mail_gateway.py @@ -122,6 +122,13 @@ class TestMailAlias(TestMailCommon): with self.assertRaises(exceptions.ValidationError): record.write({'alias_defaults': "{'custom_field': brokendict"}) + def test_alias_sanitize(self): + alias = self.env['mail.alias'].create({ + 'alias_model_id': self.env['ir.model']._get('mail.test.container').id, + 'alias_name': 'bidule...inc.', + }) + self.assertEqual(alias.alias_name, 'bidule.inc', 'Emails cannot start or end with a dot, there cannot be a sequence of dots.') + def test_alias_setup(self): alias = self.env['mail.alias'].create({ 'alias_model_id': self.env['ir.model']._get('mail.test.container').id,