我编写了一个服务器来处理跨站点 JSON 请求。这是一个旨在由 ajax 调用的 API。我让它工作了,但我仍然收到一些奇怪的警告。
由于某些 API 调用是 POSTS,因此有一个预检 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