Skip to content

contentWindow always null for cross origin iframe #3939

Closed

Description

@neojski

Working on #3808, I've found the following issue:

<html>
  <body>
    <iframe src="http://example.com" id="iframe"></iframe>
    <script>
    var iframe = document.getElementById('iframe');
    var intervalId = setInterval(function() {
      if (iframe.contentWindow) {
        clearInterval(intervalId);
        console.log('done');
        return;
      }
      console.log('will try contentWindow again in a sec')
    }, 1000);
    </script>
  </body>
</html>

It should just work (I'm using ugly setInteval because servo does not support load events on iframe). However, it does not as the iframe is cross origin.

I dug into this problem a little more and here's what I've found (thanks @jdm and @mrobinson):

In htmliframeelement.rs there's GetContentWindow which fails because child is None.

Why:

  1. process_the_iframe_attributes sends a LoadIframeUrlMsg message to constellation
  2. it is handled in constellation.rs by handle_load_iframe_url_msg
  3. in our case (cross origin) new_pipeline is None
  4. self.new_pipeline calls then Pipeline::create with script_pipeline set to None
  5. pipeline.rs matches script_pipeline with None and does not send a AttachLayoutMsg to ScriptControlChan that normally then calls handle_new_layout which adds new_page to parent_page's children (and so GetContentWindow fails)

As I'm new to the project I'm not really sure as how to fix it. In particular I don't know how do we make scripts in frames having different global contexts. I'd welcome some references as what should I read to understand it.

Activity

  1. jdm commented on Nov 8, 2014

    @jdm
    Member

    It's possible that this is right-ish behaviour. I was investigating what Firefox does with contentWindow for cross-origin frames, and the resulting object is very locked down. Most properties throw permission errors if you try to access them, so we should potentially be creating a different Window-like object from contentWindow in this case if we can't find one.

  2. jdm commented on Nov 8, 2014

    @jdm
    Member

    https://html.spec.whatwg.org/multipage/browsers.html#security-window is relevant. It's tempting to say that we should be able to use the existing Window object, except that it almost certainly exists in a separate JS runtime in the cross-origin case, so we'll need to construct a new one instead.

  3. bzbarsky commented on Nov 9, 2014

    @bzbarsky
    Contributor

    What Gecko does here is return an Xray wrapper wrapper that has a restrictive security policy (with the security policy actually codegenned from IDL annotations).

    What the HTML spec has to say on this matter right now is completely useless. What you probably want to look at is Bobby's proposal at https://etherpad.mozilla.org/html5-cross-origin-objects which describes the observable behavior that we and at least some other browser implementors (iirc Google and Microsoft actually responded when we pinged them) agreed was acceptable all around. See https://www.w3.org/Bugs/Public/show_bug.cgi?id=20701 for the spec bug tracking this.

  4. added 2 commits that reference this issue on Nov 14, 2014
  5. added a commit that references this issue on Nov 13, 2015
  6. added
    A-content/dom
    A-content/bindings
    on May 12, 2016
  7. jdm commented on May 12, 2016

    @jdm
    Member

    Relevant work:

  8. jdm commented on May 12, 2016

    @jdm
    Member

    Actually, this work is being tracked in #2382 so no need to keep this open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-content/bindingsA-content/dom

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions