缺少 CSRF 令牌,Ionic - AngularJS FullStack

CSRF token missing, Ionic - AngularJS FullStack

我有一个后端在 MEAN 堆栈上的项目,从 AngularJS Full-Stack generator 开始,在 Ionic 下有一个应用程序,当我尝试执行 POST 请求以从 Ionic 应用程序进行登录时服务器说 "CSRF token missing"

{"error":{"message":"CSRF token missing","stack":"Error: CSRF token missing\n    at checkCsrf (/Volumes/Data/Dev/carry/back/node_modules/lusca/lib/csrf.js:89:18)\n    at /Volumes/Data/Dev/carry/back/node_modules/lusca/index.js:48:21\n    at hsts (/Volumes/Data/Dev/carry/back/node_modules/lusca/lib/hsts.js:25:9)\n    at /Volumes/Data/Dev/carry/back/node_modules/lusca/index.js:48:21\n    at xframe (/Volumes/Data/Dev/carry/back/node_modules/lusca/lib/xframes.js:12:9)\n    at /Volumes/Data/Dev/carry/back/node_modules/lusca/index.js:48:21\n    at xssProtection (/Volumes/Data/Dev/carry/back/node_modules/lusca/lib/xssprotection.js:16:9)\n    at /Volumes/Data/Dev/carry/back/node_modules/lusca/index.js:48:21\n    at lusca (/Volumes/Data/Dev/carry/back/node_modules/lusca/index.js:53:9)\n    at Layer.handle [as handle_request] (/Volumes/Data/Dev/carry/back/node_modules/express/lib/router/layer.js:95:5)\n    at trim_prefix (/Volumes/Data/Dev/carry/back/node_modules/express/lib/router/index.js:312:13)\n    at /Volumes/Data/Dev/carry/back/node_modules/express/lib/router/index.js:280:7\n    at Function.process_params (/Volumes/Data/Dev/carry/back/node_modules/express/lib/router/index.js:330:12)\n    at next (/Volumes/Data/Dev/carry/back/node_modules/express/lib/router/index.js:271:10)\n    at /Volumes/Data/Dev/carry/back/node_modules/express-session/index.js:432:7\n    at /Volumes/Data/Dev/carry/back/node_modules/connect-mongo/lib/connect-mongo.js:305:11\n    at handleCallback (/Volumes/Data/Dev/carry/back/node_modules/mongoose/node_modules/mongodb/lib/utils.js:96:12)\n    at /Volumes/Data/Dev/carry/back/node_modules/mongoose/node_modules/mongodb/lib/collection.js:1341:5\n    at handleCallback (/Volumes/Data/Dev/carry/back/node_modules/mongoose/node_modules/mongodb/lib/utils.js:96:12)\n    at /Volumes/Data/Dev/carry/back/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:670:5\n    at handleCallback (/Volumes/Data/Dev/carry/back/node_modules/mongoose/node_modules/mongodb-core/lib/cursor.js:154:5)\n    at nextFunction (/Volumes/Data/Dev/carry/back/node_modules/mongoose/node_modules/mongodb-core/lib/cursor.js:675:5)"}}

尽管在请求中看到它发送了令牌和其他数据

POST /auth/local HTTP/1.1
Host: 192.168.1.13:9000
Connection: keep-alive
Content-Length: 47
Accept: application/json, text/plain, */*
X-DevTools-Emulate-Network-Conditions-Client-Id: 552547EB-CA80-4AF8-8392-DDE2A9D833A4
Origin: file://
User-Agent: Mozilla/5.0 (Linux; Android 5.1.1; E5803 Build/32.0.A.4.11; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/48.0.2564.106 Mobile Safari/537.36
Content-Type: application/json;charset=UTF-8
Accept-Encoding: gzip, deflate
Accept-Language: en-US
Cookie: connect.sid=s%3AKpTipuTW9UAqmbx_X__fuDrfGxXiGRpF.%2FKf2gm3y%2F0VwBzUygchh7%2BVfi6PLoQZhOfI5T22XlxY; XSRF-TOKEN=iZvZ2wKb3VafJb9ZGqily3pBY3nGI9gVBQaww%3D
X-Requested-With: com.todomicilio.app

我不修改快递服务器的默认配置

您似乎忘记在 <form></form> 中添加用于在 html 中制作 POST 的 csrf 令牌。

你的数据中只有cookie中有CSRF token,你还需要在form中有CSRF token(或者在特殊的http请求头中,这取决于保护实现)。

关于跨站请求伪造 (CSRF) 的几句话:

  1. 如何进行CSRF攻击:

假设我们有一个网站 "hacker.example" 并且我们还知道另一个用钱运营的网站 "bank.example"。想象一下,bank.example 有一个 POST 方法 /send-money 从当前用户帐户向另一个帐户发送资金 receiver-account 参数,银行使用 cookie 进行用户授权。

想象一下,来自银行的用户进入了黑客的网站并单击了将表单提交到 bank.example/send-money 的按钮,其中包含 receiver-account 参数的黑客值。因此,浏览器将使用 bank.example(!) 的 cookie 向 bank.example 网站发出此 POST 请求,因此,如果银行没有针对 CSRF 的保护,则此请求将被授权和黑客将从该用户那里得到一笔钱。

  1. 如何保护 CSRF 攻击(可能的解决方案之一):

您可以放置​​两个秘密令牌:一个在 cookie 中,另一个用于网站的每个 post 请求。每次用户发出 post 请求时,服务器都应该比较这两个令牌。

  1. 为什么有效?

现在,hacker.example 应该知道 csrf 令牌才能发出适当的 POST 请求。黑客只有两种可能的选择来获取它:从 bank.example 的 cookie 或从 bank.example.

的 html 代码中提取 csrf 令牌

但是 hacker.example 由于安全策略无法访问来自 bank.example 的 cookie,并且 hacker.example 无法获取 html 页面以从 [=22] 中提取 csrf 令牌=] 来自 bank.example 因为 CORS.

如果您不想使用 CSRF 令牌,请禁用 Lusca。

请参考以下内容link

https://github.com/angular-fullstack/generator-angular-fullstack/issues/1582

虽然有点晚了,但我还是想回答一下,因为我遇到了同样的问题。 这是问题的解决方案:

https://github.com/angular-fullstack/generator-angular-fullstack/pull/2613

”当 express.static() 服务时 index.html 它打破了中间件链并避免在/请求上设置 CRSF,导致第一次登录失败,只要用户没有设置 cookie .

我认为这与 #2224、#2511、#2611 和 krakenjs/lusca#95

相关并可能修复

这次提交基本上将 _index.html 重命名为 app.template.html ,这更具表现力,生成的文件将是 app.html ,这避免了 express.static() 阻塞中间件链, 因为没有 index.html 了。因此 routes.js:sendFile 被有效调用。

请注意,我尝试并使用 webpack 进行了很多尝试,试图找到另一种解决方案,这是我能找到的最优雅的解决方案。"