| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/test/histogram_tester.h" |
| 9 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 10 #include "chrome/browser/extensions/extension_apitest.h" | 11 #include "chrome/browser/extensions/extension_apitest.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_finder.h" | 14 #include "chrome/browser/ui/browser_finder.h" |
| 14 #include "chrome/browser/ui/browser_list.h" | 15 #include "chrome/browser/ui/browser_list.h" |
| 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 16 #include "chrome/common/chrome_paths.h" | 17 #include "chrome/common/chrome_paths.h" |
| 18 #include "chrome/common/url_constants.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
| 20 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/browser/notification_types.h" |
| 22 #include "content/public/browser/render_frame_host.h" |
| 18 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
| 19 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/common/result_codes.h" | 25 #include "content/public/common/result_codes.h" |
| 21 #include "content/public/common/url_constants.h" | 26 #include "content/public/common/url_constants.h" |
| 22 #include "content/public/test/browser_test_utils.h" | 27 #include "content/public/test/browser_test_utils.h" |
| 28 #include "content/public/test/test_navigation_observer.h" |
| 23 #include "extensions/browser/extension_host.h" | 29 #include "extensions/browser/extension_host.h" |
| 24 #include "extensions/browser/process_manager.h" | 30 #include "extensions/browser/process_manager.h" |
| 25 #include "extensions/common/constants.h" | 31 #include "extensions/common/constants.h" |
| 26 #include "extensions/common/extension.h" | 32 #include "extensions/common/extension.h" |
| 27 #include "extensions/test/extension_test_message_listener.h" | 33 #include "extensions/test/extension_test_message_listener.h" |
| 28 #include "extensions/test/result_catcher.h" | 34 #include "extensions/test/result_catcher.h" |
| 29 #include "net/dns/mock_host_resolver.h" | 35 #include "net/dns/mock_host_resolver.h" |
| 30 #include "net/test/embedded_test_server/embedded_test_server.h" | 36 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 38 |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 last_loaded_extension_id() + "/newtab.html"), | 283 last_loaded_extension_id() + "/newtab.html"), |
| 278 false, | 284 false, |
| 279 &newtab)); | 285 &newtab)); |
| 280 | 286 |
| 281 // Extension API should succeed. | 287 // Extension API should succeed. |
| 282 bool result = false; | 288 bool result = false; |
| 283 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()", | 289 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()", |
| 284 &result)); | 290 &result)); |
| 285 EXPECT_TRUE(result); | 291 EXPECT_TRUE(result); |
| 286 } | 292 } |
| 293 |
| 294 // Tests that calling window.open for an extension URL from a non-HTTP or HTTPS |
| 295 // URL on a new tab cannot access non-web-accessible resources. |
| 296 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, |
| 297 WindowOpenInaccessibleResourceFromDataURL) { |
| 298 base::HistogramTester uma; |
| 299 const extensions::Extension* extension = LoadExtension( |
| 300 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")); |
| 301 ASSERT_TRUE(extension); |
| 302 |
| 303 ui_test_utils::NavigateToURL(browser(), GURL("data:text/html,foo")); |
| 304 |
| 305 // test.html is not web-accessible and should not be loaded. |
| 306 GURL extension_url(extension->GetResourceURL("test.html")); |
| 307 content::WindowedNotificationObserver windowed_observer( |
| 308 content::NOTIFICATION_LOAD_STOP, |
| 309 content::NotificationService::AllSources()); |
| 310 ASSERT_TRUE(content::ExecuteScript( |
| 311 browser()->tab_strip_model()->GetActiveWebContents(), |
| 312 "window.open('" + extension_url.spec() + "');")); |
| 313 windowed_observer.Wait(); |
| 314 content::NavigationController* controller = |
| 315 content::Source<content::NavigationController>(windowed_observer.source()) |
| 316 .ptr(); |
| 317 content::WebContents* newtab = controller->GetWebContents(); |
| 318 ASSERT_TRUE(newtab); |
| 319 |
| 320 EXPECT_NE(extension_url, newtab->GetMainFrame()->GetLastCommittedURL()); |
| 321 EXPECT_FALSE(newtab->GetMainFrame()->GetSiteInstance()->GetSiteURL().SchemeIs( |
| 322 extensions::kExtensionScheme)); |
| 323 |
| 324 // Verify that the blocking was recorded correctly in UMA. |
| 325 uma.ExpectUniqueSample("Extensions.ShouldAllowOpenURL.Failure", |
| 326 2, /* FAILURE_SCHEME_NOT_HTTP_OR_HTTPS_OR_EXTENSION */ |
| 327 1); |
| 328 } |
| 329 |
| 330 // Test that navigating to an extension URL is allowed on chrome:// and |
| 331 // chrome-search:// pages, even for URLs that are not web-accessible. |
| 332 // See https://crbug.com/662602. |
| 333 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, |
| 334 NavigateToInaccessibleResourceFromChromeURL) { |
| 335 // Mint an extension URL which is not web-accessible. |
| 336 const extensions::Extension* extension = LoadExtension( |
| 337 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")); |
| 338 ASSERT_TRUE(extension); |
| 339 GURL extension_url(extension->GetResourceURL("test.html")); |
| 340 |
| 341 content::WebContents* tab = |
| 342 browser()->tab_strip_model()->GetActiveWebContents(); |
| 343 |
| 344 // Navigate to the non-web-accessible URL from chrome:// and |
| 345 // chrome-search:// pages. Verify that the page loads correctly. |
| 346 GURL history_url(chrome::kChromeUIHistoryURL); |
| 347 GURL ntp_url(chrome::kChromeSearchLocalNtpUrl); |
| 348 ASSERT_TRUE(history_url.SchemeIs(content::kChromeUIScheme)); |
| 349 ASSERT_TRUE(ntp_url.SchemeIs(chrome::kChromeSearchScheme)); |
| 350 GURL start_urls[] = {history_url, ntp_url}; |
| 351 for (size_t i = 0; i < arraysize(start_urls); i++) { |
| 352 ui_test_utils::NavigateToURL(browser(), start_urls[i]); |
| 353 EXPECT_EQ(start_urls[i], tab->GetMainFrame()->GetLastCommittedURL()); |
| 354 |
| 355 content::TestNavigationObserver observer(tab); |
| 356 ASSERT_TRUE(content::ExecuteScript( |
| 357 tab, "location.href = '" + extension_url.spec() + "';")); |
| 358 observer.Wait(); |
| 359 EXPECT_EQ(extension_url, tab->GetMainFrame()->GetLastCommittedURL()); |
| 360 std::string result; |
| 361 ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
| 362 tab, "domAutomationController.send(document.body.innerText)", &result)); |
| 363 EXPECT_EQ("HOWDIE!!!", result); |
| 364 } |
| 365 } |
| OLD | NEW |