File CVE-2025-26699.patch of Package python-Django.18994
From e88f7376fe68dbf4ebaf11fad1513ce700b45860 Mon Sep 17 00:00:00 2001
From: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com>
Date: Tue, 25 Feb 2025 09:40:54 +0100
Subject: [PATCH] [4.2.x] Fixed CVE-2025-26699 -- Mitigated potential DoS in
wordwrap template filter.
Thanks sw0rd1ight for the report.
Backport of 55d89e25f4115c5674cdd9b9bcba2bb2bb6d820b from main.
---
django/utils/text.py | 28 +++++++------------
docs/releases/4.2.20.txt | 6 ++++
.../filter_tests/test_wordwrap.py | 11 ++++++++
3 files changed, 27 insertions(+), 18 deletions(-)
Index: Django-2.2.28/django/utils/text.py
===================================================================
--- Django-2.2.28.orig/django/utils/text.py
+++ Django-2.2.28/django/utils/text.py
@@ -1,5 +1,6 @@
import html.entities
import re
+import textwrap
import unicodedata
from gzip import GzipFile
from io import BytesIO
@@ -91,23 +92,15 @@ def wrap(text, width):
Don't wrap long words, thus the output text may have lines longer than
``width``.
"""
- def _generator():
- for line in text.splitlines(True): # True keeps trailing linebreaks
- max_width = min((line.endswith('\n') and width + 1 or width), width)
- while len(line) > max_width:
- space = line[:max_width + 1].rfind(' ') + 1
- if space == 0:
- space = line.find(' ') + 1
- if space == 0:
- yield line
- line = ''
- break
- yield '%s\n' % line[:space - 1]
- line = line[space:]
- max_width = min((line.endswith('\n') and width + 1 or width), width)
- if line:
- yield line
- return ''.join(_generator())
+ wrapper = textwrap.TextWrapper(
+ width=width,
+ break_long_words=False,
+ break_on_hyphens=False,
+ )
+ result = []
+ for line in text.splitlines(True):
+ result.extend(wrapper.wrap(line))
+ return "\n".join(result)
class Truncator(SimpleLazyObject):
Index: Django-2.2.28/tests/template_tests/filter_tests/test_wordwrap.py
===================================================================
--- Django-2.2.28.orig/tests/template_tests/filter_tests/test_wordwrap.py
+++ Django-2.2.28/tests/template_tests/filter_tests/test_wordwrap.py
@@ -51,3 +51,14 @@ class FunctionTests(SimpleTestCase):
), 14),
'this is a long\nparagraph of\ntext that\nreally needs\nto be wrapped\nI\'m afraid',
)
+
+ def test_wrap_long_text(self):
+ long_text = (
+ "this is a long paragraph of text that really needs"
+ " to be wrapped I'm afraid " * 20_000
+ )
+ self.assertIn(
+ "this is a\nlong\nparagraph\nof text\nthat\nreally\nneeds to\nbe wrapped\n"
+ "I'm afraid",
+ wordwrap(long_text, 10),
+ )