Node.js Lusca csrf未按预期工作-http请求使用旧令牌
我试图在我的Express.js应用程序上使用lusca设置CSRF保护。不是这样的:Node.js Lusca csrf未按预期工作-http请求使用旧令牌,node.js,express,csrf,lusca,Node.js,Express,Csrf,Lusca,我试图在我的Express.js应用程序上使用lusca设置CSRF保护。不是这样的: this.app.use(lusca({ csrf: { cookie: {name: '_csrf'} }, hsts: { maxAge: 31536000, includeSubDomains: true, preload: true }, nosniff: true, referrerPolicy: "same-origin"
this.app.use(lusca({
csrf: {
cookie: {name: '_csrf'}
},
hsts: { maxAge: 31536000, includeSubDomains: true, preload: true },
nosniff: true,
referrerPolicy: "same-origin",
xframe: "SAMEORIGIN",
xssProtection: true,
}));
在客户端,如下所示:
const res = await axios.post(`${Constants.apiUrl()}/${Constants.paths.login}`,
credentials, {
withCredentials: true,
xsrfCookieName: '_csrf'
});
在服务器端,我还设置了一些头,以便能够发送带有请求的cookie-res.header('Access-Control-Allow-Credentials','true')
可能我遗漏了CSRF保护工作原理的一些重要部分。现在,每次响应时,我都会得到新的csrf令牌
,但这意味着我的新HTTP POST请求会发送以前已经过时的令牌。
我遗漏了什么?经过3个小时的测试,终于发现了问题。您需要添加csrf的
secret
。此外,如果使用Angular,则需要将Angular:true
添加到csrf中
this.app.use(lusca({
csrf: {
cookie: {name: '_csrf'},
secret: 'qwerty'
},
hsts: { maxAge: 31536000, includeSubDomains: true, preload: true },
nosniff: true,
referrerPolicy: "same-origin",
xframe: "SAMEORIGIN",
xssProtection: true,
}));