- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我很困惑,当我将 cookie 设置为安全时, Node 的 csrf 不工作。
//Load Cooike Parser
app.use(cookieParser(secret));
//Load Session Store
app.use(require('express-session')({
secret:secret,
cookie:{
maxAge: 1000 * 60 * 60 * 24, // 1 day,
secure: true,
httpOnly: true
},
store: sessionStore
}));
//Load POST data parser
//Form sent should be in JSON format
app.use(bodyParser.json());
//Initiate CSRF on middleware
//set the CSRF cookie Header
app.use(csrf());
app.use(function(req,res,next){
res.cookie('XSRF-TOKEN',req.csrfToken());
next();
});
此设置使用 MongoDB 存储 session 数据。阅读 express-session
文档,我发现了这个......
Please note that secure: true is a recommended option. However, it requires an https-enabled website, i.e., HTTPS is necessary for secure cookies. If secure is set, and you access your site over HTTP, the cookie will not be set. If you have your node.js behind a proxy and are using secure: true, you need to set "trust proxy" in express:
我目前在本地运行网站,因此它不是 HTTPS。我想知道 secure:true
与未通过 csrf 测试有什么关系?
最佳答案
由于所提供的代码示例未涵盖表单的创建,因此我假设您正确地包含了 _csrf
值。或者您使用 JavaScript 设置相应的 header 。
让我们首先解释为什么您不应该执行 res.cookie('XSRF-TOKEN',req.csrfToken());
。
csurf
模块的默认工作方式是在您运行 req.csrfToken()
时生成或返回 _csrf
token ,但它还将此 token 保存在 session 中。
如果您想使用 cookie 而不是 session 作为存储方法,您应该将 cookie: true
或 cookie: cookieOptions
作为初始化值传递给 csurf
而不是手动设置 cookie。
这是相关的,因为如果您不使用预期的选项参数,csurf
将尝试从 session 对象中查找 token 值以进行验证,这意味着您的 cookie 设置是无用的。
现在关于它在 HTTPS 上失败的部分。
当您设置 secure: true
时,它所做的是使用 secure
标志从服务器向浏览器发送 cookie。
根据 OWASP page :
The secure flag is an option that can be set by the application server when sending a new cookie to the user within an HTTP Response. The purpose of the secure flag is to prevent cookies from being observed by unauthorized parties due to the transmission of a the cookie in clear text.
To accomplish this goal, browsers which support the secure flag will only send cookies with the secure flag when the request is going to a HTTPS page. Said in another way, the browser will not send a cookie with the secure flag set over an unencrypted HTTP request.
这包括 session token cookie,用于在 sessionStore 中查找信息。
因此浏览器不会向服务器发送 session cookie。服务器创建一个新的空 session ,因为我们返回到默认的 csurf
操作方法,它将尝试从空 session 中查找 token 。不会有 token ,因此比较会失败。
作为旁注,这也意味着您的 session 通常会失败。
注意!作为旁注,如果您更感兴趣,我建议您阅读 OWASP CSRF mitigation cheatsheet .或者我关于 Node.js Web Application Security 的书,其中包括 CSRF 和使用 Node.js 实现的各种缓解方法。
关于node.js - 如果 session cookie 是安全的,则 CSURF 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34414106/
嘿伙计们,我已经在这个问题上呆了一个星期了,但我不太明白。 我正在构建一个移相器游戏,并使用 Node 设置了一个记分板,这是我第一次使用 Node ,我不太清楚如何使用 csurf,因为文档非常困惑
我一直在尝试对我一直在从事的个人项目进行 csurf 实现。我一直在到处搜索谷歌,试图弄清楚当我不使用像 Jade 或 EJS 这样的模板引擎时如何在表单上实现 csurf。我的 express 服务
我已在 Node.js Express 应用程序上安装了 csurf 包。 token 以表单正确显示(看起来),name="_csrf" 且值等于使用 req.csrfToken() 设置的某个哈希
我想添加csurf作为 react-server 内部的快速中间件用于通用应用程序。 我想要实现的是将 csrf token 添加到 React 组件中表单中的隐藏输入,以保持服务器呈现的网站将提供的
我正在使用 Express 中间件 csurf 进行 CSRF 保护。如果我将它与表单一起使用,将 token 放入隐藏字段,则路由背后的操作就会起作用。现在我想做一个简单的 AJAX 调用,但 cs
我在 express 中使用 nom 的 csurf 模块。自过去 3 天以来,一切都运行良好。现在每次我发布表单时都会收到无效的 csrf token 错误。我使用的是最新版本的 csurf 和 e
我尝试在我的 express 应用程序中仅在几条路线上使用 csurf。这就是方法: var express = require('express'); var session
我读了很多关于该主题的帖子,但似乎没有一个对我收到的错误消息“ForbiddenError:无效的 csrf token ”有帮助。 从下面的入口app.js文件中可以看到,我在session中设置了
我正在使用 MEAN stack 编写一个单页面应用程序,并使用 express-session 和 redis 进行 session 管理。 我想在客户端的 cookie 中使用 scrf toke
我有这种奇怪的行为 我在第一次加载页面时遇到错误,基本上是 'EBADCSRFTOKEN' 我一直在努力弄清楚为什么它只在第一次加载页面时发生加载,如果我点击刷新并获得一个新 token ,一切正常。
我一直在检查csurf的 wiki,但它是空的。该模块为用户请求添加了一个csrfToken()函数,那么,我该如何使用呢? 有人可以给出一个代码示例和解释吗?我应该在用户端做什么?我应该在服务器端做
根据 CSURF 文档,您如何设置工作服务器以在 Postman 中测试 CSURF: http://expressjs.com/en/resources/middleware/csurf.html
我已经编写了一个项目,它已接近生产阶段,我意外地在代码中发现了一个错误 我的项目有一个页面,其中有很多表单,我使用 ajax 来发布它们,我发现我没有为每个 ajax 请求更新 csrf token
我正在尝试向我的 React 应用程序添加 csrf 保护,但始终收到错误“无效 token ” import bodyParser from 'body-parser'; import cookie
我有一个 Express 站点在带有 nginx 的代理后面运行,在我的本地开发计算机上,关于使用 csurf 和不安全 cookie 的 csrf 保护,一切都运行良好。 此后我已转移到具有 SSL
我很困惑,当我将 cookie 设置为安全时, Node 的 csrf 不工作。 //Load Cooike Parser app.use(cookieParser(secret)); //Load
Express 和 Angular 都有自己的 csrf 中间件。我根本无法让它们工作,而且互联网上似乎也没有关于此的连贯指南。我的理解是 express 4.0 使用 csurf 作为它的 csrf
通过谷歌搜索浏览并尝试了我在这里和其他地方可以找到的所有内容……但我就是无法克服这个问题。我正在使用 Node、Express、EJS,并尝试在使用 jQuery ajax 发布的表单上使用 csur
我有一个简单的快速服务器设置,例如: app.use(bodyParser.json()); app.use(cookieParser()); app.use(csurf({ cookie
我是一名优秀的程序员,十分优秀!