3

我有一個MEAN堆棧上的後端項目,開始於AngularJS Full-Stack generator和Ionic下的應用程序,當我嘗試執行POST請求以從Ionic應用程序執行登錄時,服務器說「CSRF令牌丟失」CSRF令牌丟失,Ionic - AngularJS FullStack

{"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 

我不要修改Express服務器的默認配置

回答

4

似乎您忘記在<form></form>中添加用於在HTML中進行POST的csrf標記。

在您的數據中只有Cookie中有CSRF令牌,您還需要在表單中(或者在特殊的http請求頭中,這取決於保護實現)具有CSRF令牌。

關於跨站請求僞造一對夫婦的話(CSRF):

  1. 如何使CSRF攻擊:

假設我們有一個網站 「hacker.example」,也我們知道另一個網站「bank.example」,用錢操作。想象一下,bank.example有一個POST方法/send-money將錢從當前用戶帳戶發送到另一個帳戶receiver-account參數和銀行使用cookie用於用戶授權。

想象一下,來自銀行的用戶進入黑客的網站並點擊提交表單到bank.example/send-money的按鈕,黑客的值爲receiver-account參數。因此,瀏覽器會將此POST請求發送到bank.example網站,其中包含bank.example(!)的Cookie,因此,如果銀行沒有CSRF保護,則該請求將被授權,黑客將從該用戶那裏獲得一筆錢。

  • 如何保護CSRF攻擊(可能的解決方案之一):
  • 你可以放兩個祕密憑證:一個在cookie,併爲每個POST請求的網站。每次用戶發出請求時,服務器應該比較這兩個令牌。

    1. 它爲什麼會起作用?

    現在,hacker.example應該知道csrf令牌來發出適當的POST請求。黑客只有兩種可能的選項來獲取它:從bank.example的cookie中提取或從bank.example的html代碼中提取csrf令牌。

    hacker.example用不上從bank.example因爲安全策略和hacker.example的餅乾不能得到html頁面從<form>bank.example因爲CORS提取CSRF令牌。

    +0

    很NICE的解釋! – vinesh

    0

    雖然這是有點晚了,但我想回答,因爲我面臨同樣的問題。 這裏是解決問題:

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

    「當express.static()被服務的index.html它打破了中間件鏈和避免了CRSF被設置/請求,引起第一次登錄到失敗,只要用戶沒有設置cookies。

    我覺得這涉及並可能修復#2224,#2511,#2611和krakenjs/lusca#95

    這基本上犯重命名_index.html到app.template。 html更具表現力,生成的文件將是app.html,避免expr ess.static()阻止中間件鏈,因爲沒有index.html了。因此routes.js:sendFile被有效地調用。

    注意,我試着和周圍有很多玩的WebPack試圖尋找另一種解決方案,這是最優雅的一個我能找到。」

    相關問題