VAITP Dataset

← Back to the dataset

CVE-2023-39659

Arbitrary code execution in langchain-ai (v0.0.232 and earlier) via PythonAstREPLTool._run component

  • CVSS 9.8
  • CWE-74
  • Input Validation and Sanitization
  • Remote

An issue in langchain langchain-ai v.0.0.232 and before allows a remote attacker to execute arbitrary code via a crafted script to the PythonAstREPLTool._run component.

CVSS base score
9.8
Published
2023-08-15
OWASP
A03 Injection
Orthogonal defect classification
Interface
Code defect classification
Incorrect Interface
Category
Input Validation and Sanitization
Subcategory
Insecure Parsing or Deserialization
Accessibility scope
Remote
Impact
Arbitrary Code Execution
Affected component
langchain
Fixed by upgrading
Yes

Solution

Update to langchain-ai version 0.0.233 or higher

Vulnerable code sample

class PythonAstREPLTool:
    def _run(self, code):
        # Vulnerable code: directly evaluating arbitrary code
        try:
            result = eval(code)  # Unsafe evaluation of code
            return result
        except Exception as e:
            return f"Error: {str(e)}"

# Example usage
tool = PythonAstREPLTool()
result = tool._run("__import__('os').system('ls')")  # Arbitrary code execution
print(result)

Patched code sample

import ast
import subprocess

class SafeREPLTool:
    def _run(self, code):
        # Use ast.literal_eval to safely evaluate expressions
        try:
            # Only allow safe expressions
            tree = ast.parse(code, mode='eval')
            # Compile and execute the code in a restricted environment
            compiled_code = compile(tree, filename="<ast>", mode="eval")
            result = eval(compiled_code, {"__builtins__": {}})
            return result
        except Exception as e:
            return f"Error: {str(e)}"

# Example usage
tool = SafeREPLTool()
result = tool._run("1 + 1")  # Safe expression
print(result)  # Outputs: 2

Published 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.

  • Deprecate PythonRepl tools and Pandas/Xorbits/Spark DataFrame/Python/CSV agents (#12427) (langchain-ai/langchain)
    libs/langchain/langchain/agents/agent_toolkits/csv/__init__.py, libs/langchain/langchain/agents/agent_toolkits/pandas/__init__.py, libs/langchain/langchain/agents/agent_toolkits/python/__init__.py, libs/langchain/langchain/agents/agent_toolkits/spark/__init__.py, libs/langchain/langchain/agents/agent_toolkits/xorbits/__init__.py, libs/langchain/langchain/agents/agent_toolkits/__init__.py, libs/langchain/langchain/agents/load_tools.py, libs/langchain/langchain/agents/load_tools.py · _get_python_repl, libs/langchain/langchain/agents/__init__.py, libs/langchain/langchain/tools/python/__init__.py, libs/langchain/langchain/tools/__init__.py, libs/langchain/langchain/tools/__init__.py · _import_python_tool_PythonAstREPLTool and 2 more

Cite this entry

@misc{vaitp:cve202339659,
  title        = {{Arbitrary code execution in langchain-ai (v0.0.232 and earlier) via PythonAstREPLTool._run component}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2023},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2023-39659},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2023-39659/}}
}

Download this entry as JSON Download the whole dataset

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 ::

Before you use the VAITP AI models

Please read and accept the following disclaimer:

By choosing "I understand and accept", you acknowledge that you understand and agree to these terms.