In a project we want to embed 1 view page as iframe on another website with a different domain.
Drupal 8 sets the X-Frame-Options Header hard on response with the setting SAMEORIGIN which prevents this.

You'll find the following in FinishResponsesubscriber::onRespond
$response->headers->set('X-Frame-Options', 'SAMEORIGIN', FALSE);

Lets make the domains there configurable and fallback on SAMEORIGIN if there is no configuration.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

yobottehg created an issue. See original summary.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Siekee’s picture

For my projects I definitely need the Allow from possibility configurable. Any plans on incorporating this?

rvtraveller’s picture

Assigned: Unassigned » rvtraveller

I'll see what we can do here.

rvtraveller’s picture

Version: 8.1.x-dev » 8.2.x-dev
Assigned: rvtraveller » Unassigned
Status: Active » Needs review
Issue tags: +Needs security review
FileSize
3.35 KB

Patch attached which provides the ability for the user to override the X-Frame-Options header if they know what they are doing. Personally, I don't think we should provide a UI for setting this so we can keep it as an option only for those folks who intentionally dig for it.

I'm also tagging this as needing a security review since this was added as security hardening and I'm not sure if adjusting said security hardening is something they would be up for.

sitiveni’s picture

Hi there,

Tried to use header X-Frame-Options with ALLOW-FROM and received the following error when loading the parent page/domain in Chrome:

Invalid 'X-Frame-Options' header encountered when loading 'http://embed.childdomain.com/path': 'ALLOW-FROM http://www.parentdomain.com' is not a recognized directive. The header will be ignored.

The embedded content/page still loads tho. However, turns out that ALLOW-FROM is not supported in Chrome (it seems to be fine in Firefox and IE): https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Option...

Alternatively, using header Content-Security-Policy seems to do the trick (without errors in Chrome). Here's what I added to my custom EventSubscriber (implements EventSubscriberInterface):

class MyModuleEventSubscriber implements EventSubscriberInterface {

  /**
   * Set header 'Content-Security-Policy' to response to allow embedding in iFrame.
   */
  public function setHeaderContentSecurityPolicy(FilterResponseEvent $event) {
    $response = $event->getResponse();
    $response->headers->remove('X-Frame-Options');
    $response->headers->set('Content-Security-Policy', "frame-ancestors 'self' parentdomain.com *.parentdomain.com", FALSE);
  }

  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    // Response: set header content security policy
    $events[KernelEvents::RESPONSE][] = ['setHeaderContentSecurityPolicy', -10];
    
    return $events;
  }

}

Note that X-Frame-Options needs to be removed to prevent Firefox from applying it.

In order to call that EventSubscriber you need to define it as service in your module's services.yml file (e.g. my_module.services.yml) and tag it as event_subscriber:

services:
  my_module_event_subscriber:
    class: Drupal\my_module\EventSubscriber\MyModuleEventSubscriber
    tags:
      - {name: event_subscriber}
dawehner’s picture

As showsn in #6 this is something one can change via a subscriber. This IMHO seems to be the much better place than a configuration option in the UI>

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Lukas von Blarer’s picture

Maybe I am not the right person to ask about this, because I have to create a subscriber for every site I build since I embed them in my own portfolio. But still, it would be nice if it was easier to change this header.

dawehner’s picture

@Lukas von Blarer
Yeah I don't disagree with it. One thing we could do is to use a container parameter to configure that. This requires much less effort, and well I doubt we really need a configuration UI for this specific property :)

Lukas von Blarer’s picture

Sounds good.

dawehner’s picture

Status: Needs review » Needs work

Its great that we agree here :)

Anybody’s picture

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

richard.c.allen2386’s picture

Why is this set in the first place without the ability to set it? That's insane, and breaks drush run-server functionality if you need to test iframes. This entire thought seems like an overstep in core, especially if it's not configurable, or being replaced with the followup header.

profak’s picture

@sitiveni thanks!

Your solution works perfectly.

patrick.burns.pjb’s picture

We found another module that does the trick:

https://www.drupal.org/project/seckit

It has several options, but one is to disable the clickjacking setting. We tested it out on a few sites and works like a charm.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

koryp’s picture

#6 worked for me! Thanks @sitiveni!

For security, I slightly modified the function to lock it down to a specific, trusted domain:

  public function setHeaderContentSecurityPolicy(FilterResponseEvent $event) {
    $referer = $event->getRequest()->headers->get('referer');
    if (strpos( $referer, 'https://www.example.com/' ) === 0) {
      $response = $event->getResponse();
      $response->headers->remove('X-Frame-Options');
      $response->headers->set('Content-Security-Policy', "frame-ancestors 'self' example.com *.example.com", FALSE);
    }
  }
mdjamiruddin’s picture

Version: 8.6.x-dev » 7.x-dev
Status: Needs work » Fixed

I have allowed a Drupal page to display in iframe for all other websites by updating HTTP header "X-Frame-Options" to 'GOFORIT' as given below for Drupal 7

drupal_add_http_header('X-Frame-Options', 'GOFORIT');

I have used this code in a project and its verified.

For Drupal 8, please use the same according to Drupal 8 syntax.

Anybody’s picture

Status: Fixed » Needs work

This is not solved for core in general. Please stop closing issues which you solved by a workaround only for yourself.
Also stop to change the version unintentionally!

mdjamiruddin’s picture

Thanks, I understand it and will take care onward.

mangy.fox’s picture

Version: 7.x-dev » 8.7.x-dev
Status: Needs work » Needs review
FileSize
3.39 KB

Path to the test being modified has changed in recent core updates, so I have updated the patch to match.

Status: Needs review » Needs work

The last submitted patch, 24: x-frame-options-2652616-24.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mangy.fox’s picture

Status: Needs work » Needs review
FileSize
3.4 KB

Finally got around to fixing the tests. Re-rolled for 8.8.x-dev.

Status: Needs review » Needs work

The last submitted patch, 27: x-frame-options-2652616-25.patch, failed testing. View results

mangy.fox’s picture

Now with added Migrate test fixes...

mangy.fox’s picture

Status: Needs work » Needs review

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

sadashiv’s picture

I think patch works great and was able to embed the page on my second site. I am changing one thing which is missed in the patch i.e. the form element in the site configuration so there was no option to add the domain name, I added that in the new patch and have tested it with 9.0.7.

Thanks,
Sadashiv.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

askibinski’s picture

The X-Frame-Options ALLOW-FROM options is obsolete for new browsers, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Option...

I would recommend leaving this to contrib modules like seckit where you can use the new CSP frame-ancestors directive.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

geek-merlin’s picture

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
144 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Anybody’s picture

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.