Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear window.name on cross-origin navigation #14024

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions browser/test/disabled_features/window_name_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/dns/mock_host_resolver.h"
#include "url/gurl.h"

namespace {

const char kEmbeddedTestServerDirectory[] = "window_name";
const char kWindowNameScript[] = "window.name";
const char kLinkID[] = "clickme";

} // namespace

Expand Down Expand Up @@ -64,20 +66,35 @@ class BraveWindowNameBrowserTest : public InProcessBrowserTest {
return browser()->tab_strip_model()->GetActiveWebContents();
}

void SetHref(const std::string& id, const std::string& href) {
content::RenderFrameHost* frame = web_contents()->GetMainFrame();
frame->ExecuteJavaScriptForTests(
base::ASCIIToUTF16("document.getElementById('" + id + "').href='" +
href + "';\n"),
base::NullCallback());
}

void Click(const std::string& id) {
content::TestNavigationObserver observer(web_contents());
content::RenderFrameHost* frame = web_contents()->GetMainFrame();
frame->ExecuteJavaScriptForTests(
base::ASCIIToUTF16("document.getElementById('" + id + "').click();\n"),
base::NullCallback());
observer.WaitForNavigationFinished();
}

private:
std::unique_ptr<ChromeContentClient> content_client_;
std::unique_ptr<BraveContentBrowserClient> browser_content_client_;
};

IN_PROC_BROWSER_TEST_F(BraveWindowNameBrowserTest, SameOrigin) {
GURL url1 = https_server_.GetURL("a.test", "/set_window_name.html");
GURL url2 = https_server_.GetURL("a.test", "/get_window_name.html");

GURL url1 =
https_server_.GetURL("a.test", "/set_window_name_same_origin.html");
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url1));
EXPECT_EQ("foo", EvalJs(web_contents(), kWindowNameScript));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url2));
// Since these URLs are in the same origin, window.name should persist across
// navigation.
// Navigating to url1 automatically redirects to "get_window_name.html". Since
// the original and final URLs are in the same origin, window.name should
// persist across navigation.
EXPECT_EQ("foo", EvalJs(web_contents(), kWindowNameScript));
}

Expand All @@ -87,7 +104,8 @@ IN_PROC_BROWSER_TEST_F(BraveWindowNameBrowserTest, CrossOrigin) {

EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url1));
EXPECT_EQ("foo", EvalJs(web_contents(), kWindowNameScript));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url2));
SetHref(kLinkID, url2.spec());
Click(kLinkID);
// Since these URLs are in different origins, window.name should be cleared
// during navigation.
EXPECT_EQ("", EvalJs(web_contents(), kWindowNameScript));
Expand All @@ -99,7 +117,8 @@ IN_PROC_BROWSER_TEST_F(BraveWindowNameBrowserTest, CrossOriginAndBack) {

EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url1));
EXPECT_EQ("foo", EvalJs(web_contents(), kWindowNameScript));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url2));
SetHref(kLinkID, url2.spec());
Click(kLinkID);
// Since these URLs are in different origins, window.name should be cleared
// during navigation.
EXPECT_EQ("", EvalJs(web_contents(), kWindowNameScript));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@

#include "third_party/blink/renderer/core/page/frame_tree.h"

#define CrossSiteCrossBrowsingContextGroupSetNulledName \
CrossSiteCrossBrowsingContextGroupSetNulledName_ChromiumImpl
#define ExperimentalSetNulledName ExperimentalSetNulledName_ChromiumImpl

#include "src/third_party/blink/renderer/core/page/frame_tree.cc"

#undef CrossSiteCrossBrowsingContextGroupSetNulledName
#undef ExperimentalSetNulledName

namespace blink {

void FrameTree::CrossSiteCrossBrowsingContextGroupSetNulledName() {
void FrameTree::ExperimentalSetNulledName() {
SetName(g_null_atom, kReplicate);
CrossSiteCrossBrowsingContextGroupSetNulledName_ChromiumImpl();
ExperimentalSetNulledName_ChromiumImpl();
}

} // namespace blink
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
#ifndef BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_PAGE_FRAME_TREE_H_
#define BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_PAGE_FRAME_TREE_H_

#define CrossSiteCrossBrowsingContextGroupSetNulledName \
CrossSiteCrossBrowsingContextGroupSetNulledName_ChromiumImpl(); \
void CrossSiteCrossBrowsingContextGroupSetNulledName
#define ExperimentalSetNulledName \
ExperimentalSetNulledName_ChromiumImpl(); \
void ExperimentalSetNulledName

#include "src/third_party/blink/renderer/core/page/frame_tree.h"

#undef CrossSiteCrossBrowsingContextGroupSetNulledName
#undef ExperimentalSetNulledName

#endif // BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_PAGE_FRAME_TREE_H_
1 change: 1 addition & 0 deletions test/data/window_name/set_window_name.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
</script>
</head>
<body>
<a id="clickme" href="">Click me</a>
</body>
</html>
12 changes: 12 additions & 0 deletions test/data/window_name/set_window_name_same_origin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<script>
window.name = "foo";
window.location.href = "get_window_name.html";
</script>
</head>
<body>
</body>
</html>