Skip to content

Commit

Permalink
[FIX] mail: properly make urls absolute in escaped attributes
Browse files Browse the repository at this point in the history
`_replace_local_links` ensures urls are given as absolute rather
relative paths, for link `href`, img `src` and within styles. This
failed when the style contained escaped single quotes (eg:
`background-image: url("/my_url/path");`). This commit adapts the
failing regex appropriately.

X-original-commit: 2af980e
  • Loading branch information
Zynton committed Dec 2, 2021
1 parent a1075ea commit b46623b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion addons/mail/models/mail_render_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,14 @@ def _sub_relative2absolute(match):
_sub_relative2absolute.base_url = base_url
html = re.sub(r"""(<img(?=\s)[^>]*\ssrc=")(/[^/][^"]+)""", _sub_relative2absolute, html)
html = re.sub(r"""(<a(?=\s)[^>]*\shref=")(/[^/][^"]+)""", _sub_relative2absolute, html)
html = re.sub(r"""(<[^>]+\bstyle="[^"]+\burl\('?)(/[^/'][^'")]+)""", _sub_relative2absolute, html)
html = re.sub(re.compile(
r"""( # Group 1: element up to url in style
<[^>]+\bstyle=" # Element with a style attribute
[^"]+\burl\( # Style attribute contains "url(" style
(?:&\#34;|')?) # url style may start with (escaped) quote: capture it
( # Group 2: url itself
/(?:[^'")]|(?!&\#34;))+ # stop at the first closing quote
)""", re.VERBOSE), _sub_relative2absolute, html)

return wrapper(html)

Expand Down
2 changes: 1 addition & 1 deletion addons/mail/tests/test_mail_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def test_render_template_local_links(self):
rendered_local_links = [
'<div style="background-image:url(%s/web/path?a=a&b=b);"/>' % base_url,
'<div style="background-image:url(\'%s/web/path?a=a&b=b\');"/>' % base_url,
'<div style="background-image:url(&#34;/web/path?a=a&b=b&#34;);"/>'
'<div style="background-image:url(&#34;%s/web/path?a=a&b=b&#34;);"/>' % base_url
]
for source, expected in zip(local_links_template_bits, rendered_local_links):
rendered = self.env['mail.render.mixin']._replace_local_links(source)
Expand Down

0 comments on commit b46623b

Please sign in to comment.