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.

Lesson 9 of 18 · 10 min read · practice only with authorization

Diagram

A contact form that trusts its input

Follow the path. One example at a time.

Step 1 of 4

Form

The site emails the support team with the visitor’s address as Reply-To.

Illustration: Form
# 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.

  1. 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.
  2. 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.
  3. 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.

1 / 3

Which characters make header injection possible?

Sibling techniques

Pages at the same depth, plus a lab and a glossary card for this one.

  1. MapSocial engineering: the levers attackers pull and the processes that resist themSocial engineering works by using authority, urgency, helpfulness, and familiarity to get people to skip a check.

Practice

  1. LabScope deskDecide whether a situation is in scope, needs a written change, or stops.