/

CVE-2026-40871

Severity:

HIGH RISK

(7.2)

SQL Injection in Mailcow Quarantine Notification Workflow

CVE-2026-40871

Deferred

Vulnerability type

Category:

SQL Injection

CWE:

CWE-20, CWE-89, CWE-116, CWE-564

Impact:

Arbitrary SQL Execution and Data Exfiltration

Severity:

HIGH RISK

(7.2)

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

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/mailbox endpoint to create or modify mailboxes
  • Store the quarantine_category field from API input without strict validation or sanitization
  • Use quarantine_notify.py to 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_category field (e.g., via valid or compromised API credentials, or misconfigured API exposure).
  • Trigger point: Execution of the quarantine_notify.py job, which reads the stored quarantine_category value 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 SELECT queries
  • 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.

Sources

SourceLink
github.comgithub.com/mailcow/mailcow-dockerized/security/advisories/GHSA-r8fq-wrfm-cj2q

Frequently asked

questions

Yes, if the Mailcow API is reachable to an attacker (directly or via a compromised client) and they can supply values for the quarantine_category field, they can plant payloads that later lead to SQL injection when the notification job runs.

Typically some form of access is required, such as valid API credentials, a compromised mailbox account that can indirectly influence the field, or a misconfigured deployment that exposes the API broadly. The exact requirement depends on how the Mailcow API is restricted in a given environment.

Replace all string-formatted SQL with parameterized queries or ORM methods, and treat all stored values used in SQL as untrusted, enforcing strict validation or whitelisting before use.

Adopt a development practice where:

  • All SQL uses bound parameters by default.
  • Input validation is enforced at all trust boundaries, including APIs and background processes.
  • Stored data is re-validated when used in sensitive contexts.
  • Security-focused code reviews and automated checks flag any direct string interpolation in SQL and other interpreter-like sinks.
Find out what's already
exploitable in your codebase.