我在征求反馈意见。我一直在试图找到一个好的方法来验证客户端发送的POST数据,并遇到了Content-MD5标头。
这是我的解决方案,第一部分是以简单易用的插件形式使用jQuery .ajax()函数。它确实需要md5()的pidder encryption libraries和Content-MD5头所需的base64_encode()函数。
<script src="javascripts/pidcrypt.js"></script>
<script src="javascripts/pidcrypt_util.js"></script>
<script src="javascripts/md5.js"></script>
<script>
(function($){
$.fn.AJAX = function(method) {
var defaults = {
formID: $(this),
appID: 'jQuery.AJAX',
cache: true,
context: $(this),
type: 'json',
callback: function(){},
errCallback: function(){}
};
var methods = {
init: function(o){
var opts = $.extend({}, defaults, o);
$('#'+opts.formID.attr('id')).on('submit', function(e){
e.preventDefault();
$.ajax({
form: opts.formID.attr('id'),
url: opts.formID.attr('action'),
type: opts.formID.attr('method'),
data: opts.formID.serialize(),
context: opts.context,
cache: opts.cache,
crossDomain: (opts.type==='jsonp') ? true : false,
dataType: opts.type,
beforeSend: function(xhr) {
xhr.setRequestHeader('X-Alt-Referer', opts.appID);
if (opt.formID.serialize()){
xhr.setRequestHeader('X-Alt-Referer', pidCryptUtil.encodeBase64(pidCrypt.MD5($(this).serialize())));
} else {
xhr.setRequestHeader('X-Alt-Referer', pidCryptUtil.encodeBase64(pidCrypt.MD5(appID)));
}
},
success: function(x){
((opts.callback)&&($.isFunction(opts.callback))) ?
opts.callback.call(x) : console.log(x);
},
error: function(xhr, status, error){
((opts.errCallback)&&($.isFunction(opts.errCallback))) ?
opts.errCallback.call(xhr, status, error) : console.log(xhr+status+error);
}
});
return true;
});
}
};
if (methods[method]){
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if ((typeof method==='object')||(!method)){
return methods.init.apply(this, arguments);
} else {
console.log('Method '+method+' does not exist');
}
};
})(jQuery);}要使用插件,简单地创建一个HTML表单,如下所示...
<form id="test" name="test" method="post" action="proxy.php">
<label for="name">Name: <span class="required">*</span></label>
<input type="text" id="name" name="name" value="" placeholder="John Doe" required="required" />
<label for="email">Email Address: <span class="required">*</span></label>
<input type="email" id="email" name="email" value="" placeholder="johndoe@example.com" required="required" />
<label for="email">Confirm Email: <span class="required">*</span></label>
<input type="email" id="email" name="email" value="" placeholder="johndoe@example.com" required="required" />
</form>现在将插件绑定到表单,如下所示...
$('#test').AJAX();此时,将POST数据发送到proxy.php脚本的工作客户端方法已经就位。这里的一个主要区别是,与发送表单post数据不同,一些定制头部与XMLHttpRequest格式的表单数据一起发送。
现在,在服务器上执行几个简单的验证。首先检查以确保请求是一个XMLHttpRequest,然后检查以确保X-Alt-Referer匹配,然后在处理之前检查以确保post数据匹配相同的post数据(序列化的)散列匹配。从技术上讲,它的工作原理很像校验和。
<?php
/* set the custom applicaiton string */
$appID = 'jQuery.AJAX'; // the plug-in URL https://github.com/jas-/jQuery.AJAX
/* verify an XMLHttpRequest was made */
if (strcmp($_SERVER['HTTP_X_REQUESTED_WITH'], 'XMLHttpRequest')!==0){
exit('An XMLHttpRequest was not made');
}
/* verify associated X-ALT-Header value */
if (strcmp($_SERVER['HTTP_X_ALT_REFERER'], $appID)!==0){
exit('The X-Alt-Referer information recieved is invalid');
}
/* verify associated Content-MD5 header value */
if (strcmp(base64_decode($_SERVER['HTTP_CONTENT_MD5']), md5(serialize($_POST)))!==0){
exit('The Content-MD5 value is incorrect');
}
?>有没有人有理由不使用这种类型的帖子数据验证?提前谢谢。
发布于 2012-01-02 22:06:41
您想要保护什么?Content-MD5报头是一个很好的解决方案,可以防止TCP校验和没有捕获到的传输错误,这种错误发生的频率比大多数人意识到的要多。对于主动修改数据的攻击者来说,这是完全无用的,因为攻击者只需要重新计算头部。
不使用这个头的唯一原因是性能,客户端和服务器端都有开销(例如移动客户端)。你必须权衡成本和它为你提供的(小)保护。
我发现这个blog entry在这个问题上很有帮助。
https://stackoverflow.com/questions/8697128
复制相似问题