From f003eaf884a384e71507b4ee5815853938030324 Mon Sep 17 00:00:00 2001 From: Julien Castiaux Date: Tue, 2 Jan 2024 16:30:00 +0100 Subject: [PATCH] [FIX] link_tracker: make relative target URLs absolute Install mass_mailing with demo data, send a mailing using the "Thank you for joining us" template. Inside your mail client, click on the LOGIN button, this open your web browser on a link-tracker URL but the page fails to load because "The page isn't redirecting properly". Inside the template of that "Thank you for joining us" mail, the logging button is basically defined as follow: `LOGIN`, a URL with a fragment that is empty, a redirection to the current page. Upon rendering that template and send it to the recipients, all links are wrapped inside a link-tracker for well tracking purpose, this created a link `/r/xyz` targeting `#`. Upon accessing that `/r/xyz` URL the client would be redirected to `#` which in that context is actually `/r/xyz#`: the link-tracker itself. The browser detects that there is a redirecting loop and show an error instead. The problem is solved by saving an absolute link with the link-tracker instead of a relative one. Task-3603607 closes odoo/odoo#150678 Signed-off-by: Thibault Delavallee (tde) Signed-off-by: Julien Castiaux (juc) --- addons/link_tracker/models/mail_render_mixin.py | 5 ++++- addons/link_tracker/tests/test_mail_render_mixin.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/addons/link_tracker/models/mail_render_mixin.py b/addons/link_tracker/models/mail_render_mixin.py index f7ff2d4548b..dcb0f4ccd18 100644 --- a/addons/link_tracker/models/mail_render_mixin.py +++ b/addons/link_tracker/models/mail_render_mixin.py @@ -38,6 +38,9 @@ class MailRenderMixin(models.AbstractModel): short_schema = base_url + '/r/' for match in set(re.findall(tools.HTML_TAG_URL_REGEX, html)): long_url = match[1] + # Make relative links absolute + if long_url.startswith(('/', '?', '#')): + long_url = base_url + long_url # Don't shorten already-shortened links if long_url.startswith(short_schema): continue @@ -50,7 +53,7 @@ class MailRenderMixin(models.AbstractModel): link = self.env['link.tracker'].search_or_create(create_vals) if link.short_url: # `str` manipulation required to support replacing "&" characters, common in urls - new_href = match[0].replace(long_url, link.short_url) + new_href = match[0].replace(match[1], link.short_url) html = html.replace(markupsafe.Markup(match[0]), markupsafe.Markup(new_href)) return html diff --git a/addons/link_tracker/tests/test_mail_render_mixin.py b/addons/link_tracker/tests/test_mail_render_mixin.py index b008fe75eca..c60198daf23 100644 --- a/addons/link_tracker/tests/test_mail_render_mixin.py +++ b/addons/link_tracker/tests/test_mail_render_mixin.py @@ -30,6 +30,7 @@ class TestMailRenderMixin(common.HttpCase): """, ' test_escaped < > ', 'label', + '', ] self.env["mail.render.mixin"]._shorten_links("".join(test_links), {}) @@ -49,6 +50,7 @@ class TestMailRenderMixin(common.HttpCase): ("url", "=", "https://url_with_params.com?a=b&c=d"), ("label", "=", "label"), ], + [("url", "=", self.base_url + '#')], ] trackers_to_fail = [ [("url", "=", "https://test_542152qsdqsd.com"), ("label", "ilike", "_")] @@ -86,6 +88,7 @@ class TestMailRenderMixin(common.HttpCase): 'And a third: Here\n' 'And a forth: Here\n' 'And a fifth: Here too\n' + 'And a 6th: Here
\n' 'And a last, more complex: There!' .format(base_url=self.base_url) ) @@ -95,6 +98,7 @@ class TestMailRenderMixin(common.HttpCase): 'And a third: Here\n' 'And a forth: Here\n' 'And a fifth: Here too\n' + 'And a 6th: Here
\n' 'And a last, more complex: There!' .format(base_url=self.base_url) )