Skip to content

Commit 71a7a26

Browse files
[AUTO-CHERRYPICK] Patch python-jinja2 for CVE-2025-27516 [High] - branch 3.0-dev (#12966)
Co-authored-by: Kanishk Bansal <103916909+Kanishk-Bansal@users.noreply.github.com>
1 parent 2ac1c5c commit 71a7a26

5 files changed

Lines changed: 152 additions & 5 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
From b289cbd6a87485ecf81bb959f11f177c71a2e041 Mon Sep 17 00:00:00 2001
2+
From: Kanishk-Bansal <kbkanishk975@gmail.com>
3+
Date: Fri, 7 Mar 2025 14:03:44 +0000
4+
Subject: [PATCH] CVE-2025-27516
5+
6+
Upstream Reference : https://github.com/pallets/jinja/commit/90457bbf33b8662926ae65cdde4c4c32e756e403
7+
---
8+
src/jinja2/filters.py | 37 ++++++++++++++++---------------------
9+
1 file changed, 16 insertions(+), 21 deletions(-)
10+
11+
diff --git a/src/jinja2/filters.py b/src/jinja2/filters.py
12+
index ed07c4c..706d899 100644
13+
--- a/src/jinja2/filters.py
14+
+++ b/src/jinja2/filters.py
15+
@@ -5,6 +5,7 @@ import re
16+
import typing
17+
import typing as t
18+
from collections import abc
19+
+from inspect import getattr_static
20+
from itertools import chain
21+
from itertools import groupby
22+
23+
@@ -1373,31 +1374,25 @@ def do_reverse(value: t.Union[str, t.Iterable[V]]) -> t.Union[str, t.Iterable[V]
24+
def do_attr(
25+
environment: "Environment", obj: t.Any, name: str
26+
) -> t.Union[Undefined, t.Any]:
27+
- """Get an attribute of an object. ``foo|attr("bar")`` works like
28+
- ``foo.bar`` just that always an attribute is returned and items are not
29+
- looked up.
30+
+ """Get an attribute of an object. ``foo|attr("bar")`` works like
31+
+ ``foo.bar``, but returns undefined instead of falling back to ``foo["bar"]``
32+
+ if the attribute doesn't exist.
33+
34+
See :ref:`Notes on subscriptions <notes-on-subscriptions>` for more details.
35+
"""
36+
+ # Environment.getattr will fall back to obj[name] if obj.name doesn't exist.
37+
+ # But we want to call env.getattr to get behavior such as sandboxing.
38+
+ # Determine if the attr exists first, so we know the fallback won't trigger.
39+
try:
40+
- name = str(name)
41+
- except UnicodeError:
42+
- pass
43+
- else:
44+
- try:
45+
- value = getattr(obj, name)
46+
- except AttributeError:
47+
- pass
48+
- else:
49+
- if environment.sandboxed:
50+
- environment = t.cast("SandboxedEnvironment", environment)
51+
-
52+
- if not environment.is_safe_attribute(obj, name, value):
53+
- return environment.unsafe_undefined(obj, name)
54+
-
55+
- return value
56+
-
57+
- return environment.undefined(obj=obj, name=name)
58+
+ # This avoids executing properties/descriptors, but misses __getattr__
59+
+ # and __getattribute__ dynamic attrs.
60+
+ getattr_static(obj, name)
61+
+ except AttributeError:
62+
+ # This finds dynamic attrs, and we know it's not a descriptor at this point.
63+
+ if not hasattr(obj, name):
64+
+ return environment.undefined(obj=obj, name=name)
65+
+
66+
+ return environment.getattr(obj, name)
67+
68+
69+
@typing.overload
70+
--
71+
2.45.2
72+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
From 26b3a167236e300f05780c4b675ec6e2dba04e24 Mon Sep 17 00:00:00 2001
2+
From: Sam Meluch <sammeluch@microsoft.com>
3+
Date: Mon, 10 Mar 2025 15:28:30 -0700
4+
Subject: [PATCH] fix mariner test deps
5+
6+
---
7+
requirements/tests.txt | 12 +++++-------
8+
tests/test_loader.py | 6 ++++--
9+
2 files changed, 9 insertions(+), 11 deletions(-)
10+
11+
diff --git a/requirements/tests.txt b/requirements/tests.txt
12+
index 4cd3fe99..31c9bb06 100644
13+
--- a/requirements/tests.txt
14+
+++ b/requirements/tests.txt
15+
@@ -7,17 +7,15 @@
16+
#
17+
attrs==21.4.0
18+
# via pytest
19+
-iniconfig==1.1.1
20+
+exceptiongroup==1.1.1
21+
# via pytest
22+
-packaging==21.3
23+
+iniconfig==1.1.1
24+
# via pytest
25+
-pluggy==1.0.0
26+
+packaging==23.2
27+
# via pytest
28+
-py==1.11.0
29+
+pluggy==1.5.0
30+
# via pytest
31+
-pyparsing==3.0.8
32+
- # via packaging
33+
-pytest==7.1.2
34+
+pytest==7.4.0
35+
# via -r requirements/tests.in
36+
tomli==2.0.1
37+
# via pytest
38+
39+
diff --git a/tests/test_loader.py b/tests/test_loader.py
40+
index 04c921d24..77d686ef5 100644
41+
--- a/tests/test_loader.py
42+
+++ b/tests/test_loader.py
43+
@@ -183,6 +183,7 @@ def test_filename_normpath(self):
44+
45+
class TestModuleLoader:
46+
archive = None
47+
+ mod_env = None
48+
49+
def compile_down(self, prefix_loader, zip="deflated"):
50+
log = []
51+
@@ -196,13 +197,14 @@ def compile_down(self, prefix_loader, zip="deflated"):
52+
self.mod_env = Environment(loader=loaders.ModuleLoader(self.archive))
53+
return "".join(log)
54+
55+
- def teardown(self):
56+
- if hasattr(self, "mod_env"):
57+
+ def teardown_method(self):
58+
+ if self.archive is not None:
59+
if os.path.isfile(self.archive):
60+
os.remove(self.archive)
61+
else:
62+
shutil.rmtree(self.archive)
63+
self.archive = None
64+
+ self.mod_env = None
65+
66+
def test_log(self, prefix_loader):
67+
log = self.compile_down(prefix_loader)
68+
--
69+
2.34.1

SPECS/python-jinja2/python-jinja2.spec

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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: 2%{?dist}
4+
Release: 3%{?dist}
55
License: BSD
66
Vendor: Microsoft Corporation
77
Distribution: Azure Linux
@@ -12,6 +12,8 @@ Patch0: CVE-2024-22195.patch
1212
Patch1: CVE-2024-34064.patch
1313
Patch2: CVE-2024-56201.patch
1414
Patch3: CVE-2024-56326.patch
15+
Patch4: CVE-2025-27516.patch
16+
Patch5: python-jinja2-testing-deps.patch
1517
BuildArch: noarch
1618

1719
%description
@@ -25,6 +27,7 @@ BuildRequires: python3-setuptools
2527
BuildRequires: python3-xml
2628
%if 0%{?with_check}
2729
BuildRequires: python3-pip
30+
BuildRequires: python3-pytest
2831
%endif
2932
Requires: python3
3033
Requires: python3-markupsafe
@@ -47,8 +50,8 @@ sed -i 's/\r$//' LICENSE.rst # Fix wrong EOL encoding
4750
%py3_install
4851

4952
%check
50-
pip3 install tox
51-
tox -e py%{python3_version_nodots}
53+
pip3 install tox packaging==23.2
54+
tox -v -e py%{python3_version_nodots} --
5255

5356
%files -n python3-jinja2
5457
%defattr(-,root,root)
@@ -57,6 +60,9 @@ tox -e py%{python3_version_nodots}
5760
%{python3_sitelib}/Jinja2-%{version}-py%{python3_version}.egg-info
5861

5962
%changelog
63+
* Fri Mar 07 2025 Kanishk Bansal <kanbansal@microsoft.com> - 3.1.2-3
64+
- Address CVE-2025-27516 with an upstream patch and fix the ptest
65+
6066
* Thu Jan 2 2025 Kanishk Bansal <kanbansal@microsoft.com> - 3.1.2-2
6167
- Address CVE-2024-22195, CVE-2024-34064, CVE-2024-56201, CVE-2024-56326 with an upstream patch.
6268

toolkit/resources/manifests/package/toolchain_aarch64.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ python3-debuginfo-3.12.9-1.azl3.aarch64.rpm
540540
python3-devel-3.12.9-1.azl3.aarch64.rpm
541541
python3-flit-core-3.9.0-1.azl3.noarch.rpm
542542
python3-gpg-1.23.2-2.azl3.aarch64.rpm
543-
python3-jinja2-3.1.2-2.azl3.noarch.rpm
543+
python3-jinja2-3.1.2-3.azl3.noarch.rpm
544544
python3-libcap-ng-0.8.4-1.azl3.aarch64.rpm
545545
python3-libs-3.12.9-1.azl3.aarch64.rpm
546546
python3-libxml2-2.11.5-4.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
@@ -548,7 +548,7 @@ python3-debuginfo-3.12.9-1.azl3.x86_64.rpm
548548
python3-devel-3.12.9-1.azl3.x86_64.rpm
549549
python3-flit-core-3.9.0-1.azl3.noarch.rpm
550550
python3-gpg-1.23.2-2.azl3.x86_64.rpm
551-
python3-jinja2-3.1.2-2.azl3.noarch.rpm
551+
python3-jinja2-3.1.2-3.azl3.noarch.rpm
552552
python3-libcap-ng-0.8.4-1.azl3.x86_64.rpm
553553
python3-libs-3.12.9-1.azl3.x86_64.rpm
554554
python3-libxml2-2.11.5-4.azl3.x86_64.rpm

0 commit comments

Comments
 (0)