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

+46
View File
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
"""Minimal DNS responder for tests: answers every query NXDOMAIN (UDP and TCP on 127.0.0.1:53)."""
import socket, socketserver, struct, threading
def nx(query):
if len(query) < 12:
return None
qid, flags = struct.unpack(">HH", query[:4])
rd = flags & 0x0100
# QR=1, opcode copied, AA=1, RD copied, RA=1, RCODE=3 (NXDOMAIN)
rflags = 0x8000 | (flags & 0x7800) | 0x0400 | rd | 0x0080 | 3
qd = struct.unpack(">H", query[4:6])[0]
# find end of question section
i, n = 12, 0
while n < qd and i < len(query):
while i < len(query) and query[i] != 0:
i += query[i] + 1
i += 5
n += 1
return struct.pack(">HHHHHH", qid, rflags, qd, 0, 0, 0) + query[12:i]
class UDP(socketserver.BaseRequestHandler):
def handle(self):
data, sock = self.request
r = nx(data)
if r: sock.sendto(r, self.client_address)
class TCP(socketserver.BaseRequestHandler):
def handle(self):
hdr = self.request.recv(2)
if len(hdr) < 2: return
ln = struct.unpack(">H", hdr)[0]
data = b""
while len(data) < ln:
c = self.request.recv(ln - len(data))
if not c: return
data += c
r = nx(data)
if r: self.request.sendall(struct.pack(">H", len(r)) + r)
socketserver.ThreadingUDPServer.allow_reuse_address = True
socketserver.ThreadingTCPServer.allow_reuse_address = True
u = socketserver.ThreadingUDPServer(("127.0.0.1", 53), UDP)
t = socketserver.ThreadingTCPServer(("127.0.0.1", 53), TCP)
threading.Thread(target=t.serve_forever, daemon=True).start()
u.serve_forever()