我对 Node 很陌生,所以如果这是一个菜鸟问题,请原谅我。我正在尝试在 github 上使用 convert 这个项目来使用 ejs views ,但是很难理解他们是如何创建 csrf 令牌的。
我正在使用的种子项目 - https://github.com/sahat/hackathon-starter
使用 lusca 生成 csrf https://github.com/krakenjs/lusca
我在他们的种子项目中看到的代码(至少我认为是相关的)
var csrf = require('lusca').csrf();
/**
* CSRF whitelist.
*/
//app.js
var csrfExclude = ['/url1', '/url2'];
//original project uses jade
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade'); //i'm going to change this to ejs, but don't know where to get the csrf value(below) from
app.use(function(req, res, next) {
// CSRF protection.
if (_.contains(csrfExclude, req.path)) return next();
csrf(req, res, next);
});
app.use(function(req, res, next) {
// Make user object available in templates.
res.locals.user = req.user;
next();
});
app.use(function(req, res, next) {
// Remember original destination before login.
var path = req.path.split('/')[1];
if (/auth|login|logout|signup|fonts|favicon/i.test(path)) {
return next();
}
req.session.returnTo = req.path;
next();
});
//route controllers
app.get('/', homeController.index);
//in separate controller file - home.js
exports.index = function(req, res) {
res.render('home', {
title: 'Home'
});
};
//inside their jade file - this is converted to html tag --- <meta name="csrf-token" content="cRcgih7Vl1Ms2Xz0zgIeAyWwQm6s4kp3/8OS4=">
meta(name='csrf-token', content=_csrf)
//so value _csrf is converted to cRcgih7Vl1Ms2Xz0zgIeAyWwQm6s4kp3/8OS4=
Run Code Online (Sandbox Code Playgroud)
我的困惑是 _csrf 标签是从哪里拉出来的?我试图通过所有文件 grep 那个关键工作,但实际上并没有看到它在任何地方设置(可能遗漏了什么?)。我正在查看我的检查员并能够看到会话变量设置为 req.session._csrfSecret = nLzJqL3YIAJVzA== ,但这看起来与上面使用的键不同。基于 /8OS4,我认为该值实际上是在某处连接的。
我的问题是 - 在玉模板中,这个 _csrf 值来自哪里?我在任何地方的 js 代码中都没有看到 jade 从哪里抓取它(我没有看到 _csrf 在任何地方的响应中设置)。
或者使用 lusca 创建和保留 csrf 值的正常方法是什么?
谢谢你的帮助!
我正准备回答这个问题,但看起来有人抢先了我: https: //groups.google.com/forum/#!topic /nodejs/Zwnw4wOAtxw 。只是发布此内容以防其他人需要帮助。
| 归档时间: |
|
| 查看次数: |
2810 次 |
| 最近记录: |