NewApp Monitoring is liveSign up free
AgentbeamBeta
AI agent vulnerability · LangChain

CVE-2023-36281: langchain vulnerable to arbitrary code execution

criticalCVSS 4.0 9.3 · 3.1 9.8GHSA-7gfq-f96f-g85j

LangChain before 0.0.312 can execute attacker code when an application calls load_prompt on an untrusted JSON file. The file can carry a jinja2 template, and jinja2 templates are not sandboxed. Version 0.0.312 stops loading jinja2-formatted prompt template files.

Published Aug 22, 2023 · Updated Sep 30, 2024 · Analysis Nov 2, 2026

What an attacker can do

load_prompt reads a prompt definition from a file. If the file declares a jinja2 template format, LangChain renders the template with jinja2, which is not sandboxed. A template can reach Python internals through objects such as __subclasses__ and run arbitrary commands. The CVE text mentions both __subclasses__ and a template as the route.

The attacker must get a malicious JSON prompt file in front of load_prompt. That happens when an application lets users upload or point to prompt files, pulls prompts from a shared hub or repository, or loads them from a path someone else controls. The CVSS vector (AV:N, AC:L, PR:N, UI:N) scores 9.8, since the attacker needs no account when the app takes such files from the network.

The result is code execution as the application's user, with its environment variables, API keys and files. Apps that only load prompt files they wrote themselves are not at practical risk.

Who is affected

The pip package langchain before 0.0.312. The CVE text names v0.0.171, and the issue was filed against the 0.0.1xx series. Applications that call load_prompt or load_prompt_from_config with files they do not fully control are the exposed ones. Check:

pip show langchain

Search your code for load_prompt and for template_format set to jinja2.

langchain

Ecosystem
PyPI
Affected
< 0.0.312
Fixed in
0.0.312
Check
pip show langchain

How to fix it

Upgrade langchain to 0.0.312 or later:

pip install --upgrade "langchain>=0.0.312"

Pull request 10252, merged on October 10, 2023, removes support for loading jinja2-formatted prompt template files. You can still build a PromptTemplate with jinja2 by hand, but the class now warns that you must not pass untrusted input to it. The sources list no other workaround.

How to tell if you were hit

Search your repositories and file stores for JSON or YAML prompt files whose template_format is jinja2, and for templates containing __subclasses__, __class__, __globals__ or similar Python internals. In logs, look for load_prompt calls on paths that users supply. On the host, watch for unexpected child processes of the Python service.

Original advisory

From the GitHub Advisory Database (CC BY 4.0).

An issue in langchain v.0.0.171 allows a remote attacker to execute arbitrary code via the via the a json file to the load_prompt parameter. This is related to __subclasses__ or a template.

Credited to eyurtsev.

Questions

How is code executed here?

Through a jinja2 template in a prompt file. Jinja2 is not sandboxed, so a template can reach Python internals such as __subclasses__ and run commands when it is rendered.

Which version fixes CVE-2023-36281?

langchain 0.0.312, which no longer loads jinja2-formatted prompt template files.

Can I still use jinja2 templates?

You can build a PromptTemplate in code with jinja2, but the class carries a warning against passing untrusted input to it.

Is it exploited in the wild?

The sources do not say so, and the entry has no CISA catalog data.

References

How this page is made: the data panel comes from the GitHub Advisory Database, CISA’s KEV catalog, the NVD and FIRST EPSS, and updates twice a day. The analysis is written by an AI model from the sources listed above and checked by automated rules before it is published. Check versions against the vendor's advisory before you act. Methodology

More LangChain vulnerabilities

All LangChain entries