{"record":{"id":"96fa3ced4f26ca1b","repo":"appwrite/appwrite","slug":"a-command-must-not-span-lines","errorCode":null,"errorMessage":"A command must not span lines","messagePattern":"A command must not span lines","errorType":"validation","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"packages/smtp/src/Client.php","lineNumber":146,"sourceCode":"        return $result;\n    }\n\n    public function capabilities(): Capabilities\n    {\n        $this->start();\n\n        return $this->capabilities;\n    }\n\n    /**\n     * The escape hatch, so an unmodelled verb is never a reason to fork.\n     *\n     * @param  int|list<int>  $expect\n     */\n    public function command(string $command, int|array $expect): Reply\n    {\n        if (preg_match('/[\\r\\n]/', $command) === 1) {\n            throw new \\InvalidArgumentException('A command must not span lines');\n        }\n\n        return $this->exchange(\n            \\is_array($expect) ? $expect : [$expect],\n            $command . \"\\r\\n\",\n        );\n    }\n\n    /**\n     * Write, then read one reply, keeping the connection only when the server\n     * gave an answer.\n     *\n     * A 4yz or 5yz is an answer: the session continues and the caller decides.\n     * A socket that failed, or a reply that cannot be parsed, means the stream\n     * is dead or out of step with us — and a client that stayed ready would\n     * send its next MAIL FROM into that.\n     *\n     * @param  list<int>  $expect","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/appwrite/appwrite/blob/7eab5f1970bacb06f8cce069c27b7d14a4178153/packages/smtp/src/Client.php#L128-L164","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Build commands from validated Address/Envelope objects instead of string concatenation — the higher-level API validates for you.","Strip or reject \\r and \\n in any interpolated value before calling command(): str_replace([\"\\r\", \"\\n\"], '', $value).","Treat the throw as an injection attempt when the input came from a user; reject the request.","Catch InvalidArgumentException around low-level client use if input is not fully trusted."],"exampleFix":"// before\n$client->command(\"MAIL FROM:<$from>\", 250);\n// after\n$from = str_replace([\"\\r\", \"\\n\"], '', $from);\n$client->command(\"MAIL FROM:<$from>\", 250);","handlingStrategy":"validation","validationCode":"if (strpbrk($command, \"\\r\\n\") !== false) {\n    throw new ValueError('SMTP command contains line breaks');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $client->command($cmd, $expect);\n} catch (\\InvalidArgumentException $e) {\n    // log and abort the transaction\n}","preventionTips":["Prefer the high-level Message/Envelope API over hand-built commands","Sanitize every value interpolated into a command string","Never pass raw user input into low-level Client methods","Log offending commands — line breaks usually indicate injection attempts"],"tags":["php","smtp","security","injection"],"backgroundTag":"header-injection-detected","analyzedSha":"7eab5f1970bacb06f8cce069c27b7d14a4178153","analyzedAt":"2026-09-23T00:38:38.905Z","contentChangedAt":"2026-09-23T00:38:38.905Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}