问题描述
我对使用CSRF保护有一些疑问.当我使用没有有效csrf令牌的发布"路由时,出现错误:Object.exports.error ....禁止使用...",是否可以将没有有效令牌的请求重定向到特殊路由?>
这是我的代码:
app.use(express.csrf());app.use(函数(req,res,next){res.locals.csrftoken = req.csrfToken();下一个();});
另一个问题是,保护该令牌的好方法是什么?最好将其存储在Cookie中,而不要使用隐藏字段?还是在安全性上没有任何区别?
form(method ="post",action ="/test")输入(类型=隐藏",名称="_ csrf",值=#{csrftoken}")
第三个问题是:禁用Cookie的用户无法访问任何路由,对吗?那我可以只在特殊路线上使用保护吗?
谢谢!
1)是.您可以使用
res.redirect('/redirect_route');
请参见 http://expressjs.com/4x/api.html#res.redirect
2)我认为cookie或隐藏字段根本不安全,您可以使用自己喜欢的东西
3)是.您可以使用中间件来保护特殊路由
app.get('/account',checkToken,routes.account);函数checkUser(req,res,next){res.locals.csrftoken = req.csrfToken();下一个();}
I have some Questions about using CSRF protection. When I use a "post" route without a valid csrf-token i get a "Error: Forbidden at Object.exports.error ....", Is it possible to redirect request without a valid token to a special route?
Here is my code:
app.use(express.csrf());
app.use(function (req, res, next) {
res.locals.csrftoken = req.csrfToken();
next();
});
Another question is, whats a good way to protect this token? Is it better to store it in a cookie, instead using a hidden field? Or isn't there any difference reffering to the security?
form(method="post",action="/test")
input(type="hidden", name="_csrf", value="#{csrftoken}")
Third Questions is: Users with disabled cookies can't access any routes, right? So can i use the protection only for special routes?
Thanks!
1) Yes. You can use
res.redirect('/redirect_route');
see http://expressjs.com/4x/api.html#res.redirect
2) I thing cookie or hidden field is not secure at all, you can use what you like
3) Yes. You can use middleware to protect special routes
app.get('/account', checkToken, routes.account);
function checkUser(req, res, next) {
res.locals.csrftoken = req.csrfToken();
next();
}
这篇关于使用express.csrf()进行错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!