好的,所以我将他们的代币存储在 session 中:
Session::get('token', 'randomtokenstringhere');
在每个表单上,无论成功与否,我都会生成一个新 token ,并更新其 session token ,现在假设 user 1
位于更新个人资料页面上,并且我序列化了一个表单,页面加载时有一个 token 回显 token 变量),他们提交表单,该表单运行 ajax,要么完成,要么出现错误,我的系统生成一个新 token ,但现在在编辑个人资料页面上,他们仍在使用旧 token ,因此现在它们的 token 将永远不会匹配。
如何解决此问题以获得额外的安全性?
这是我的 ajax 的示例:
<!-- accept, reject, add and cancel friend requests -->
$('.add_friend').click(function(event) { // bind function to submit event of form
event.preventDefault();
$.ajax({
url: $(this).attr('href'),
success: function(responseText) {
if($(event.target).text() == 'Add as friend'){
$(event.target).attr("href", "<?php echo Config::get('URL'); ?>friends/cancel_request/" + $(event.target).attr('id') + "/<?php echo System::escape(Session::get('token')); ?>");
$(event.target).text('Cancel Request');
$(event.target).removeClass("add_friend btn btn-success").addClass("cancel_request btn btn-info");
}else{
$(event.target).attr("href", "<?php echo Config::get('URL'); ?>friends/addfriend/" + $(event.target).attr('id') + "/<?php echo System::escape(Session::get('token'));?>");
$(event.target).text('Add as friend');
$(event.target).removeClass("cancel_request btn btn-info").addClass("add_friend btn btn-success");
}
}
});
return false;
});
<!-- End Like a status -->
当然运行以下链接:
<a class="add_friend btn btn-success" id="<?php echo System::escape($likes->timeline_likes_id); ?>" href="<?php echo Config::get('URL'); ?>friends/addfriend/<?php echo System::escape($likes->user_id).'/'.System::escape(Session::get('token')); ?>"><?php echo System::translate("Add as friend"); ?></a>
他们的 token 是:System::escape(Session::get('token')); ?>
只会在页面刷新时加载,这默认了我使用 ajax 的原因
最佳答案
您需要实现 nonces !来自 wikipedia article :
A nonce is an arbitrary number used only once in a cryptographic communication, in the spirit of a nonce word. They are often random or pseudo-random numbers. Many nonces also include a timestamp to ensure exact timeliness ... To ensure that a nonce is used only once, it should be time-variant (including a suitably fine-grained timestamp in its value), or generated with enough random bits to ensure a probabilistically insignificant chance of repeating a previously generated value. Some authors define pseudorandomness (or unpredictability) as a requirement for a nonce.
无论如何,任何实际的实现都需要至少 2 个函数。第一个类似于 createNonce()
,第二个类似于 verifyNonce()
。如果您正在寻找一个可以为您执行此操作的库,我使用过 OpenID过去。
有很多有效的方法可以做到这一点,并且编写自己的实现相当简单。基本概念是创建一个包含可复制数据的字符串。然后散列该数据并将其放入表单中的字段中。然后,当您获取表单数据时,您可以重新创建哈希并将其与表单发送的哈希进行比较(类似于验证密码的方式)。您需要确保您的哈希包含定期更改的数据(例如 microtime()
),以便您的随机数是唯一的。对于用户登录的系统,我经常使用该用户的某些内容(例如密码哈希的子字符串),以便每个用户都获得一个唯一的随机数,即使它们都是在同一微秒内生成的。
关于javascript - 我通过 jquery 字符串发送跨站点伪造 token ,但在下一页上我生成一个新 token ,然后它们的 token 永远不会匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32235715/