CVE-2023-36095
Harrison Chase Langchain v0.0.194 has a critical arbitrary code execution via Python exec calls in PALChain functions from_math_prompt and from_colored_object_prompt
- CVSS 9.8
- CWE-94 Improper Control of Generation of Code ('Code Injection')
- Input Validation and Sanitization
- Remote
An issue in Harrison Chase langchain v.0.0.194 allows an attacker to execute arbitrary code via the python exec calls in the PALChain, affected functions include from_math_prompt and from_colored_object_prompt.
- CVSS base score
- 9.8
- Published
- 2023-08-05
- OWASP
- A03 Injection
- Orthogonal defect classification
- Function
- Code defect classification
- Incorrect Functionality
- Category
- Input Validation and Sanitization
- Subcategory
- Command Injection
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- langchain
- Fixed by upgrading
- Yes
Solution
Update Harrison Chase langchain to version 0.0.195 or higher.
Vulnerable code sample
class PALChain:
def __init__(self):
pass
def from_math_prompt(self, prompt):
# Vulnerable code using exec
exec(f"result = {prompt}")
return result
def from_colored_object_prompt(self, prompt):
# Vulnerable code using exec
exec(f"result = {prompt}")
return result
# Example usage
chain = PALChain()
# An attacker could input a malicious prompt
result = chain.from_math_prompt("os.system('echo Vulnerable')")
print(result) # This could execute arbitrary codePatched code sample
class SafePALChain:
def __init__(self):
pass
def from_math_prompt(self, prompt):
# Instead of using exec, use a safe evaluation method
return self.safe_eval(prompt)
def from_colored_object_prompt(self, prompt):
# Instead of using exec, use a safe evaluation method
return self.safe_eval(prompt)
def safe_eval(self, expression):
# Use a restricted environment for evaluation
allowed_names = {"sqrt": __import__('math').sqrt}
# Use eval with a limited scope to prevent arbitrary code execution
return eval(expression, {"__builtins__": None}, allowed_names)
# Example usage
safe_chain = SafePALChain()
result = safe_chain.from_math_prompt("sqrt(16)")
print(result) # Outputs: 4.0Published fix
Public sources link this CVE to the changes below. The code samples above were generated to illustrate the defect; these links lead to the code the project actually changed.
- Mitigate issue #5872 (Prompt injection -> RCE in PAL chain) (#6003) (langchain-ai/langchain)
langchain/chains/pal/base.py,langchain/chains/pal/base.py · PALChain.from_colored_object_prompt,langchain/chains/pal/base.py · PALChain.from_math_prompt,langchain/chains/pal/base.py · PALChain._call,langchain/utilities/python.py,langchain/utilities/python.py · PythonREPL.run - Some mitigations for RCE in PAL chain (#7870) (langchain-ai/langchain)
langchain/chains/pal/base.py,langchain/chains/pal/base.py · PALChain.from_colored_object_prompt,langchain/chains/pal/base.py · PALChain.from_math_prompt,langchain/chains/pal/base.py · PALChain.raise_deprecation
Cite this entry
@misc{vaitp:cve202336095,
title = {{Harrison Chase Langchain v0.0.194 has a critical arbitrary code execution via Python exec calls in PALChain functions from_math_prompt and from_colored_object_prompt}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2023},
note = {VAITP Python Vulnerability Dataset, entry CVE-2023-36095},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2023-36095/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
