2eb651
From 2b76a5a3aa898fd1621c72c6da935cddfb484424 Mon Sep 17 00:00:00 2001
2eb651
From: Lumir Balhar <lbalhar@redhat.com>
2eb651
Date: Fri, 12 Mar 2021 14:34:06 +0100
2eb651
Subject: [PATCH] CVE-2020-28493
2eb651
2eb651
---
2eb651
 Jinja2-2.10.1/jinja2/utils.py | 94 +++++++++++++++++++++--------------
2eb651
 1 file changed, 56 insertions(+), 38 deletions(-)
2eb651
2eb651
diff --git a/Jinja2-2.10.1/jinja2/utils.py b/Jinja2-2.10.1/jinja2/utils.py
2eb651
index 502a311..25dd78f 100644
2eb651
--- a/Jinja2-2.10.1/jinja2/utils.py
2eb651
+++ b/Jinja2-2.10.1/jinja2/utils.py
2eb651
@@ -12,24 +12,12 @@ import re
2eb651
 import json
2eb651
 import errno
2eb651
 from collections import deque
2eb651
+from string import ascii_letters as _letters
2eb651
+from string import digits as _digits
2eb651
 from threading import Lock
2eb651
 from jinja2._compat import text_type, string_types, implements_iterator, \
2eb651
      url_quote
2eb651
 
2eb651
-
2eb651
-_word_split_re = re.compile(r'(\s+)')
2eb651
-_punctuation_re = re.compile(
2eb651
-    '^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % (
2eb651
-        '|'.join(map(re.escape, ('(', '<', '<'))),
2eb651
-        '|'.join(map(re.escape, ('.', ',', ')', '>', '\n', '>')))
2eb651
-    )
2eb651
-)
2eb651
-_simple_email_re = re.compile(r'^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$')
2eb651
-_striptags_re = re.compile(r'(|<[^>]*>)')
2eb651
-_entity_re = re.compile(r'&([^;]+);')
2eb651
-_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
2eb651
-_digits = '0123456789'
2eb651
-
2eb651
 # special singleton representing missing values for the runtime
2eb651
 missing = type('MissingType', (), {'__repr__': lambda x: 'missing'})()
2eb651
 
2eb651
@@ -203,35 +191,65 @@ def urlize(text, trim_url_limit=None, rel=None, target=None):
2eb651
     trim_url = lambda x, limit=trim_url_limit: limit is not None \
2eb651
                          and (x[:limit] + (len(x) >=limit and '...'
2eb651
                          or '')) or x
2eb651
-    words = _word_split_re.split(text_type(escape(text)))
2eb651
+    words = re.split(r"(\s+)", text_type(escape(text)))
2eb651
     rel_attr = rel and ' rel="%s"' % text_type(escape(rel)) or ''
2eb651
     target_attr = target and ' target="%s"' % escape(target) or ''
2eb651
 
2eb651
     for i, word in enumerate(words):
2eb651
-        match = _punctuation_re.match(word)
2eb651
+        head, middle, tail = "", word, ""
2eb651
+        match = re.match(r"^([(<]|<)+", middle)
2eb651
+
2eb651
         if match:
2eb651
-            lead, middle, trail = match.groups()
2eb651
-            if middle.startswith('www.') or (
2eb651
-                '@' not in middle and
2eb651
-                not middle.startswith('http://') and
2eb651
-                not middle.startswith('https://') and
2eb651
-                len(middle) > 0 and
2eb651
-                middle[0] in _letters + _digits and (
2eb651
-                    middle.endswith('.org') or
2eb651
-                    middle.endswith('.net') or
2eb651
-                    middle.endswith('.com')
2eb651
-                )):
2eb651
-                middle = '%s' % (middle,
2eb651
-                    rel_attr, target_attr, trim_url(middle))
2eb651
-            if middle.startswith('http://') or \
2eb651
-               middle.startswith('https://'):
2eb651
-                middle = '%s' % (middle,
2eb651
-                    rel_attr, target_attr, trim_url(middle))
2eb651
-            if '@' in middle and not middle.startswith('www.') and \
2eb651
-               not ':' in middle and _simple_email_re.match(middle):
2eb651
-                middle = '%s' % (middle, middle)
2eb651
-            if lead + middle + trail != word:
2eb651
-                words[i] = lead + middle + trail
2eb651
+            head = match.group()
2eb651
+            middle = middle[match.end() :]
2eb651
+
2eb651
+        # Unlike lead, which is anchored to the start of the string,
2eb651
+        # need to check that the string ends with any of the characters
2eb651
+        # before trying to match all of them, to avoid backtracking.
2eb651
+        if middle.endswith((")", ">", ".", ",", "\n", ">")):
2eb651
+            match = re.search(r"([)>.,\n]|>)+$", middle)
2eb651
+
2eb651
+            if match:
2eb651
+                tail = match.group()
2eb651
+                middle = middle[: match.start()]
2eb651
+
2eb651
+        if middle.startswith("www.") or (
2eb651
+            "@" not in middle
2eb651
+            and not middle.startswith("http://")
2eb651
+            and not middle.startswith("https://")
2eb651
+            and len(middle) > 0
2eb651
+            and middle[0] in _letters + _digits
2eb651
+            and (
2eb651
+                middle.endswith(".org")
2eb651
+                or middle.endswith(".net")
2eb651
+                or middle.endswith(".com")
2eb651
+            )
2eb651
+        ):
2eb651
+            middle = '%s' % (
2eb651
+                middle,
2eb651
+                rel_attr,
2eb651
+                target_attr,
2eb651
+                trim_url(middle),
2eb651
+            )
2eb651
+
2eb651
+        if middle.startswith("http://") or middle.startswith("https://"):
2eb651
+            middle = '%s' % (
2eb651
+                middle,
2eb651
+                rel_attr,
2eb651
+                target_attr,
2eb651
+                trim_url(middle),
2eb651
+            )
2eb651
+
2eb651
+        if (
2eb651
+            "@" in middle
2eb651
+            and not middle.startswith("www.")
2eb651
+            and ":" not in middle
2eb651
+            and re.match(r"^\S@\w[\w.-]*\.\w$", middle)
2eb651
+        ):
2eb651
+            middle = '%s' % (middle, middle)
2eb651
+
2eb651
+        words[i] = head + middle + tail
2eb651
+
2eb651
     return u''.join(words)
2eb651
 
2eb651
 
2eb651
-- 
2eb651
2.29.2
2eb651