I have a PHP driven HTML contact form on my site. Currently I use the PHP mail() function. Because of that I have to do many user input validation to avoid email header injection attacks. I think I'm secure, but I probably forgot something and I want to move to a solid PHP email library. The library I selected is Swiftmailer.
Now I want to check if Swiftmailer address the following:
- Removes or escape
<
and>
characters in sender names. - Removes newlines (
\n
,\r\n
...) from sender names. - Removes or escape newlines from email subject.
- Normalize newlines in message body (the content of the email). As per the PHP docs,
\n
should be used in content and\r\n
as email headers separator.
PS: I tried to contact the Swiftmailer team with my questions without success so I'm trying here.
Edit:
I did some test cases with Swiftmailer and this is what I found so far:
- When you have a
<
or>
in the name of a sender, you get a Undeliverable email error mail. This can somewhat lead in a DOS attack of your mail server (maybe I'm wrong). Is this normal?! - The newlines are escaped so the injection attack fails.
- The newlines are escaped so the injection attack fails.
- Tested but I'm unable to see what Swiftmailer do (if it does something). So I'm still in the dark here.
Can someone clarify #1 and #4 for me? I'm not sure if it's normal behavior...
Protect from header injection attacks without stripping request data content
(from the homepage) and from a rough scan through the code I would assume that if you pass in anything that's not valid, you'll get an exception instead of a silent acceptance. – Harhay