如何让Sinatra避免添加X-Frame-Options标头?

Bru*_*uce 35 ruby iframe sinatra x-frame-options

我正在使用Sinatra返回一些IFRAME内容,我想允许跨域src.不幸的是,Sinatra会自动为我的回复添加一个X-Frame-Options标头.我怎么把它关掉?

mat*_*att 80

Sinatra使用Rack :: Protection,特别是frame_options选项,这就是设置X-Frame-Options标题.

您可以配置使用哪些保护.默认情况下,Sinatra会将其中的大多数设置为开启(有些仅在您使用会话时启用,而Rack :: Protection本身默认情况下不启用某些功能).

要防止发送X-Frame-Options标头,您需要frame_options像这样禁用:

set :protection, :except => :frame_options
Run Code Online (Sandbox Code Playgroud)


xen*_*tek 5

Another solution, and the one I ended up with in production, involves monkey-patching Rack::Protection::FrameOptions:

# This monkeypatch is needed to ensure the X-Frame-Options header is
# never set by rack-protection.
module Rack
  module Protection
    class FrameOptions < Base
      def call(env)
        status, headers, body = @app.call(env)
        [status, headers, body]
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)