简体   繁体   English

Kraken.js CSRF处理

[英]Kraken.js CSRF Handling

i have a problem with krakenjs, i'm a node/express newbie. 我有krakenjs的问题,我是节点/表达新手。

krakenjs is set to default csrf protection (i know how to disable, but i dont want to), but i dont know how to handle csrf and avoiding 403 error. krakenjs设置为默认的csrf保护(我知道如何禁用,但我不想),但我不知道如何处理csrf并避免403错误。

in ejs file i got this line. 在ejs文件中我得到了这一行。

<input type="hidden" name="_crsf" value="<%= _csrf %>" />

it generates proper csrf, there is no problem in there. 它生成适当的csrf,那里没有问题。

and here is my route 这是我的路线

server.post('/isengard/fact/new', function(req,res){
    var new_fact = Fact({
        title : req.body.fact_title,
        description : req.body.fact_description,
        source : req.body.fact_source
    });
    new_fact.save(function(err){
        if(err) return handleError(err);
        var model = {status:true};
        res.render('isengard/create',model);
    });
});

but when i send form (POST), i'm getting this error. 但是当我发送表格(POST)时,我收到了这个错误。

403 Error: Forbidden
at Object.exports.error (/Users/onur/Documents/node/sage/node_modules/express/node_modules/connect/lib/utils.js:63:13)
at createToken (/Users/onur/Documents/node/sage/node_modules/express/node_modules/connect/lib/middleware/csrf.js:82:55)
at /Users/onur/Documents/node/sage/node_modules/express/node_modules/connect/lib/middleware/csrf.js:48:24
at csrf (/Users/onur/Documents/node/sage/node_modules/kraken-js/node_modules/lusca/index.js:112:13)
at /Users/onur/Documents/node/sage/node_modules/kraken-js/node_modules/lusca/index.js:60:21
at xframe (/Users/onur/Documents/node/sage/node_modules/kraken-js/node_modules/lusca/index.js:131:9)
at /Users/onur/Documents/node/sage/node_modules/kraken-js/node_modules/lusca/index.js:60:21
at p3p (/Users/onur/Documents/node/sage/node_modules/kraken-js/node_modules/lusca/index.js:144:9)
at /Users/onur/Documents/node/sage/node_modules/kraken-js/node_modules/lusca/index.js:60:21
at Object.appsec (/Users/onur/Documents/node/sage/node_modules/kraken-js/node_modules/lusca/index.js:65:9)

can anyone explain me how to handle csrf? 任何人都可以解释我如何处理csrf?

Actually, your problem is that you have: 实际上,你的问题是你有:

<input type="hidden" name="_crsf" value="<%= _csrf %>" />

instead of: 代替:

<input type="hidden" name="_csrf" value="<%= _csrf %>" />

Note the typo in the name attribute. 请注意name属性中的拼写错误。

The trick is that you need to wrap your POST test inside a GET and parse the necessary CSRF token from the cookie. 诀窍是你需要将你的POST测试包装在GET中并从cookie中解析必要的CSRF令牌。

Here's an example: https://stackoverflow.com/a/18776974/1935918 这是一个例子: https//stackoverflow.com/a/18776974/1935918

csrf in kraken is pretty much entirely handled by the csrf connect middleware (with the one addition being exposing the token to your views as _csrf ). kraken中的csrf几乎完全由csrf连接中间件处理(另外一个是将标记暴露给你的视图_csrf )。

A little more information would go a long way (req/res headers at the least but an HAR would be awesome) but I can see a few ways this might happen: 更多信息会有很长的路要走(至少req / res标题,但HAR会很棒)但我可以看到这种情况可能发生的几种方式:

  1. The csrf secret (not token, mind you) is being regenerated or removed some time between the initial GET and the POST . 在初始GETPOST之间的某个时间重新生成或删除了csrf 机密 (不是令牌,请注意)。 The only way this is possible is if the value stored as _csrfSecret in the session is changed or deleted between requests. 唯一可行的方法是在请求之间更改或删除会话中存储为_csrfSecret的值。 Make sure your session is working properly. 确保您的会话正常运行。
  2. One of the security headers is giving you grief. 其中一个安全标题让你感到悲伤。 Try turning them off temporarily with something like the following in your middleware-development.json : 尝试使用middleware-development.json的以下内容暂时关闭它们:

     { "middleware": { "appsec": { "csp": false, "xframe": false, "p3p": false } } } 

Unless you need csrf protection, put this in your config.json to disable it altogether. 除非你需要csrf保护,否则请将它放在config.json中以完全禁用它。 Then your app runs as it otherwise would. 然后你的应用程序就会运行。

"middleware": {        
      "appsec": {
        "priority": 110,
        "module": {
            "name": "lusca",
            "arguments": [
                {
                    "csrf": false,
                    "xframe": "SAMEORIGIN",
                    "p3p": false,
                    "csp": false
                }
            ]
        }
    },
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM