Files
BARANTMail/SMTPSyntax.h
T
barantandClaude Opus 5.5 d3d7e5f3e5 SMTP: STARTTLS, smuggling fix, and RFC 5321 conformance
Security:
- SMTP smuggling (CVE-2023-51764 family): only <CRLF>.<CRLF> ends DATA
  (strict CRLF framing); bare CR/LF in the text is stored as CR LF.
  Before, '<LF>.<LF>' and four other variants ended the message and the
  rest ran as fresh commands - a second, forged message was delivered.
- STARTTLS on port 25 (RFC 3207), using the configured tls cert; AUTH
  is only offered/accepted once encrypted (538 5.7.11 otherwise).
- The 250 reply no longer discloses the queue file's full path.
- Queue file names %-encode the addresses ('/' or '|' in an address
  broke the name or pointed it into another directory).

Conformance (RFC 5321, 1870, 2034, 2920, 3207, 3463, 3848, 4616, 4954,
6152):
- 500 for unknown/empty/over-long commands (no reply at all before -
  clients hung); 501 for missing/invalid arguments (bare MAIL/RCPT/HELO
  were out-of-bounds reads).
- HELO/EHLO required before MAIL; nested MAIL refused; EHLO/HELO/RSET/
  end-of-DATA reset the transaction.
- MAIL: SIZE= (552 when over the limit), BODY=7BIT|8BITMIME, AUTH=;
  555 for anything else; case-insensitive keywords; address syntax
  checked.
- RCPT: postmaster (bare and any case) accepted; case-insensitive
  domain and local-part; 550 5.1.1 unknown user vs 550 5.7.1 relay
  denied; aliases accepted (they were rejected at RCPT before); 1000
  recipient cap (452 4.5.3).
- ENHANCEDSTATUSCODES advertised and used on every reply.
- AUTH PLAIN added alongside LOGIN; strict base64; 535 5.7.8 on
  failure; 503 before EHLO, during a transaction, or when already
  authenticated (the last one sent no reply before).
- Received: per RFC 5321 4.4 with client IP, protocol per RFC 3848
  (ESMTP/ESMTPS/ESMTPSA) and queue id; Return-Path: added at local
  delivery.

tests/smtp_conformance.py: 112 checks. origin/master passes 37 of the
72 it can reach before the server stops answering; this passes 112/112.
tests/imap_pop3_regression.py: IMAPS/POP3S unaffected (3/3).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0121tAhQVe4qbCwYgVjKQS4B
2026-11-02 11:56:41 -08:00

104 lines
3.5 KiB
C++

#ifndef __SMTPSyntax_h__
# define __SMTPSyntax_h__
# include <string>
# include <utility>
# include <vector>
namespace mail {
///
/// SMTPSyntax
///
/// Parsing and validation shared by the SMTP command handlers, kept in
/// one place so every command applies the same grammar (RFC 5321
/// 4.1.2) rather than each handler doing its own partial version.
/// Pure functions - no server or session state.
///
namespace SMTPSyntax {
///
/// The parsed argument of "MAIL FROM:<path> params" or
/// "RCPT TO:<path> params". On failure `reply` holds the complete
/// reply line to send (code, enhanced code and text, no CRLF).
///
struct PathArgument {
bool ok = false;
std::string reply;
std::string address; // contents of <...>, may be empty for MAIL FROM:<>
std::vector<std::pair<std::string, std::string>> params; // keyword upper-cased
};
///
/// The argument text after a command verb: everything past the
/// first run of spaces. "" when there is none.
///
std::string argument(const std::string &line);
///
/// Parse the argument of MAIL ("FROM:") or RCPT ("TO:"). The
/// keyword is matched case-insensitively (RFC 5321 2.4); a single
/// space after the colon is tolerated, as most servers do, though
/// the grammar has none. Parameters are checked for esmtp-param
/// syntax only; deciding which ones are supported is the caller's.
///
PathArgument parsePath(const std::string &argument, const std::string &keyword, bool allowEmpty);
///
/// RFC 5321 4.1.2 Mailbox (Local-part "@" ( Domain / address-literal ))
/// with the 4.5.3.1 size limits (local-part 64, domain 255).
///
bool validMailbox(const std::string &address);
///
/// Split "local@domain" at the LAST "@" (a quoted local-part may
/// contain one). Domain is lower-cased - domains are case-insensitive
/// (RFC 5321 2.4) and the mail store keys directories on them.
///
void splitAddress(const std::string &address, std::string &local, std::string &domain);
///
/// True when a local-part/domain can be used as a path component
/// under the mail store without escaping it ("/" and NUL are legal
/// in an RFC 5321 local-part, but not as a directory name, and "."/
/// ".." would walk the tree).
///
bool safePathComponent(const std::string &s);
bool iequals(const std::string &a, const std::string &b);
std::string upper(std::string s);
std::string lower(std::string s);
///
/// Strict RFC 4648 base64 decode, as RFC 4954 4 requires for SASL
/// responses: returns false for any character outside the alphabet,
/// bad padding, or a length that isn't a multiple of 4.
///
bool decodeBase64(const std::string &in, std::string &out);
///
/// The queue file name is "<id>|<recipient>|<sender>" (see
/// __SMTP_DATA::queueMail()). "/" and "|" are both legal in an
/// address, and either one breaks that name - a "/" turns it into
/// a path into some other directory, a "|" shifts the fields - so
/// the address parts are %-encoded ("%", "/", "|") on the way in
/// and decoded by SMTPServer::inCreate() on the way out.
///
std::string encodeQueueField(const std::string &s);
std::string decodeQueueField(const std::string &s);
}
}
#endif