Summary
directorytree/imapengine versions before 1.22.3 are vulnerable to IMAP command injection because user-controlled data is inserted into IMAP ID commands without proper escaping or quoting.
An attacker who can influence the value passed to the id() function can inject additional IMAP syntax, allowing them to execute arbitrary IMAP commands against a victim's mailbox, including reading or deleting emails and terminating sessions.
Affected context
The vulnerability appears in PHP applications that use directorytree/imapengine (pre-1.22.3) to communicate with IMAP servers and pass externally influenced values into the ImapConnection::id() function.
It becomes relevant when:
- The application builds IMAP ID metadata using user-controlled or indirectly user-controlled values (e.g., headers, client info, request parameters), and
- Those values are sent directly to the IMAP server as part of an ID command without strict encoding or a safe formatting routine.
In such scenarios, quote characters and CRLF sequences inside the value can prematurely terminate fields and append attacker-chosen IMAP commands.
Exploit preconditions
- Attack surface: Application features that call the id() function or otherwise construct IMAP ID commands using data derived from HTTP requests, session attributes, device identifiers, or other user-controllable inputs.
- Required access: Ability to submit or influence the data passed into the ID command (e.g., via web forms, headers, query parameters, or API payloads) in the same logical context as the victim's session.
- Trigger point: Invocation of ImapConnection::id() (or equivalent wrapper) during IMAP session setup or capability negotiation, where the crafted value is included in the ID command string.
- Why exploitation is possible: The implementation concatenates raw, unescaped user input into an IMAP command, trusting that it will remain a single value. Special characters such as double quotes and CRLF sequences are not neutralized, allowing the injected data to break out of the intended parameter and introduce new IMAP directives.
Why this happens
This issue stems from improper neutralization of special elements used in a downstream IMAP command context, corresponding to CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection').
Instead of treating user input as opaque data, the vulnerable code interpolates it directly into an IMAP ID command string:
- The ID command expects well-formed, quoted fields.
- User input containing double quotes or CRLF (
\r\n) characters can prematurely terminate a field and inject arbitrary protocol elements.
The underlying bug class is command/protocol injection: a data value is handled as if it were trusted IMAP syntax, with no consistent encoding, quoting, or validation to keep it within its intended field.
Potential impact
- Attackers could execute arbitrary IMAP commands within the context of the victim's authenticated session.
- Attackers may read messages from the victim's mailbox, including sensitive content.
- Attackers may delete or move emails, affecting integrity and availability of mailbox data.
- Attackers could terminate the victim's IMAP session, causing denial of service to legitimate users.
- Attackers may alter mailbox state (flags, folders, subscriptions) through injected commands.
General mitigation guidance
Effective mitigation focuses on preventing untrusted data from being interpreted as IMAP syntax:
- Constrain the data used in IMAP ID commands to a fixed, server-side–controlled set of values (e.g., static product name/version, whitelisted fields), avoiding direct reliance on user-provided input.
- Apply strict encoding or escaping routines when interpolating dynamic values into IMAP commands so that special characters are always treated as literal data, not protocol delimiters.
- Validate input before use: enforce conservative character sets (e.g., alphanumeric plus a small set of safe punctuation) for any value that might flow into IMAP protocol fields.
- Centralize IMAP command construction behind a small, audited API that builds commands from tokens and data values rather than string concatenation.
- Run mail access components with least privilege, limiting the blast radius of a compromised mailbox or abused IMAP session.
How teams detect issues like this
Potential detection approaches include:
- Static analysis: Scan for string concatenation or interpolation that builds IMAP commands from untrusted sources, especially around the id() method or related IMAP APIs. Flag any use of user-controlled data that is not passed through a dedicated encoder or validator.
- Code review: Examine IMAP integration code for:
- Direct construction of protocol lines using variables derived from HTTP requests, cookies, or headers.
- Lack of a shared, well-documented escaping function for IMAP fields.
- Broader patterns where user input influences low-level protocol commands.
- Logging and monitoring: Instrument IMAP access layers to log abnormal or malformed ID command payloads, including unexpected CRLFs or quoting patterns, and alert on suspicious sequences.
- Policy enforcement: Use pre-commit hooks, CI pipelines, or automated review tools to enforce rules that forbid raw user input in protocol-level command strings and require approved encoding wrappers.

