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

CVE-2023-36095: langchain Code Injection vulnerability

criticalCVSS 4.0 9.3 · 3.1 9.8GHSA-gwqq-6vq7-5j86

LangChain's PALChain (for example PALChain.from_math_prompt(llm).run) has the model write Python and then runs it with exec, so a crafted prompt gives an attacker arbitrary code execution on the host. Anyone running langchain before 0.0.236 with a PAL chain on user input is affected. Upgrade to 0.0.236 or later, and prefer langchain-experimental with external sandboxing.

Published Aug 5, 2023 · Updated Sep 27, 2024 · Analysis Nov 2, 2026

What an attacker can do

PALChain is a program-aided language chain. It sends the user's question to an LLM, receives Python code and passes that code to exec in the application's own process. Nothing checked the generated code. The reporter's example embeds import os and os.system('ls') in the prompt next to a simple math question, and the model emits it.

So the attacker only needs to reach a prompt that feeds a PALChain, for example a public chatbot or an API that does math or object reasoning. No account or interaction is required if the app is open, which is why the CVSS vector says network, no privileges. The code runs with the application's permissions. That exposes environment variables, API keys, local files and anything the host can reach, and it allows persistence.

The root cause is trusting model output as code. Prompt injection from retrieved documents or web pages can steer the model into writing the malicious code too, so the user does not have to be the attacker.

Who is affected

pip package langchain before 0.0.236, when the application uses PALChain, for example via from_math_prompt or from_colored_object_prompt, and passes untrusted text to run(). Check the version:

pip show langchain

Apps that never construct a PAL chain do not reach the exec call.

langchain

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

How to fix it

Upgrade to langchain 0.0.236 or later:

pip install --upgrade "langchain>=0.0.236"

Pull request 7870 added static checks on the generated code using Python's ast library. They block imports, block arbitrary execution commands, enforce an execution time limit and require the solution expression to be present. The maintainers later moved PALChain to the langchain-experimental package, and it was removed from langchain starting with v0.0.247. They added notices that in-process Python sandboxing is hard. If you still need PAL, run the generated code in an external sandbox such as a container with no secrets and no network.

How to tell if you were hit

Search code for PALChain and the from_math_prompt and from_colored_object_prompt constructors. Review application logs and LLM traces for generated programs that contain import, os.system, subprocess, open( or socket. Look for unexpected child processes or outbound connections from the service running the chain. If agents on developer machines call such chains, Beam's records of agent shell commands and file access can help show what ran.

Original advisory

From the GitHub Advisory Database (CC BY 4.0).

An issue in Harrison Chase langchain allows an attacker to execute arbitrary code via the PALChain,from_math_prompt(llm).run in the python exec method.

Questions

Which langchain version fixes CVE-2023-36095?

0.0.236 contains the fix from pull request 7870. PALChain was later removed from langchain at 0.0.247 and now lives in langchain-experimental.

Does this need authentication?

Not by itself. The attacker needs only to get text into a PALChain prompt. Whether that requires a login depends on how your application exposes the chain.

Is the 0.0.236 fix a complete sandbox?

No. It adds ast-based checks and a time limit. The maintainers' own notice says in-process Python sandboxing is hard and advises sandboxing execution externally.

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