Summary
A SQL injection vulnerability in the AnythingLLM built-in SQL Agent plugin allows arbitrary SQL command execution on connected databases when constructing table schema queries.
Any user who can invoke the SQL Agent can supply crafted table names that are concatenated directly into queries, potentially leading to data exfiltration, corruption, or escalation within the database.
Affected context
This vulnerability appears in AnythingLLM deployments that:
- Enable and use the built-in SQL Agent plugin, and
- Connect the agent to relational databases (MySQL, PostgreSQL, or MSSQL), and
- Allow users or integrators to influence or provide table names the agent uses for schema inspection.
The issue manifests in the getTableSchemaSql() method across all supported database connectors, where table names are interpolated into SQL statements without safe handling.
Exploit preconditions
- Attack surface: AnythingLLM SQL Agent interface (UI, API, or any feature that triggers the SQL Agent).
- Required access: Ability to invoke the SQL Agent plugin and influence the
table_namevalue (directly or indirectly via prompts or inputs). - Trigger point: Execution of
getTableSchemaSql()when the agent attempts to gather table schema information using the attacker-controlled table name. - Why exploitation is possible: The SQL query string is constructed using direct string concatenation of the untrusted
table_nameparameter, with no sanitization or parameterization, allowing injected SQL to be interpreted by the database engine.
Why this happens
The underlying bug is a classic SQL injection pattern where untrusted input is integrated into SQL statements through string concatenation.
The getTableSchemaSql() method in each connector (MySQL, PostgreSQL, MSSQL) builds schema-inspection queries by directly appending the table_name parameter without proper quoting, escaping, or parameter binding.
This aligns with CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), in which attacker-controlled data is treated as part of the SQL grammar rather than as data, enabling arbitrary query modification.
Potential impact
- Read access to sensitive tables and records that the application's database user can query
- Modification or deletion of database records, potentially corrupting LLM context or application state
- Creation or alteration of database schemas (tables, views, or functions), depending on granted privileges
- Abuse of stored procedures or database-specific features that could lead to further compromise
- Lateral movement within the data layer if shared accounts or cross-database links are present
General mitigation guidance
Effective mitigation centers on treating all table identifiers and query parameters as untrusted and separating data from SQL logic.
Recommended patterns include:
- Using parameterized queries or prepared statements wherever possible, including for schema-related queries
- Applying strict input validation and allowlists for table names and other identifiers before constructing SQL
- Using database abstraction layers or query builders that handle identifier quoting and parameter binding safely
- Restricting the database user account used by the SQL Agent to the minimum required privileges
- Introducing a controlled mapping between user-facing names and actual table identifiers to avoid direct user control over SQL identifiers
How teams detect issues like this
Detection can rely on a combination of code-centric and operational approaches:
- Static analysis rules that flag string concatenation in SQL construction, especially around schema inspection functions such as
getTableSchemaSql() - Manual code review focused on database connector implementations, looking for unparameterized query building and direct interpolation of variables like
table_name - Security-focused linters or SAST tools integrated into CI pipelines to enforce policies against dynamic SQL without parameterization
- Database monitoring and logging for anomalous queries, unexpected schema operations, or patterns typical of SQL injection attempts

