Teman

Pencarian

Home » » PHP Secure E-mails

PHP Secure E-mails


PHP E-mail Injections


Pertama, lihat program PHP yang sama dengan program pada bab 20.

Program21-1.php
<html>
<body>
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //send email
  $email = $_REQUEST['email'] ; 
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  mail("someone@example.com", "Subject: $subject",
  $message, "From: $email" );
  echo "Thank you for using our mail form";
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>
</body>
</html>

Masalah yang timbul pada kode di atas adalah user yang tidak terotorisasi dapat menyisipkan data ke dalam header mail melalui form input.
Apa yang akan terjadi jika user memasukkan teks-teks berikut ke dalam field input email pada form?

someone@example.com%0ACc:person2@example.com
%0ABcc:person3@example.com,person3@example.com,
anotherperson4@example.com,person5@example.com
%0ABTo:person6@example.com

Fungsi mail() akan meletakkan teks di atas ke dalam header mail seperti biasa, dan sekarang header telah mempunyai tambahan field Cc:, Bcc:, dan To:. Ketika user menekan tombol submit, maka e-mail akan terkirim ke seluruh alamat di atas.


Mencegah PHP E-mail Injections

 

Cara yang terbaik untuk menghentikan e-mail injections adalah dengan menggunakan validasi input.
Program di bawah ini sama dengan program di atas tetapi sekarang kita telah menambahkan mekanisme pengecekan input yang melakukan pemeriksaan field email pada form.

Program21-2.php
<html>
<body>
<?php
function spamcheck($field)
  {
//eregi() performs a case insensitive regular expression match
  if(eregi("to:",$field) || eregi("cc:",$field)) 
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }
//if "email" is filled out, send email
if (isset($_REQUEST['email']))
  {
  //check if the email address is invalid
  $mailcheck = spamcheck($_REQUEST['email']);
  if ($mailcheck==TRUE)
    {
    echo "Invalid input";
    }
  else
    { 
    //send email
    $email = $_REQUEST['email'] ; 
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    mail("someone@example.com", "Subject: $subject",
    $message, "From: $email" );
    echo "Thank you for using our mail form";
    }
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>
</body>
</html>

0 komentar:

Posting Komentar