Miggo Logo

CVE-2024-56326:
Jinja has a sandbox breakout through indirect reference to format method

7.8

CVSS Score

Basic Information

EPSS Score
-
Published
12/23/2024
Updated
12/27/2024
KEV Status
No
Technology
TechnologyPython

Technical Details

CVSS Vector
CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H
Package NameEcosystemVulnerable VersionsFirst Patched Version
jinja2pip<= 3.1.43.1.5

Vulnerability Intelligence
Miggo AIMiggo AI

Miggo AIRoot Cause Analysis

The vulnerability (GHSA-q2x7-8rv6-6q7h / CVE-2024-56326) in Jinja2 allowed a sandbox escape through indirect calls to str.format. The core issue was that an attacker could obtain a direct reference to a string's format (or format_map) method and then have it executed by a custom filter, bypassing the sandbox's checks.

The provided commit 48b0687e05a5466a91cd5812d604fa37ad0943b4 addresses this by modifying how str.format methods are handled. Specifically, the SandboxedEnvironment.getitem and SandboxedEnvironment.getattr methods were updated to use a new helper function, wrap_str_format. This new function wraps the str.format or str.format_map method at the time of access (i.e., when getitem or getattr is called to retrieve it).

Before this patch, getitem and getattr would return the raw, unwrapped format method. This raw method, if called indirectly (e.g., passed as an argument to a custom filter that then invokes it), would not be subject to the sandbox's specific str.format handling (which was previously attempted within the SandboxedEnvironment.call method via inspect_format_method and format_string, now removed).

Therefore, SandboxedEnvironment.getitem and SandboxedEnvironment.getattr are identified as the vulnerable functions because they were the points at which an attacker could obtain the exploitable unwrapped method reference. The patch directly modifies these functions to ensure that any str.format method retrieved through them is already sandboxed, regardless of how it's subsequently called.

Vulnerable functions

Only Mi**o us*rs **n s** t*is s**tion

WAF Protection Rules

WAF Rule

*n ov*rsi**t in *ow t** Jinj* s*n**ox** *nvironm*nt **t**ts **lls to `str.*orm*t` *llows *n *tt**k*r t**t *ontrols t** *ont*nt o* * t*mpl*t* to *x**ut* *r*itr*ry Pyt*on *o**. To *xploit t** vuln*r**ility, *n *tt**k*r n***s to *ontrol t** *ont*nt o*

Reasoning

T** vuln*r**ility (**S*-q*x*-*rv*-*q** / *V*-****-*****) in Jinj** *llow** * s*n**ox *s**p* t*rou** in*ir**t **lls to `str.*orm*t`. T** *or* issu* w*s t**t *n *tt**k*r *oul* o*t*in * *ir**t r***r*n** to * strin*'s `*orm*t` (or `*orm*t_m*p`) m*t*o* *n