Jinja sandbox breakout through attr filter selecting format method
Description
Jinja is an extensible templating engine. Prior to 3.1.6, an oversight in how the Jinja sandboxed environment interacts with the |attr filter allows an attacker that controls the content of a template to execute arbitrary Python code. To exploit the vulnerability, an attacker needs to control the content of a template. Whether that is the case depends on the type of application using Jinja. This vulnerability impacts users of applications which execute untrusted templates. Jinja's sandbox does catch calls to str.format and ensures they don't escape the sandbox. However, it's possible to use the |attr filter to get a reference to a string's plain format method, bypassing the sandbox. After the fix, the |attr filter no longer bypasses the environment's attribute lookup. This vulnerability is fixed in 3.1.6.
AI Insight
LLM-synthesized narrative grounded in this CVE's description and references.
Jinja before 3.1.6 allows sandbox escape via the |attr filter, enabling arbitrary code execution from an untrusted template.
Vulnerability
CVE-2025-27516 is a sandbox escape in Jinja, a Python templating engine. The flaw arises from an oversight in how the sandboxed environment interacts with the |attr filter. While the sandbox explicitly prevents calls to str.format from escaping, an attacker can use |attr to obtain a reference to a plain string's format method, bypassing the sandbox controls [1][4].
Exploitation
To exploit the vulnerability, an attacker must control the content of a template. This is relevant for applications that render untrusted templates. The |attr filter previously allowed bypassing the environment's attribute lookup, enabling access to Python internals [2][4].
Impact
A successful exploit allows arbitrary Python code execution within the Jinja environment, potentially leading to full server compromise depending on the host application's permissions [4].
Mitigation
The vulnerability is fixed in Jinja version 3.1.6. The patch modifies the do_attr function to use getattr_static and then delegates to environment.getattr, ensuring sandbox attribute lookup restrictions are enforced [2][4].
AI Insight generated on May 20, 2026. Synthesized from this CVE's description and the cited reference URLs; citations are validated against the source bundle.
Affected packages
Versions sourced from the GitHub Security Advisory.
| Package | Affected versions | Patched versions |
|---|---|---|
Jinja2PyPI | < 3.1.6 | 3.1.6 |
Affected products
67- osv-coords65 versionspkg:apk/chainguard/checkovpkg:apk/chainguard/grafana-oncallpkg:apk/chainguard/grafana-oncall-compatpkg:apk/chainguard/jupyter-base-notebookpkg:apk/chainguard/jupyter-base-notebook-oci-entrypointpkg:apk/chainguard/nemopkg:apk/chainguard/py3.10-torchvision-cuda-11.8pkg:apk/chainguard/py3.10-torchvision-cuda-12.3pkg:apk/chainguard/py3.10-vllm-cuda-11.8pkg:apk/chainguard/py3.10-vllm-cuda-12.6pkg:apk/chainguard/py3.10-wheels-torchvision-cuda-11.8pkg:apk/chainguard/py3.10-wheels-torchvision-cuda-12.3pkg:apk/chainguard/py3.10-wheels-vllm-cuda-11.8pkg:apk/chainguard/py3.11-text-generation-inferencepkg:apk/chainguard/py3.11-torchaudio-cuda-12.3pkg:apk/chainguard/py3.11-torchvision-cuda-11.8pkg:apk/chainguard/py3.11-torchvision-cuda-12.3pkg:apk/chainguard/py3.11-wheels-torchaudio-cuda-12.3pkg:apk/chainguard/py3.11-wheels-torchvision-cuda-11.8pkg:apk/chainguard/py3.11-wheels-torchvision-cuda-12.3pkg:apk/chainguard/py3.12-torchvision-cuda-12.3pkg:apk/chainguard/py3.12-wheels-torchvision-cuda-11.8pkg:apk/chainguard/py3.12-wheels-torchvision-cuda-12.3pkg:apk/chainguard/py3.9-torchvision-cuda-11.8pkg:apk/chainguard/py3.9-torchvision-cuda-12.3pkg:apk/chainguard/py3.9-wheels-torchvision-cuda-11.8pkg:apk/chainguard/py3.9-wheels-torchvision-cuda-12.3pkg:apk/chainguard/reflexpkg:apk/chainguard/tensorflow-cpu-jupyterpkg:apk/chainguard/tensorflow-gpu-jupyter-2.18pkg:apk/chainguard/text-generation-inferencepkg:apk/chainguard/text-generation-inference-compatpkg:apk/wolfi/checkovpkg:apk/wolfi/grafana-oncallpkg:apk/wolfi/grafana-oncall-compatpkg:apk/wolfi/jupyter-base-notebookpkg:apk/wolfi/jupyter-base-notebook-oci-entrypointpkg:apk/wolfi/reflexpkg:apk/wolfi/tensorflow-cpu-jupyterpkg:pypi/jinja2pkg:rpm/almalinux/python3-jinja2pkg:rpm/almalinux/python3-jinja2%2Bi18npkg:rpm/opensuse/python-Jinja2&distro=openSUSE%20Leap%2015.6pkg:rpm/suse/python-Jinja2&distro=SUSE%20Enterprise%20Storage%207.1pkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20High%20Performance%20Computing%2015%20SP3-LTSSpkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20High%20Performance%20Computing%2015%20SP4-ESPOSpkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20High%20Performance%20Computing%2015%20SP4-LTSSpkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20High%20Performance%20Computing%2015%20SP5-ESPOSpkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20High%20Performance%20Computing%2015%20SP5-LTSSpkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20Micro%205.1pkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20Micro%205.2pkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20Micro%205.3pkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20Micro%205.4pkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20Micro%205.5pkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20Module%20for%20Basesystem%2015%20SP6pkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20Module%20for%20Public%20Cloud%2015%20SP4pkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20Module%20for%20Python%203%2015%20SP6pkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20Server%2015%20SP3-LTSSpkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20Server%2015%20SP4-LTSSpkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20Server%2015%20SP5-LTSSpkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20Server%20for%20SAP%20Applications%2015%20SP3pkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20Server%20for%20SAP%20Applications%2015%20SP4pkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Enterprise%20Server%20for%20SAP%20Applications%2015%20SP5pkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Micro%206.0pkg:rpm/suse/python-Jinja2&distro=SUSE%20Linux%20Micro%206.1
< 3.2.432-r0+ 64 more
- (no CPE)range: < 3.2.432-r0
- (no CPE)range: < 1.15.2-r1
- (no CPE)range: < 1.15.2-r1
- (no CPE)range: < 7.3.2-r2
- (no CPE)range: < 0.0.0_git20251013-r0
- (no CPE)range: < 1.23.0-r11
- (no CPE)range: < 0.18.1-r3
- (no CPE)range: < 0.18.1-r4
- (no CPE)range: < 0.7.3-r1
- (no CPE)range: < 0.7.3-r1
- (no CPE)range: < 0.18.1-r3
- (no CPE)range: < 0.18.1-r4
- (no CPE)range: < 0.7.3-r1
- (no CPE)range: < 3.3.6-r3
- (no CPE)range: < 2.3.1-r1
- (no CPE)range: < 0.18.1-r4
- (no CPE)range: < 0.18.1-r4
- (no CPE)range: < 2.3.1-r1
- (no CPE)range: < 0.18.1-r4
- (no CPE)range: < 0.18.1-r4
- (no CPE)range: < 0.18.1-r2
- (no CPE)range: < 0.18.1-r2
- (no CPE)range: < 0.18.1-r2
- (no CPE)range: < 0.18.1-r2
- (no CPE)range: < 0.18.1-r2
- (no CPE)range: < 0.18.1-r2
- (no CPE)range: < 0.18.1-r2
- (no CPE)range: < 0.7.2-r0
- (no CPE)range: < 2.18.0-r1
- (no CPE)range: < 2.18.0-r1
- (no CPE)range: < 3.3.6-r3
- (no CPE)range: < 3.3.6-r3
- (no CPE)range: < 3.2.432-r0
- (no CPE)range: < 1.15.2-r1
- (no CPE)range: < 1.15.2-r1
- (no CPE)range: < 7.3.2-r2
- (no CPE)range: < 0.0.0_git20251013-r0
- (no CPE)range: < 0.7.2-r0
- (no CPE)range: < 2.18.0-r1
- (no CPE)range: < 3.1.6
- (no CPE)range: < 2.10.1-7.el8_10
- (no CPE)range: < 3.1.6-1.el10_0
- (no CPE)range: < 3.1.2-150400.12.14.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 3.1.2-150400.12.14.1
- (no CPE)range: < 3.1.2-150400.12.14.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 2.10.1-150000.3.21.1
- (no CPE)range: < 3.1.2-8.1
- (no CPE)range: < 3.1.4-slfo.1.1_2.1
- pallets/jinjav5Range: < 3.1.6
Patches
13 files changed · +30 −21
CHANGES.rst+4 −0 modified@@ -5,6 +5,10 @@ Version 3.1.6 Unreleased +- The ``|attr`` filter does not bypass the environment's attribute lookup, + allowing the sandbox to apply its checks. :ghsa:`cpwx-vrp4-4pq7` + + Version 3.1.5 -------------
src/jinja2/filters.py+16 −21 modified@@ -6,6 +6,7 @@ import typing import typing as t from collections import abc +from inspect import getattr_static from itertools import chain from itertools import groupby @@ -1411,31 +1412,25 @@ def do_reverse(value: t.Union[str, t.Iterable[V]]) -> t.Union[str, t.Iterable[V] 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. + """Get an attribute of an object. ``foo|attr("bar")`` works like + ``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
tests/test_security.py+10 −0 modified@@ -190,3 +190,13 @@ def run(value, arg): 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()
Vulnerability mechanics
Generated on May 9, 2026. Inputs: CWE entries + fix-commit diffs from this CVE's patches. Citations validated against bundle.
References
6- github.com/advisories/GHSA-cpwx-vrp4-4pq7ghsaADVISORY
- nvd.nist.gov/vuln/detail/CVE-2025-27516ghsaADVISORY
- github.com/pallets/jinja/commit/90457bbf33b8662926ae65cdde4c4c32e756e403ghsax_refsource_MISCWEB
- github.com/pallets/jinja/security/advisories/GHSA-cpwx-vrp4-4pq7ghsax_refsource_CONFIRMWEB
- lists.debian.org/debian-lts-announce/2025/04/msg00022.htmlghsaWEB
- lists.debian.org/debian-lts-announce/2025/04/msg00045.htmlghsaWEB
News mentions
0No linked articles in our index yet.