ruby-on-rails - Heroku 上禁用的 HTTP 方法

标签 ruby-on-rails heroku

我有一个托管在 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/

相关文章:

postgresql - Heroku:从 S3 导入失败

Django python-rq -- DatabaseError SSL 错误 : decryption failed or bad record mac

heroku - 使用 Anaconda 和 OpenCV 将 Docker 应用程序部署到 Heroku 时出现 "Could not find a version that satisfies the requirement cv2==1.0"

ruby-on-rails - 城市/国家文本字段验证

ruby-on-rails - mysql2 gem 无法构建原生扩展

ruby-on-rails - Pocket API 授权 : Forbidden

postgresql - Heroku 页面 :psql stalls out

ruby-on-rails - Rvm 和 gems,bundle show 和 gem 列表

html - 隐藏一个 DIV [Rails]

django - heroku PostGIS 同步数据库错误