我有一个托管在 Heroku 上的 Ruby on Rails 应用程序。
为了进行安全审查以允许我们的应用程序,我们被告知要禁用许多 HTTP 方法。
"The application web server must be configured to disable the TRACE and other HTTP methods if not being used."
Heroku 可以做到这一点吗?如果没有,有没有办法在应用程序级别禁用这些方法?
最佳答案
在应用程序级别,您可以将其添加到 application_controller.rb 文件中
before_filter :reject_methods
def reject_methods
if ['TRACE','PATCH'].include?(request.method)
#raise ActionController::RoutingError.new('Not Found')
raise ActionController::MethodNotAllowed.new('Method not allowed') #405
# or whatever you want to do (redirect, error message, ...)
end
end
或者你可以尝试使用 https://github.com/jtrupiano/rack-rewrite (检查任意重写)与这样的东西(未经测试):
rewrite %r{(.*)}, lambda { |match, rack_env|
rack_env["REQUEST_METHOD"] == "TRACE" ? "405.html" : match[1]
}
或者您可以通过将其放入文件 ./lib 中来使用自己的中间件:
module Rack
class RejectMethods
def initialize(app)
@app = app
end
def call(env)
status, headers, body = @app.call(env)
if env["REQUEST_METHOD"] == "TRACE" || env["REQUEST_METHOD"] == "PATCH"
body.close if body.respond_to? :close
[status, headers, []]
else
[status, headers, body]
end
end
end
end
并在application.rb中调用它
config.autoload_paths += %W(#{config.root}/lib)
config.middleware.use "RejectMethods"
关于ruby-on-rails - Heroku 上禁用的 HTTP 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17473760/