Skip to content

fix: reject CR/LF/NUL in addresses, header names and attachment metadata - #6

Merged
muhammetsafak merged 5 commits into
mainfrom
fix/reject-header-injection-chars
Nov 24, 2026
Merged

muhammetsafak merged 5 commits into
mainfrom
fix/reject-header-injection-chars

Conversation

@muhammetsafak

Copy link
Copy Markdown
Contributor

Summary

  • Address (email and name), the setFrom() return path and every recipient list entry now throw InvalidAddressException for a CR, LF or NUL, regardless of the validate option. Leading/trailing line breaks are still trimmed and a recipient string is still split on whitespace, so existing inputs keep working.
  • setHeader() throws MailerException for a header name containing CR/LF/NUL.
  • Attachment name, disposition and MIME type throw AttachmentException; userAgent and charset options throw ConfigurationException.
  • SmtpTransport refuses an unsafe recipient before connecting and refuses to write an SMTP command containing CR/LF/NUL.
  • Docs (API reference, Error Handling, FAQ, Sending Mail, Configuration, Attachments, Encoding & Headers) and CHANGELOG.md describe the new behaviour.

Header values and subject keep stripping

setHeader() values (and the subject, via QEncoder) already had CR/LF stripped, which is documented and safe. They are not changed to throw, because a value with a trailing newline (for example from a template) would start failing; NUL is now stripped as well. Only fields that were previously passed through unsanitised are rejected.

Test plan

  • vendor/bin/phpunit (131 tests; 35 of the new ones fail without the fix)
  • vendor/bin/phpstan analyse
  • vendor/bin/php-cs-fixer fix --dry-run --diff
  • composer validate --strict
  • wiki-sync.sh --check docs

Turning validation off also removed the only line-break check, so a
request-supplied address could add headers or SMTP commands. Address,
setFrom() return paths, recipient lists, attachment names, dispositions and
MIME types, header names and the userAgent/charset options now refuse CR, LF
and NUL regardless of the validate option. SmtpTransport also refuses them in
recipients and commands before writing to the socket.

setHeader() values and the subject keep their documented stripping and now
drop NUL too, so existing callers do not start throwing.
A failure anywhere in an SMTP send (greeting, EHLO, STARTTLS, TLS, AUTH,
recipients, DATA) now closes the connection and clears the authenticated
flag, so the next send cannot skip STARTTLS and write AUTH in clear text.
enableCrypto() refuses to start TLS with buffered clear-text bytes.

Also: Message-ID is built from the parsed sender; angle brackets in an
address are rejected; reserved headers win regardless of case and invalid
header names throw; setAltMessage() strips CR; dot-stuffing handles bare CR
newlines; attachment names are quoted safely and the Content-ID is
sanitised; QEncoder copes with invalid UTF-8 and non-UTF-8 charsets;
HeaderRenderer and the mail()/sendmail transports refuse injectable
PreparedEmail fields; MIME boundaries from the factory are checked.
The reserved-header filter dropped a custom Cc, Bcc, To or Subject even when
no CC/BCC recipients were set, so setHeader('Bcc', ...) silently lost its
copy. A custom header now loses only to a header the library writes for the
message (case and surrounding whitespace ignored): the Mailer drops it
against the headers it builds, and the transports against the ones they add
(To, Subject, Bcc). Sendmail and SMTP output is compared with 3.0.0 in tests.

Also xtext-encode the ORCPT address when DSN is on, test that a failed send
after a successful AUTH forces a new AUTH, test the BCC address check of the
mail()/sendmail transports, and drop the SMTP command() guard that no path
can reach any more.
The transports dropped a custom To unconditionally, so a message with only
BCC or CC recipients lost setHeader('To', 'undisclosed-recipients:;'), which
3.0.0 kept (mail()) or, for the exact name on sendmail and SMTP, overwrote with
an empty value. To is now dropped only when the message has To recipients, and
an empty To no longer overwrites a custom one. The Mailer decides it for the
whole message, so BCC batches after the first do not get the custom To back.

A matrix test covers To/Cc/Bcc, library writing or not, exact and lower-case
names, on mail(), sendmail and SMTP, against the output recorded from 3.0.0.
The docs now say which transports read recipients from hand-set Cc/Bcc headers.
…l() Subject drop

Sending-Mail and the changelog now name To next to Cc and Bcc as headers that
mail() and sendmail -t read recipients from, and Transports.md no longer says
Bcc never appears in the SMTP headers (only setBCC() recipients do not). A new
test covers dropping a custom Subject header in MailTransport.
@muhammetsafak
muhammetsafak merged commit 04e2e30 into main Nov 24, 2026
9 checks passed
@muhammetsafak
muhammetsafak deleted the fix/reject-header-injection-chars branch November 24, 2026 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant