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 <memory> |
| 6 |
5 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" |
6 #include "base/macros.h" | 11 #include "base/macros.h" |
7 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "content/browser/child_process_security_policy_impl.h" |
8 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 14 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
9 #include "content/public/browser/navigation_entry.h" | 15 #include "content/public/browser/navigation_entry.h" |
| 16 #include "content/public/browser/render_process_host.h" |
10 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 17 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
11 #include "content/public/browser/resource_throttle.h" | 18 #include "content/public/browser/resource_throttle.h" |
12 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
13 #include "content/public/test/browser_test_utils.h" | 20 #include "content/public/test/browser_test_utils.h" |
14 #include "content/public/test/content_browser_test.h" | 21 #include "content/public/test/content_browser_test.h" |
15 #include "content/public/test/content_browser_test_utils.h" | 22 #include "content/public/test/content_browser_test_utils.h" |
16 #include "content/public/test/test_navigation_observer.h" | 23 #include "content/public/test/test_navigation_observer.h" |
17 #include "content/shell/browser/shell.h" | 24 #include "content/shell/browser/shell.h" |
18 #include "content/shell/browser/shell_content_browser_client.h" | 25 #include "content/shell/browser/shell_content_browser_client.h" |
19 #include "content/shell/browser/shell_resource_dispatcher_host_delegate.h" | 26 #include "content/shell/browser/shell_resource_dispatcher_host_delegate.h" |
| 27 #include "content/test/content_browser_test_utils_internal.h" |
20 #include "net/base/escape.h" | 28 #include "net/base/escape.h" |
21 #include "net/dns/mock_host_resolver.h" | 29 #include "net/dns/mock_host_resolver.h" |
22 #include "net/test/embedded_test_server/embedded_test_server.h" | 30 #include "net/test/embedded_test_server/embedded_test_server.h" |
23 #include "net/url_request/url_request.h" | 31 #include "net/url_request/url_request.h" |
24 #include "net/url_request/url_request_status.h" | 32 #include "net/url_request/url_request_status.h" |
| 33 #include "testing/gmock/include/gmock/gmock-matchers.h" |
25 #include "url/gurl.h" | 34 #include "url/gurl.h" |
26 | 35 |
27 namespace content { | 36 namespace content { |
28 | 37 |
29 // Tracks a single request for a specified URL, and allows waiting until the | 38 // Tracks a single request for a specified URL, and allows waiting until the |
30 // request is destroyed, and then inspecting whether it completed successfully. | 39 // request is destroyed, and then inspecting whether it completed successfully. |
31 class TrackingResourceDispatcherHostDelegate | 40 class TrackingResourceDispatcherHostDelegate |
32 : public ShellResourceDispatcherHostDelegate { | 41 : public ShellResourceDispatcherHostDelegate { |
33 public: | 42 public: |
34 TrackingResourceDispatcherHostDelegate() : throttle_created_(false) { | 43 TrackingResourceDispatcherHostDelegate() : throttle_created_(false) { |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 EXPECT_EQ(1, controller.GetEntryCount()); | 420 EXPECT_EQ(1, controller.GetEntryCount()); |
412 EXPECT_EQ(0, controller.GetCurrentEntryIndex()); | 421 EXPECT_EQ(0, controller.GetCurrentEntryIndex()); |
413 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL()); | 422 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL()); |
414 | 423 |
415 // Make sure the request for url2 did not complete. | 424 // Make sure the request for url2 did not complete. |
416 EXPECT_FALSE(tracking_delegate().WaitForTrackedURLAndGetCompleted()); | 425 EXPECT_FALSE(tracking_delegate().WaitForTrackedURLAndGetCompleted()); |
417 | 426 |
418 shell()->web_contents()->SetDelegate(old_delegate); | 427 shell()->web_contents()->SetDelegate(old_delegate); |
419 } | 428 } |
420 | 429 |
| 430 // Test that verifies that a cross-process transfer retains ability to read |
| 431 // files encapsulated by HTTP POST body that is forwarded to the new renderer. |
| 432 // Invalid handling of this scenario has been suspected as the cause of at least |
| 433 // some of the renderer kills tracked in https://crbug.com/613260. |
| 434 IN_PROC_BROWSER_TEST_F(CrossSiteTransferTest, PostWithFileData) { |
| 435 // Navigate to the page with form that posts via 307 redirection to |
| 436 // |redirect_target_url| (cross-site from |form_url|). Using 307 (rather than |
| 437 // 302) redirection is important to preserve the HTTP method and POST body. |
| 438 GURL form_url(embedded_test_server()->GetURL( |
| 439 "a.com", "/form_that_posts_cross_site.html")); |
| 440 GURL redirect_target_url(embedded_test_server()->GetURL("x.com", "/echoall")); |
| 441 EXPECT_TRUE(NavigateToURL(shell(), form_url)); |
| 442 |
| 443 // Prepare a file to upload. |
| 444 base::ScopedTempDir temp_dir; |
| 445 base::FilePath file_path; |
| 446 std::string file_content("test-file-content"); |
| 447 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 448 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &file_path)); |
| 449 ASSERT_LT( |
| 450 0, base::WriteFile(file_path, file_content.data(), file_content.size())); |
| 451 |
| 452 // Fill out the form to refer to the test file. |
| 453 std::unique_ptr<FileChooserDelegate> delegate( |
| 454 new FileChooserDelegate(file_path)); |
| 455 shell()->web_contents()->SetDelegate(delegate.get()); |
| 456 EXPECT_TRUE(ExecuteScript(shell()->web_contents(), |
| 457 "document.getElementById('file').click();")); |
| 458 EXPECT_TRUE(delegate->file_chosen()); |
| 459 |
| 460 // Remember the old process id for a sanity check below. |
| 461 int old_process_id = shell()->web_contents()->GetRenderProcessHost()->GetID(); |
| 462 |
| 463 // Submit the form. |
| 464 TestNavigationObserver form_post_observer(shell()->web_contents(), 1); |
| 465 EXPECT_TRUE( |
| 466 ExecuteScript(shell(), "document.getElementById('file-form').submit();")); |
| 467 form_post_observer.Wait(); |
| 468 |
| 469 // Verify that we arrived at the expected, redirected location. |
| 470 EXPECT_EQ(redirect_target_url, |
| 471 shell()->web_contents()->GetLastCommittedURL()); |
| 472 |
| 473 // Verify that the test really verifies access of a *new* renderer process. |
| 474 int new_process_id = shell()->web_contents()->GetRenderProcessHost()->GetID(); |
| 475 ASSERT_NE(new_process_id, old_process_id); |
| 476 |
| 477 // MAIN VERIFICATION: Check if the new renderer process is able to read the |
| 478 // file. |
| 479 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 480 new_process_id, file_path)); |
| 481 |
| 482 // Verify that POST body got preserved by 307 redirect. This expectation |
| 483 // comes from: https://tools.ietf.org/html/rfc7231#section-6.4.7 |
| 484 std::string actual_page_body; |
| 485 EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 486 shell()->web_contents(), |
| 487 "window.domAutomationController.send(" |
| 488 "document.getElementsByTagName('pre')[0].innerText);", |
| 489 &actual_page_body)); |
| 490 EXPECT_THAT(actual_page_body, ::testing::HasSubstr(file_content)); |
| 491 EXPECT_THAT(actual_page_body, |
| 492 ::testing::HasSubstr(file_path.BaseName().AsUTF8Unsafe())); |
| 493 EXPECT_THAT(actual_page_body, |
| 494 ::testing::HasSubstr("form-data; name=\"file\"")); |
| 495 } |
| 496 |
421 } // namespace content | 497 } // namespace content |
OLD | NEW |