From 67290253dd6de7e23dcd75352dce7b572f0e5eb8 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Thu, 30 Jun 2022 12:42:54 -0400 Subject: [PATCH] Clear window.name on cross-origin navigation --- .../window_name_browsertest.cc | 37 ++++++++++++++----- .../blink/renderer/core/page/frame_tree.cc | 9 ++--- .../blink/renderer/core/page/frame_tree.h | 8 ++-- test/data/window_name/set_window_name.html | 1 + .../set_window_name_same_origin.html | 12 ++++++ 5 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 test/data/window_name/set_window_name_same_origin.html diff --git a/browser/test/disabled_features/window_name_browsertest.cc b/browser/test/disabled_features/window_name_browsertest.cc index fd33867c6f6e..81964ddc05a8 100644 --- a/browser/test/disabled_features/window_name_browsertest.cc +++ b/browser/test/disabled_features/window_name_browsertest.cc @@ -14,6 +14,7 @@ #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" @@ -21,6 +22,7 @@ namespace { const char kEmbeddedTestServerDirectory[] = "window_name"; const char kWindowNameScript[] = "window.name"; +const char kLinkID[] = "clickme"; } // namespace @@ -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 content_client_; std::unique_ptr 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)); } @@ -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)); @@ -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)); diff --git a/chromium_src/third_party/blink/renderer/core/page/frame_tree.cc b/chromium_src/third_party/blink/renderer/core/page/frame_tree.cc index 7f9931ee9351..2600e079578d 100644 --- a/chromium_src/third_party/blink/renderer/core/page/frame_tree.cc +++ b/chromium_src/third_party/blink/renderer/core/page/frame_tree.cc @@ -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 diff --git a/chromium_src/third_party/blink/renderer/core/page/frame_tree.h b/chromium_src/third_party/blink/renderer/core/page/frame_tree.h index d368319cb61e..de4c968a6adc 100644 --- a/chromium_src/third_party/blink/renderer/core/page/frame_tree.h +++ b/chromium_src/third_party/blink/renderer/core/page/frame_tree.h @@ -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_ diff --git a/test/data/window_name/set_window_name.html b/test/data/window_name/set_window_name.html index 5f6b14f97798..fb9c8639dcea 100644 --- a/test/data/window_name/set_window_name.html +++ b/test/data/window_name/set_window_name.html @@ -7,5 +7,6 @@ + Click me diff --git a/test/data/window_name/set_window_name_same_origin.html b/test/data/window_name/set_window_name_same_origin.html new file mode 100644 index 000000000000..74d85dd34d3f --- /dev/null +++ b/test/data/window_name/set_window_name_same_origin.html @@ -0,0 +1,12 @@ + + + + + + + + +