Summary
A SQL Injection vulnerability in Kestra's authenticated flow search endpoint allows crafted queries to be injected into PostgreSQL.
When exploited, PostgreSQL can be coerced into executing arbitrary OS commands via COPY ... TO PROGRAM ..., resulting in remote code execution on the host running the database.
Affected context
This vulnerability affects Kestra deployments, particularly those using the default docker-compose setup where Kestra is backed by PostgreSQL and the /api/v1/main/flows/search endpoint is exposed to authenticated users.
The issue arises when user-controlled search parameters are passed into dynamically constructed SQL queries without robust parameterization or sanitization.
Any environment where the database user has permission to invoke COPY ... TO PROGRAM ... is at elevated risk, as this capability allows escalation from data-layer compromise to code execution on the underlying host.
Exploit preconditions
- Attack surface: HTTP API endpoint
GET /api/v1/main/flows/search. - Required access: Valid authenticated Kestra user account with permission to access the flow search functionality.
- User interaction: Visiting a specially crafted link or triggering a request containing malicious search input.
- Database configuration: PostgreSQL in use, with a database role that has privileges to execute
COPY ... TO PROGRAM .... - Why exploitation is possible: Untrusted input from request parameters is incorporated into SQL statements in a way that alters the structure of the query, allowing attackers to append
COPY ... TO PROGRAM ...directives and execute arbitrary OS commands.
Why this happens
The underlying bug is classic SQL Injection (CWE-89).
Application logic responsible for searching flows accepts user-supplied criteria and embeds them into SQL queries in a dynamically concatenated form rather than using strictly parameterized statements or safe query builders.
As a result, an attacker-controlled value can break out of the intended data context and inject additional SQL syntax, including dangerous PostgreSQL constructs like COPY ... TO PROGRAM ....
This pattern reflects insufficient separation between data and control in database interactions, as well as overprivileged database roles that can invoke operating system commands.
Potential impact
- Execution of arbitrary OS commands on the host running PostgreSQL.
- Full compromise of the Kestra application data stored in the database (read, modify, and delete).
- Lateral movement to other services or hosts accessible from the compromised database server.
- Installation of backdoors or malware on the host system.
- Exfiltration or tampering with workflow definitions, execution histories, and secrets stored or referenced by Kestra.
General mitigation guidance
Effective mitigation focuses on eliminating injection opportunities and limiting what the database can do even if an injection occurs:
- Use parameterized queries or an ORM/query builder that does not allow raw string concatenation of SQL fragments with user input.
- Apply strict input validation and canonicalization on search and filter fields, restricting them to well-defined patterns (e.g., whitelisted columns, operators, and formats).
- Avoid exposing powerful database features like
COPY ... TO PROGRAM ...to application-level roles; apply least privilege to database accounts. - Separate data filtering logic (search parameters) from query construction logic so that user input can never influence SQL structure, only bound values.
- Introduce defense-in-depth measures such as database-level security policies, restricted OS-level permissions for the database user, and hardened container/host configurations to limit the blast radius of a compromise.
How teams detect issues like this
Organizations can detect and monitor this class of vulnerability and its exploitation using a combination of code-centric and operational approaches:
- Static application security testing (SAST): Scan source code for dynamic SQL string construction that interpolates HTTP parameters or request fields directly, especially around search and filtering logic.
- Code review practices: Flag any use of string concatenation to build SQL, and require developers to justify or refactor those patterns into parameterized forms.
- Database monitoring: Enable logging of all SQL statements and watch for suspicious patterns such as
COPY ... TO PROGRAM ..., unexpected shell commands, or unusual bulk operations. - Web and API monitoring: Use web application firewalls or API gateways with rules that detect common SQL Injection signatures in query parameters and URLs.
- Policy enforcement in CI/CD: Integrate automated tools that block builds or deployments when insecure SQL construction patterns are detected, or when database permissions exceed predefined least-privilege baselines.

