Skip to content

[Bug]: sandboxed iframe can modify history of parent browsing context #35391

Closed

Description

@jeremyspiegel

Preflight Checklist

Electron Version

20.0.3, same behavior going back to 17.0.0

What operating system are you using?

macOS

Operating System Version

macOS 12.5.1

What arch are you using?

arm64 (including Apple Silicon)

Last Known Working Electron version

n/a

Expected Behavior

I expect that calling history.back() from a sandboxed iframe should do nothing, which is the behavior in Chrome 104.0.5112.101.

Actual Behavior

Create three .html files:

page1.html:

<html>
    <body>
        Page 1
        <a href="/page2.html">Go to Page 2</a>
    </body>
</html>

page2.html:

<html>
    <body>
        Page 2
        <iframe src="page_iframe.html" sandbox="allow-scripts"></iframe>
    </body>
</html>

page_iframe.html:

<html>
    <body>
        <button onclick="history.back()">Go Back</button>
    </body>
</html>

Serve them:

npx http-server .

Load page 1 in Electron:

  mainWindow.loadURL('http://localhost:8080/page1.html')

and click the link to page2.html. Then click the Go Back button that is shown in the iframe on Page 2 and notice that the window is navigated back to page1.html.

Testcase Gist URL

https://gist.github.com/7d7b3f33d4a6283e9e3cc000da2ab6f9

Additional Information

This was previously reported in #3734 and #21680 but not with a reproducible case.

Activity

  1. jeremyspiegel commented on Aug 20, 2022

    @jeremyspiegel
    ContributorAuthor

    I did some tests on older Electron versions and it appears this was introduced in between Electron 14.0.0-nightly.20210427 and 14.0.0-nightly.20210430, maybe related to this change: #28839?

    Edit: That change (#28839) removed params.should_clear_history_list = true; in WebContents::LoadURL which made it seem like the issue was introduced there, but that's only because my test relied on setting up a history by calling mainWindow.loadURL twice. I modified my repro gist to use a link from page 1 to page 2 and with that code I see the issue going back as far as Electron 10.

  2. jeremyspiegel commented on Aug 23, 2022

    @jeremyspiegel
    ContributorAuthor

    I believe I've figured out the issue.

    When the sandboxed iframe calls history.back(), the renderer calls into the browser at RenderFrameHostImpl::GoToEntryAtOffset:

      if (delegate_->IsAllowedToGoToEntryAtOffset(offset)) {
        if (IsSandboxed(network::mojom::WebSandboxFlags::kTopNavigation)) {
          // Keep track of whether this is a session history from a sandboxed iframe
          // with top level navigation disallowed.
          frame_tree_->controller().GoToOffsetInSandboxedFrame(
              offset, GetFrameTreeNodeId());
        } else {
          frame_tree_->controller().GoToOffsetFromRenderer(offset);
        }
      }

    The call to delegate_->IsAllowedToGoToEntryAtOffset(offset) goes to WebContentsImpl::IsAllowedToGoToEntryAtOffset:

    bool WebContentsImpl::IsAllowedToGoToEntryAtOffset(int32_t offset) {
      // TODO(https://crbug.com/1170277): This should probably be renamed to
      // WebContentsDelegate::IsAllowedToGoToEntryAtOffset or
      // ShouldGoToEntryAtOffset
      return !delegate_ || delegate_->OnGoToEntryOffset(offset);
    }

    And Electron's implementation of delegate_->OnGoToEntryOffset(offset) in electron::api::WebContents::OnGoToEntryOffset handles the navigation itself, rather than allowing RenderFrameHostImpl::GoToEntryAtOffset's default behavior of navigating in the frame instead of the parent context:

    bool WebContents::OnGoToEntryOffset(int offset) {
      GoToOffset(offset);
      return false;
    }

    I tried removing the override of OnGoToEntryOffset in electron::api::WebContents (the default implementation of WebContentsDelegate::OnGoToEntryOffset is just return true), and that fixed the issue for me.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions