这种形式安全吗?

时间:2012-09-07 09:11:40

标签: php jquery html forms security

我有这个表单,用户通过该表单向我发送电子邮件。我不知道它是否安全,或者只有涉及sql时才出现安全问题......

HTML:

<form id="form4" action="send_mic.php"  name="form4" method="post" >

           <textarea name="message4" cols="4" rows="4"  id="message4" ></textarea><br />

           <input type="text"  id="name4" name="name4" value="" /><br />

           <input type="text"  id="email4" name="email4" value=""  /><br />

          <input type="submit" value="" id="submit" />

</form>

jquery的:

<script type="text/javascript">
$(document).ready(function () {
    $('#form4').ajaxForm({
        beforeSubmit: validate
    });

    function validate(formData, jqForm, options) {
        var name = $('input[name=name4]').fieldValue();
        var email = $('input[name=email4]').fieldValue();
        var message = $('textarea[name=message4]').fieldValue();

        if (!name[0]) {
            alert('Please enter a value for name');
            return false;
        }
        if (!email[0]) {
            alert('Please enter a value for email');
            return false;
        }
        if (!message[0]) {
            alert('Please enter a value for message');
            return false;
        }

        else {

        $("#content").fadeOut(1000, function () {
            $(this).html("<img src='images/postauto3.png'/>").fadeIn(2000);
        });

        var message = $('textarea[name=message4]').val('');
        var name = $('input[name=name4]').val('');
        var email = $('input[name=email4]').val('');

            } 
    }

});



    </script> 

PHP:

<?php
        if($_POST){
                $email = $_POST['email4'];
                $name = $_POST ['name4'];
                $message = $_POST ['message4'];
                // response hash
                $ajaxresponse = array('type'=>'', 'message4'=>'');

                try {
                        // do some sort of data validations, very simple example below
                        $all_fields = array('name4', 'email4', 'message4');

                        foreach($all_fields as $field){
                                if(empty($_POST[$field])){
                                        throw new Exception('Required field "'.ucfirst($field).'" missing input.');
                                }
                        }

                        // ok, if field validations are ok
                        // now Send Email, ect.

                        // let's assume everything is ok, setup successful response
                        $subject = "New Contact";
                        //get todays date
                        $todayis = date("l, F j, Y, g:i a") ;

                        $message = " $todayis \n
                        Attention: \n\n
                        Please see the message below: \n\n
                        Email Address: $email \n\n
                        Message: $message \n\n

                        ";

                        $from = "From: $email\r\n";


                        //put your email address here
                        mail("contact@....ro", $subject, $message, $from);

                        //prep json response
                        $ajaxresponse['type'] = 'success';
                        $ajaxresponse['message'] = 'Thank You! Will be in touch soon';  
                } catch(Exception $e){
                        $ajaxresponse['type'] = 'error';
                        $ajaxresponse['message'] = $e->getMessage();
                }
                // now we are ready to turn this hash into JSON
                print json_encode($ajaxresponse);
                exit;
        }
?>

那么,使用表单发送电子邮件时是否存在安全问题?这个可以吗? 谢谢!

7 个答案:

答案 0 :(得分:6)

通常,拇指规则应始终为:永远不要信任用户提供的数据。不,您的代码不是防弹。由于您没有验证或清理用户输入,并且在您易受攻击的同时使用mail()。用户可以轻松为您提供email4提交的精心设计的价值。由于您直接使用表单数据,因此可以使用email4向您的外发邮件注入其他邮件头。这些标题可以是BCC:CC:,甚至是TO:,然后您只需充当垃圾邮件中继。例如,如果我发布此

some@address.com
CC: spamvictim1@foo.com, spamvictim2@foo.com, spamvictim3@foo.com,
X-Spam-Owned: Whoa

作为您的email4,然后您的标题将如下所示:

To: some@address.com
CC: spamvictim1@foo.com, spamvictim2@foo.com, spamvictim3@foo.com, 
X-Spam-Owned: Whoa

发布多行数据,只需将文本与CRLF粘合在一起。

为了避免这样的安全漏洞你应该考虑放弃mail()并使用更聪明的东西来处理这样的事情(不是mail()不好,但你需要知道你是什么正在做,因为它比高级功能低得多)。我建议使用PHPMailer或类似的包。您应该始终验证用户提供的数据(特别是确保单行字段,如主题实际上是单行 - 剥离CRLF就足够了)。在您自动提交表单时添加验证码。

答案 1 :(得分:4)

  1. 您可以添加验证码以防止垃圾邮件。
  2. 您可以使用以下方法防止电子邮件注入:

    filter_var($ email,FILTER_VALIDATE_EMAIL)

答案 2 :(得分:1)

我认为这种形式是安全的,这意味着没有人可以通过这种形式真正地访问您的网站 但是你需要添加一些东西以获得更好的结果: 你还应该检查php服务器端的post变量,这意味着你应该检查电子邮件/名称/消息是否有效 2.您应该添加一些验证密码以防止垃圾邮件

答案 3 :(得分:1)

您还可以使用

包装服务器端代码
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  /* special ajax here */
  die($content);
}

这将确保服务器上发出ajax请求。

请注意您在问题中使用的jQuery选择器中使用的ID。

答案 4 :(得分:1)

即使您不使用数据库,电子邮件发送也可能存在安全问题。当然你不能被这个表单攻击,但是当用户在EMail字段中输入这样的内容时会出现问题:

email-address1@example.com  // there is a new line here
CC:email-address2@example.com,email-addresses3@example.com,.............................email-addressesn@example.com

因此,您可以做的最好的事情是清理邮件功能的所有输入字段,以防止此类垃圾邮件传递。由于@WebnetMobile.com已经感到悲伤,从不信任用户输入

答案 5 :(得分:0)

我没有看到安全问题,因为您没有在服务器端修改任何内容。可能是垃圾邮件的问题。添加一些验证码。其余的看起来还不错。

答案 6 :(得分:0)

您应该在表单

中添加验证码,客户端和服务器端验证
相关问题