Skip to content

Commit

Permalink
fix: lock protect markdown rendering
Browse files Browse the repository at this point in the history
The MarkdownRenderer is not thread-safe, see miyuchina/mistletoe#210

Fixes WEBLATE-1Q34
  • Loading branch information
nijel committed Dec 17, 2024
1 parent 0ed89d2 commit d06096a
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions weblate/utils/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

import re
import threading
from functools import reduce

import mistletoe
Expand All @@ -14,6 +15,7 @@
from weblate.auth.models import User

MENTION_RE = re.compile(r"(?<!\w)(@[\w.@+-]+)\b")
MARKDOWN_LOCK = threading.Lock()


def get_mention_users(text):
Expand Down Expand Up @@ -118,7 +120,7 @@ def check_url(self, url: str) -> bool:
return bool(self._allowed_url_re.match(url))


def render_markdown(text):
def render_markdown(text: str) -> str:
users = {u.username.lower(): u for u in get_mention_users(text)}
parts = MENTION_RE.split(text)
for pos, part in enumerate(parts):
Expand All @@ -131,5 +133,5 @@ def render_markdown(text):
f'**[{part}]({user.get_absolute_url()} "{user.get_visible_name()}")**'
)
text = "".join(parts)
with SaferWeblateHtmlRenderer() as renderer:
with MARKDOWN_LOCK, SaferWeblateHtmlRenderer() as renderer:
return mark_safe(renderer.render(mistletoe.Document(text))) # noqa: S308

0 comments on commit d06096a

Please sign in to comment.