File: tab_under_blocker_browsertest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (392 lines) | stat: -rw-r--r-- 16,232 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <string>

#include "base/containers/contains.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/ui/blocked_content/tab_under_navigation_throttle.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/blocked_content/popup_opener_tab_helper.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/policy_constants.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "extensions/test/test_extension_dir.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/page_transition_types.h"
#include "url/gurl.h"

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/blocked_content/framebust_block_tab_helper.h"
#endif

class TabUnderBlockerBrowserTest : public extensions::ExtensionBrowserTest {
 public:
  TabUnderBlockerBrowserTest() {
    scoped_feature_list_.InitAndEnableFeature(kBlockTabUnders);

    provider_.SetDefaultReturns(
        /*is_initialization_complete_return=*/true,
        /*is_first_policy_load_complete_return=*/true);
    policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
  }

  TabUnderBlockerBrowserTest(const TabUnderBlockerBrowserTest&) = delete;
  TabUnderBlockerBrowserTest& operator=(const TabUnderBlockerBrowserTest&) =
      delete;

  ~TabUnderBlockerBrowserTest() override {}

  void SetUpOnMainThread() override {
    extensions::ExtensionBrowserTest::SetUpOnMainThread();
    host_resolver()->AddRule("*", "127.0.0.1");
    ASSERT_TRUE(embedded_test_server()->Start());
  }

  void UpdatePopupPolicy(ContentSetting popup_setting) {
    policy::PolicyMap policy;
    policy.Set(policy::key::kDefaultPopupsSetting,
               policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
               policy::POLICY_SOURCE_CLOUD, base::Value(popup_setting),
               nullptr /* external_data_fetcher */);
    provider_.UpdateChromePolicy(policy);
  }

  static std::string GetError(const GURL& blocked_url) {
    return base::StringPrintf(kBlockTabUnderFormatMessage,
                              blocked_url.spec().c_str());
  }

  content::WebContents* NavigateAndOpenPopup(const GURL& url) {
    EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

    content::TestNavigationObserver navigation_observer(nullptr, 1);
    navigation_observer.StartWatchingNewWebContents();

    content::WebContents* opener =
        browser()->tab_strip_model()->GetActiveWebContents();
    EXPECT_TRUE(content::ExecJs(opener, "window.open('/title1.html')"));
    navigation_observer.Wait();

    EXPECT_TRUE(blocked_content::PopupOpenerTabHelper::FromWebContents(opener)
                    ->has_opened_popup_since_last_user_gesture());
    EXPECT_EQ(opener->GetVisibility(), content::Visibility::HIDDEN);

    return opener;
  }

  bool IsUiShownForUrl(content::WebContents* web_contents, const GURL& url) {
// TODO(csharrison): Implement android checking when crbug.com/611756 is
// resolved.
#if BUILDFLAG(IS_ANDROID)
    return false;
#else
    return base::Contains(
        FramebustBlockTabHelper::FromWebContents(web_contents)->blocked_urls(),
        url);
#endif
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
  testing::NiceMock<policy::MockConfigurationPolicyProvider> provider_;
};

class TabUnderBlockerFencedFrameTest : public TabUnderBlockerBrowserTest {
 public:
  content::test::FencedFrameTestHelper& fenced_frame_test_helper() {
    return fenced_frame_helper_;
  }

  void CreateFencedFrame(content::WebContents* web_contents) {
    const GURL fenced_frame_url =
        embedded_test_server()->GetURL("/fenced_frames/title1.html");
    ASSERT_TRUE(fenced_frame_test_helper().CreateFencedFrame(
        web_contents->GetPrimaryMainFrame(), fenced_frame_url));
    EXPECT_EQ(web_contents->GetVisibility(), content::Visibility::HIDDEN);
  }

 protected:
  content::test::FencedFrameTestHelper fenced_frame_helper_;
};

IN_PROC_BROWSER_TEST_F(TabUnderBlockerBrowserTest, SimpleTabUnder_IsBlocked) {
  content::WebContents* opener =
      NavigateAndOpenPopup(embedded_test_server()->GetURL("/title1.html"));

  content::TestNavigationObserver tab_under_observer(opener, 1);
  const GURL cross_origin_url =
      embedded_test_server()->GetURL("a.com", "/title1.html");

  std::string expected_error = GetError(cross_origin_url);
  content::WebContentsConsoleObserver console_observer(opener);
  console_observer.SetPattern(expected_error);

  EXPECT_TRUE(
      content::ExecJs(opener,
                      base::StringPrintf("window.location = '%s';",
                                         cross_origin_url.spec().c_str()),
                      content::EXECUTE_SCRIPT_NO_USER_GESTURE));
  tab_under_observer.Wait();
  EXPECT_FALSE(tab_under_observer.last_navigation_succeeded());

  // Round trip to the renderer to ensure the message was sent. Don't use Wait()
  // to be consistent with the NotBlocked test below, which has to use this
  // technique.
  EXPECT_TRUE(content::ExecJs(opener, "var a = 0;"));
  EXPECT_EQ(expected_error, console_observer.GetMessageAt(0u));
  EXPECT_TRUE(IsUiShownForUrl(opener, cross_origin_url));
}

IN_PROC_BROWSER_TEST_F(TabUnderBlockerFencedFrameTest,
                       SimpleTabUnder_WithFencedFrame_IsBlocked) {
  content::WebContents* opener =
      NavigateAndOpenPopup(embedded_test_server()->GetURL("/title1.html"));

  CreateFencedFrame(opener);

  content::TestNavigationObserver tab_under_observer(opener, 1);
  const GURL cross_origin_url =
      embedded_test_server()->GetURL("a.com", "/title1.html");

  std::string expected_error = GetError(cross_origin_url);
  content::WebContentsConsoleObserver console_observer(opener);
  console_observer.SetPattern(expected_error);

  EXPECT_TRUE(
      content::ExecJs(opener,
                      base::StringPrintf("window.location = '%s';",
                                         cross_origin_url.spec().c_str()),
                      content::EXECUTE_SCRIPT_NO_USER_GESTURE));
  tab_under_observer.Wait();
  EXPECT_FALSE(tab_under_observer.last_navigation_succeeded());

  // Round trip to the renderer to ensure the message was sent. Don't use Wait()
  // to be consistent with the NotBlocked test below, which has to use this
  // technique.
  EXPECT_TRUE(content::ExecJs(opener, "var a = 0;"));
  EXPECT_EQ(expected_error, console_observer.GetMessageAt(0u));
  EXPECT_TRUE(IsUiShownForUrl(opener, cross_origin_url));
}

IN_PROC_BROWSER_TEST_F(TabUnderBlockerBrowserTest,
                       RedirectAfterGesture_IsNotBlocked) {
  content::WebContents* opener =
      NavigateAndOpenPopup(embedded_test_server()->GetURL("/title1.html"));

  // Perform some user gesture on the page.
  content::SimulateMouseClick(opener, 0, blink::WebMouseEvent::Button::kLeft);

  content::TestNavigationObserver tab_under_observer(opener, 1);
  const GURL cross_origin_url =
      embedded_test_server()->GetURL("a.com", "/title1.html");
  content::WebContentsConsoleObserver console_observer(opener);
  console_observer.SetPattern(GetError(cross_origin_url));
  EXPECT_TRUE(
      content::ExecJs(opener,
                      base::StringPrintf("window.location = '%s';",
                                         cross_origin_url.spec().c_str()),
                      content::EXECUTE_SCRIPT_NO_USER_GESTURE));
  tab_under_observer.Wait();
  EXPECT_TRUE(tab_under_observer.last_navigation_succeeded());

  // Round trip to the renderer to ensure the message would have been sent.
  EXPECT_TRUE(content::ExecJs(opener, "var a = 0;"));
  EXPECT_TRUE(console_observer.messages().empty());
  EXPECT_FALSE(IsUiShownForUrl(opener, cross_origin_url));
}

IN_PROC_BROWSER_TEST_F(TabUnderBlockerFencedFrameTest,
                       RedirectAfterGesture_WithFF_IsNotBlocked) {
  content::WebContents* opener =
      NavigateAndOpenPopup(embedded_test_server()->GetURL("/title1.html"));

  CreateFencedFrame(opener);

  // Perform some user gesture on the page.
  content::SimulateMouseClick(opener, 0, blink::WebMouseEvent::Button::kLeft);

  content::TestNavigationObserver tab_under_observer(opener, 1);
  const GURL cross_origin_url =
      embedded_test_server()->GetURL("a.com", "/title1.html");
  content::WebContentsConsoleObserver console_observer(opener);
  console_observer.SetPattern(GetError(cross_origin_url));
  EXPECT_TRUE(
      content::ExecJs(opener,
                      base::StringPrintf("window.location = '%s';",
                                         cross_origin_url.spec().c_str()),
                      content::EXECUTE_SCRIPT_NO_USER_GESTURE));
  tab_under_observer.Wait();
  EXPECT_TRUE(tab_under_observer.last_navigation_succeeded());

  // Round trip to the renderer to ensure the message would have been sent.
  EXPECT_TRUE(content::ExecJs(opener, "var a = 0;"));
  EXPECT_TRUE(console_observer.messages().empty());
  EXPECT_FALSE(IsUiShownForUrl(opener, cross_origin_url));
}

IN_PROC_BROWSER_TEST_F(TabUnderBlockerBrowserTest,
                       SpoofCtrlClickTabUnder_IsBlocked) {
  content::WebContents* opener =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL("/links.html")));
  const std::string cross_origin_url =
      embedded_test_server()->GetURL("a.com", "/title1.html").spec();

  static constexpr char kScript[] =
      "var evt = new MouseEvent('click', {"
      "  view : window,"
#if BUILDFLAG(IS_MAC)
      "  metaKey : true"
#else
      "  ctrlKey : true"
#endif
      "});"
      "document.getElementById('title1').dispatchEvent(evt);"
      "window.location = '%s';";

  content::TestNavigationObserver navigation_observer(nullptr, 1);
  content::TestNavigationObserver tab_under_observer(opener, 1);
  navigation_observer.StartWatchingNewWebContents();
  EXPECT_TRUE(content::ExecJs(
      opener, base::StringPrintf(kScript, cross_origin_url.c_str()).c_str()));
  navigation_observer.Wait();
  tab_under_observer.Wait();
  EXPECT_FALSE(tab_under_observer.last_navigation_succeeded());
}

IN_PROC_BROWSER_TEST_F(TabUnderBlockerBrowserTest,
                       ControlledByEnterprisePolicy) {
  // Allow tab-unders via enterprise policy, should disable tab-under blocking.
  UpdatePopupPolicy(CONTENT_SETTING_ALLOW);

  content::WebContents* opener =
      NavigateAndOpenPopup(embedded_test_server()->GetURL("/title1.html"));

  // First tab-under attempt should succeed.
  {
    content::TestNavigationObserver tab_under_observer(opener, 1);
    const GURL cross_origin_url =
        embedded_test_server()->GetURL("a.com", "/title1.html");
    EXPECT_TRUE(
        content::ExecJs(opener,
                        base::StringPrintf("window.location = '%s';",
                                           cross_origin_url.spec().c_str()),
                        content::EXECUTE_SCRIPT_NO_USER_GESTURE));
    tab_under_observer.Wait();

    EXPECT_TRUE(tab_under_observer.last_navigation_succeeded());
    EXPECT_FALSE(IsUiShownForUrl(opener, cross_origin_url));
  }

  // Disallow tab-unders via policy and try to tab-under again in the background
  // tab. Should fail to navigate.
  {
    UpdatePopupPolicy(CONTENT_SETTING_BLOCK);
    content::TestNavigationObserver tab_under_observer(opener, 1);
    const GURL cross_origin_url =
        embedded_test_server()->GetURL("b.com", "/title1.html");
    EXPECT_TRUE(
        content::ExecJs(opener,
                        base::StringPrintf("window.location = '%s';",
                                           cross_origin_url.spec().c_str()),
                        content::EXECUTE_SCRIPT_NO_USER_GESTURE));
    tab_under_observer.Wait();

    EXPECT_FALSE(tab_under_observer.last_navigation_succeeded());
    EXPECT_TRUE(IsUiShownForUrl(opener, cross_origin_url));
  }
}

// TODO(csharrison): Add a test verifying that navigating _to_ an extension in a
// tab-under is not blocked. This is a bit trickier to test since it involves
// ensuring a background page is up and running for the extension, and using the
// chrome.tabs API.
IN_PROC_BROWSER_TEST_F(TabUnderBlockerBrowserTest,
                       NavigateFromExtensions_Allowed) {
  const extensions::Extension* extension =
      LoadExtension(test_data_dir_.AppendASCII("simple_with_file"));

  const GURL extension_url = extension->GetResourceURL("file.html");
  content::WebContents* opener = NavigateAndOpenPopup(extension_url);

  content::TestNavigationObserver tab_under_observer(opener, 1);
  const GURL cross_origin_url =
      embedded_test_server()->GetURL("b.com", "/title1.html");
  EXPECT_TRUE(
      content::ExecJs(opener,
                      base::StringPrintf("window.location = '%s';",
                                         cross_origin_url.spec().c_str()),
                      content::EXECUTE_SCRIPT_NO_USER_GESTURE));
  tab_under_observer.Wait();

  EXPECT_TRUE(tab_under_observer.last_navigation_succeeded());
  EXPECT_FALSE(IsUiShownForUrl(opener, cross_origin_url));
}

IN_PROC_BROWSER_TEST_F(TabUnderBlockerBrowserTest, ControlledBySetting) {
  // Allow tab-unders via popup/redirect blocker settings, should disable
  // tab-under blocking.
  const GURL top_level_url = embedded_test_server()->GetURL("/title1.html");
  content::WebContents* opener = NavigateAndOpenPopup(top_level_url);

  // First tab-under attempt should be blocked.
  {
    content::TestNavigationObserver tab_under_observer(opener, 1);
    const GURL cross_origin_url =
        embedded_test_server()->GetURL("b.com", "/title1.html");
    EXPECT_TRUE(
        content::ExecJs(opener,
                        base::StringPrintf("window.location = '%s';",
                                           cross_origin_url.spec().c_str()),
                        content::EXECUTE_SCRIPT_NO_USER_GESTURE));
    tab_under_observer.Wait();

    EXPECT_FALSE(tab_under_observer.last_navigation_succeeded());
    EXPECT_TRUE(IsUiShownForUrl(opener, cross_origin_url));
  }

  // Allow tab-unders via settings, should be allowed.
  {
    HostContentSettingsMap* settings_map =
        HostContentSettingsMapFactory::GetForProfile(browser()->profile());
    settings_map->SetContentSettingDefaultScope(top_level_url, GURL(),
                                                ContentSettingsType::POPUPS,
                                                CONTENT_SETTING_ALLOW);
    content::TestNavigationObserver tab_under_observer(opener, 1);
    const GURL cross_origin_url =
        embedded_test_server()->GetURL("a.com", "/title1.html");
    EXPECT_TRUE(
        content::ExecJs(opener,
                        base::StringPrintf("window.location = '%s';",
                                           cross_origin_url.spec().c_str()),
                        content::EXECUTE_SCRIPT_NO_USER_GESTURE));
    tab_under_observer.Wait();

    EXPECT_TRUE(tab_under_observer.last_navigation_succeeded());
    EXPECT_FALSE(IsUiShownForUrl(opener, cross_origin_url));
  }
}