Files
LambdaMail/services/protocols/internal/application/usecase/smtp_rejection.go
T
Lucas RayanandClaude Opus 5 33d91fa247 #19: fix F1-F3 defects found auditing the phases against PLAN.md
Every fix here is a case where the code and the plan disagreed, and the
code was wrong.

Certificates (section 8.2, 8.4). The parser documented that it skipped
expired entries but never checked, so an expired certificate would be
served until every handshake failed. LastReload() only advanced when
acme.json changed - and it does not change for sixty days - which made
the timestamp risk R2 monitors indistinguishable from a watcher gone
blind. Expiry is now checked on every pass, LastReload records the check
and LastChange the swap, and the <7d/<24h alerts of section 8.4 exist.

ARC (section 5). buildArcSealer resolved the DKIM key at startup, before
runDnsReconciler had provisioned one. On a first boot ARC was disabled
until someone restarted the process, which the log even admitted while
offering no way for it to start. A LazySealer binds on first use instead.

Health (section 8.4). There was no degraded state at all. /health now
reports one - still 200, because restarting cannot fix a certificate
Traefik never issued.

Bounces. DSNs were sent From postmaster@lambdamail.local, a domain that
does not resolve, so the notification was itself undeliverable. They now
carry the configured mail host, a Message-ID, and Auto-Submitted so a
vacation responder cannot loop against them.

Fan-out atomicity (section 6.1). Persist ran one transaction per
recipient, so a failure halfway through an alias fan-out left earlier
recipients with a copy while the sender was told to retry - and the retry
delivered to them twice. PersistAll commits the whole delivery or none of
it. Verified against Postgres by reverting the repository to the old
behaviour and confirming the new test fails on it.

SMTP replies. The use case encoded replies in error text ("550 5.7.1 ...")
which the adapter re-parsed by prefix, so rewording a message could turn a
permanent refusal into a retry. SmtpRejection carries the decision and the
adapter renders it.

Repository tests. These only run with TEST_DATABASE_URL, which CI does
set - so the job was red: copy and expunge seeded a 68-character digest
into a CHAR(64) column, and three others used fixed digests that collide
with the UNIQUE index on any re-run. A shared testBlobDigest() helper
fixes both; the suite now passes twice in a row against one database.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-02 07:57:22 -03:00

56 lines
2.2 KiB
Go

package usecase
import "fmt"
// SmtpRejection is a use case's decision to refuse a message, carrying the
// reply the SMTP layer must send back.
//
// The decision belongs to the application layer - only it knows that a virus
// is permanent and a greylist is temporary - but the wire format belongs to
// the presentation layer. This type is the seam: the use case names the
// outcome, the adapter renders it. Encoding the reply in the error *text* and
// re-parsing it downstream would make a reworded message silently change the
// code a remote MTA receives, turning a permanent refusal into a retry loop.
type SmtpRejection struct {
// Code is the three-digit reply code (RFC 5321 section 4.2.1).
Code int
// EnhancedCode is the class.subject.detail triplet of RFC 3463.
EnhancedCode [3]int
// Message is the human-readable text, without any code prefix.
Message string
}
func (e *SmtpRejection) Error() string {
return fmt.Sprintf("%d %d.%d.%d %s",
e.Code, e.EnhancedCode[0], e.EnhancedCode[1], e.EnhancedCode[2], e.Message)
}
// IsPermanent reports whether the sender must give up rather than retry.
func (e *SmtpRejection) IsPermanent() bool {
return e.Code >= 500 && e.Code < 600
}
// rejectDmarc refuses a message whose sender publishes p=reject and whose
// DMARC evaluation failed.
func rejectDmarc() *SmtpRejection {
return &SmtpRejection{Code: 550, EnhancedCode: [3]int{5, 7, 1}, Message: "DMARC policy rejects this message"}
}
// rejectVirus refuses an infected message. It is permanent: the same bytes
// will still be infected on a retry.
func rejectVirus(virusName string) *SmtpRejection {
return &SmtpRejection{Code: 554, EnhancedCode: [3]int{5, 7, 1}, Message: "Virus detected: " + virusName}
}
// rejectSpam refuses a message that scored above the reject threshold.
func rejectSpam() *SmtpRejection {
return &SmtpRejection{Code: 554, EnhancedCode: [3]int{5, 7, 1}, Message: "Spam threshold exceeded"}
}
// deferGreylisted asks the sender to try again. It is deliberately temporary:
// greylisting works precisely because a legitimate MTA retries and a spam
// cannon does not.
func deferGreylisted() *SmtpRejection {
return &SmtpRejection{Code: 451, EnhancedCode: [3]int{4, 7, 1}, Message: "Greylisted, please try again later"}
}