Summary
pandas-ai v3.0.0 contains a SQL injection vulnerability in the pandasai.agent.base._execute_sql_query component, where user-controlled input is incorporated into SQL statements without sufficient safeguards.
In environments where the agent can be influenced by untrusted prompts or parameters, an attacker may be able to execute arbitrary SQL queries, leading to data exfiltration, tampering, or destructive operations on the underlying database.
Affected context
The vulnerability arises when pandas-ai is used to generate or execute SQL queries against a database, and the _execute_sql_query helper accepts values ultimately influenced by untrusted input (such as user prompts, parameters, or upstream application data).
This is most relevant in LLM-augmented analytics or agent workflows where natural language input can shape the final SQL string and where no strong parameterization or query whitelisting is enforced.
The issue is present in pandas-ai v3.0.0 and manifests wherever direct SQL strings are passed through _execute_sql_query without proper validation or binding.
Exploit preconditions
- Attack surface: Any API, notebook, chat interface, or application that lets users influence SQL queries executed by pandas-ai.
- Required access: Ability to supply or steer the input that flows into the agent logic (e.g., prompts, query text, or parameters).
- Trigger point: Execution of
_execute_sql_querywith a SQL string that includes attacker-controlled fragments. - Why exploitation is possible: The component appears to build or accept a complete SQL string and pass it to the database driver without enforcing parameterized queries, robust escaping, or strict query templates, allowing hostile input to alter the intended SQL command structure.
Why this happens
This vulnerability is a classic example of CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection').
The _execute_sql_query routine likely treats SQL text as a simple string and either concatenates user- or model-influenced fragments into that string or accepts them without constraint.
Because the database engine interprets the final string as executable SQL, any embedded control characters (quotes, comment markers, statement separators, or injected clauses) can change the meaning of the query, bypass logical checks, or introduce additional commands.
The core insecure pattern is trusting dynamic SQL text instead of separating data from code via parameter binding or using constrained, predefined query templates.
Potential impact
- Exposure of sensitive data through unauthorized
SELECTqueries. - Tampering with application data via injected
INSERT,UPDATE, orDELETEstatements. - Destructive operations such as dropping tables or truncating datasets, where database permissions allow.
- Bypassing application-level business logic or authorization checks enforced at the query layer.
- Pivoting to other systems if the database supports stored procedures or extended functions that reach beyond data access.
General mitigation guidance
Effective mitigation focuses on removing the ability for untrusted input to modify SQL control flow:
- Prefer parameterized queries or bound variables instead of dynamically concatenating values into SQL strings, even for seemingly simple filters.
- Introduce strict query templates or an allowed-query layer, where user or model inputs can only fill predetermined parameters (e.g., column filters, literal values) rather than arbitrary SQL fragments.
- Apply schema-aware validation for any user-influenced values, restricting them to expected formats, enumerated sets, or strongly typed ranges before they reach the database layer.
- Limit the agent's database access using least privilege, granting only those operations and schemas that are necessary for its intended analytics tasks.
- Consider a query review or simulation mode in higher-risk contexts, where generated SQL is inspected or executed against a safe environment before touching production data.
How teams detect issues like this
Detection efforts can combine code-level analysis with runtime monitoring:
- Use static analysis rules or linters to flag direct string concatenation or f-strings used to build SQL passed into
_execute_sql_queryor similar helpers. - During code review, look for places where prompts, user input, or LLM-generated text is treated as SQL without parameter binding or validation.
- Implement database-level monitoring and logging to detect anomalous patterns, such as queries containing unexpected comments, UNION operations, or data definition language (DDL) statements in primarily read-only workflows.
- Apply security policies in CI/CD pipelines that disallow the introduction of new dynamic SQL paths unless they demonstrate use of parameterized APIs.

