Severity:

HIGH RISK

(7.3)

Code Injection in PandasAI Chat Message Handler

CVE-2026-4998

Deferred

Vulnerability type

Category:

Code Injection

CWE:

CWE-74, CWE-94

Impact:

Remote Code Execution

Severity:

HIGH RISK

(7.3)

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

PandasAI up to version 3.0.0 contains a code injection vulnerability in the CodeExecutor.execute function used by the Chat Message Handler.

When attacker-controlled content is incorporated into dynamically executed code, a remote attacker can cause arbitrary code execution on the host running PandasAI.

Affected context

This vulnerability arises in environments where PandasAI is used to interpret or execute code derived from chat messages or other dynamic prompts.

It is most relevant when:

  • The Chat Message Handler translates user input into code that is then executed by CodeExecutor.execute
  • There is no strict separation between untrusted user content and executable code
  • PandasAI is exposed via APIs, web frontends, notebooks, or agents that interact with untrusted or semi-trusted users

Exploit preconditions

  • Attack surface: Remote interfaces using PandasAI (APIs, chatbots, notebooks, automation agents) that process attacker-supplied messages.
  • Required access: Ability to send crafted chat messages or prompts that PandasAI will process and route to CodeExecutor.execute.
  • Trigger point: The vulnerable Chat Message Handler constructs and passes code, incorporating attacker-controlled fragments, to CodeExecutor.execute for evaluation.
  • Why exploitation is possible: Untrusted input is embedded into executable Python code (or a similar code string) without robust sanitization, contextual escaping, or enforcement of a constrained execution environment, enabling an attacker to inject arbitrary statements.

Why this happens

The core issue is an insecure pattern where user-influenced content is treated as code and executed in a powerful runtime environment.

In PandasAI's Chat Message Handler, the CodeExecutor.execute function appears to accept code that may be derived directly or indirectly from chat messages.

Without strong input validation, context-aware escaping, or a constrained interpreter, this aligns with:

  • CWE-94: Improper Control of Generation of Code ('Code Injection') — dynamically constructed code includes untrusted data.
  • CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component — special characters and language constructs are not neutralized before being passed to the execution engine.

The abstract insecure pattern is: "generate executable code from user-driven text and run it in a high-privilege environment without strict boundaries."

Potential impact

  • Remote execution of arbitrary Python code in the context of the PandasAI process
  • Reading, modifying, or deleting data accessible to the application (files, databases, environment variables)
  • Compromise of API keys, credentials, and other secrets loaded into the runtime
  • Pivoting to other systems reachable from the compromised host
  • Tampering with analytics results, data science workflows, or AI outputs
  • Denial of service by exhausting resources or terminating critical processes

General mitigation guidance

Risk reduction typically focuses on avoiding direct execution of user-influenced code and constraining any unavoidable execution.

Common defensive patterns include:

  • Eliminate direct eval-style execution: Refactor so that user input is mapped to a fixed set of safe operations, functions, or templates instead of arbitrary code.
  • Introduce strict command and expression whitelists: Allow only a bounded, declarative subset of operations (e.g., controlled data transformations) rather than full language constructs.
  • Use sandboxed execution environments: Run any dynamic code in an isolated interpreter or container with minimal OS privileges, restricted filesystem access, and no direct network access by default.
  • Enforce robust input validation and encoding: Treat all chat messages as untrusted; strip or reject constructs that imply control flow, imports, file or network access.
  • Apply least privilege to runtime: Run the PandasAI service under a low-privilege account with access only to the data and resources it strictly needs.
  • Harden deployment configurations: Segregate the PandasAI workload onto isolated infrastructure segments and apply strict outbound firewall policies.

How teams detect issues like this

Organizations can detect and monitor for this class of issue using a mix of static and operational techniques:

  • Static analysis patterns:
  • Search for uses of exec, eval, ast.literal_eval misuse, subprocess, or custom interpreters where parameters are influenced by user input.
  • Flag code paths where chat messages, prompts, or HTTP request bodies flow into dynamic code construction.
  • Code review signals:
  • Any logic that "builds code from strings" or templates driven by user content.
  • Hand-rolled sandboxes or partial filtering of code-like strings, which are often bypassable.
  • Runtime monitoring:
  • Application logs that show unexpected imports, filesystem accesses, or shell invocations originating from AI/chat workflows.
  • Anomalous outbound connections or command patterns from the PandasAI host.
  • Policy enforcement in CI/CD:
  • Enforce policies that block new uses of dangerous execution APIs unless justified and reviewed.
  • Incorporate SAST/linters and security scanners to highlight dynamic execution hotspots early in development.

Sources

SourceLink
gist.github.comgist.github.com/YLChen-007/78ed1dbcccdb8895adb230dddde3316d
vuldb.comvuldb.com/submit/778270
vuldb.comvuldb.com/vuln/353885
vuldb.comvuldb.com/vuln/353885/cti

Frequently asked

questions

Exploitability depends on how PandasAI is exposed. If the Chat Message Handler is accessible to untrusted users over a network (for example, via a chatbot API or web interface), the vulnerability can often be exploited remotely by sending crafted prompts that reach CodeExecutor.execute.

The attacker typically needs the ability to interact with a PandasAI-powered interface that processes their messages and routes them to the code execution path. No direct shell access is required; simple message-sending or API access may be enough, depending on the integration.

The safest pattern is to remove or strictly constrain dynamic code execution: replace free-form code evaluation with a controlled set of operations, run any remaining dynamic code inside a tightly sandboxed environment, and ensure no direct execution of raw user-derived strings.

Adopt design principles that treat all user- and model-generated text as untrusted, avoid eval-style patterns, enforce least privilege at the OS and platform level, and integrate security reviews and static analysis focused on dynamic execution paths into the development lifecycle.

Find out what's already
exploitable in your codebase.