VYPR
Critical severityNVD Advisory· Published Apr 10, 2024· Updated Aug 1, 2024

Server-Side Template Injection in BerriAI/litellm

CVE-2024-2952

Description

BerriAI/litellm is vulnerable to Server-Side Template Injection (SSTI) via the /completions endpoint. The vulnerability arises from the hf_chat_template method processing the chat_template parameter from the tokenizer_config.json file through the Jinja template engine without proper sanitization. Attackers can exploit this by crafting malicious tokenizer_config.json files that execute arbitrary code on the server.

AI Insight

LLM-synthesized narrative grounded in this CVE's description and references.

BerriAI/litellm SSTI via /completions endpoint due to unsanitized Jinja template processing in hf_chat_template.

CVE-2024-2952 is a Server-Side Template Injection (SSTI) vulnerability found in BerriAI/litellm, an open-source AI Gateway that provides a unified API for over 100 large language models [1]. The flaw resides in the /completions endpoint, specifically within the hf_chat_template method. This method processes the chat_template parameter from a Hugging Face model's tokenizer_config.json file through the Jinja template engine without proper sanitization of user-controlled input [2][3].

An attacker can exploit this vulnerability by crafting a malicious tokenizer_config.json file that contains Jinja template directives. Because the template is rendered server-side using Jinja2's Environment class (which allows arbitrary code execution by default), the attacker can inject expressions that execute arbitrary Python code on the litellm server [2][4]. The attack requires the ability to influence the chat_template value, which can occur when an application allows users to specify a model or tokenizer configuration from external sources.

Successful exploitation grants the attacker arbitrary code execution on the server running litellm. This could lead to full system compromise, including data exfiltration, lateral movement within the environment, or disruption of the AI gateway service. The vulnerability is classified as critical due to the high impact of remote code execution without authentication [2].

The vulnerability was addressed in a commit referenced as 8a1cdc9, which replaced the standard Jinja2 Environment with an ImmutableSandboxedEnvironment and used a BaseLoader to restrict template features [4]. Users are strongly advised to update to the patched version immediately. No known public exploits have been reported at the time of publication, but the technical details are publicly available.

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.

PackageAffected versionsPatched versions
litellmPyPI
< 1.34.421.34.42

Affected products

2
  • ghsa-coords
    Range: < 1.34.42
  • berriai/berriai/litellmv5
    Range: unspecified

Patches

1
8a1cdc901708

Merge pull request #2941 from BerriAI/litellm_fix_sec_report

https://github.com/BerriAI/litellmIshaan JaffApr 11, 2024via ghsa
1 file changed · +11 7
  • litellm/llms/prompt_templates/factory.py+11 7 modified
    @@ -1,7 +1,8 @@
     from enum import Enum
     import requests, traceback
     import json, re, xml.etree.ElementTree as ET
    -from jinja2 import Template, exceptions, Environment, meta
    +from jinja2 import Template, exceptions, meta, BaseLoader
    +from jinja2.sandbox import ImmutableSandboxedEnvironment
     from typing import Optional, Any
     import imghdr, base64
     from typing import List
    @@ -219,6 +220,15 @@ def phind_codellama_pt(messages):
     
     
     def hf_chat_template(model: str, messages: list, chat_template: Optional[Any] = None):
    +    # Define Jinja2 environment
    +    env = ImmutableSandboxedEnvironment()
    +
    +    def raise_exception(message):
    +        raise Exception(f"Error message - {message}")
    +
    +    # Create a template object from the template text
    +    env.globals["raise_exception"] = raise_exception
    +
         ## get the tokenizer config from huggingface
         bos_token = ""
         eos_token = ""
    @@ -249,12 +259,6 @@ def _get_tokenizer_config(hf_model_name):
             eos_token = tokenizer_config["eos_token"]
             chat_template = tokenizer_config["chat_template"]
     
    -    def raise_exception(message):
    -        raise Exception(f"Error message - {message}")
    -
    -    # Create a template object from the template text
    -    env = Environment()
    -    env.globals["raise_exception"] = raise_exception
         try:
             template = env.from_string(chat_template)
         except Exception as e:
    

Vulnerability mechanics

Generated on May 9, 2026. Inputs: CWE entries + fix-commit diffs from this CVE's patches. Citations validated against bundle.

References

8

News mentions

0

No linked articles in our index yet.