tags:

views:

475

answers:

2

Hi,

I've created a tellafriend form for a CMS. I need some hidden fields in the form so that I can pass homepage address, link to logo, and the web admin email address. However, the value of the hidden fields is not passed to my mail file. You could also try the form on my website http://www.zoosh.me/tellafriend.php Is there a bug in php or something is wrong with my files? I would really appreciate your help guys.

Thanks, Ovi

<form id="tellafriend" method="post" action="mail.php">
 <fieldset>
  <img id="telllogo" width="170" alt="Logo" src="/perch/resources/1253956138myself-w170.jpg"/>
  <input width="170" type="hidden" alt="Logo" value="/perch/resources/1253956138myself-w170.jpg" name="logo"/>
  <input type="hidden" value="http://www.zoosh.me" name="webaddress"/>
  <ul class="wrapper">
   <li>
    <label class="label" for="yourname">Your Name:</label>
    <input id="yourname" class="text jquery-live-validation-on invalid" type="text" value="" name="yourname"/>
    <img alt="Invalid" src="images/invalid.png"/>
   </li>
   <li>
    <label for="youremail">Your Email:</label>
    <input id="youremail" class="text jquery-live-validation-on invalid" type="text" value="" name="youremail"/>
    <img alt="Invalid" src="images/invalid.png"/>
   </li>
   <li>
    <label for="friendsname">Friend's Name:</label>
    <input id="friendsname" class="text jquery-live-validation-on invalid" type="text" value="" name="friendsname"/>
    <img alt="Invalid" src="images/invalid.png"/>
   </li>
   <li>
    <label for="friendsemail">Friend's Email:</label>
    <input id="friendsemail" class="text jquery-live-validation-on invalid" type="text" value="" name="friendsemail"/>
    <img alt="Invalid" src="images/invalid.png"/>
   </li>
   <li>
    <label for="message">
     Your Message
     <br/>
     <small id="charLeft">150 Characters left</small>
    </label>
    <textarea id="message" class="jquery-live-validation-on invalid" cols="10" rows="3" name="message"/>
    <img alt="Invalid" src="images/invalid.png"/>
   </li>
   <li class="inputSubmit">
    <input id="submit" class="submit" type="submit" value="Send"/>
   </li>
  </ul>
  <input type="hidden" value="[email protected]" name="adminaddress"/>
 </fieldset>
</form>

Here is the code for the mail.php file that processes the form and sends an email to my visitor's friends.

<?php
$yourname = $_POST['yourname'];
$youremail = $_POST['youremail'];
$news = $_POST['news'];
$friendsname = $_POST['friendsname'];
$friendsemail = $_POST['friendsemail'];
$adminemail = $_POST['adminemail'];
$logo = $_POST['logo'];
$webaddress = $_POST['webaddress'];
$subject = "I've found a great website!";
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "Reply-To: " . strip_tags($from) . "\r\n";
$headers .= "BCC: [email protected]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = $_POST['message'];


$body="<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title>Zoosh</title>
</head>
<body>
<table width='90%' cellpadding='0' cellspacing='0'>
<tr>
<td align='center' valign='top'>
 <table width='411' cellpadding='0' cellspacing='0'>
  <tr>
   <td><img src='http://recycledoc.com/emails/zoosh_tellafriend/tdbg.png' width='1' height='450' alt='Tdbg'></td>
   <td background='http://recycledoc.com/emails/zoosh_tellafriend/tellafriendbg.jpg' valign='top' style='padding-top:20px; padding-right:20px; padding-bottom:20px; padding-left:20px;'>
    <table width='370' cellpadding='0' cellspacing='0'>
     <tr>
      <td valign='top' width='170' style='padding-right:10px'><img src='"
      . $webaddress . $logo . "' />
      </td>
      <td valign='top' width='190' style='font-family:Helvetica,Arial,Verdana,sans-serif; font-size:12px; color:#555;'>
       <p style='margin-top:0; margin-bottom:0;'>
        <span style='font-weight:bold;'>From:</span>" . $yourname .         "<br>
        <span style='font-weight:bold;'>Email:</span> <a style='text-decoration:none; color:#6927B2;' href='mailto:" . $youremail . "'>" . $youremail . "</a></p>
       <p style='padding-top:200px;'>" . $message .

       "</p>
       <a href='" . $webaddress . "'><img src='http://recycledoc.com/emails/zoosh_tellafriend/visit.png' width='120' height='20' alt='Visit'></a>
      </td>
     </tr>
    </table>
   </td>
  </tr>
 </table>
</td>
</tr>
</table>

</body>
</html>";

if (mail($friendsemail, $subject, $body, $headers)) {
echo "Thank you for telling your friend about my website. <a href='#' id='goback'>Click here</a> to tell another friend.";
} else {
echo "Sorry. There was a problem sending your email. Please try again!";
}

mail($adminemail, $subject, $body, $headers);
mail($youremail, $subject, $body, $headers);
+4  A: 

You're circumventing the normal submission process of the form and submitting it through AJAX:

data: 'yourname=' + yourname + '&youremail=' + youremail + '&friendsname=' + friendsname + '&friendsemail=' + friendsemail + '&message=' + message,

This does not include logo, webaddress or adminaddress, so of course they do not arrive at the PHP script.

Also you are not correctly escaping those values, so if someone includes a ‘&’ or other special characters in one of those fields, it'll break. Use encodeURIComponent, or, since you are using jQuery's ajax function, just pass a lookup in and let jQuery take care of that for you:

data: {'yourname': yourname, ...

There are more escaping problems like this.

$headers = "From: " . strip_tags($from) . "\r\n";

Strip_tags is not useful here. Mail headers are plain text; HTML tags have no special meaning. What is dangerous, however, is newlines. This would allow an attacker to add any header they like to the mail, or even possibly send multiple, completely-controlled-by-attacker mails.

You should strongly sanitise anything you are going to put in a mail header; non-ASCII and control characters in particular must be stripped.

  <td valign='top' width='170' style='padding-right:10px'><img src='"
  . $webaddress . $logo . "' />

HTML injection. $webaddress and $logo can contain quotes, allowing an attacker to insert arbitrary HTML and JavaScript code. You need htmlspecialchars($s, ENT_QUOTES) every time you put text into HTML.

It is also dangerous to allow the user to choose any webaddress, logo, adminaddress, etc. This is a gift to spammers: they will submit their own data and message, hijacking your webform to “tell a friend” about their own penis pills instead, and getting your server widely blocked. If you must have a ‘tell a friend’ feature you need to really lock down what the parameters are allowed to be; just putting them in a hidden field is no protection.

bobince
great answers if people can up the score!
RageZ
Thanks a lot. I'm afraid I don't know how to pass hidden fields via ajax.
Ovi
Same way as your other fields, give them an id, slect it and read the `val()`. Or, tvanfosson suggested using `serialize` to get the data for all the form at once, which sounds like a good idea (but his comment's vanished, so maybe there was a reason it wasn't a good idea after all!) http://docs.jquery.com/Ajax/serialize
bobince
I've sorted out the problems with the hidden fields. I'm not using them any longer but instead I allow the web admin to add the website address and the link to the logo straight into the mail.php file from the CMS.However, could you please give me a few links to resources on how to sanitize the mail headers? I am very new to web programming so I would appreciate anything that is simple to understand. Thank you a lot for all your help.
Ovi
You could try replacing `[^ -~]` (that is, all characters outside of printable ASCII) with blank strings. Specifically for e-mail addresses, there's also `filter_var` with the setting FILTER_SANITIZE_EMAIL, which can be applied as well.
bobince
+1  A: 

Your PHP script accesses $_POST['adminemail'], but the hidden field is called adminaddress

mabwi