2

I've found myself working on another developer's code. By his own admission he's just a beginner so I want to give solid advice.

I came across this:

<?php

$headerFields = array(
        "From: ".$_POST[...],
        "MIME-Version: 1.0",
        "Content-Type: text/html;charset=utf-8"
    );

    mail("submissions@[...].com","Submission to [...] from " . $_POST[...] . "","New submission to [...]

    Name*: " . $_POST[...] . " [...big-snip...]",
    implode("\r\n", $headerFields)
    );

(edited heavily, obviously)

I tend to not use mail() very often but my instinct is to sanitise everything with extreme prejudice. Is there any specific danger is leaving this as is or could someone of sufficient skill do some damage?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

2 Answers2

3

Yes you are correct that every input from users need to be sanitized and validated.

Golden rule: "Never trust your user"

One issue is that a non technical user might put anything in the name. for example they input email instead of name, we don't know what kind and type of user we can encounter. some user will input dots or even spaces even if there's a label that only alphanumeric is acceptable. This can cause data integrity issues. based on my experience(learned from hard way), i encountered a user that put N/A in a field he/she don't know instead of leaving it blank and it caused a trouble. so validation is a MUST.

Another issue is that, html is allowed in emails. a user that has programming background can put a link in that and the receiver might click it and may lead to a malicious website. Also what if i put there(replacing the $_POST). It will ruin your header fields and might cause problem.:

$headerFields = array(
    "From: ". "\r\n FROM: DERP\r\n TO: IWANTTODESTROYTHEWORLD\r\n WOW: ASDASD\r\n MIME-Version: wow",
    "MIME-Version: 1.0",
    "Content-Type: text/html;charset=utf-8"
);

Above example is not so dangerous, will/might yield only errors but some user might have that extra capabilities to inject malicious codes so clean it up and having a proper validation is a must

Ceeee
  • 1,392
  • 2
  • 15
  • 32
1

Someone of sufficient skill could inject some malicious code in the posted fields and have it executed. Ie: modified headers.

http://securephpwiki.com/index.php/Email_Injection

Concerning pure PHP injection, my quick search couldn't find an example with mail(), but it doesn't mean it is impossible.

Lambic
  • 145
  • 6