English 中文(简体)
使用 csrf 符号
原标题:csrf token using

我有兴趣通过使用 Csrf 代号来保护我的网络应用程序。 我的问题是我需要如何将该代号发回服务器: 使用查询参数或 http header x- csrf-token?

区别是什么?

最佳回答

由于您正在使用Express, 您可以使用其 CSRF 中间软件 (通过连接): http://www.senchalabs.org/connect/csrf.html

您可以在此查看注释来源 : https://github.com/senchalabs/connect/blob/master/lib/midddleware/csrf.js

您需要做的只是将中间软件包含在内, 然后在您的 POST 表格( 或 PUT 等任何要求变异状态) 中设置变量 < code\\ csrf 以拥有值 req.session._ csrf

这里的选中示例 : https://github.com/senchalabs/connect/blob/master/examples/csrf.js

<强> UPDATE

自连接 2.9.0 以来, 您必须使用 req. csrfToken () 而不是 req.session._ csrf

完整示例: rel=“不跟随 Norefererr>>https://github.com/senchalabs/connect/blob/master/examples/csrf.js

提交: rel=

<强>UPDATE2

连接的中间软件被分割成不同的模块(及相关的Respos), 您可以在此找到所有模块( 包括 CSRF 之一) : < a href="https:// github.com/ senchalabs/ connect #middleware" rel=“ nofollow noreferrer" >https://github.com/ senchalabs/ connect #midddleware

问题回答

在我看来,您在提交表格时,应该在隐藏的字段中使用 csrf POST 参数。这是唯一的出路。

但对于 AJAX 请求, 我强烈建议您使用 < code> X- CSRF- Token 页眉。 主要是因为如果正确, 它会节省您记忆的杂务, 以添加每次 POST 请求的标记。 或者, 使用 JQuery Form 等图书馆时, 在提交时添加额外的 POST 参数会变成黑客化 。

例如,如果您在 AJAX 请求中使用 jQuery, 它会为您提供 < a href=" http://api.jquery.com/ajaxSend/" rel=“ nofollow” > a hook < /a > 您可以在请求之前自动和透明地设置 < code> X- CSRF-Token 。 因此, 客户端代码需要修改的非常少。 您会自动地跳动代码 。

-- -- -- -- --

举例来说,我成功地利用了几乎所有基于强哥一号项目的项目,具体实施如下:

jQuery(document).ajaxSend(function(event, xhr, settings) {

  function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie !=   ) {
      var cookies = document.cookie.split( ; );
      for (var i = 0; i < cookies.length; i++) {
        var cookie = jQuery.trim(cookies[i]);
        // Does this cookie string begin with the name we want?
        if (cookie.substring(0, name.length + 1) == (name +  = )) {
          cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
          break;
        }
      }
    }
    return cookieValue;
  }

  function sameOrigin(url) {
    // url could be relative or scheme relative or absolute
    var host = document.location.host; // host + port
    var protocol = document.location.protocol;
    var sr_origin =  //  + host;
    var origin = protocol + sr_origin;
    // Allow absolute or scheme relative URLs to same origin
    return (url == origin || url.slice(0, origin.length + 1) == origin +  / ) ||
           (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin +  / ) ||
           // or any other URL that isn t scheme relative or absolute i.e relative.
           !(/^(//|http:|https:).*/.test(url));
  }

  function safeMethod(method) {
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
  }

  if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
    xhr.setRequestHeader("X-CSRFToken", getCookie( csrf.token ));
  }
});

在服务器侧面, 您只需要设置含有 CSRF 符号的 cookie 即可让客户端容易获得该符号。 我替换了 < code> app. use( 表达式. csrf ()) :

app.use((function(options) {

  var csrf = express.csrf(options);

  return function(req, res, next) {

    function onCsrfCalled() {
      var token = req.session._csrf;
      var cookie = req.cookies[ csrf.token ];

      // Define a cookie if not present
      if(token && cookie !== token) {
        res.cookie( csrf.token , token);
      }

      // Define vary header
      res.header( Vary ,  Cookie );

      next();
    }

    csrf(req, res, onCsrfCalled);
  }
})());




相关问题
REST and CSRF (Cross-Site Request Forgery)

Is Cross-Site Request Forgery possible against a stateless RESTful service? I m not talking about pseudo-REST where the server remembers that you re logged in via a cookie. I m talking about pure no-...

Django Error: Cannot Import csrf.py

My Django app started breaking all of a sudden and I cannot understand why. I can t even get it to run now. I m running revision 11798. When I use the stand-alone server to test my app, it suddenly ...

Rails, OAuth, and CSRF protection

I am using REST and OAuth to talk to a Rails app (from an iPhone app, but that should not be relevant). However, I am running into some issues with Rails CSRF protection (via protects_from_forgery). ...

Completely disable Django s CSRF protection in SVN Trunk

I ve spend a few hours in frustration, trying to disable the CSRF which Django now tries to force on me, to no avail. Had anyone else tried this with more success? I m fine with anything that works, ...

Django CSRF Framework having many failures

I m getting many failures from the CSRF Django middleware on my site (the version from SVN trunk.) The only errors I get are: CSRF failure: reason=CSRF token missing or incorrect. How could I ...

热门标签