summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-02-25 09:40:54 +0100
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-03-06 09:38:40 +0100
commit55d89e25f4115c5674cdd9b9bcba2bb2bb6d820b (patch)
tree525180b9fb582c8ad14e2c3c73c20c6075f15545 /django
parent9a729fb61add16d89a4b42b491aec2d22f1ae69a (diff)
Fixed CVE-2025-26699 -- Mitigated potential DoS in wordwrap template filter.
Thanks sw0rd1ight for the report.
Diffstat (limited to 'django')
-rw-r--r--django/utils/text.py28
1 files changed, 10 insertions, 18 deletions
diff --git a/django/utils/text.py b/django/utils/text.py
index bad8f2f2da..05b781b011 100644
--- a/django/utils/text.py
+++ b/django/utils/text.py
@@ -1,6 +1,7 @@
import gzip
import re
import secrets
+import textwrap
import unicodedata
from collections import deque
from gzip import GzipFile
@@ -49,24 +50,15 @@ def wrap(text, width):
``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)
def add_truncation_text(text, truncate=None):