【发布时间】:2019-11-08 11:47:49
【问题描述】:
我有一个 Rails API,我在我的application.rb 中设置了Rack::Cors,以防止来自https://my-website.com 以外的任何来源的请求:
config.middleware.insert_before 0, Rack::Cors do
allow do
origins 'https://my-website.com'
resource '*',
headers: %w[Authorization],
methods: %i[get post put delete options head],
expose: %w[Authorization]
end
end
另外我允许 websocket 连接为:
config.action_cable.url = %r{/wss:\/\/*/}
config.action_cable.allowed_request_origins = 'https://my-website.com'
最后,我的Application Load Balancer 有一个status 检查器,为此我允许/status 的http 请求为:
config.force_ssl = true
config.ssl_options = {
hsts: { subdomains: true },
redirect: { exclude: ->(request) { request.path =~ /status/ } }
}
所以你可以看到除了/status 之外的每条路径都应该是https 但我仍然在我的生产错误跟踪器中遇到如下错误:
#590 ActionController::RoutingError: No route matches [OPTIONS] "/"
在详细信息中,我得到的来源是:
当我的CORS configuration 中没有添加起点时,某人如何设法到达我的实际路线?
【问题讨论】:
-
有人可以使用非浏览器向您的服务器发出 HTTP 请求。
-
当有人访问
https://ip/时,最后一行(OPTIONS /)是一个非常标准的“CORS飞行前请求” -
CORS 不是“服务器保护”。这只是对浏览器的“建议”。
-
是的,你是对的:当你有一个公共端点时,任何人都可以请求。此外,当你有一个生产应用程序时,你每天都会在日志中看到来自机器人、黑客等的大量请求。所以,唯一的办法就是实现认证。
标签: ruby-on-rails security cors ruby-on-rails-5