使用Ajax提交表单是否安全


Would submitting a form using Ajax be secure?

所以我用html构建了我的表单,并用JS验证了它,它看起来和我想要的一样。现在很明显,我将在服务器端使用PHP验证输入,但我想知道使用Ajax提交表单,然后在服务器端验证,而不是使用"提交"类型按钮和"操作"属性提交表单是否足够安全。基本上,让服务器端验证依赖于提交它的JS是否安全?

这是我的表格:

<form name="contactForm" id="contactForm"><!-- The form has no action attribute because its submitted via Ajax -->
<div id="inputsWrapper">
    <div>
        <label for="fullName">Your Name: <span class="required">(required)</span></label>
        <input type="text" name="fullName" id="fullName" title="First &amp; last name" value="First &amp; last name" maxlength="50" />
    </div>
    <div>
        <label for="email">Your E-mail: <span class="required">(required)</span></label>
        <input type="text" name="email" id="email" title="E-mail address" value="E-mail address" maxlength="500" />
    </div>
    <div>
        <label for="subject">In Regards To: <span class="required">(required)</span></label>
        <input type="text" name="subject" id="subject" title="Subject" value="Subject" maxlength="50"/>
    </div>
    <div>
        <label for="message">Your Message: <span class="required">(required)</span></label>
        <textarea name="message" id="message" title="Enter your message here" cols="40" rows="10">Enter your message here</textarea>
    </div>
</div> <!-- End inputsWrapper -->
<input type="button" name="sendBtn" id="sendBtn" value="Send Message" /><!-- This button has a listener assigned to it in JS and submits the form on click -->

单击按钮后,Ajax将通过POST将表单提交给我的PHP脚本,然后返回有效或无效。这样做是否安全?谢谢你的建议。

我想知道使用Ajax提交表单,然后在服务器端验证,而不是使用"提交"类型按钮和"操作"属性提交表单是否足够安全。

是的。来自系统外部的输入就是来自系统外部。

基本上,让服务器端验证依赖于提交它的JS是否安全?

您的JavaScript应该不引人注目,并实现渐进式增强。

脚本的输入应该是相同的,无论它是否来自Ajax的常规表单提交(如果你使用像jQuery的serialize这样的东西,这非常容易),所以你不需要让服务器依赖JS来获得响应。

表单提交和Ajax请求处理方式的唯一区别应该是响应的格式。

无论哪种方式,进入系统的数据最终都在提交者的控制之下,因此您需要对其进行适当的健全性检查和转义。

从安全角度来看,通过Ajax或浏览器默认表单发布行为不会有什么不同。这两个请求都将是常规的HTTPPOST/GET请求。