Severity:

CRITICAL RISK

(9.8)

Code Injection via Template Engine in SGLang rerank Jinja2

CVE-2026-5760

Analyzed

Vulnerability type

Category:

Code Injection via Template Engine

CWE:

CWE-94

Impact:

Remote Code Execution

Severity:

CRITICAL RISK

(9.8)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

SGLang's /v1/rerank endpoint can reach remote code execution when it loads a model file that defines a malicious tokenizer.chat_template.

Because Jinja2 templates are rendered with an unsandboxed jinja2.Environment(), attacker-controlled templates can execute arbitrary code during request handling.

Affected context

The vulnerability appears in SGLang deployments that:

  • Expose the /v1/rerank API endpoint
  • Load model artifacts that include a tokenizer.chat_template field
  • Render Jinja2-based chat templates using a plain, unsandboxed jinja2.Environment()

It becomes relevant when model files originate from locations that are not fully trusted, such as external registries, shared model hubs, or user-uploaded artifacts.

Exploit preconditions

  • Attack surface: The /v1/rerank HTTP API combined with the model loading mechanism.
  • Required access: Ability to supply or influence a model file that SGLang will load (e.g., uploading a model, pointing the service to a particular model path, or compromising the model supply chain).
  • Trigger point: Loading a model file containing a crafted tokenizer.chat_template followed by a request that causes the template to be rendered by the reranking endpoint.
  • Why exploitation is possible: The Jinja2 template engine runs with a full, unsandboxed environment, allowing template expressions to access powerful Python objects and execute logic beyond simple formatting.

Why this happens

The core issue is the use of a powerful templating engine (Jinja2) as if it were a harmless string interpolation mechanism, without sandboxing or constraining available features.

Jinja2 templates rendered through a generic jinja2.Environment() can access attributes, filters, and constructs that amount to executing attacker-supplied code when the template content is not trusted.

This maps to CWE-94: Improper Control of Generation of Code ('Code Injection'), where text interpreted by a runtime (here, the template engine) is influenced by untrusted model metadata and executed with the application's privileges.

In this case, the tokenizer.chat_template field from model files is treated as trusted logic, not as untrusted data, creating a code injection channel.

Potential impact

  • Arbitrary code execution with the privileges of the SGLang process
  • Full compromise of the host running the reranking service or its container
  • Access to API keys, model weights, and sensitive in-memory data
  • Tampering with inference results or reranking behavior
  • Lateral movement within the environment where SGLang is deployed

General mitigation guidance

Effective mitigation focuses on treating templates from model artifacts as untrusted input and strictly limiting what the template engine can do:

  • Prefer sandboxed or restricted Jinja2 environments, disabling access to dangerous attributes, filters, and Python internals.
  • Consider using data-only formatting mechanisms (e.g., simple variable substitution) instead of a general-purpose template language for untrusted sources.
  • Enforce a fixed, application-controlled set of templates and avoid loading template logic from model files whenever possible.
  • Apply allowlist-based validation on template content (e.g., only permit variable interpolation and simple control flow) before rendering.
  • Run the reranking service under least-privilege accounts and apply OS/container sandboxing to limit the blast radius if a template is abused.
  • Separate template configuration from model artifacts so that model downloads cannot automatically introduce new executable template logic.

How teams detect issues like this

Detection typically combines code-level inspection with configuration and runtime monitoring:

  • Use static analysis or code search to identify calls to jinja2.Environment() or Environment.from_string()/get_template() where the template content can originate from model metadata or other untrusted sources.
  • During code review, flag any pattern where templates are loaded from external files, model configs, or user-controlled data without explicit sandboxing or allowlisting.
  • Add automated CI checks to enforce the use of a hardened, shared template environment instead of ad-hoc Jinja2 environments.
  • Log template sources and model identifiers used by the reranking endpoint, and monitor for unexpected or newly introduced templates.
  • Optionally, use security testing tools or fuzzing to exercise template rendering paths with crafted templates to ensure sandbox policies are effective.

Sources

SourceLink
github.comgithub.com/Stuub/SGLang-0.5.9-RCE
github.comgithub.com/sgl-project/sglang/pull/23660
kb.cert.orgkb.cert.org/vuls/id/915947

Frequently asked

questions

It can be, if an attacker can cause the service to load a model that includes a malicious tokenizer.chat_template and then trigger rendering via the /v1/rerank API. In environments where model sources are tightly controlled, this may be constrained to supply-chain or insider scenarios.

The attacker needs control over, or influence on, the model file or artifact that SGLang loads, rather than direct shell or filesystem access. This could occur through a compromised model registry, a malicious model upload feature, or misconfigured automation that pulls models from untrusted locations.

The safest pattern is to decouple executable template logic from model artifacts: use a fixed, application-owned set of templates, and render them with a sandboxed or restricted template environment that only supports the minimal necessary features.

Treat any text that a dynamic engine interprets (templates, queries, scripts) the same way you treat code: never source it from untrusted artifacts, enforce strong sandboxing and allowlists, and add design-time reviews and automated checks for template and code injection risks.

Find out what's already
exploitable in your codebase.