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

Side by Side Diff: content/browser/web_contents/web_contents_impl_browsertest.cc

Issue 1917073002: Block webpages from navigating to view-source URLs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add layout and browser tests Created 4 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "base/macros.h" 5 #include "base/macros.h"
6 #include "base/strings/utf_string_conversions.h" 6 #include "base/strings/utf_string_conversions.h"
7 #include "base/values.h" 7 #include "base/values.h"
8 #include "build/build_config.h" 8 #include "build/build_config.h"
9 #include "content/browser/frame_host/navigation_entry_impl.h" 9 #include "content/browser/frame_host/navigation_entry_impl.h"
10 #include "content/browser/renderer_host/render_widget_host_impl.h" 10 #include "content/browser/renderer_host/render_widget_host_impl.h"
(...skipping 752 matching lines...) Expand 10 before | Expand all | Expand 10 after
763 shell()->web_contents()->SetPageScale(1.5); 763 shell()->web_contents()->SetPageScale(1.5);
764 EXPECT_CALL(observer, OnPageScaleFactorChanged(::testing::FloatEq(1.5))); 764 EXPECT_CALL(observer, OnPageScaleFactorChanged(::testing::FloatEq(1.5)));
765 observer.WaitForPageScaleUpdate(); 765 observer.WaitForPageScaleUpdate();
766 766
767 // Navigate to reset the page scale factor. 767 // Navigate to reset the page scale factor.
768 shell()->LoadURL(embedded_test_server()->GetURL("/title2.html")); 768 shell()->LoadURL(embedded_test_server()->GetURL("/title2.html"));
769 EXPECT_CALL(observer, OnPageScaleFactorChanged(::testing::_)); 769 EXPECT_CALL(observer, OnPageScaleFactorChanged(::testing::_));
770 observer.WaitForPageScaleUpdate(); 770 observer.WaitForPageScaleUpdate();
771 } 771 }
772 772
773 // Test that a direct navigation to a view-source URL works.
774 IN_PROC_BROWSER_TEST_F(WebContentsImplBrowserTest, ViewSourceDirectNavigation) {
775 ASSERT_TRUE(embedded_test_server()->Start());
776 const GURL kUrl(embedded_test_server()->GetURL("/simple_page.html"));
777 const GURL kViewSourceURL(kViewSourceScheme + std::string(":") + kUrl.spec());
778 NavigateToURL(shell(), kViewSourceURL);
779 // Displayed view-source URLs don't include the scheme of the effective URL
Charlie Reis 2016/05/24 22:54:16 nit: Just for HTTP, right?
meacer 2016/05/31 23:57:54 Done.
780 // (e.g. view-source:example.com instead of view-source:http://example.com).
781 EXPECT_EQ(base::ASCIIToUTF16(std::string("view-source:") + kUrl.host() + ":" +
782 kUrl.port() + kUrl.path()),
783 shell()->web_contents()->GetTitle());
Charlie Reis 2016/05/24 22:54:16 Maybe we can also check that we're in view-source
meacer 2016/05/31 23:57:54 Done, except for window.open case where there is n
784 }
785
786 // Test that window.open to a view-source URL is blocked.
787 IN_PROC_BROWSER_TEST_F(WebContentsImplBrowserTest,
788 ViewSourceWindowOpen_ShouldBeBlocked) {
789 ASSERT_TRUE(embedded_test_server()->Start());
790 const GURL kUrl(embedded_test_server()->GetURL("/simple_page.html"));
791 const GURL kViewSourceURL(kViewSourceScheme + std::string(":") + kUrl.spec());
792 NavigateToURL(shell(), kUrl);
793
794 ShellAddedObserver new_shell_observer;
795 EXPECT_TRUE(ExecuteScript(shell()->web_contents(),
796 "window.open('" + kViewSourceURL.spec() + "');"));
797 Shell* new_shell = new_shell_observer.GetShell();
798 WaitForLoadStop(new_shell->web_contents());
799 // EXPECT_EQ("", static_cast<WebContentsImpl*>(new_shell->web_contents())
Charlie Reis 2016/05/24 22:54:16 nit: Remove.
meacer 2016/05/31 23:57:54 Done.
800 EXPECT_EQ("", new_shell->web_contents()->GetURL().spec());
Charlie Reis 2016/05/24 22:54:16 Can we use a ConsoleObserverDelegate on shell() in
meacer 2016/05/31 23:57:54 Unfortunately no, because the console message gets
Charlie Reis 2016/06/01 22:59:51 Acknowledged.
801 }
802
803 // Test that a content initiated navigation to a view-source URL is blocked.
804 IN_PROC_BROWSER_TEST_F(WebContentsImplBrowserTest,
805 ViewSourceRedirect_ShouldBeBlocked) {
806 ASSERT_TRUE(embedded_test_server()->Start());
807 const GURL kUrl(embedded_test_server()->GetURL("/simple_page.html"));
808 const GURL kViewSourceURL(kViewSourceScheme + std::string(":") + kUrl.spec());
809 NavigateToURL(shell(), kUrl);
810
811 std::unique_ptr<ConsoleObserverDelegate> console_delegate(
812 new ConsoleObserverDelegate(
813 shell()->web_contents(),
814 "Not allowed to load local resource: view-source:*"));
815 shell()->web_contents()->SetDelegate(console_delegate.get());
816
817 EXPECT_TRUE(
818 ExecuteScript(shell()->web_contents(),
819 "window.location = '" + kViewSourceURL.spec() + "';"));
820 console_delegate->Wait();
821 // Original page shouldn't navigate away.
822 EXPECT_EQ(kUrl, shell()->web_contents()->GetURL());
823 }
824
825 // Test that view source mode for a webui page can be opened.
826 IN_PROC_BROWSER_TEST_F(WebContentsImplBrowserTest, ViewSourceWebUI) {
827 const char kUrl[] = "view-source:chrome://chrome/settings";
828 const GURL kGURL(kUrl);
829 NavigateToURL(shell(), kGURL);
830 EXPECT_EQ(base::ASCIIToUTF16(kUrl), shell()->web_contents()->GetTitle());
831 }
832
773 IN_PROC_BROWSER_TEST_F(WebContentsImplBrowserTest, NewNamedWindow) { 833 IN_PROC_BROWSER_TEST_F(WebContentsImplBrowserTest, NewNamedWindow) {
774 ASSERT_TRUE(embedded_test_server()->Start()); 834 ASSERT_TRUE(embedded_test_server()->Start());
775 835
776 GURL url = embedded_test_server()->GetURL("/click-noreferrer-links.html"); 836 GURL url = embedded_test_server()->GetURL("/click-noreferrer-links.html");
777 EXPECT_TRUE(NavigateToURL(shell(), url)); 837 EXPECT_TRUE(NavigateToURL(shell(), url));
778 838
779 { 839 {
780 ShellAddedObserver new_shell_observer; 840 ShellAddedObserver new_shell_observer;
781 841
782 // Open a new, named window. 842 // Open a new, named window.
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
1005 content::ExecuteScript(root->current_frame_host(), alert_location)); 1065 content::ExecuteScript(root->current_frame_host(), alert_location));
1006 dialog_manager.Wait(); 1066 dialog_manager.Wait();
1007 EXPECT_EQ(GURL("http://a.com/title1.html"), 1067 EXPECT_EQ(GURL("http://a.com/title1.html"),
1008 GURL(dialog_manager.last_message()).ReplaceComponents(clear_port)); 1068 GURL(dialog_manager.last_message()).ReplaceComponents(clear_port));
1009 1069
1010 wc->SetDelegate(nullptr); 1070 wc->SetDelegate(nullptr);
1011 wc->SetJavaScriptDialogManagerForTesting(nullptr); 1071 wc->SetJavaScriptDialogManagerForTesting(nullptr);
1012 } 1072 }
1013 1073
1014 } // namespace content 1074 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698