Email header injection: when contact forms send mail for attackers
Web forms that build email headers from user input can be abused to add recipients or rewrite messages, turning your server into a spam relay. How CRLF injection into headers works, and how mail libraries and validation stop it.
Diagram
A contact form that trusts its input
Step 1 of 4
Form
The site emails the support team with the visitor’s address as Reply-To.
# Form # The site emails the support team with the visitor’s # address as Reply-To.
The example on the open step is one illustration, or the fix you can paste into an application you own.
What this page is made of
3 pieces. Each one says what fails, who is affected, what you would notice, and what you change.
Header concatenation
- What fails
- Building headers as strings lets newlines create new headers.
- Who is affected
- Your domain’s reputation and recipients of spam.
- What you would notice
- mail() or SMTP calls with string-built headers.
- What you change
- Use structured message APIs.
Open relays by form
- What fails
- Forms that let users pick recipients send mail anywhere.
- Who is affected
- Everyone receiving spam from your server.
- What you would notice
- A To or Bcc field taken from the request.
- What you change
- Fix recipients server-side.
Rate limits
- What fails
- Even a safe form can be abused for volume.
- Who is affected
- Your support inbox and mail reputation.
- What you would notice
- Bursts of form submissions.
- What you change
- Rate-limit and add bot protection.
Testing your own form
In a staging environment, submit an address containing an encoded newline and a harmless extra header such as X-Test: 1, then check the received message headers. If X-Test appears, the form is vulnerable.
Check yourself
Recognition and the fix. Nothing here is a procedure.
Which characters make header injection possible?
Sibling techniques
Pages at the same depth, plus a lab and a glossary card for this one.