如何使用Backbone.js的发布数据时,为了防止CSRF? [英] How to protect against CSRF when using Backbone.js to post data?

查看:158
本文介绍了如何使用Backbone.js的发布数据时,为了防止CSRF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Backbone.js的处理引擎盖下发布数据到服务器,所以没有简单的方法来插入的有效载荷CSRF令牌。我怎样才能保护我对CSRF的网站在这种情况呢?

Backbone.js handles posting data to server under the hood, so there is no easy way to insert a CSRF token in the payload. How can I protect my site against CSRF in this situation?

在此SO回答: http://stackoverflow.com/a/10386412/954376 ,该建议是验证的X-请求-By头是XMLHTT prequest。这是否足以阻止所有CSRF的尝试?

In this SO answer: http://stackoverflow.com/a/10386412/954376, the suggestion is to verify the x-Requested-By header to be XMLHTTPRequest. Is this enough to block all CSRF attempts?

在Django文档,建议是增加CSRF令牌在每一个AJAX请求另一个自定义标题:<一href=\"https://docs.djangoproject.com/en/1.5/ref/contrib/csrf/#ajax\">https://docs.djangoproject.com/en/1.5/ref/contrib/csrf/#ajax.这是必要的吗?

In Django docs, the suggestion is to add CSRF token in another custom header in every AJAX request: https://docs.djangoproject.com/en/1.5/ref/contrib/csrf/#ajax. Is this necessary?

据我了解,如果攻击使用隐藏的表单,我很安全,只需确保请求来自XMLHTT prequest。但是它有什么CSRF攻击技巧,可以伪造的头?

I understand if the attack uses hidden form, I am safe by just assuring the request is from XMLHTTPRequest. But is there any CSRF attack tricks that can forge the header?

推荐答案

您可以使用的 prefilter 令牌添加到所有的请求:

You can use a prefilter to add the token to all requests:

$.ajaxPrefilter(function(opts) {
    if (opts.data) {
        opts.data += "&";
    }
    opts.data += "csrfToken=" + token;
});

您可能需要添加额外的逻辑,如果你不总是发送令牌。

You may need to add additional logic if you don't always send the token.

这篇关于如何使用Backbone.js的发布数据时,为了防止CSRF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆