/

CVE-2026-41640

Severity:

HIGH RISK

(7.5)

SQL Injection in NocoBase queryParentSQL Recursive CTE

CVE-2026-41640

Analyzed

Vulnerability type

Category:

SQL Injection

CWE:

CWE-89

Impact:

Arbitrary SQL Execution Within the Application Database

Severity:

HIGH RISK

(7.5)

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

A SQL injection vulnerability in NocoBase's core database package allows crafted primary key values to be concatenated directly into a recursive CTE query.

When an attacker can create records with malicious string primary keys, any later operation that triggers recursive eager loading on that collection may execute injected SQL within the database.

Affected context

This vulnerability appears in NocoBase deployments where:

  • Collections are configured such that primary key values are strings (or treated as strings) and can be influenced by end users or integrations.
  • The queryParentSQL() function constructs recursive CTE queries by concatenating node IDs directly into SQL.
  • Recursive eager loading is triggered for collections whose primary keys may contain untrusted data.

It is most relevant in multi-tenant or extensible deployments where users can create records, define data via APIs, or synchronize from external systems that may not sanitize identifiers.

Exploit preconditions

  • Attack surface: Application endpoints or workflows that:
  • Allow creation or import of records whose primary key values can contain arbitrary strings.
  • Later trigger recursive eager loading on the affected collection (e.g., hierarchical/relational views, tree structures, or parent-child graphs).
  • Required access:
  • Ability to insert or influence records in the vulnerable collection (via UI, API, automation, or sync pipeline).
  • No special database-level privileges are required beyond the application's own database account.
  • Trigger point:
  • Invocation of queryParentSQL() with a nodeIds array that includes the malicious primary key(s), causing the recursive CTE SQL string to be assembled and executed.
  • Why exploitation is possible:
  • queryParentSQL() joins nodeIds into the SQL query using raw string concatenation.
  • The primary keys originate from database rows that may contain untrusted user-controlled content.
  • No parameterization, escaping, or strict typing is enforced at this SQL construction point, enabling attacker-controlled tokens to break out of the intended context and inject arbitrary SQL.

Why this happens

The root cause is the construction of SQL queries using unparameterized string concatenation of values that can originate from untrusted sources.

In this case, the queryParentSQL() function builds a recursive CTE by combining node IDs into the query text, implicitly trusting that primary key values are safe to interpolate. When an attacker is able to create a record with a crafted string primary key, that value is later read from the database, placed into the nodeIds array, and concatenated into the SQL. Because the values are not bound as parameters and no strict type or schema enforcement is applied at this boundary, the database interprets attacker-supplied fragments as part of the query itself.

This pattern maps directly to CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), where dynamic SQL construction fails to separate data from code.

Potential impact

  • Execution of attacker-controlled SQL statements in the context of the application's database account.
  • Unauthorized reading of sensitive data from other tables or tenants.
  • Unauthorized modification or deletion of records, including business-critical data.
  • Bypassing of application-level authorization enforced in higher layers.
  • Creation of backdoor data or structures to facilitate further abuse or persistence.
  • In some environments, potential pivoting into database-level features (e.g., stored procedures, extended functions) if available to the application role.

General mitigation guidance

Effective mitigation focuses on eliminating dynamic SQL string concatenation and reducing trust in identifier-like values:

  • Prefer parameterized queries or query-builder abstractions that separate code from data for all SQL execution paths, including recursive CTEs and bulk operations.
  • Enforce strict typing and validation on primary keys and other identifiers; restrict them to predictable formats (e.g., integers, UUIDs) and reject values that contain SQL-significant characters.
  • Centralize SQL construction logic in well-reviewed utility functions instead of ad hoc string concatenation in multiple locations.
  • Treat values read back from the database as potentially tainted if they originally came from user input or untrusted integrations, and re-validate before reusing them in query construction.
  • Apply least-privilege to the database account used by the application so that, even if injection occurs, the accessible operations and schemas are constrained.
  • Encourage a secure-by-default framework design where low-code/no-code users cannot inadvertently define schemas or workflows that bypass safe query-building mechanisms.

How teams detect issues like this

Organizations can approach detection using a mix of code-centric and behavior-centric techniques:

  • Static analysis:
  • Add rules to flag any SQL assembly that concatenates variables (especially IDs, keys, or user-derived fields) into query strings without parameter binding.
  • Search for custom recursive CTE builders or helper functions that build WITH RECURSIVE statements via string operations.
  • Code review signals:
  • Identify places where primary keys or node identifiers are read from rows and then appended directly into SQL strings.
  • Question any logic that assumes database-stored identifiers are inherently safe and can be inlined into queries.
  • Dynamic and runtime monitoring:
  • Monitor database logs for anomalous queries containing unexpected literal fragments, comments, or control tokens in identifier positions.
  • Use SQL firewall or query normalization tools to detect or block queries that deviate significantly from expected templates.
  • Automated tooling:
  • Integrate SAST tools and secure coding linters into CI pipelines to continuously check for dynamic SQL patterns.
  • Maintain internal policies that prohibit direct SQL concatenation and require the use of vetted query abstractions.

Sources

SourceLink
github.comgithub.com/nocobase/nocobase/commit/202e2b8efe44ba90adbf1087f6f70881ff947604
github.comgithub.com/nocobase/nocobase/pull/9133
github.comgithub.com/nocobase/nocobase/releases/tag/v2.0.39
github.comgithub.com/nocobase/nocobase/security/advisories/GHSA-4948-f92q-f432

Frequently asked

questions

Yes, if an external user or integration can create or influence records whose primary keys are later used by queryParentSQL() during recursive eager loading, the attack can be triggered through normal application requests without direct database access.

The attacker needs the ability to create or modify records in the affected collection—typically through a standard application feature, API endpoint, or sync channel. No direct database authentication is required; the attack runs under the application's existing database credentials.

Replace all dynamic SQL concatenation that incorporates primary keys or node IDs with parameterized queries or strongly-typed query builder calls, and ensure recursive CTE logic relies on bound parameters rather than interpolated strings.

Establish coding standards that forbid direct SQL string concatenation, enforce use of parameterized query utilities, validate identifier formats rigorously, and integrate security-focused code review and static analysis to catch injection patterns before deployment.

Find out what's already
exploitable in your codebase.