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
This commit is contained in:
barantandClaude Opus 5.5 authored and brad Arant committed 2026-11-02 11:56:41 -08:00
1 parent 407cc2e915
commit d3d7e5f3e5
27 files changed
+1821 -358

No files matched your search

+25 -16
View File
@@ -1,34 +1,43 @@
#include "__SMTP_EHLO.h"
#include "SMTPServer.h"
#include "SMTPSyntax.h"
namespace mail {
int __SMTP_EHLO::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
// RFC 5321 4.1.1.1 requires a domain/address-literal argument -
// request[1] on a bare "EHLO" with none would have been an
// out-of-bounds read on the split list (ZString::operator[] does
// no bounds checking), not a graceful rejection.
if (request.getList().size() < 2) {
session.out << "501 Syntax error: EHLO requires a domain/address-literal argument" << CRLF;
// RFC 5321 4.1.1.1 requires a domain/address-literal argument.
std::string arg = SMTPSyntax::argument(line(request));
if (arg.empty()) {
session.out << "501 5.5.4 Syntax: EHLO hostname" << CRLF;
return 1;
}
session.clientDomainName = request[1];
// EHLO also aborts any transaction in progress (RFC 5321 4.1.4).
session.greeted = true;
session.esmtp = true;
session.resetTransaction();
session.clientDomainName = arg.substr(0, arg.find(' ')).c_str();
session.out << "250-" << server.hostName << CRLF;
// Both already true of this implementation, not just advertised
// hopefully: multiple pipelined command lines arriving in one TCP
// segment are already handled correctly (TCPSocket::onDataReceived
// loops over every complete line it has buffered before returning,
// not just the first), and message bodies are stored byte-for-byte
// with no 7-bit stripping anywhere in this codebase.
// Multiple pipelined command lines arriving in one read are
// processed in order (TCPSocket::onDataReceived loops over every
// complete line it has), and replies go out in the same order.
session.out << "250-PIPELINING" << CRLF;
// Message bodies are stored byte-for-byte; no 7-bit stripping.
session.out << "250-8BITMIME" << CRLF;
// Enforced by __SMTP_DATA.cpp while accumulating the body.
// Declared in MAIL FROM (SIZE=) and enforced while receiving DATA.
session.out << "250-SIZE " << (long)SMTPServer::maxMessageSize << CRLF;
session.out << "250-AUTH LOGIN" << CRLF;
// Every 2xx/4xx/5xx reply after this one carries a RFC 3463 code.
session.out << "250-ENHANCEDSTATUSCODES" << CRLF;
// RFC 3207 4.2: only while the session is still in clear text.
if (server.startTLSContext && !session.isTLS())
session.out << "250-STARTTLS" << CRLF;
// RFC 4954 4/14: no plain-text password mechanisms over an
// unencrypted connection when encryption is available.
if (server.authAllowed(session))
session.out << "250-AUTH PLAIN LOGIN" << CRLF;
session.out << "250 HELP" << CRLF;
session.state = READY;
return 1;
}