Compare commits
11 Commits
3d43c1499c
...
ad740d5fb2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ad740d5fb2 | ||
|
|
976ca4187a | ||
|
|
211bb982ae | ||
|
|
077beb6857 | ||
|
|
f5a073ff6c | ||
|
|
dc5e5c2b95 | ||
|
|
d3b014d1e0 | ||
|
|
bf97ca7b4d | ||
|
|
6d3a072dad | ||
|
|
058d08c490 | ||
|
|
68db6bec83 |
105
0001-disallow-invalid-characters-in-keys-to-xmlattr-filte.patch
Normal file
105
0001-disallow-invalid-characters-in-keys-to-xmlattr-filte.patch
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
From d655030770081e2dfe46f90e27620472a502289d Mon Sep 17 00:00:00 2001
|
||||||
|
From: David Lord <davidism@gmail.com>
|
||||||
|
Date: Tue, 7 May 2024 10:19:28 +0800
|
||||||
|
Subject: [PATCH] disallow invalid characters in keys to xmlattr filter
|
||||||
|
|
||||||
|
---
|
||||||
|
Jinja2-3.1.3/CHANGES.rst | 6 ++++++
|
||||||
|
Jinja2-3.1.3/src/jinja2/filters.py | 21 ++++++++++++++++-----
|
||||||
|
Jinja2-3.1.3/tests/test_filters.py | 11 ++++++-----
|
||||||
|
3 files changed, 28 insertions(+), 10 deletions(-)
|
||||||
|
|
||||||
|
diff --git a/Jinja2-3.1.3/CHANGES.rst b/Jinja2-3.1.3/CHANGES.rst
|
||||||
|
index 08a1785..f70cacb 100644
|
||||||
|
--- a/Jinja2-3.1.3/CHANGES.rst
|
||||||
|
+++ b/Jinja2-3.1.3/CHANGES.rst
|
||||||
|
@@ -1,5 +1,11 @@
|
||||||
|
.. currentmodule:: jinja2
|
||||||
|
|
||||||
|
+- The ``xmlattr`` filter does not allow keys with ``/`` solidus, ``>``
|
||||||
|
+ greater-than sign, or ``=`` equals sign, in addition to disallowing spaces.
|
||||||
|
+ Regardless of any validation done by Jinja, user input should never be used
|
||||||
|
+ as keys to this filter, or must be separately validated first.
|
||||||
|
+ GHSA-h75v-3vvj-5mfj
|
||||||
|
+
|
||||||
|
Version 3.1.3
|
||||||
|
-------------
|
||||||
|
|
||||||
|
diff --git a/Jinja2-3.1.3/src/jinja2/filters.py b/Jinja2-3.1.3/src/jinja2/filters.py
|
||||||
|
index c7ecc9b..c73dd89 100644
|
||||||
|
--- a/Jinja2-3.1.3/src/jinja2/filters.py
|
||||||
|
+++ b/Jinja2-3.1.3/src/jinja2/filters.py
|
||||||
|
@@ -248,7 +248,9 @@ def do_items(value: t.Union[t.Mapping[K, V], Undefined]) -> t.Iterator[t.Tuple[K
|
||||||
|
yield from value.items()
|
||||||
|
|
||||||
|
|
||||||
|
-_space_re = re.compile(r"\s", flags=re.ASCII)
|
||||||
|
+# Check for characters that would move the parser state from key to value.
|
||||||
|
+# https://html.spec.whatwg.org/#attribute-name-state
|
||||||
|
+_attr_key_re = re.compile(r"[\s/>=]", flags=re.ASCII)
|
||||||
|
|
||||||
|
|
||||||
|
@pass_eval_context
|
||||||
|
@@ -257,8 +259,13 @@ def do_xmlattr(
|
||||||
|
) -> str:
|
||||||
|
"""Create an SGML/XML attribute string based on the items in a dict.
|
||||||
|
|
||||||
|
- If any key contains a space, this fails with a ``ValueError``. Values that
|
||||||
|
- are neither ``none`` nor ``undefined`` are automatically escaped.
|
||||||
|
+ **Values** that are neither ``none`` nor ``undefined`` are automatically
|
||||||
|
+ escaped, safely allowing untrusted user input.
|
||||||
|
+ User input should not be used as **keys** to this filter. If any key
|
||||||
|
+ contains a space, ``/`` solidus, ``>`` greater-than sign, or ``=`` equals
|
||||||
|
+ sign, this fails with a ``ValueError``. Regardless of this, user input
|
||||||
|
+ should never be used as keys to this filter, or must be separately validated
|
||||||
|
+ first.
|
||||||
|
|
||||||
|
.. sourcecode:: html+jinja
|
||||||
|
|
||||||
|
@@ -278,6 +285,10 @@ def do_xmlattr(
|
||||||
|
As you can see it automatically prepends a space in front of the item
|
||||||
|
if the filter returned something unless the second parameter is false.
|
||||||
|
|
||||||
|
+ .. versionchanged:: 3.1.4
|
||||||
|
+ Keys with ``/`` solidus, ``>`` greater-than sign, or ``=`` equals sign
|
||||||
|
+ are not allowed.
|
||||||
|
+
|
||||||
|
.. versionchanged:: 3.1.3
|
||||||
|
Keys with spaces are not allowed.
|
||||||
|
"""
|
||||||
|
@@ -287,8 +298,8 @@ def do_xmlattr(
|
||||||
|
if value is None or isinstance(value, Undefined):
|
||||||
|
continue
|
||||||
|
|
||||||
|
- if _space_re.search(key) is not None:
|
||||||
|
- raise ValueError(f"Spaces are not allowed in attributes: '{key}'")
|
||||||
|
+ if _attr_key_re.search(key) is not None:
|
||||||
|
+ raise ValueError(f"Invalid character in attribute name: {key!r}")
|
||||||
|
|
||||||
|
items.append(f'{escape(key)}="{escape(value)}"')
|
||||||
|
|
||||||
|
diff --git a/Jinja2-3.1.3/tests/test_filters.py b/Jinja2-3.1.3/tests/test_filters.py
|
||||||
|
index f50ed13..d8e9114 100644
|
||||||
|
--- a/Jinja2-3.1.3/tests/test_filters.py
|
||||||
|
+++ b/Jinja2-3.1.3/tests/test_filters.py
|
||||||
|
@@ -474,11 +474,12 @@ class TestFilter:
|
||||||
|
assert 'bar="23"' in out
|
||||||
|
assert 'blub:blub="<?>"' in out
|
||||||
|
|
||||||
|
- def test_xmlattr_key_with_spaces(self, env):
|
||||||
|
- with pytest.raises(ValueError, match="Spaces are not allowed"):
|
||||||
|
- env.from_string(
|
||||||
|
- "{{ {'src=1 onerror=alert(1)': 'my_class'}|xmlattr }}"
|
||||||
|
- ).render()
|
||||||
|
+ @pytest.mark.parametrize("sep", ("\t", "\n", "\f", " ", "/", ">", "="))
|
||||||
|
+ def test_xmlattr_key_invalid(self, env: Environment, sep: str) -> None:
|
||||||
|
+ with pytest.raises(ValueError, match="Invalid character"):
|
||||||
|
+ env.from_string("{{ {key: 'my_class'}|xmlattr }}").render(
|
||||||
|
+ key=f"class{sep}onclick=alert(1)"
|
||||||
|
+ )
|
||||||
|
|
||||||
|
def test_sort1(self, env):
|
||||||
|
tmpl = env.from_string("{{ [2, 3, 1]|sort }}|{{ [2, 3, 1]|sort(true) }}")
|
||||||
|
--
|
||||||
|
2.27.0
|
||||||
|
|
||||||
Binary file not shown.
BIN
Jinja2-3.1.3.tar.gz
Normal file
BIN
Jinja2-3.1.3.tar.gz
Normal file
Binary file not shown.
@ -1,85 +0,0 @@
|
|||||||
From 7dd3680e6eea0d77fde024763657aa4d884ddb23 Mon Sep 17 00:00:00 2001
|
|
||||||
From: Calum Hutton <calum.hutton@snyk.io>
|
|
||||||
Date: Thu, 26 Oct 2023 12:08:53 +0100
|
|
||||||
Subject: [PATCH] xmlattr filter disallows keys with spaces
|
|
||||||
|
|
||||||
Reference:https://github.com/pallets/jinja/commit/7dd3680e6eea0d77fde024763657aa4d884ddb23
|
|
||||||
Conflict:remove CHANGES.rst
|
|
||||||
|
|
||||||
---
|
|
||||||
Jinja2-3.1.2/src/jinja2/filters.py | 28 +++++++++++++++++++++-------
|
|
||||||
Jinja2-3.1.2/tests/test_filters.py | 6 ++++++
|
|
||||||
2 files changed, 27 insertions(+), 7 deletions(-)
|
|
||||||
|
|
||||||
diff --git a/Jinja2-3.1.2/src/jinja2/filters.py b/Jinja2-3.1.2/src/jinja2/filters.py
|
|
||||||
index ed07c4c..c7ecc9b 100644
|
|
||||||
--- a/Jinja2-3.1.2/src/jinja2/filters.py
|
|
||||||
+++ b/Jinja2-3.1.2/src/jinja2/filters.py
|
|
||||||
@@ -248,13 +248,17 @@ def do_items(value: t.Union[t.Mapping[K, V], Undefined]) -> t.Iterator[t.Tuple[K
|
|
||||||
yield from value.items()
|
|
||||||
|
|
||||||
|
|
||||||
+_space_re = re.compile(r"\s", flags=re.ASCII)
|
|
||||||
+
|
|
||||||
+
|
|
||||||
@pass_eval_context
|
|
||||||
def do_xmlattr(
|
|
||||||
eval_ctx: "EvalContext", d: t.Mapping[str, t.Any], autospace: bool = True
|
|
||||||
) -> str:
|
|
||||||
"""Create an SGML/XML attribute string based on the items in a dict.
|
|
||||||
- All values that are neither `none` nor `undefined` are automatically
|
|
||||||
- escaped:
|
|
||||||
+
|
|
||||||
+ If any key contains a space, this fails with a ``ValueError``. Values that
|
|
||||||
+ are neither ``none`` nor ``undefined`` are automatically escaped.
|
|
||||||
|
|
||||||
.. sourcecode:: html+jinja
|
|
||||||
|
|
||||||
@@ -273,12 +277,22 @@ def do_xmlattr(
|
|
||||||
|
|
||||||
As you can see it automatically prepends a space in front of the item
|
|
||||||
if the filter returned something unless the second parameter is false.
|
|
||||||
+
|
|
||||||
+ .. versionchanged:: 3.1.3
|
|
||||||
+ Keys with spaces are not allowed.
|
|
||||||
"""
|
|
||||||
- rv = " ".join(
|
|
||||||
- f'{escape(key)}="{escape(value)}"'
|
|
||||||
- for key, value in d.items()
|
|
||||||
- if value is not None and not isinstance(value, Undefined)
|
|
||||||
- )
|
|
||||||
+ items = []
|
|
||||||
+
|
|
||||||
+ for key, value in d.items():
|
|
||||||
+ if value is None or isinstance(value, Undefined):
|
|
||||||
+ continue
|
|
||||||
+
|
|
||||||
+ if _space_re.search(key) is not None:
|
|
||||||
+ raise ValueError(f"Spaces are not allowed in attributes: '{key}'")
|
|
||||||
+
|
|
||||||
+ items.append(f'{escape(key)}="{escape(value)}"')
|
|
||||||
+
|
|
||||||
+ rv = " ".join(items)
|
|
||||||
|
|
||||||
if autospace and rv:
|
|
||||||
rv = " " + rv
|
|
||||||
diff --git a/Jinja2-3.1.2/tests/test_filters.py b/Jinja2-3.1.2/tests/test_filters.py
|
|
||||||
index 73f0f0b..a184649 100644
|
|
||||||
--- a/Jinja2-3.1.2/tests/test_filters.py
|
|
||||||
+++ b/Jinja2-3.1.2/tests/test_filters.py
|
|
||||||
@@ -474,6 +474,12 @@ class TestFilter:
|
|
||||||
assert 'bar="23"' in out
|
|
||||||
assert 'blub:blub="<?>"' in out
|
|
||||||
|
|
||||||
+ def test_xmlattr_key_with_spaces(self, env):
|
|
||||||
+ with pytest.raises(ValueError, match="Spaces are not allowed"):
|
|
||||||
+ env.from_string(
|
|
||||||
+ "{{ {'src=1 onerror=alert(1)': 'my_class'}|xmlattr }}"
|
|
||||||
+ ).render()
|
|
||||||
+
|
|
||||||
def test_sort1(self, env):
|
|
||||||
tmpl = env.from_string("{{ [2, 3, 1]|sort }}|{{ [2, 3, 1]|sort(true) }}")
|
|
||||||
assert tmpl.render() == "[1, 2, 3]|[3, 2, 1]"
|
|
||||||
--
|
|
||||||
2.33.0
|
|
||||||
|
|
||||||
83
backport-CVE-2024-56201.patch
Normal file
83
backport-CVE-2024-56201.patch
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
From 56a724644b1ad9cb03745c10cca732715cdc79e9 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Sigurd Spieckermann <sigurd.spieckermann@gmail.com>
|
||||||
|
Date: Fri, 26 May 2023 14:32:36 +0200
|
||||||
|
Subject: [PATCH] fix f-string syntax error in code generation
|
||||||
|
|
||||||
|
Reference:https://github.com/pallets/jinja/commit/56a724644b1ad9cb03745c10cca732715cdc79e9
|
||||||
|
|
||||||
|
---
|
||||||
|
Jinja2-3.1.3/CHANGES.rst | 3 +++
|
||||||
|
Jinja2-3.1.3/src/jinja2/compiler.py | 7 ++++++-
|
||||||
|
Jinja2-3.1.3/tests/test_compile.py | 19 +++++++++++++++++++
|
||||||
|
3 files changed, 28 insertions(+), 1 deletion(-)
|
||||||
|
|
||||||
|
diff --git a/Jinja2-3.1.3/CHANGES.rst b/Jinja2-3.1.3/CHANGES.rst
|
||||||
|
index f70cacb..b0e9a77 100644
|
||||||
|
--- a/Jinja2-3.1.3/CHANGES.rst
|
||||||
|
+++ b/Jinja2-3.1.3/CHANGES.rst
|
||||||
|
@@ -1,5 +1,8 @@
|
||||||
|
.. currentmodule:: jinja2
|
||||||
|
|
||||||
|
+- Escape template name before formatting it into error messages, to avoid
|
||||||
|
+ issues with names that contain f-string syntax.
|
||||||
|
+ :issue:`1792`, :ghsa:`gmj6-6f8f-6699`
|
||||||
|
- The ``xmlattr`` filter does not allow keys with ``/`` solidus, ``>``
|
||||||
|
greater-than sign, or ``=`` equals sign, in addition to disallowing spaces.
|
||||||
|
Regardless of any validation done by Jinja, user input should never be used
|
||||||
|
diff --git a/Jinja2-3.1.3/src/jinja2/compiler.py b/Jinja2-3.1.3/src/jinja2/compiler.py
|
||||||
|
index ff95c80..1ebdcd9 100644
|
||||||
|
--- a/Jinja2-3.1.3/src/jinja2/compiler.py
|
||||||
|
+++ b/Jinja2-3.1.3/src/jinja2/compiler.py
|
||||||
|
@@ -1121,9 +1121,14 @@ class CodeGenerator(NodeVisitor):
|
||||||
|
)
|
||||||
|
self.writeline(f"if {frame.symbols.ref(alias)} is missing:")
|
||||||
|
self.indent()
|
||||||
|
+ # The position will contain the template name, and will be formatted
|
||||||
|
+ # into a string that will be compiled into an f-string. Curly braces
|
||||||
|
+ # in the name must be replaced with escapes so that they will not be
|
||||||
|
+ # executed as part of the f-string.
|
||||||
|
+ position = self.position(node).replace("{", "{{").replace("}", "}}")
|
||||||
|
message = (
|
||||||
|
"the template {included_template.__name__!r}"
|
||||||
|
- f" (imported on {self.position(node)})"
|
||||||
|
+ f" (imported on {position})"
|
||||||
|
f" does not export the requested name {name!r}"
|
||||||
|
)
|
||||||
|
self.writeline(
|
||||||
|
diff --git a/Jinja2-3.1.3/tests/test_compile.py b/Jinja2-3.1.3/tests/test_compile.py
|
||||||
|
index 42a773f..b33a877 100644
|
||||||
|
--- a/Jinja2-3.1.3/tests/test_compile.py
|
||||||
|
+++ b/Jinja2-3.1.3/tests/test_compile.py
|
||||||
|
@@ -1,6 +1,9 @@
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
|
+import pytest
|
||||||
|
+
|
||||||
|
+from jinja2 import UndefinedError
|
||||||
|
from jinja2.environment import Environment
|
||||||
|
from jinja2.loaders import DictLoader
|
||||||
|
|
||||||
|
@@ -26,3 +29,19 @@ def test_import_as_with_context_deterministic(tmp_path):
|
||||||
|
expect = [f"'bar{i}': " for i in range(10)]
|
||||||
|
found = re.findall(r"'bar\d': ", content)[:10]
|
||||||
|
assert found == expect
|
||||||
|
+
|
||||||
|
+
|
||||||
|
+def test_undefined_import_curly_name():
|
||||||
|
+ env = Environment(
|
||||||
|
+ loader=DictLoader(
|
||||||
|
+ {
|
||||||
|
+ "{bad}": "{% from 'macro' import m %}{{ m() }}",
|
||||||
|
+ "macro": "",
|
||||||
|
+ }
|
||||||
|
+ )
|
||||||
|
+ )
|
||||||
|
+
|
||||||
|
+ # Must not raise `NameError: 'bad' is not defined`, as that would indicate
|
||||||
|
+ # that `{bad}` is being interpreted as an f-string. It must be escaped.
|
||||||
|
+ with pytest.raises(UndefinedError):
|
||||||
|
+ env.get_template("{bad}").render()
|
||||||
|
--
|
||||||
|
2.33.0
|
||||||
|
|
||||||
187
backport-CVE-2024-56326.patch
Normal file
187
backport-CVE-2024-56326.patch
Normal file
@ -0,0 +1,187 @@
|
|||||||
|
From 91a972f5808973cd441f4dc06873b2f8378f30c7 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Lydxn <hlyndon20@gmail.com>
|
||||||
|
Date: Mon, 23 Sep 2024 15:09:10 -0700
|
||||||
|
Subject: [PATCH] sandbox indirect calls to str.format
|
||||||
|
---
|
||||||
|
Jinja2-3.1.3/CHANGES.rst | 3 ++
|
||||||
|
Jinja2-3.1.3/src/jinja2/sandbox.py | 81 +++++++++++++++--------------
|
||||||
|
Jinja2-3.1.3/tests/test_security.py | 18 +++++++
|
||||||
|
3 files changed, 64 insertions(+), 38 deletions(-)
|
||||||
|
|
||||||
|
diff --git a/Jinja2-3.1.3/CHANGES.rst b/Jinja2-3.1.3/CHANGES.rst
|
||||||
|
index f70cacb..e043649 100644
|
||||||
|
--- a/Jinja2-3.1.3/CHANGES.rst
|
||||||
|
+++ b/Jinja2-3.1.3/CHANGES.rst
|
||||||
|
@@ -16,6 +16,9 @@ Released 2024-01-10
|
||||||
|
- ``xmlattr`` filter does not allow keys with spaces. GHSA-h5c8-rqwp-cp95
|
||||||
|
- Make error messages stemming from invalid nesting of ``{% trans %}`` blocks
|
||||||
|
more helpful. :pr:`1916`
|
||||||
|
+- The sandboxed environment handles indirect calls to ``str.format``, such as
|
||||||
|
+ by passing a stored reference to a filter that calls its argument.
|
||||||
|
+ :ghsa:`q2x7-8rv6-6q7h`
|
||||||
|
|
||||||
|
|
||||||
|
Version 3.1.2
|
||||||
|
diff --git a/Jinja2-3.1.3/src/jinja2/sandbox.py b/Jinja2-3.1.3/src/jinja2/sandbox.py
|
||||||
|
index 06d7414..dae5a48 100644
|
||||||
|
--- a/Jinja2-3.1.3/src/jinja2/sandbox.py
|
||||||
|
+++ b/Jinja2-3.1.3/src/jinja2/sandbox.py
|
||||||
|
@@ -7,6 +7,7 @@ import typing as t
|
||||||
|
from _string import formatter_field_name_split # type: ignore
|
||||||
|
from collections import abc
|
||||||
|
from collections import deque
|
||||||
|
+from functools import update_wrapper
|
||||||
|
from string import Formatter
|
||||||
|
|
||||||
|
from markupsafe import EscapeFormatter
|
||||||
|
@@ -80,20 +81,6 @@ _mutable_spec: t.Tuple[t.Tuple[t.Type, t.FrozenSet[str]], ...] = (
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
-def inspect_format_method(callable: t.Callable) -> t.Optional[str]:
|
||||||
|
- if not isinstance(
|
||||||
|
- callable, (types.MethodType, types.BuiltinMethodType)
|
||||||
|
- ) or callable.__name__ not in ("format", "format_map"):
|
||||||
|
- return None
|
||||||
|
-
|
||||||
|
- obj = callable.__self__
|
||||||
|
-
|
||||||
|
- if isinstance(obj, str):
|
||||||
|
- return obj
|
||||||
|
-
|
||||||
|
- return None
|
||||||
|
-
|
||||||
|
-
|
||||||
|
def safe_range(*args: int) -> range:
|
||||||
|
"""A range that can't generate ranges with a length of more than
|
||||||
|
MAX_RANGE items.
|
||||||
|
@@ -313,6 +300,9 @@ class SandboxedEnvironment(Environment):
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
+ fmt = self.wrap_str_format(value)
|
||||||
|
+ if fmt is not None:
|
||||||
|
+ return fmt
|
||||||
|
if self.is_safe_attribute(obj, argument, value):
|
||||||
|
return value
|
||||||
|
return self.unsafe_undefined(obj, argument)
|
||||||
|
@@ -330,6 +320,9 @@ class SandboxedEnvironment(Environment):
|
||||||
|
except (TypeError, LookupError):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
+ fmt = self.wrap_str_format(value)
|
||||||
|
+ if fmt is not None:
|
||||||
|
+ return fmt
|
||||||
|
if self.is_safe_attribute(obj, attribute, value):
|
||||||
|
return value
|
||||||
|
return self.unsafe_undefined(obj, attribute)
|
||||||
|
@@ -345,34 +338,49 @@ class SandboxedEnvironment(Environment):
|
||||||
|
exc=SecurityError,
|
||||||
|
)
|
||||||
|
|
||||||
|
- def format_string(
|
||||||
|
- self,
|
||||||
|
- s: str,
|
||||||
|
- args: t.Tuple[t.Any, ...],
|
||||||
|
- kwargs: t.Dict[str, t.Any],
|
||||||
|
- format_func: t.Optional[t.Callable] = None,
|
||||||
|
- ) -> str:
|
||||||
|
- """If a format call is detected, then this is routed through this
|
||||||
|
- method so that our safety sandbox can be used for it.
|
||||||
|
+ def wrap_str_format(self, value: t.Any) -> t.Optional[t.Callable[..., str]]:
|
||||||
|
+ """If the given value is a ``str.format`` or ``str.format_map`` method,
|
||||||
|
+ return a new function than handles sandboxing. This is done at access
|
||||||
|
+ rather than in :meth:`call`, so that calls made without ``call`` are
|
||||||
|
+ also sandboxed.
|
||||||
|
"""
|
||||||
|
+ if not isinstance(
|
||||||
|
+ value, (types.MethodType, types.BuiltinMethodType)
|
||||||
|
+ ) or value.__name__ not in ("format", "format_map"):
|
||||||
|
+ return None
|
||||||
|
+
|
||||||
|
+ f_self: t.Any = value.__self__
|
||||||
|
+
|
||||||
|
+ if not isinstance(f_self, str):
|
||||||
|
+ return None
|
||||||
|
+
|
||||||
|
+ str_type: t.Type[str] = type(f_self)
|
||||||
|
+ is_format_map = value.__name__ == "format_map"
|
||||||
|
formatter: SandboxedFormatter
|
||||||
|
- if isinstance(s, Markup):
|
||||||
|
- formatter = SandboxedEscapeFormatter(self, escape=s.escape)
|
||||||
|
+
|
||||||
|
+ if isinstance(f_self, Markup):
|
||||||
|
+ formatter = SandboxedEscapeFormatter(self, escape=f_self.escape)
|
||||||
|
else:
|
||||||
|
formatter = SandboxedFormatter(self)
|
||||||
|
|
||||||
|
- if format_func is not None and format_func.__name__ == "format_map":
|
||||||
|
- if len(args) != 1 or kwargs:
|
||||||
|
- raise TypeError(
|
||||||
|
- "format_map() takes exactly one argument"
|
||||||
|
- f" {len(args) + (kwargs is not None)} given"
|
||||||
|
- )
|
||||||
|
+ vformat = formatter.vformat
|
||||||
|
+
|
||||||
|
+ def wrapper(*args: t.Any, **kwargs: t.Any) -> str:
|
||||||
|
+ if is_format_map:
|
||||||
|
+ if kwargs:
|
||||||
|
+ raise TypeError("format_map() takes no keyword arguments")
|
||||||
|
+
|
||||||
|
+ if len(args) != 1:
|
||||||
|
+ raise TypeError(
|
||||||
|
+ f"format_map() takes exactly one argument ({len(args)} given)"
|
||||||
|
+ )
|
||||||
|
+
|
||||||
|
+ kwargs = args[0]
|
||||||
|
+ args = ()
|
||||||
|
|
||||||
|
- kwargs = args[0]
|
||||||
|
- args = ()
|
||||||
|
+ return str_type(vformat(f_self, args, kwargs))
|
||||||
|
|
||||||
|
- rv = formatter.vformat(s, args, kwargs)
|
||||||
|
- return type(s)(rv)
|
||||||
|
+ return update_wrapper(wrapper, value)
|
||||||
|
|
||||||
|
def call(
|
||||||
|
__self, # noqa: B902
|
||||||
|
@@ -382,9 +390,6 @@ class SandboxedEnvironment(Environment):
|
||||||
|
**kwargs: t.Any,
|
||||||
|
) -> t.Any:
|
||||||
|
"""Call an object from sandboxed code."""
|
||||||
|
- fmt = inspect_format_method(__obj)
|
||||||
|
- if fmt is not None:
|
||||||
|
- return __self.format_string(fmt, args, kwargs, __obj)
|
||||||
|
|
||||||
|
# the double prefixes are to avoid double keyword argument
|
||||||
|
# errors when proxying the call.
|
||||||
|
diff --git a/Jinja2-3.1.3/tests/test_security.py b/Jinja2-3.1.3/tests/test_security.py
|
||||||
|
index 0e8dc5c..9c8bad6 100644
|
||||||
|
--- a/Jinja2-3.1.3/tests/test_security.py
|
||||||
|
+++ b/Jinja2-3.1.3/tests/test_security.py
|
||||||
|
@@ -171,3 +171,21 @@ class TestStringFormatMap:
|
||||||
|
'{{ ("a{x.foo}b{y}"|safe).format_map({"x":{"foo": 42}, "y":"<foo>"}) }}'
|
||||||
|
)
|
||||||
|
assert t.render() == "a42b<foo>"
|
||||||
|
+
|
||||||
|
+
|
||||||
|
+ def test_indirect_call(self):
|
||||||
|
+ def run(value, arg):
|
||||||
|
+ return value.run(arg)
|
||||||
|
+
|
||||||
|
+ env = SandboxedEnvironment()
|
||||||
|
+ env.filters["run"] = run
|
||||||
|
+ t = env.from_string(
|
||||||
|
+ """{% set
|
||||||
|
+ ns = namespace(run="{0.__call__.__builtins__[__import__]}".format)
|
||||||
|
+ %}
|
||||||
|
+ {{ ns | run(not_here) }}
|
||||||
|
+ """
|
||||||
|
+ )
|
||||||
|
+
|
||||||
|
+ with pytest.raises(SecurityError):
|
||||||
|
+ t.render()
|
||||||
|
--
|
||||||
|
2.43.0
|
||||||
|
|
||||||
103
backport-CVE-2025-27516.patch
Normal file
103
backport-CVE-2025-27516.patch
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
From b428dd9593a26e7191708572f294b24ddd1d156f Mon Sep 17 00:00:00 2001
|
||||||
|
From: maoyanping <maoyanping@xfusion.com>
|
||||||
|
Date: Fri, 18 Apr 2025 16:58:20 +0800
|
||||||
|
Subject: [PATCH] backport-CVE-2025-27516
|
||||||
|
|
||||||
|
---
|
||||||
|
Jinja2-3.1.3/CHANGES.rst | 3 +++
|
||||||
|
Jinja2-3.1.3/src/jinja2/filters.py | 35 +++++++++++++++--------------------
|
||||||
|
Jinja2-3.1.3/tests/test_security.py | 10 ++++++++++
|
||||||
|
3 files changed, 28 insertions(+), 20 deletions(-)
|
||||||
|
|
||||||
|
diff --git a/Jinja2-3.1.3/CHANGES.rst b/Jinja2-3.1.3/CHANGES.rst
|
||||||
|
index 2001e3c..af86746 100644
|
||||||
|
--- a/Jinja2-3.1.3/CHANGES.rst
|
||||||
|
+++ b/Jinja2-3.1.3/CHANGES.rst
|
||||||
|
@@ -1,5 +1,8 @@
|
||||||
|
.. currentmodule:: jinja2
|
||||||
|
|
||||||
|
+- The ``|attr`` filter does not bypass the environment's attribute lookup,
|
||||||
|
+ allowing the sandbox to apply its checks. :ghsa:`cpwx-vrp4-4pq7`
|
||||||
|
+
|
||||||
|
- Escape template name before formatting it into error messages, to avoid
|
||||||
|
issues with names that contain f-string syntax.
|
||||||
|
:issue:`1792`, :ghsa:`gmj6-6f8f-6699`
|
||||||
|
diff --git a/Jinja2-3.1.3/src/jinja2/filters.py b/Jinja2-3.1.3/src/jinja2/filters.py
|
||||||
|
index c73dd89..aacb30d 100644
|
||||||
|
--- a/Jinja2-3.1.3/src/jinja2/filters.py
|
||||||
|
+++ b/Jinja2-3.1.3/src/jinja2/filters.py
|
||||||
|
@@ -5,6 +5,7 @@ import re
|
||||||
|
import typing
|
||||||
|
import typing as t
|
||||||
|
from collections import abc
|
||||||
|
+from inspect import getattr_static
|
||||||
|
from itertools import chain
|
||||||
|
from itertools import groupby
|
||||||
|
|
||||||
|
@@ -1399,30 +1400,24 @@ def do_attr(
|
||||||
|
environment: "Environment", obj: t.Any, name: str
|
||||||
|
) -> t.Union[Undefined, t.Any]:
|
||||||
|
"""Get an attribute of an object. ``foo|attr("bar")`` works like
|
||||||
|
- ``foo.bar`` just that always an attribute is returned and items are not
|
||||||
|
- looked up.
|
||||||
|
+ ``foo.bar``, but returns undefined instead of falling back to ``foo["bar"]``
|
||||||
|
+ if the attribute doesn't exist.
|
||||||
|
|
||||||
|
See :ref:`Notes on subscriptions <notes-on-subscriptions>` for more details.
|
||||||
|
"""
|
||||||
|
+ # Environment.getattr will fall back to obj[name] if obj.name doesn't exist.
|
||||||
|
+ # But we want to call env.getattr to get behavior such as sandboxing.
|
||||||
|
+ # Determine if the attr exists first, so we know the fallback won't trigger.
|
||||||
|
try:
|
||||||
|
- name = str(name)
|
||||||
|
- except UnicodeError:
|
||||||
|
- pass
|
||||||
|
- else:
|
||||||
|
- try:
|
||||||
|
- value = getattr(obj, name)
|
||||||
|
- except AttributeError:
|
||||||
|
- pass
|
||||||
|
- else:
|
||||||
|
- if environment.sandboxed:
|
||||||
|
- environment = t.cast("SandboxedEnvironment", environment)
|
||||||
|
-
|
||||||
|
- if not environment.is_safe_attribute(obj, name, value):
|
||||||
|
- return environment.unsafe_undefined(obj, name)
|
||||||
|
-
|
||||||
|
- return value
|
||||||
|
-
|
||||||
|
- return environment.undefined(obj=obj, name=name)
|
||||||
|
+ # This avoids executing properties/descriptors, but misses __getattr__
|
||||||
|
+ # and __getattribute__ dynamic attrs.
|
||||||
|
+ getattr_static(obj, name)
|
||||||
|
+ except AttributeError:
|
||||||
|
+ # This finds dynamic attrs, and we know it's not a descriptor at this point.
|
||||||
|
+ if not hasattr(obj, name):
|
||||||
|
+ return environment.undefined(obj=obj, name=name)
|
||||||
|
+
|
||||||
|
+ return environment.getattr(obj, name)
|
||||||
|
|
||||||
|
|
||||||
|
@typing.overload
|
||||||
|
diff --git a/Jinja2-3.1.3/tests/test_security.py b/Jinja2-3.1.3/tests/test_security.py
|
||||||
|
index 9c8bad6..0a0fddf 100644
|
||||||
|
--- a/Jinja2-3.1.3/tests/test_security.py
|
||||||
|
+++ b/Jinja2-3.1.3/tests/test_security.py
|
||||||
|
@@ -189,3 +189,13 @@ class TestStringFormatMap:
|
||||||
|
|
||||||
|
with pytest.raises(SecurityError):
|
||||||
|
t.render()
|
||||||
|
+
|
||||||
|
+ def test_attr_filter(self) -> None:
|
||||||
|
+ env = SandboxedEnvironment()
|
||||||
|
+ t = env.from_string(
|
||||||
|
+ """{{ "{0.__call__.__builtins__[__import__]}"
|
||||||
|
+ | attr("format")(not_here) }}"""
|
||||||
|
+ )
|
||||||
|
+
|
||||||
|
+ with pytest.raises(SecurityError):
|
||||||
|
+ t.render()
|
||||||
|
\ No newline at end of file
|
||||||
|
--
|
||||||
|
2.33.0
|
||||||
|
|
||||||
@ -1,41 +0,0 @@
|
|||||||
From 3fadee01b712580d811bdd2cb06cd8a1b5ee0821 Mon Sep 17 00:00:00 2001
|
|
||||||
From: David Lord <davidism@gmail.com>
|
|
||||||
Date: Thu, 29 Dec 2022 10:39:00 -0800
|
|
||||||
Subject: [PATCH] update dependencies
|
|
||||||
|
|
||||||
---
|
|
||||||
Jinja2-3.1.2/tests/test_loader.py | 6 ++++--
|
|
||||||
1 file changed, 4 insertions(+), 2 deletions(-)
|
|
||||||
|
|
||||||
diff --git a/Jinja2-3.1.2/tests/test_loader.py b/Jinja2-3.1.2/tests/test_loader.py
|
|
||||||
index 04c921d..77d686e 100644
|
|
||||||
--- a/Jinja2-3.1.2/tests/test_loader.py
|
|
||||||
+++ b/Jinja2-3.1.2/tests/test_loader.py
|
|
||||||
@@ -183,6 +183,7 @@ class TestFileSystemLoader:
|
|
||||||
|
|
||||||
class TestModuleLoader:
|
|
||||||
archive = None
|
|
||||||
+ mod_env = None
|
|
||||||
|
|
||||||
def compile_down(self, prefix_loader, zip="deflated"):
|
|
||||||
log = []
|
|
||||||
@@ -196,13 +197,14 @@ class TestModuleLoader:
|
|
||||||
self.mod_env = Environment(loader=loaders.ModuleLoader(self.archive))
|
|
||||||
return "".join(log)
|
|
||||||
|
|
||||||
- def teardown(self):
|
|
||||||
- if hasattr(self, "mod_env"):
|
|
||||||
+ def teardown_method(self):
|
|
||||||
+ if self.archive is not None:
|
|
||||||
if os.path.isfile(self.archive):
|
|
||||||
os.remove(self.archive)
|
|
||||||
else:
|
|
||||||
shutil.rmtree(self.archive)
|
|
||||||
self.archive = None
|
|
||||||
+ self.mod_env = None
|
|
||||||
|
|
||||||
def test_log(self, prefix_loader):
|
|
||||||
log = self.compile_down(prefix_loader)
|
|
||||||
--
|
|
||||||
2.33.0
|
|
||||||
|
|
||||||
@ -1,18 +1,20 @@
|
|||||||
%global _name Jinja2
|
%global _name Jinja2
|
||||||
|
|
||||||
Name: python-jinja2
|
Name: python-jinja2
|
||||||
Version: 3.1.2
|
Version: 3.1.3
|
||||||
Release: 3
|
Release: 5
|
||||||
Summary: A full-featured template engine for Python
|
Summary: A full-featured template engine for Python
|
||||||
License: BSD-3-Clause
|
License: BSD-3-Clause
|
||||||
URL: http://jinja.pocoo.org/
|
URL: http://jinja.pocoo.org/
|
||||||
Source0: https://files.pythonhosted.org/packages/source/J/Jinja2/Jinja2-%{version}.tar.gz
|
Source0: https://files.pythonhosted.org/packages/source/J/Jinja2/Jinja2-%{version}.tar.gz
|
||||||
|
|
||||||
Patch1: backport-update-dependencies.patch
|
|
||||||
Patch2: backport-CVE-2024-22195.patch
|
|
||||||
|
|
||||||
BuildArch: noarch
|
BuildArch: noarch
|
||||||
|
|
||||||
|
Patch0001: 0001-disallow-invalid-characters-in-keys-to-xmlattr-filte.patch
|
||||||
|
Patch0002: backport-CVE-2024-56326.patch
|
||||||
|
Patch0003: backport-CVE-2024-56201.patch
|
||||||
|
Patch0004: backport-CVE-2025-27516.patch
|
||||||
|
|
||||||
%description
|
%description
|
||||||
Jinja2 is one of the most used template engines for Python. It is inspired by Django's
|
Jinja2 is one of the most used template engines for Python. It is inspired by Django's
|
||||||
templating system but extends it with an expressive language that gives template authors
|
templating system but extends it with an expressive language that gives template authors
|
||||||
@ -65,6 +67,33 @@ popd
|
|||||||
%doc Jinja2-%{version}/examples
|
%doc Jinja2-%{version}/examples
|
||||||
|
|
||||||
%changelog
|
%changelog
|
||||||
|
* Fri Apr 18 2025 maoyanping <maoyanping@xfusion.com> - 3.1.3-5
|
||||||
|
- Type: CVE
|
||||||
|
- CVE: CVE-2025-27516
|
||||||
|
- SUG: NA
|
||||||
|
- DESC: fix CVE-2025-27516
|
||||||
|
|
||||||
|
* Thu Dec 26 2024 weihaohao <weihaohao2@huawei.com> - 3.1.3-4
|
||||||
|
- Type: CVE
|
||||||
|
- CVE: CVE-2024-56201
|
||||||
|
- SUG: NA
|
||||||
|
- DESC: fix CVE-2024-56201
|
||||||
|
|
||||||
|
* Thu Dec 26 2024 changtao <changtao@kylinos.cn> - 3.1.3-3
|
||||||
|
- Type: CVE
|
||||||
|
- CVE: CVE-2024-56326
|
||||||
|
- SUG: NA
|
||||||
|
- DESC: fix CVE-2024-56326
|
||||||
|
|
||||||
|
* Tue May 7 2024 xuchenchen <xuchenchen@kylinos.cn> - 3.1.3-2
|
||||||
|
- Type: CVE
|
||||||
|
- CVE: CVE-2024-34064
|
||||||
|
- SUG: NA
|
||||||
|
- DESC: fix disallow invalid characters in keys to xmlattr filter
|
||||||
|
|
||||||
|
* Thu Jan 25 2024 shixuantong <shixuantong1@huawei.com> - 3.1.3-1
|
||||||
|
- Upgrade package to 3.1.3
|
||||||
|
|
||||||
* Thu Jan 18 2024 weihaohao <weihaohao2@huawei.com> - 3.1.2-3
|
* Thu Jan 18 2024 weihaohao <weihaohao2@huawei.com> - 3.1.2-3
|
||||||
- Type:CVE
|
- Type:CVE
|
||||||
- CVE:CVE-2024-22195
|
- CVE:CVE-2024-22195
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user