Repository navigation
fix: reject CR/LF/NUL in addresses, header names and attachment metadata - #6
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Address(email and name), thesetFrom()return path and every recipient list entry now throwInvalidAddressExceptionfor a CR, LF or NUL, regardless of thevalidateoption. Leading/trailing line breaks are still trimmed and a recipient string is still split on whitespace, so existing inputs keep working.setHeader()throwsMailerExceptionfor a header name containing CR/LF/NUL.AttachmentException;userAgentandcharsetoptions throwConfigurationException.SmtpTransportrefuses an unsafe recipient before connecting and refuses to write an SMTP command containing CR/LF/NUL.CHANGELOG.mddescribe the new behaviour.Header values and subject keep stripping
setHeader()values (and the subject, viaQEncoder) 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 analysevendor/bin/php-cs-fixer fix --dry-run --diffcomposer validate --strictwiki-sync.sh --check docs