Email header injection: when a contact form picks the recipients
Email header injection (also called SMTP header injection) happens when a web form copies visitor input into the headers of an outgoing email and does not refuse line breaks. A line break ends one header and begins another, so a value meant to be a single name or subject can add recipients or fields the developer never wrote. The form then works as a mail relay for whoever finds it, and the spam leaves from your server.
Quick facts
- What it is
- Visitor input with a line break reaches the header block of an email the application sends
- Weaknesses
- CWE-93 improper neutralization of CRLF sequences; CWE-140 improper neutralization of delimiters
- OWASP test
- WSTG-INJT-10, IMAP/SMTP injection
- OWASP category
- A05:2025 Injection
- Why line breaks matter
- Each header field is a line; a CRLF followed by anything other than a space or tab starts a new field (RFC 5322 §2.2)
- Main fix
- Never build headers by joining strings with visitor input; refuse CR and LF in header fields and let a mail library compose the message
The message format comes from RFC 5322. The weakness names are from the CWE catalogue, and the testing approach from the OWASP Web Security Testing Guide.
How an email message is laid out
An email is plain text in two parts: a block of header fields, then an empty line, then the body. RFC 5322 defines header fields as lines that begin with a field name followed by a colon (section 2.2). To, Cc, Subject and From are fields of this kind. The lines end in CRLF, the same pair of characters that ends header lines in HTTP, which is why this flaw is a close relative of CRLF injection.
Long fields may be wrapped onto further lines. The standard calls this folding: a CRLF is allowed before whitespace, and a CRLF that is immediately followed by a space or tab is removed when the field is read back (section 2.2.3). So the parser has a simple rule. A line break followed by whitespace continues the same field. A line break followed by anything else is the start of the next one.
The same document says that CR and LF must only occur together as CRLF (section 2.3) and sets a limit of 998 characters per line, with 78 recommended (section 2.1.1). A single-line value from a form field, such as a name or a subject, has no legitimate reason to contain a line break at all.
One field is worth knowing by name. Bcc carries the addresses of recipients who are not to be revealed to the others (section 3.6.3). It is a recipient list that the people on the other side never see, which suits a mass mailing and is the reason header injection is so attractive for spam.
How a contact form becomes a mailer
A typical contact form takes a name, an email address, a subject and a message. The server sends a notification to the site owner. To make the owner's reply button work, the code often puts the visitor's address in a Reply-To or From field, and the visitor's subject goes into Subject. These are the places where input becomes a header.
The weak pattern is easy to recognise: the script assembles one long string of header lines by concatenating literals and form values, then hands it to a low-level mail function. Nothing in that string tells the mail system which characters came from the developer and which from the visitor. If a form value holds a line break, the next piece of text is read as a header of its own.
From there the details depend on the sending code. Where it derives the envelope recipients from the To, Cc and Bcc fields, a field added through the form changes who receives the message. Where it does not, the added field can still change the subject, the reply address or the content type. Either way the visitor has written part of a message that your server signs and sends.
The flaw is not limited to the email field; every value that reaches a header counts, including a name used to build a display name.
It belongs to the family of injection flaws in which data is read as structure. Compare command injection, where data is read as a shell command, and LDAP injection, where it changes a directory filter. The fix has the same shape in each case: keep data and structure apart.
What goes wrong
The worst case is the one in the lead: a form that sends one notification becomes a way to send mail to anyone. OWASP's testing guide lists relaying spam among the effects of mail-protocol injection, next to evading application restrictions, bypassing anti-automation controls and leaking information. The same entry notes that the technique matters most when the mail server is not directly reachable from the internet, because the web application is then the only door.
The cost lands on the sender. Messages leave from your server or from the account of your mail provider, so complaints and blocklisting attach to your domain and address range. Legitimate notifications, password resets and invoices can then be filtered along with the spam. A provider that sees an abnormal volume may also suspend the sending account.
Other results are smaller but real. A forged Reply-To can steer the owner's reply to someone else. A changed Subject or content type can make a genuine-looking notification carry text of the attacker's choosing, which feeds phishing.
Automation does the rest. The forms that exist on almost every site are found and tried by spam bots at scale; a vulnerable one is simply reused.
Checking your own forms
Test only forms you own or are authorised to test, and send the results to a mailbox you control. The OWASP guide frames the check as sending unexpected input to the mail-related parameters and watching how the application and the mail system react; a secure application, it says, should return an error and tell the client that something went wrong.
- List every value that reaches a header. Read the code that sends mail: name, address, subject, any custom field. Anything used in
To,Cc,Bcc,From,Reply-ToorSubjectcounts. - Look at what the code does with line breaks. The correct answer is that it rejects them or the library does. If the answer is "nothing", the form is open.
- Read a delivered message in full. View the raw source of a test email and confirm the header block holds only the fields you expect.
- Check sending logs. Unexpected recipients, a jump in volume from one form, or bounces to addresses you never used point to abuse that has already started.
Where a WAF helps and where it does not
A WAF sees the HTTP request that carries the form. It can recognise a line break, usually percent-encoded, inside a parameter that should hold a single line of text, and match it against a rule for that category. It can also see a form being submitted far more often than a person would submit it, and rate limiting on that path slows an abuser down whether or not the form is flawed.
What the filter cannot see is the email. It does not know which form values your code turns into headers, whether the library refuses line breaks, or what your mail server will accept. It matches what is on the request; an encoding it does not decode, or a value that is combined with others on the server, can pass. A rule is also a pattern match and not a repair: a form that builds headers by concatenation stays flawed after the rule is switched on.
The three modes of Bridge WAF fit this the usual way. In off nothing is inspected. In monitor a match is recorded in the activity log and the request is still served, so you can see which requests to the contact form carry encoded line breaks and whether any real visitor does. In block a matching request is refused and still recorded. Start with monitor, read the log for a while, and switch to block only when the matches are what you expected; see WAF monitor mode.
Treat the filter as an extra layer. The fix that actually closes the hole is in the code that sends the mail.
How to prevent email header injection
- Do not assemble headers yourself. Use the mail library or framework mail API that takes each header as a separate argument and composes the message. Libraries of this kind reject or encode line breaks in header values.
- Refuse CR and LF in single-line fields. Reject the submission, with an error the visitor can read, if a name, address or subject contains either character. Rejecting is safer than stripping, because a silent change can hide an attack and can leave a different dangerous sequence behind.
- Validate the address as an address. Accept one address in the expected form, not a list. Check it with the library's parser, not with a hand-written pattern that lets whitespace through.
- Fix the recipient. A contact form should send to an address in your configuration. Never take the recipient, or a copy list, from the request.
- Put visitor text in the body, not in headers. If you want the visitor's name in the message, write it in the body and keep the subject a constant you control. When a header must carry visitor text, escape it as the library documents.
- Limit use of the form. A per-address and per-client limit, a challenge such as CAPTCHA, and a cap on messages per hour make a flawed form a smaller problem and a sound form a smaller target.
- Watch outgoing mail. Alert on volume from the web server and on bounces. The cost of a late discovery is mostly reputation, and reputation is slow to repair.
Questions
What is email header injection?
Email header injection is a flaw in which a line break in visitor input reaches the header block of an outgoing email, so the visitor can add headers such as extra recipients. A contact form with this flaw can be used to send spam.
Why do line breaks matter in email headers?
RFC 5322 defines each header field as a line, and a line break followed by anything other than a space or tab begins a new field. A value that contains a line break can therefore start a header the developer did not write.
How do you prevent email header injection?
Send mail through a library that composes headers from separate arguments instead of joining strings, reject CR and LF in fields that go into headers, take the recipient from configuration, and limit how often the form can be used.
Can a WAF stop email header injection?
A WAF can match encoded line breaks in form parameters and limit how often a form is submitted, but it does not see the email and does not repair code that builds headers from input. The fix belongs in the sending code; use monitor mode first to see what the rule would match.
Is email header injection the same as CRLF injection?
It is a form of it. Both rely on a CRLF sequence in input ending one line and starting another; CRLF injection is the general weakness (CWE-93) and email header injection is the case where the lines are the headers of an email.