Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: content/browser/cross_site_transfer_browsertest.cc

Issue 2908433003: RenderFrameProxyHost::OnOpenURL needs to validate resource request body. (Closed)
Patch Set: Covering RenderFrameHostImpl as well and rearranging the test to pass on Android. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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> 5 #include <memory>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "content/browser/child_process_security_policy_impl.h" 14 #include "content/browser/child_process_security_policy_impl.h"
15 #include "content/browser/loader/resource_dispatcher_host_impl.h" 15 #include "content/browser/loader/resource_dispatcher_host_impl.h"
16 #include "content/public/browser/navigation_entry.h" 16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/browser/navigation_handle.h" 17 #include "content/public/browser/navigation_handle.h"
18 #include "content/public/browser/render_frame_host.h"
18 #include "content/public/browser/render_process_host.h" 19 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/resource_dispatcher_host_delegate.h" 20 #include "content/public/browser/resource_dispatcher_host_delegate.h"
20 #include "content/public/browser/resource_throttle.h" 21 #include "content/public/browser/resource_throttle.h"
21 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/content_switches.h" 23 #include "content/public/common/content_switches.h"
23 #include "content/public/test/browser_test_utils.h" 24 #include "content/public/test/browser_test_utils.h"
24 #include "content/public/test/content_browser_test.h" 25 #include "content/public/test/content_browser_test.h"
25 #include "content/public/test/content_browser_test_utils.h" 26 #include "content/public/test/content_browser_test_utils.h"
26 #include "content/public/test/navigation_handle_observer.h" 27 #include "content/public/test/navigation_handle_observer.h"
27 #include "content/public/test/test_navigation_observer.h" 28 #include "content/public/test/test_navigation_observer.h"
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 "window.domAutomationController.send(" 509 "window.domAutomationController.send("
509 "document.getElementsByTagName('pre')[0].innerText);", 510 "document.getElementsByTagName('pre')[0].innerText);",
510 &actual_page_body)); 511 &actual_page_body));
511 EXPECT_THAT(actual_page_body, ::testing::HasSubstr(file_content)); 512 EXPECT_THAT(actual_page_body, ::testing::HasSubstr(file_content));
512 EXPECT_THAT(actual_page_body, 513 EXPECT_THAT(actual_page_body,
513 ::testing::HasSubstr(file_path.BaseName().AsUTF8Unsafe())); 514 ::testing::HasSubstr(file_path.BaseName().AsUTF8Unsafe()));
514 EXPECT_THAT(actual_page_body, 515 EXPECT_THAT(actual_page_body,
515 ::testing::HasSubstr("form-data; name=\"file\"")); 516 ::testing::HasSubstr("form-data; name=\"file\""));
516 } 517 }
517 518
519 // Test that verifies that if navigation originator doesn't have access to a
520 // file, then no access is granted after a cross-process transfer of POST data.
521 // This is a regression test for https://crbug.com/726067.
522 //
523 // This test is somewhat similar to
524 // http/tests/navigation/form-targets-cross-site-frame-post.html layout test
525 // except that it 1) tests with files, 2) simulates a malicious scenario and 3)
526 // verifies file acecss (all of these 3 things are not possible with layout
527 // tests).
528 //
529 // This test is very similar to CrossSiteTransferTest.PostWithFileData above,
530 // except that it simulates a malicious form / POST originator.
531 IN_PROC_BROWSER_TEST_P(CrossSiteTransferTest, MaliciousPostWithFileData) {
532 // The initial test window is a named form target.
533 GURL initial_target_url(
534 embedded_test_server()->GetURL("initial-target.com", "/title1.html"));
535 EXPECT_TRUE(NavigateToURL(shell(), initial_target_url));
536 WebContents* target_contents = shell()->web_contents();
537 EXPECT_TRUE(ExecuteScript(target_contents, "window.name = 'form-target';"));
538
539 // Create a new window containing a form targeting |target_contents|.
540 GURL form_url(embedded_test_server()->GetURL(
541 "main.com", "/form_that_posts_cross_site.html"));
542 WebContentsAddedObserver new_contents_observer;
543 EXPECT_TRUE(ExecuteScript(target_contents, "window.open('" + form_url.spec() +
alexmos 2017/05/25 23:44:05 optional: There's a OpenPopup in content_browser_t
Łukasz Anforowicz 2017/05/26 00:05:14 Done.
544 "', 'form-window');"));
Łukasz Anforowicz 2017/05/25 19:56:01 I've rearranged the order of windows - now the win
545 WebContents* form_contents = new_contents_observer.GetWebContents();
546 WaitForLoadStop(form_contents);
547 EXPECT_TRUE(ExecuteScript(
548 form_contents,
549 "document.getElementById('file-form').target = 'form-target';"));
550
551 // Verify the current locations and process placement of |target_contents|
552 // and |form_contents|.
553 EXPECT_EQ(initial_target_url, target_contents->GetLastCommittedURL());
554 EXPECT_EQ(form_url, form_contents->GetLastCommittedURL());
555 EXPECT_NE(target_contents->GetMainFrame()->GetProcess()->GetID(),
556 form_contents->GetMainFrame()->GetProcess()->GetID());
557
558 // Prepare a file to upload.
559 base::ThreadRestrictions::ScopedAllowIO allow_io_for_temp_dir;
560 base::ScopedTempDir temp_dir;
561 base::FilePath file_path;
562 std::string file_content("test-file-content");
563 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
564 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.GetPath(), &file_path));
565 ASSERT_LT(
566 0, base::WriteFile(file_path, file_content.data(), file_content.size()));
567
568 // Fill out the form to refer to the test file.
569 std::unique_ptr<FileChooserDelegate> delegate(
570 new FileChooserDelegate(file_path));
571 form_contents->Focus();
572 form_contents->SetDelegate(delegate.get());
573 EXPECT_TRUE(
574 ExecuteScript(form_contents, "document.getElementById('file').click();"));
575 EXPECT_TRUE(delegate->file_chosen());
alexmos 2017/05/25 23:44:06 Might as well sanity-check that at this point, EXP
Łukasz Anforowicz 2017/05/26 00:05:14 Done.
576
577 // Simulate a malicious situation, where the renderer doesn't really have
578 // access to the file.
579 ChildProcessSecurityPolicyImpl* security_policy =
580 ChildProcessSecurityPolicyImpl::GetInstance();
581 security_policy->RevokeAllPermissionsForFile(
582 form_contents->GetMainFrame()->GetProcess()->GetID(), file_path);
583 EXPECT_FALSE(security_policy->CanReadFile(
584 form_contents->GetMainFrame()->GetProcess()->GetID(), file_path));
585 EXPECT_FALSE(security_policy->CanReadFile(
586 target_contents->GetMainFrame()->GetProcess()->GetID(), file_path));
587
588 // Submit the form and wait until the malicious renderer gets killed.
589 RenderProcessHostWatcher process_exit_observer(
590 form_contents->GetMainFrame()->GetProcess(),
591 RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
592 EXPECT_TRUE(ExecuteScript(
593 form_contents,
594 "setTimeout(\n"
595 " function() { document.getElementById('file-form').submit(); },\n"
596 " 0);"));
597 process_exit_observer.Wait();
598 EXPECT_FALSE(process_exit_observer.did_exit_normally());
599
600 // The target frame should still be at the original location - the malicious
601 // navigation should have been stopped.
602 EXPECT_EQ(initial_target_url, target_contents->GetLastCommittedURL());
603
604 // Both processes still shouldn't have access.
605 EXPECT_FALSE(security_policy->CanReadFile(
606 form_contents->GetMainFrame()->GetProcess()->GetID(), file_path));
607 EXPECT_FALSE(security_policy->CanReadFile(
608 target_contents->GetMainFrame()->GetProcess()->GetID(), file_path));
609 }
610
518 INSTANTIATE_TEST_CASE_P(CrossSiteTransferTest, 611 INSTANTIATE_TEST_CASE_P(CrossSiteTransferTest,
519 CrossSiteTransferTest, 612 CrossSiteTransferTest,
520 ::testing::Values(TestParameter::LOADING_WITHOUT_MOJO, 613 ::testing::Values(TestParameter::LOADING_WITHOUT_MOJO,
521 TestParameter::LOADING_WITH_MOJO)); 614 TestParameter::LOADING_WITH_MOJO));
522 615
523 } // namespace content 616 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698