¿Cómo consigo que Sinatra se abstenga de agregar el encabezado X-Frame-Options?


Estoy usando Sinatra para devolver algunos contenidos de IFRAME, y me gustaría permitir src entre dominios. Desafortunadamente, Sinatra está agregando automáticamente un encabezado X-Frame-Options a mi respuesta. ¿Cómo lo apago?

Author: Bruce, 2011-10-20

6 answers

Sinatra utiliza Rack::Protection, en particular la opción frame_options, que es lo que está configurando el encabezado X-Frame-Options.

Puede configurar qué protecciones se utilizan. Sinatra activa la mayoría de ellas por defecto, (algunas solo están habilitadas si también estás usando sesiones, y Rack::Protection no habilita algunas por defecto).

Para evitar el envío de la cabecera X-Frame-Options necesitas desactivar frame_options así:

set :protection, :except => :frame_options
 78
Author: matt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-22 21:03:48

Otra solución, y la que terminé con en producción, involucra el parche de mono 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
 4
Author: xentek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-07 19:18:34

Ninguna de las opciones presentadas aquí funcionó para mi aplicación sinatra. Terminé agregando un filtro después para modificar el encabezado X-Frame-Options para permitir que la aplicación sea enmarcada por Facebook.

after do
  headers({ 'X-Frame-Options' => 'ALLOW-FROM apps.facebook.com' })
end
 3
Author: xentek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-31 02:08:03

La respuesta" set :protection, :except => :frame_options " no funcionó para mí, usando Sinatra-1.3.3

Tuve que hackear una solución; puse este mutha en mi config.ru archivo. Obs puedes cambiar el encabezado para que sea lo que quieras.

Config.ru

class Rack::Protection::FrameOptions
  def header
    @header ||= {}
  end
end
 2
Author: ted price,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-28 06:03:53

Creo que he encontrado una buena manera de manejar esto, pero agradecería comentarios

El objetivo es desactivar las opciones X-Frame solo por una ruta para mantener todos los beneficios de protección de rack:

    app.get'/hello_world' do
      headers({ 'X-Frame-Options' => '' })
      "HELLO WORLD"
    end

Creo que esta es una buena opción, ya que parece evitar que la protección de rack agregue el encabezado SAMEORIGIN en esta ruta

 2
Author: stujo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-12 21:28:21

En realidad, la solución dada por @matt sigue funcionando con Sinatra v1.4.5.

Sí, Sinatra está usando Rack:: Protection y de acuerdo con Configurando la protección contra ataques

Puede desactivar la protección en absoluto (lo cual no se recomienda):

disable :protection

O solo deshabilitar frame_options:

set :protection, :except => :frame_options

Aparte de eso, si tu problema no es debido a X-Frame-Options, puede ser Access-Control-Allow-Origin, entonces lo que usted tiene lo que hay que hacer es agregar la siguiente línea a su ruta antes de la declaración de devolución:

response['Access-Control-Allow-Origin'] = 'http://www.example.com/'
 1
Author: Jing Li,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-19 12:32:37