Rails 4 appears to set a default value of SAMEORIGIN
for the X-Frame-Options
HTTP response header. This is great for security, but it does not allow for parts of your app to be available in an iframe
on a different domain.
You can override the value of X-Frame-Options
globally using the config.action_dispatch.default_headers
setting:
config.action_dispatch.default_headers['X-Frame-Options'] = "ALLOW-FROM https://apps.facebook.com"
But how do you override it for just a single controller or action?
X-Frame-Options Deprecated While the X-Frame-Options header is supported by the major browsers, it has been obsoleted in favour of the frame-ancestors directive from the CSP Level 2 specification.
X-Frame-Options allows content publishers to prevent their own content from being used in an invisible frame by attackers. The DENY option is the most secure, preventing any use of the current page in a frame. More commonly, SAMEORIGIN is used, as it does enable the use of frames, but limits them to the current domain.
Double-click the HTTP Response Headers icon in the feature list in the middle. In the Actions pane on the right side, click Add. In the dialog box that appears, type X-Frame-Options in the Name field and type SAMEORIGIN in the Value field. Click OK to save your changes.
If you want to remove the header completely, you can create an after_action
filter:
class FilesController < ApplicationController after_action :allow_iframe, only: :embed def embed end private def allow_iframe response.headers.except! 'X-Frame-Options' end end
Or, of course, you can code the after_action
to set the value to something different:
class FacebookController < ApplicationController after_action :allow_facebook_iframe private def allow_facebook_iframe response.headers['X-Frame-Options'] = 'ALLOW-FROM https://apps.facebook.com' end end
Note that you need to clear your cache in certain browsers (Chrome for me) while debugging this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With