appwrite/appwrite · error · InvalidArgumentException

A command must not span lines

Error message

A command must not span lines

What it means

SMTP commands are sent as a single CRLF-terminated line on the wire; a CR or LF inside the command would terminate it early or smuggle extra commands (SMTP injection). Client::command() therefore throws this InvalidArgumentException when the command string contains \r or \n.

Solutions

  1. Build commands from validated Address/Envelope objects instead of string concatenation — the higher-level API validates for you.
  2. Strip or reject \r and \n in any interpolated value before calling command(): str_replace(["\r", "\n"], '', $value).
  3. Treat the throw as an injection attempt when the input came from a user; reject the request.
  4. Catch InvalidArgumentException around low-level client use if input is not fully trusted.

Example fix

// before
$client->command("MAIL FROM:<$from>", 250);
// after
$from = str_replace(["\r", "\n"], '', $from);
$client->command("MAIL FROM:<$from>", 250);
Defensive patterns

Strategy: validation

Validate before calling

if (strpbrk($command, "\r\n") !== false) {
    throw new ValueError('SMTP command contains line breaks');
}

Try / catch

try {
    $client->command($cmd, $expect);
} catch (\InvalidArgumentException $e) {
    // log and abort the transaction
}

Prevention

When it happens

Trigger: Calling low-level Client methods (hello, sendRaw, etc.) with a command assembled from untrusted input containing newlines; manually building commands like "MAIL FROM:<$sender>" where $sender contains \r\n; injecting via sendRaw with multi-line payload.

Common situations: Using the raw Client instead of Envelope/Message APIs and concatenating user-supplied addresses or parameters into commands; log/debug code that appends newlines; exploitation attempts passing CRLF in email fields.

Related errors


AI-assisted analysis of appwrite/appwrite@7eab5f1970 (2026-09-23). Data as JSON: /api/errors/96fa3ced4f26ca1b. Report an issue: GitHub.

Appendix: source

Thrown at packages/smtp/src/Client.php:146

        return $result;
    }

    public function capabilities(): Capabilities
    {
        $this->start();

        return $this->capabilities;
    }

    /**
     * The escape hatch, so an unmodelled verb is never a reason to fork.
     *
     * @param  int|list<int>  $expect
     */
    public function command(string $command, int|array $expect): Reply
    {
        if (preg_match('/[\r\n]/', $command) === 1) {
            throw new \InvalidArgumentException('A command must not span lines');
        }

        return $this->exchange(
            \is_array($expect) ? $expect : [$expect],
            $command . "\r\n",
        );
    }

    /**
     * Write, then read one reply, keeping the connection only when the server
     * gave an answer.
     *
     * A 4yz or 5yz is an answer: the session continues and the caller decides.
     * A socket that failed, or a reply that cannot be parsed, means the stream
     * is dead or out of step with us — and a client that stayed ready would
     * send its next MAIL FROM into that.
     *
     * @param  list<int>  $expect

View on GitHub (pinned to 7eab5f1970)