# CVE-2024-34359: llama-cpp-python vulnerable to Remote Code Execution by Server-Side Template Injection in Model Metadata

> Attackers can execute arbitrary code by providing a malicious GGUF model file with a crafted Jinja2 chat template in its metadata.

- **Severity:** Critical 9.7 (`CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:H/I:H/A:H`)
- **Exploited in the wild:** Not known
- **Login needed:** Not needed
- **Attackable:** Over the internet
- **Victim action needed:** A user has to click or open something
- **Fix available:** Yes
- **Published:** 10 May 2024
- **Updated:** 2 Aug 2024
- **Also known as:** GHSA-56xg-wfcc-g829, PYSEC-2026-392

## What is CVE-2024-34359?

CVE-2024-34359 is a critical code injection vulnerability in abetlen llama-cpp-python, published on 10 May 2024. An attacker can exploit it over the internet without logging in, and only if a user opens a file or follows a link.

### Official description

llama-cpp-python is the Python bindings for llama.cpp. `llama-cpp-python` depends on class `Llama` in `llama.py` to load `.gguf` llama.cpp or Latency Machine Learning Models. The `__init__` constructor built in the `Llama` takes several parameters to configure the loading and running of the model. Other than `NUMA, LoRa settings`, `loading tokenizers,` and `hardware settings`, `__init__` also loads the `chat template` from targeted `.gguf` 's Metadata and furtherly parses it to `llama_chat_format.Jinja2ChatFormatter.to_chat_handler()` to construct the `self.chat_handler` for this model. Nevertheless, `Jinja2ChatFormatter` parse the `chat template` within the Metadate with sandbox-less `jinja2.Environment`, which is furthermore rendered in `__call__` to construct the `prompt` of interaction. This allows `jinja2` Server Side Template Injection which leads to remote code execution by a carefully constructed payload.

Weakness: CWE-76 (Improper Neutralization of Equivalent Special Elements)

## Which versions are affected by CVE-2024-34359?

3 open source packages ship an affected version, including llama-cpp-python.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `llama-cpp-python` | PyPI | From 0.2.30 | 0.2.72 |
| `llama-cpp-python` | PyPI | From 0.2.30 | 0.2.72 |
| `llama-cpp-python` | PyPI | From 0.2.30 | 0.2.72 |

## How do I fix CVE-2024-34359?

Upgrade llama-cpp-python to 0.2.72 or later.

## Is CVE-2024-34359 being exploited?

It is not confirmed as exploited in the wild, but public exploit code for CVE-2024-34359 exists.

## Fix prompt for AI coding agents

```text
Check this codebase for CVE-2024-34359 and fix it if it is affected.

What it is: CVE-2024-34359 is a critical code injection vulnerability in abetlen llama-cpp-python, published on 10 May 2024. An attacker can exploit it over the internet without logging in, and only if a user opens a file or follows a link.
Exploitation: It is not confirmed as exploited in the wild, but public exploit code for CVE-2024-34359 exists.

Affected packages and fixed versions:
- llama-cpp-python (PyPI): affected from 0.2.30; fixed in 0.2.72
- llama-cpp-python (PyPI): affected from 0.2.30; fixed in 0.2.72
- llama-cpp-python (PyPI): affected from 0.2.30; fixed in 0.2.72

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/CVE-2024-34359/ (Markdown: https://insights.sbomify.com/CVE-2024-34359/index.md)
```

## Sources

- CVE record: https://www.cve.org/CVERecord?id=CVE-2024-34359
- NVD: https://nvd.nist.gov/vuln/detail/CVE-2024-34359
- OSV: https://osv.dev/vulnerability/CVE-2024-34359

---
From sbomify Insights: https://insights.sbomify.com/CVE-2024-34359/
Summaries are generated and may contain mistakes; the official description above is the record.
