Summary
A database query injection flaw in Strapi's Content-Type Builder write API allowed authenticated administrators to inject arbitrary database statements through the column.defaultTo attribute during content-type creation or modification.
By marking values as raw and bypassing sanitization, attackers could execute arbitrary SQL at the database layer, which, depending on the database engine, could lead to file access, denial of service, or even remote code execution on the database server.
Affected context
This vulnerability affects Strapi installations using:
- Version 4.x prior to 4.26.1
- Version 5.x prior to 5.33.2
where:
- The Content-Type Builder write APIs are exposed and usable (typically in development or misconfigured production environments).
- Administrators can create or modify content types through the admin panel or programmatic calls.
- Schema migrations are handled by Knex, and the
defaultToconfiguration in content-type definitions is propagated into migration logic.
The issue manifests when defaultTo values are allowed to be expressed as raw tuples, leading to unvalidated data being passed into low-level database APIs during schema migration operations.
Exploit preconditions
- Attack surface: Strapi Content-Type Builder write endpoints (e.g.,
/content-type-builder/content-typesand related APIs). - Required access: Authenticated administrator account or any role with permission to create or edit content types via the Content-Type Builder.
- Trigger point: Creation or modification of a content type field whose
column.defaultTois set to a tuple of the form[value, { isRaw: true }], thereby flagging the value as raw SQL. - Why exploitation is possible: The migration logic forwards the raw
defaultTovalue directly into Knex'sdb.connection.raw()without sanitization or parameterization. This allows attacker-supplied SQL fragments to be executed as-is by the database engine during schema migration, effectively bypassing query construction safeguards.
Why this happens
The root cause lies in treating configuration metadata for schema definition as fully trusted and allowing it to opt into raw query execution.
Specifically, the column.defaultTo attribute accepts a tuple form that includes { isRaw: true }, which signals that the associated value should be treated as raw SQL. In the vulnerable design, this value is passed straight into Knex's db.connection.raw() during schema migration. Because the Content-Type Builder write API is exposed to authenticated administrators, any administrator with sufficient permissions can inject arbitrary SQL statements into this field.
This aligns with CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), as user-controlled input is concatenated or embedded directly into SQL commands without proper neutralization, binding, or validation. The bug is fundamentally an injection-prone API design combined with over-trusting configuration data, rather than enforcing strict schema or parameterized patterns for all database interactions.
Potential impact
- Arbitrary SQL statement execution against the application database.
- Unauthorized reading of sensitive data stored in the database.
- Arbitrary file read on database servers that provide file-access utilities (e.g., loading files through SQL functions).
- Denial of service by causing schema migration errors that crash or destabilize the Strapi process.
- Escalation to remote code execution on database servers where SQL functions permit external program execution or shell access.
- Data corruption or destructive operations such as dropping tables, truncating data, or altering schemas in unexpected ways.
General mitigation guidance
Effective mitigation centers on ensuring that schema and configuration inputs cannot carry executable SQL and that risky endpoints are not exposed in production environments.
Common defensive patterns include:
- Eliminate raw SQL configuration paths: Avoid supporting features that allow configuration fields (like
defaultTo) to pass raw SQL directly into low-level database APIs. If raw access is unavoidable, confine it to strongly trusted, offline, or code-only contexts rather than network-exposed administrative interfaces. - Enforce strict validation and schema constraints: Treat content-type definitions as untrusted input even from administrators. Apply strong server-side validation, only allowing safe data types and disallowing markers such as
{ isRaw: true }or any indication of raw query execution. - Use parameterized and templated query builders: Structure database interactions so that all dynamic values are added via parameter binding or safe query builder methods, never concatenated into raw strings.
- Restrict powerful admin capabilities in production: Limit access to content-type creation or modification APIs in production deployments, or constrain them to development environments. Employ role-based access control and separate "schema designer" privileges from day-to-day admin roles where possible.
- Harden database environments: Configure the database to restrict dangerous utilities (file-system access, external program execution) and ensure it operates under least-privilege OS accounts to reduce the blast radius of any successful injection.
How teams detect issues like this
Detection focuses on identifying unsafe usage of raw SQL paths in schema and migration code, and monitoring for anomalous schema changes:
- Static analysis and code review:
- Flag calls to raw database APIs (e.g.,
db.connection.raw(),raw(), or equivalent) where any part of the input derives from user/admin-controlled configuration such as content-type definitions, migration metadata, or JSON config. - Look for patterns where
defaultTo, column definitions, or field attributes accept tuples or objects that can signal "raw" behavior or bypass sanitization layers. - Enforce architectural guidelines that restrict raw SQL APIs to whitelisted modules or internal-only utilities, and require code review sign-off for any new raw query usage.
- Configuration and policy enforcement:
- Implement CI/CD checks that prohibit or warn on the presence of
{ isRaw: true }or comparable flags in configuration or migration code that can be updated through HTTP APIs. - Enforce environment-based policies so that content-type builder write APIs are inaccessible in production or restricted to internal networks.
- Runtime monitoring:
- Enable database auditing or logging to detect unexpected schema modifications, unusual use of file-access functions, or execution of rarely used administrative SQL functions.
- Correlate admin actions (content-type changes) with downstream database operations to spot suspicious patterns, such as complex statements appearing in default values or migrations.
- Automated security tooling:
- Integrate security-focused linters or SAST tools that recognize SQL injection risk patterns in Node.js and Strapi applications, particularly around migration and schema-builder code.

