Skip to content
Myr

Merge commit from fork

Before adoption: this commit came in with the history the project adopted, so no landing records it.

attr filter uses env.getattr

authored by David Lord · committed by GitHub · parents 033c200, 065334d · Browse files · 3 files+29 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+29 added−20 removed

  • tests:+10 added−0 removed
  • other:+19 added−20 removed

3 files+29 added−20 removed

modified:src/jinja2/filters.py+15 added-20 removed
4 unmodified lines
5import re6import typing7import typing as t8from collections import abc9from inspect import getattr_static10from itertools import chain11from itertools import groupby1213from markupsafe import escape
1397 unmodified lines
1411@pass_environment1412def do_attr(1413 environment: "Environment", obj: t.Any, name: str1414) -> t.Union[Undefined, t.Any]:1414 """Get an attribute of an object. ``foo|attr("bar")`` works like1415 ``foo.bar`` just that always an attribute is returned and items are not1416 looked up.1415 """Get an attribute of an object. ``foo|attr("bar")`` works like1416 ``foo.bar``, but returns undefined instead of falling back to ``foo["bar"]``1417 if the attribute doesn't exist.14181419 See :ref:`Notes on subscriptions <notes-on-subscriptions>` for more details.1420 """1421 # Environment.getattr will fall back to obj[name] if obj.name doesn't exist.1422 # But we want to call env.getattr to get behavior such as sandboxing.1423 # Determine if the attr exists first, so we know the fallback won't trigger.1424 try:1421 name = str(name)1422 except UnicodeError:1423 pass1424 else:1425 try:1426 value = getattr(obj, name)1427 except AttributeError:1428 pass1429 else:1430 if environment.sandboxed:1431 environment = t.cast("SandboxedEnvironment", environment)1425 # This avoids executing properties/descriptors, but misses __getattr__1426 # and __getattribute__ dynamic attrs.1427 getattr_static(obj, name)1428 except AttributeError:1429 # This finds dynamic attrs, and we know it's not a descriptor at this point.1430 if not hasattr(obj, name):1431 return environment.undefined(obj=obj, name=name)14321433 if not environment.is_safe_attribute(obj, name, value):1434 return environment.unsafe_undefined(obj, name)1433 return environment.getattr(obj, name)14341436 return value14351438 return environment.undefined(obj=obj, name=name)143914401436@typing.overload1437def sync_do_map(1438 context: "Context",1439 value: t.Iterable[t.Any],
434 unmodified lines
modified:tests/test_security.py+10 added
188 unmodified lines
189 )190191 with pytest.raises(SecurityError):192 t.render()193194 def test_attr_filter(self) -> None:195 env = SandboxedEnvironment()196 t = env.from_string(197 """{{ "{0.__call__.__builtins__[__import__]}"198 | attr("format")(not_here) }}"""199 )200201 with pytest.raises(SecurityError):202 t.render()
modified:CHANGES.rst+4 added
3 unmodified lines
4-------------56Unreleased78- The ``|attr`` filter does not bypass the environment's attribute lookup,9 allowing the sandbox to apply its checks. :ghsa:`cpwx-vrp4-4pq7`101112Version 3.1.513-------------1415Released 2024-12-21
1042 unmodified lines