[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: `<a href="#">LOGIN</a>`, 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) <tde@openerp.com>
Signed-off-by: Julien Castiaux (juc) <juc@odoo.com>
This commit is contained in:
Julien Castiaux
2024-01-24 10:44:40 +00:00
parent 05fd991389
commit f003eaf884
2 changed files with 8 additions and 1 deletions
@@ -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
@@ -30,6 +30,7 @@ class TestMailRenderMixin(common.HttpCase):
""",
'<a href="https://test_escaped.com" title="title" fake="fake"> test_escaped &lt; &gt; </a>',
'<a href="https://url_with_params.com?a=b&c=d">label</a>',
'<a href="#"></a>',
]
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: <a href="{base_url}">Here</a>\n'
'And a forth: <a href="{base_url}">Here</a>\n'
'And a fifth: <a href="{base_url}">Here too</a>\n'
'And a 6th: <a href="/web">Here</a><br>\n'
'And a last, more complex: <a href="https://boinc.berkeley.edu/forum_thread.php?id=14544&postid=106833">There!</a>'
.format(base_url=self.base_url)
)
@@ -95,6 +98,7 @@ class TestMailRenderMixin(common.HttpCase):
'And a third: <a href="{base_url}/r/([\\w]+)">Here</a>\n'
'And a forth: <a href="{base_url}/r/([\\w]+)">Here</a>\n'
'And a fifth: <a href="{base_url}/r/([\\w]+)">Here too</a>\n'
'And a 6th: <a href="{base_url}/r/([\\w]+)">Here</a><br>\n'
'And a last, more complex: <a href="{base_url}/r/([\\w]+)">There!</a>'
.format(base_url=self.base_url)
)