我已經編寫了一個服務器來處理跨站點JSON請求。這是一個旨在被ajax調用的API。我得到了它的工作,但我仍然收到一些奇怪的警告。Sinatra/Thin:CORS xhr預檢請求中的CSRF警告
由於一些API調用是柱的,有其觸發此警告(薄輸出)預檢OPTIONS請求:
127.0.0.1 - - [15/Aug/2013 22:24:20] "OPTIONS /login HTTP/1.1" 200 - 0.0080
W, [2013-08-15T22:24:20.124254 #3236] WARN -- : attack prevented by Rack::Prote
ction::HttpOrigin
下面是導致此請求的預檢頭:
OPTIONS /login HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:4567
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
Access-Control-Request-Headers: origin, content-type
Accept: */*
Referer: http://localhost:4567/index.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
另外,我想知道我得到這樣的警告:
SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
This poses a security threat. It is strongly recommended that you
provide a secret to prevent exploits that may be possible from crafted
cookies. This will not be supported in future versions of Rack, and
future versions will even invalidate your existing user cookies.
這裏的服務器代碼標頭應該被允許CORS XHR:
enable :sessions
before do
headers['Access-Control-Allow-Origin'] = 'http://localhost:4567'
headers['Access-Control-Allow-Headers'] = 'origin, content-type, accept'
headers['Access-Control-Allow-Credentials'] = 'true'
if request.request_method == 'OPTIONS'
headers["Access-Control-Allow-Methods"] = "POST, GET"
halt 200
end
end