Summary
Mailcow: dockerized contained a second-order SQL injection vulnerability where unvalidated input stored via the API was later interpolated into SQL queries for quarantine notifications.
An attacker able to call the mailbox creation API could plant malicious data that is executed later by a background job, enabling arbitrary SQL execution and exfiltration of sensitive data through notification emails.
Affected context
The vulnerability appears in Mailcow deployments that:
- Expose or use the
/api/v1/add/mailboxendpoint to create or modify mailboxes - Store the
quarantine_categoryfield from API input without strict validation or sanitization - Use
quarantine_notify.pyto send quarantine notification emails based on stored configuration - Construct SQL statements with string formatting instead of parameterized queries in the notification workflow
It is most relevant in environments where untrusted or semi-trusted clients can interact with the Mailcow API.
Exploit preconditions
- Attack surface: Mailcow API (
/api/v1/add/mailbox) and subsequent quarantine notification job. - Required access: Ability to invoke the mailbox-add API with control over the
quarantine_categoryfield (e.g., via valid or compromised API credentials, or misconfigured API exposure). - Trigger point: Execution of the
quarantine_notify.pyjob, which reads the storedquarantine_categoryvalue and concatenates it into SQL queries using%string formatting. - Why exploitation is possible: User-controlled data is persisted without proper validation (CWE-20/CWE-116) and later embedded directly into SQL statements without parameterization (CWE-89, CWE-564), causing a second-order injection when the background job runs.
Why this happens
The core issue is improper construction of SQL queries from previously stored, untrusted data.
The quarantine_category field accepted via the Mailcow API is not validated or sanitized and is stored as-is. Later, quarantine_notify.py builds SQL queries using Python % string interpolation instead of prepared statements or parameterized queries.
This pattern matches CWE-89 (Improper Neutralization of Special Elements used in an SQL Command) and CWE-564 (SQL Injection: Stored Data), where application logic assumes previously saved data is safe and directly concatenates it into SQL.
Contributing factors include insufficient input validation (CWE-20) and lack of robust output encoding or neutralization for use in SQL contexts (CWE-116).
Potential impact
- Execution of arbitrary SQL queries within the Mailcow database context
- Exfiltration of sensitive information, such as admin credentials or mailbox data, via crafted
UNION SELECTqueries - Disclosure of internal configuration or user metadata in quarantine notification emails
- Tampering with mailbox or quarantine records, potentially affecting email delivery or integrity
- Lateral movement within the application by manipulating privilege-related database tables
General mitigation guidance
Effective mitigation focuses on both query construction and data handling patterns:
- Use parameterized SQL queries or an ORM that enforces bound parameters for all database interactions, especially in background jobs and scripts.
- Treat all stored data as untrusted when it re-enters a sensitive context (e.g., SQL, command-line, templates), applying validation appropriate to its intended use.
- Apply strict server-side validation for fields like
quarantine_category, enforcing whitelists or enumerations instead of accepting arbitrary strings. - Normalize and constrain configuration-type fields to predefined sets or IDs instead of free-form text.
- Reduce privileges of the database account used by notification processes, limiting the blast radius if injection occurs.
- Centralize database access logic so that ad-hoc scripts follow the same safe-query patterns as the main application.
How teams detect issues like this
Detection can combine code-focused and behavior-focused approaches:
- Static analysis: Scan for SQL query construction via string concatenation or
%formatting, especially in scripts and background jobs, and flag usage where any part of the query originates from persisted user input. - Code review: Look for business logic that reads user-controlled or database-stored values and uses them directly in SQL, without clear parameter binding or validation. Second-order injection risks often appear in reporting, notification, and maintenance scripts.
- Runtime monitoring: Enable database query logging and inspect for anomalous patterns such as unexpected
UNION SELECT, stacked queries, or uncharacteristic WHERE clauses referencing user-controlled fields. - Security testing: In integration or staging environments, perform tests where benign but distinctive marker values are stored via APIs and later examined in downstream SQL queries, confirming they are safely bound and not interpreted as syntax.
- Policy enforcement: Introduce coding standards and CI checks that prohibit raw SQL string interpolation, requiring parameterization or vetted query builder utilities.

