Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
gersona committed Jun 21, 2024
1 parent 58aa0aa commit f0adf47
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions weblate/utils/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def __init__(self, match):
self.content = ""


class SafeWeblateHtmlRenderer(mistletoe.HtmlRenderer):
class SaferWeblateHtmlRenderer(mistletoe.HtmlRenderer):
"""
A subclass of :class:`mistletoe.HtmlRenderer` which adds a layer of protection against malicious input.
A renderer which adds a layer of protection against malicious input.
1. Check if the URL is valid based on scheme and content
2. Strip HTML tags from the content.
Expand Down Expand Up @@ -71,10 +71,12 @@ def render_auto_link(self, token: span_token.AutoLink) -> str:
"""
Render an auto link token.
If the URL is valid, render the auto link as usual. Otherwise, escape the URL.
If the URL is valid, render the auto link as usual.
Otherwise, escape the URL.
"""

def valid_email(email: str) -> bool:
"""Check if an email address is valid."""
pattern = re.compile(
r"(mailto:)?[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
)
Expand All @@ -88,7 +90,8 @@ def render_image(self, token: span_token.Image) -> str:
"""
Render an image token.
If the URL is valid, add the necessary attributes to the image tag. Otherwise, escape the URL.
If the URL is valid, add the necessary attributes to the image tag.
Otherwise, escape the URL.
"""
if self.check_url(token.src):
return super().render_image(token)
Expand All @@ -114,5 +117,5 @@ def render_markdown(text):
f'**[{part}]({user.get_absolute_url()} "{user.get_visible_name()}")**'
)
text = "".join(parts)
with SafeWeblateHtmlRenderer() as renderer:
with SaferWeblateHtmlRenderer() as renderer:
return renderer.render(mistletoe.Document(text))

0 comments on commit f0adf47

Please sign in to comment.