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
Provenance
- Commit
90457bb- Tree
4a9fe8f- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
033c200- Parent 2
065334d
3 files changed+29 added−20 removed
modified:src/jinja2/filters.py+15 added-20 removed
4 unmodified lines5 import re6 import typing7 import typing as t8 from collections import abc9+from inspect import getattr_static10 from itertools import chain11 from itertools import groupby12 13 from markupsafe import escape1397 unmodified lines1411 @pass_environment1412 def 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.1418 1419 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)1432 1433- if not environment.is_safe_attribute(obj, name, value):1434- return environment.unsafe_undefined(obj, name)1433+ return environment.getattr(obj, name)1434 1436- return value1435 1438- return environment.undefined(obj=obj, name=name)1439-1440-1436 @typing.overload1437 def sync_do_map(1438 context: "Context",1439 value: t.Iterable[t.Any],434 unmodified linesmodified:tests/test_security.py+10 added
188 unmodified lines189 )190 191 with pytest.raises(SecurityError):192 t.render()193+194+ 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+ )200+201+ with pytest.raises(SecurityError):202+ t.render()modified:CHANGES.rst+4 added
3 unmodified lines4 -------------5 6 Unreleased7 8+- The ``|attr`` filter does not bypass the environment's attribute lookup,9+ allowing the sandbox to apply its checks. :ghsa:`cpwx-vrp4-4pq7`10+11+12 Version 3.1.513 -------------14 15 Released 2024-12-211042 unmodified lines