Skip to content

Commit 39fefe3

Browse files
[AUTO-CHERRYPICK] python-jinja2 : addresss CVE-2024-22195, CVE-2024-34064, CVE-2024-56201, CVE-2024-56326 - branch 3.0-dev (#11868)
Co-authored-by: Kanishk Bansal <103916909+Kanishk-Bansal@users.noreply.github.com>
1 parent d8f78a8 commit 39fefe3

7 files changed

Lines changed: 312 additions & 4 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
From 3bfb481d9e1c469387ecc7717c4c5cba411d8c79 Mon Sep 17 00:00:00 2001
2+
From: Kanishk-Bansal <kbkanishk975@gmail.com>
3+
Date: Thu, 2 Jan 2025 06:52:42 +0000
4+
Subject: [PATCH] Fix CVE-2024-22195
5+
6+
---
7+
src/jinja2/filters.py | 28 +++++++++++++++++++++-------
8+
tests/test_filters.py | 6 ++++++
9+
2 files changed, 27 insertions(+), 7 deletions(-)
10+
11+
diff --git a/src/jinja2/filters.py b/src/jinja2/filters.py
12+
index ed07c4c..c7ecc9b 100644
13+
--- a/src/jinja2/filters.py
14+
+++ b/src/jinja2/filters.py
15+
@@ -248,13 +248,17 @@ def do_items(value: t.Union[t.Mapping[K, V], Undefined]) -> t.Iterator[t.Tuple[K
16+
yield from value.items()
17+
18+
19+
+_space_re = re.compile(r"\s", flags=re.ASCII)
20+
+
21+
+
22+
@pass_eval_context
23+
def do_xmlattr(
24+
eval_ctx: "EvalContext", d: t.Mapping[str, t.Any], autospace: bool = True
25+
) -> str:
26+
"""Create an SGML/XML attribute string based on the items in a dict.
27+
- All values that are neither `none` nor `undefined` are automatically
28+
- escaped:
29+
+
30+
+ If any key contains a space, this fails with a ``ValueError``. Values that
31+
+ are neither ``none`` nor ``undefined`` are automatically escaped.
32+
33+
.. sourcecode:: html+jinja
34+
35+
@@ -273,12 +277,22 @@ def do_xmlattr(
36+
37+
As you can see it automatically prepends a space in front of the item
38+
if the filter returned something unless the second parameter is false.
39+
+
40+
+ .. versionchanged:: 3.1.3
41+
+ Keys with spaces are not allowed.
42+
"""
43+
- rv = " ".join(
44+
- f'{escape(key)}="{escape(value)}"'
45+
- for key, value in d.items()
46+
- if value is not None and not isinstance(value, Undefined)
47+
- )
48+
+ items = []
49+
+
50+
+ for key, value in d.items():
51+
+ if value is None or isinstance(value, Undefined):
52+
+ continue
53+
+
54+
+ if _space_re.search(key) is not None:
55+
+ raise ValueError(f"Spaces are not allowed in attributes: '{key}'")
56+
+
57+
+ items.append(f'{escape(key)}="{escape(value)}"')
58+
+
59+
+ rv = " ".join(items)
60+
61+
if autospace and rv:
62+
rv = " " + rv
63+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
From 71ac6eb2bb8ff62f7b70ca21b1d33fc59f01396d Mon Sep 17 00:00:00 2001
2+
From: Kanishk-Bansal <kbkanishk975@gmail.com>
3+
Date: Thu, 2 Jan 2025 13:53:49 +0000
4+
Subject: [PATCH] Fix CVE-2024-34064
5+
6+
---
7+
src/jinja2/filters.py | 22 +++++++++++++++++-----
8+
1 file changed, 17 insertions(+), 5 deletions(-)
9+
10+
diff --git a/src/jinja2/filters.py b/src/jinja2/filters.py
11+
index c7ecc9b..bdf6f22 100644
12+
--- a/src/jinja2/filters.py
13+
+++ b/src/jinja2/filters.py
14+
@@ -248,7 +248,9 @@ def do_items(value: t.Union[t.Mapping[K, V], Undefined]) -> t.Iterator[t.Tuple[K
15+
yield from value.items()
16+
17+
18+
-_space_re = re.compile(r"\s", flags=re.ASCII)
19+
+# Check for characters that would move the parser state from key to value.
20+
+# https://html.spec.whatwg.org/#attribute-name-state
21+
+_attr_key_re = re.compile(r"[\s/>=]", flags=re.ASCII)
22+
23+
24+
@pass_eval_context
25+
@@ -257,8 +259,14 @@ def do_xmlattr(
26+
) -> str:
27+
"""Create an SGML/XML attribute string based on the items in a dict.
28+
29+
- If any key contains a space, this fails with a ``ValueError``. Values that
30+
- are neither ``none`` nor ``undefined`` are automatically escaped.
31+
+ **Values** that are neither ``none`` nor ``undefined`` are automatically
32+
+ escaped, safely allowing untrusted user input.
33+
+
34+
+ User input should not be used as **keys** to this filter. If any key
35+
+ contains a space, ``/`` solidus, ``>`` greater-than sign, or ``=`` equals
36+
+ sign, this fails with a ``ValueError``. Regardless of this, user input
37+
+ should never be used as keys to this filter, or must be separately validated
38+
+ first.
39+
40+
.. sourcecode:: html+jinja
41+
42+
@@ -278,6 +286,10 @@ def do_xmlattr(
43+
As you can see it automatically prepends a space in front of the item
44+
if the filter returned something unless the second parameter is false.
45+
46+
+ .. versionchanged:: 3.1.4
47+
+ Keys with ``/`` solidus, ``>`` greater-than sign, or ``=`` equals sign
48+
+ are not allowed.
49+
+
50+
.. versionchanged:: 3.1.3
51+
Keys with spaces are not allowed.
52+
"""
53+
@@ -287,8 +299,8 @@ def do_xmlattr(
54+
if value is None or isinstance(value, Undefined):
55+
continue
56+
57+
- if _space_re.search(key) is not None:
58+
- raise ValueError(f"Spaces are not allowed in attributes: '{key}'")
59+
+ if _attr_key_re.search(key) is not None:
60+
+ raise ValueError(f"Invalid character in attribute name: {key!r}")
61+
62+
items.append(f'{escape(key)}="{escape(value)}"')
63+
64+
--
65+
2.45.2
66+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
From da58d627323696d5b0167cc9f1cc9e1070b5e18b Mon Sep 17 00:00:00 2001
2+
From: Kanishk-Bansal <kbkanishk975@gmail.com>
3+
Date: Thu, 2 Jan 2025 14:24:25 +0000
4+
Subject: [PATCH] fix CVE-2024-56201
5+
6+
---
7+
src/jinja2/compiler.py | 7 ++++++-
8+
1 file changed, 6 insertions(+), 1 deletion(-)
9+
10+
diff --git a/src/jinja2/compiler.py b/src/jinja2/compiler.py
11+
index 3458095..27d86c3 100644
12+
--- a/src/jinja2/compiler.py
13+
+++ b/src/jinja2/compiler.py
14+
@@ -1122,9 +1122,14 @@ class CodeGenerator(NodeVisitor):
15+
)
16+
self.writeline(f"if {frame.symbols.ref(alias)} is missing:")
17+
self.indent()
18+
+ # The position will contain the template name, and will be formatted
19+
+ # into a string that will be compiled into an f-string. Curly braces
20+
+ # in the name must be replaced with escapes so that they will not be
21+
+ # executed as part of the f-string.
22+
+ position = self.position(node).replace("{", "{{").replace("}", "}}")
23+
message = (
24+
"the template {included_template.__name__!r}"
25+
- f" (imported on {self.position(node)})"
26+
+ f" (imported on {position})"
27+
f" does not export the requested name {name!r}"
28+
)
29+
self.writeline(
30+
--
31+
2.45.2
32+
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
From 241312a0fb234989db874a723dbb2f05907796d3 Mon Sep 17 00:00:00 2001
2+
From: Kanishk-Bansal <kbkanishk975@gmail.com>
3+
Date: Thu, 2 Jan 2025 14:40:27 +0000
4+
Subject: [PATCH] Fix CVE-2024-56326
5+
6+
---
7+
src/jinja2/sandbox.py | 75 +++++++++++++++++++++----------------------
8+
1 file changed, 37 insertions(+), 38 deletions(-)
9+
10+
diff --git a/src/jinja2/sandbox.py b/src/jinja2/sandbox.py
11+
index 06d7414..c3f4a33 100644
12+
--- a/src/jinja2/sandbox.py
13+
+++ b/src/jinja2/sandbox.py
14+
@@ -7,6 +7,7 @@ import typing as t
15+
from _string import formatter_field_name_split # type: ignore
16+
from collections import abc
17+
from collections import deque
18+
+from functools import update_wrapper
19+
from string import Formatter
20+
21+
from markupsafe import EscapeFormatter
22+
@@ -80,20 +81,6 @@ _mutable_spec: t.Tuple[t.Tuple[t.Type, t.FrozenSet[str]], ...] = (
23+
)
24+
25+
26+
-def inspect_format_method(callable: t.Callable) -> t.Optional[str]:
27+
- if not isinstance(
28+
- callable, (types.MethodType, types.BuiltinMethodType)
29+
- ) or callable.__name__ not in ("format", "format_map"):
30+
- return None
31+
-
32+
- obj = callable.__self__
33+
-
34+
- if isinstance(obj, str):
35+
- return obj
36+
-
37+
- return None
38+
-
39+
-
40+
def safe_range(*args: int) -> range:
41+
"""A range that can't generate ranges with a length of more than
42+
MAX_RANGE items.
43+
@@ -313,6 +300,9 @@ class SandboxedEnvironment(Environment):
44+
except AttributeError:
45+
pass
46+
else:
47+
+ fmt = self.wrap_str_format(value)
48+
+ if fmt is not None:
49+
+ return fmt
50+
if self.is_safe_attribute(obj, argument, value):
51+
return value
52+
return self.unsafe_undefined(obj, argument)
53+
@@ -330,6 +320,9 @@ class SandboxedEnvironment(Environment):
54+
except (TypeError, LookupError):
55+
pass
56+
else:
57+
+ fmt = self.wrap_str_format(value)
58+
+ if fmt is not None:
59+
+ return fmt
60+
if self.is_safe_attribute(obj, attribute, value):
61+
return value
62+
return self.unsafe_undefined(obj, attribute)
63+
@@ -345,34 +338,43 @@ class SandboxedEnvironment(Environment):
64+
exc=SecurityError,
65+
)
66+
67+
- def format_string(
68+
- self,
69+
- s: str,
70+
- args: t.Tuple[t.Any, ...],
71+
- kwargs: t.Dict[str, t.Any],
72+
- format_func: t.Optional[t.Callable] = None,
73+
- ) -> str:
74+
- """If a format call is detected, then this is routed through this
75+
- method so that our safety sandbox can be used for it.
76+
+ def wrap_str_format(self, value: t.Any) -> t.Optional[t.Callable[..., str]]:
77+
+ """If the given value is a ``str.format`` or ``str.format_map`` method,
78+
+ return a new function than handles sandboxing. This is done at access
79+
+ rather than in :meth:`call`, so that calls made without ``call`` are
80+
+ also sandboxed.
81+
"""
82+
+ if not isinstance(
83+
+ value, (types.MethodType, types.BuiltinMethodType)
84+
+ ) or value.__name__ not in ("format", "format_map"):
85+
+ return None
86+
+ f_self: t.Any = value.__self__
87+
+ if not isinstance(f_self, str):
88+
+ return None
89+
+ str_type: t.Type[str] = type(f_self)
90+
+ is_format_map = value.__name__ == "format_map"
91+
formatter: SandboxedFormatter
92+
- if isinstance(s, Markup):
93+
- formatter = SandboxedEscapeFormatter(self, escape=s.escape)
94+
+
95+
+ if isinstance(f_self, Markup):
96+
+ formatter = SandboxedEscapeFormatter(self, escape=f_self.escape)
97+
else:
98+
formatter = SandboxedFormatter(self)
99+
100+
- if format_func is not None and format_func.__name__ == "format_map":
101+
- if len(args) != 1 or kwargs:
102+
- raise TypeError(
103+
- "format_map() takes exactly one argument"
104+
- f" {len(args) + (kwargs is not None)} given"
105+
- )
106+
+ vformat = formatter.vformat
107+
+ def wrapper(*args: t.Any, **kwargs: t.Any) -> str:
108+
+ if is_format_map:
109+
+ if kwargs:
110+
+ raise TypeError("format_map() takes no keyword arguments")
111+
+ if len(args) != 1:
112+
+ raise TypeError(
113+
+ f"format_map() takes exactly one argument ({len(args)} given)"
114+
+ )
115+
+ kwargs = args[0]
116+
+ args = ()
117+
118+
- kwargs = args[0]
119+
- args = ()
120+
+ return str_type(vformat(f_self, args, kwargs))
121+
122+
- rv = formatter.vformat(s, args, kwargs)
123+
- return type(s)(rv)
124+
+ return update_wrapper(wrapper, value)
125+
126+
def call(
127+
__self, # noqa: B902
128+
@@ -382,9 +384,6 @@ class SandboxedEnvironment(Environment):
129+
**kwargs: t.Any,
130+
) -> t.Any:
131+
"""Call an object from sandboxed code."""
132+
- fmt = inspect_format_method(__obj)
133+
- if fmt is not None:
134+
- return __self.format_string(fmt, args, kwargs, __obj)
135+
136+
# the double prefixes are to avoid double keyword argument
137+
# errors when proxying the call.
138+
--
139+
2.45.2
140+

SPECS/python-jinja2/python-jinja2.spec

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
Summary: A fast and easy to use template engine written in pure Python
22
Name: python-jinja2
33
Version: 3.1.2
4-
Release: 1%{?dist}
4+
Release: 2%{?dist}
55
License: BSD
66
Vendor: Microsoft Corporation
77
Distribution: Azure Linux
88
Group: Development/Languages/Python
99
URL: https://jinja.pocoo.org/
1010
Source0: https://files.pythonhosted.org/packages/source/j/jinja2/Jinja2-%{version}.tar.gz
11+
Patch0: CVE-2024-22195.patch
12+
Patch1: CVE-2024-34064.patch
13+
Patch2: CVE-2024-56201.patch
14+
Patch3: CVE-2024-56326.patch
1115
BuildArch: noarch
1216

1317
%description
@@ -33,7 +37,7 @@ inspired non-XML syntax but supports inline expressions and an optional
3337
sandboxed environment.
3438

3539
%prep
36-
%autosetup -n Jinja2-%{version}
40+
%autosetup -p1 -n Jinja2-%{version}
3741
sed -i 's/\r$//' LICENSE.rst # Fix wrong EOL encoding
3842

3943
%build
@@ -53,6 +57,9 @@ tox -e py%{python3_version_nodots}
5357
%{python3_sitelib}/Jinja2-%{version}-py%{python3_version}.egg-info
5458

5559
%changelog
60+
* Thu Jan 2 2025 Kanishk Bansal <kanbansal@microsoft.com> - 3.1.2-2
61+
- Address CVE-2024-22195, CVE-2024-34064, CVE-2024-56201, CVE-2024-56326 with an upstream patch.
62+
5663
* Mon Nov 27 2023 Andrew Phelps <anphel@microsoft.com> - 3.1.2-1
5764
- Upgrade to version 3.1.2
5865

toolkit/resources/manifests/package/toolchain_aarch64.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ python3-debuginfo-3.12.3-5.azl3.aarch64.rpm
538538
python3-devel-3.12.3-5.azl3.aarch64.rpm
539539
python3-flit-core-3.9.0-1.azl3.noarch.rpm
540540
python3-gpg-1.23.2-2.azl3.aarch64.rpm
541-
python3-jinja2-3.1.2-1.azl3.noarch.rpm
541+
python3-jinja2-3.1.2-2.azl3.noarch.rpm
542542
python3-libcap-ng-0.8.4-1.azl3.aarch64.rpm
543543
python3-libs-3.12.3-5.azl3.aarch64.rpm
544544
python3-libxml2-2.11.5-2.azl3.aarch64.rpm

toolkit/resources/manifests/package/toolchain_x86_64.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ python3-debuginfo-3.12.3-5.azl3.x86_64.rpm
546546
python3-devel-3.12.3-5.azl3.x86_64.rpm
547547
python3-flit-core-3.9.0-1.azl3.noarch.rpm
548548
python3-gpg-1.23.2-2.azl3.x86_64.rpm
549-
python3-jinja2-3.1.2-1.azl3.noarch.rpm
549+
python3-jinja2-3.1.2-2.azl3.noarch.rpm
550550
python3-libcap-ng-0.8.4-1.azl3.x86_64.rpm
551551
python3-libs-3.12.3-5.azl3.x86_64.rpm
552552
python3-libxml2-2.11.5-2.azl3.x86_64.rpm

0 commit comments

Comments
 (0)