PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Securing contact forms with PEAR MAIL

 
Post new topic   Reply to topic    PEAR Forum Forum Index -> Mail
View previous topic :: View next topic  
Author Message
squidliberty



Joined: 14 Sep 2007
Posts: 2

PostPosted: Fri Sep 14, 2007 12:14 am    Post subject: Securing contact forms with PEAR MAIL Reply with quote

What do I need to do to ensure that contact forms utilizing Mail::send() are not susceptible to mail injection exploits (see http://www.securephpwiki.com/index.php/Email_Injection)? Is the following code (from pear.php.net) secure?
Code:
<?php
include('Mail.php');

$recipients = 'joe@example.com';

$headers['From']    = 'richard@example.com';
$headers['To']      = 'joe@example.com';
$headers['Subject'] = 'Test message';

$body = 'Test message';

$params['sendmail_path'] = '/usr/lib/sendmail';

// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory('sendmail', $params);

$mail_object->send($recipients, $headers, $body);
?>

Thanks for your feedback!
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1048

PostPosted: Fri Sep 14, 2007 10:06 am    Post subject: Reply with quote

Your example code doesn't use any external values; it's secure. If you use something like
Code:
$headers['To'] = $_GET['email'];
, though, it would get unsecure. Solution: Check the value. (<= something you should do almost ever when dealing with user-input)
Back to top
View user's profile Send private message
squidliberty



Joined: 14 Sep 2007
Posts: 2

PostPosted: Fri Sep 14, 2007 4:08 pm    Post subject: Re:Securing contact forms with PEAR MAIL Reply with quote

Thanks for your speedy reply!
mark wrote:
Your example code doesn't use any external values; it's secure. If you use something like $headers['To'] = $_GET['email'];, though, it would get unsecure. Solution: Check the value. (<= something you should do almost ever when dealing with user-input)

Oops. I guess that was a bad example. So, if I'm using user submitted data, what is the minimum required to ensure security against injection? Is checking the input with eregi("(\r|\n)", $from), as suggested by the SecurePHP link above, really sufficient?
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1048

PostPosted: Fri Sep 14, 2007 5:43 pm    Post subject: Reply with quote

Using this eregi() call would be a good idea. But I didn't had in mind that PEAR's Mail package already does such checks. Therefore, you can trust the package and use something like in my example:
Code:
$headers['To'] = $_GET['email'];


The code that the package uses for all header fields is the following method:
Code:

    /**
     * Sanitize an array of mail headers by removing any additional header
     * strings present in a legitimate header's value.  The goal of this
     * filter is to prevent mail injection attacks.
     *
     * @param array $headers The associative array of headers to sanitize.
     *
     * @access private
     */
    function _sanitizeHeaders(&$headers)
    {
        foreach ($headers as $key => $value) {
            $headers[$key] =
                preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i',
                             null, $value);
        }
    }
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> Mail All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement

'Actiemonitor' online projectmanagement software