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

Side by Side Diff: chrome/browser/tab_contents/navigation_metrics_recorder_browsertest.cc

Issue 2702503002: Block renderer-initiated main frame navigations to data URLs (Closed)
Patch Set: nasko comments, fix most tests Created 3 years, 8 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/test/histogram_tester.h" 5 #include "base/test/histogram_tester.h"
6 #include "chrome/browser/tab_contents/navigation_metrics_recorder.h" 6 #include "chrome/browser/tab_contents/navigation_metrics_recorder.h"
7 #include "chrome/browser/ui/browser.h" 7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/tabs/tab_strip_model.h" 8 #include "chrome/browser/ui/tabs/tab_strip_model.h"
9 #include "chrome/test/base/in_process_browser_test.h" 9 #include "chrome/test/base/in_process_browser_test.h"
10 #include "chrome/test/base/ui_test_utils.h" 10 #include "chrome/test/base/ui_test_utils.h"
11 #include "components/rappor/test_rappor_service.h" 11 #include "components/rappor/test_rappor_service.h"
12 #include "content/public/browser/web_contents.h" 12 #include "content/public/browser/web_contents.h"
13 #include "content/public/test/browser_test_utils.h" 13 #include "content/public/test/browser_test_utils.h"
14 #include "content/public/test/test_navigation_observer.h" 14 #include "content/public/test/test_navigation_observer.h"
15 15
16 namespace { 16 namespace {
17 17
18 typedef InProcessBrowserTest NavigationMetricsRecorderBrowserTest; 18 typedef InProcessBrowserTest NavigationMetricsRecorderBrowserTest;
19 19
20 // Performs a content initiated navigation to |url|.
21 void RedirectToUrl(content::WebContents* web_contents, const GURL& url) {
22 content::TestNavigationObserver observer(web_contents, 1);
23 EXPECT_TRUE(content::ExecuteScript(
24 web_contents, std::string("window.location.href='") + url.spec() + "'"));
25 observer.Wait();
26 }
27
28 IN_PROC_BROWSER_TEST_F(NavigationMetricsRecorderBrowserTest, TestMetrics) { 20 IN_PROC_BROWSER_TEST_F(NavigationMetricsRecorderBrowserTest, TestMetrics) {
29 content::WebContents* web_contents = 21 content::WebContents* web_contents =
30 browser()->tab_strip_model()->GetActiveWebContents(); 22 browser()->tab_strip_model()->GetActiveWebContents();
31 23
32 NavigationMetricsRecorder* recorder = 24 NavigationMetricsRecorder* recorder =
33 content::WebContentsUserData<NavigationMetricsRecorder>::FromWebContents( 25 content::WebContentsUserData<NavigationMetricsRecorder>::FromWebContents(
34 web_contents); 26 web_contents);
35 ASSERT_TRUE(recorder); 27 ASSERT_TRUE(recorder);
36 rappor::TestRapporServiceImpl rappor_service; 28 rappor::TestRapporServiceImpl rappor_service;
37 recorder->set_rappor_service_for_testing(&rappor_service); 29 recorder->set_rappor_service_for_testing(&rappor_service);
38 30
39 base::HistogramTester histograms; 31 base::HistogramTester histograms;
40 ui_test_utils::NavigateToURL(browser(), 32 ui_test_utils::NavigateToURL(browser(),
41 GURL("data:text/html, <html></html>")); 33 GURL("data:text/html, <html></html>"));
42 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 1); 34 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 1);
43 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 1); 35 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 1);
44 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 1); 36 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 1);
45 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", 37 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage",
46 5 /* data: */, 1); 38 5 /* data: */, 1);
47 // Since there was no previous URL, Rappor shouldn't record anything. 39 // Since there was no previous URL, Rappor shouldn't record anything.
48 EXPECT_EQ(0, rappor_service.GetReportsCount()); 40 EXPECT_EQ(0, rappor_service.GetReportsCount());
49 41
50 // Navigate to an empty page and redirect it to a data: URL. Rappor should 42 // Navigate to an empty page and redirect it to a data: URL. Rappor should
51 // record a report. 43 // record a report.
52 ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); 44 // TODO(meacer): This isn't meaningful after data URL redirects are only
45 // allowed from other dataURLs. Remove the Rappor histogram.
53 content::TestNavigationObserver observer(web_contents, 1); 46 content::TestNavigationObserver observer(web_contents, 1);
54 EXPECT_TRUE(content::ExecuteScript( 47 EXPECT_TRUE(content::ExecuteScript(
55 web_contents, "window.location.href='data:text/html, <html></html>'")); 48 web_contents,
49 "window.location.href='data:text/html, <html>page2</html>'"));
56 observer.Wait(); 50 observer.Wait();
57 51
58 EXPECT_EQ(1, rappor_service.GetReportsCount()); 52 EXPECT_EQ(1, rappor_service.GetReportsCount());
59 std::string sample; 53 std::string sample;
60 rappor::RapporType type; 54 rappor::RapporType type;
61 EXPECT_TRUE(rappor_service.GetRecordedSampleForMetric( 55 EXPECT_TRUE(rappor_service.GetRecordedSampleForMetric(
62 "Navigation.Scheme.Data", &sample, &type)); 56 "Navigation.Scheme.Data", &sample, &type));
63 EXPECT_EQ("about://", sample); 57 EXPECT_EQ("data://", sample);
64 EXPECT_EQ(rappor::ETLD_PLUS_ONE_RAPPOR_TYPE, type); 58 EXPECT_EQ(rappor::ETLD_PLUS_ONE_RAPPOR_TYPE, type);
65 } 59 }
66 60
67 IN_PROC_BROWSER_TEST_F(NavigationMetricsRecorderBrowserTest, DataURLMimeTypes) { 61 IN_PROC_BROWSER_TEST_F(NavigationMetricsRecorderBrowserTest, DataURLMimeTypes) {
68 base::HistogramTester histograms; 62 base::HistogramTester histograms;
69 content::WebContents* web_contents =
70 browser()->tab_strip_model()->GetActiveWebContents();
71 63
72 // HTML: 64 // HTML:
73 RedirectToUrl(web_contents, GURL("data:text/html, <html></html>")); 65 ui_test_utils::NavigateToURL(browser(),
66 GURL("data:text/html, <html></html>"));
74 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 1); 67 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 1);
75 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 1); 68 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 1);
76 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 1); 69 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 1);
77 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", 70 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage",
78 5 /* data: */, 1); 71 5 /* data: */, 1);
79 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 1); 72 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 0);
80 histograms.ExpectBucketCount("Navigation.MainFrameScheme.DataUrl.MimeType",
81 NavigationMetricsRecorder::MIME_TYPE_HTML, 1);
82 73
83 // SVG: 74 // SVG:
84 RedirectToUrl(web_contents, 75 ui_test_utils::NavigateToURL(
85 GURL("data:image/svg+xml,<!DOCTYPE svg><svg version=\"1.1\" " 76 browser(), GURL("data:image/svg+xml,<!DOCTYPE svg><svg version=\"1.1\" "
86 "xmlns=\"http://www.w3.org/2000/svg\"></svg>")); 77 "xmlns=\"http://www.w3.org/2000/svg\"></svg>"));
87 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 2); 78 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 2);
88 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 2); 79 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 2);
89 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 2); 80 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 2);
90 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", 81 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage",
91 5 /* data: */, 2); 82 5 /* data: */, 2);
92 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 2); 83 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 0);
93 histograms.ExpectBucketCount("Navigation.MainFrameScheme.DataUrl.MimeType",
94 NavigationMetricsRecorder::MIME_TYPE_SVG, 1);
95 84
96 // Base64 encoded HTML: 85 // Base64 encoded HTML:
97 RedirectToUrl(web_contents, 86 ui_test_utils::NavigateToURL(
98 GURL("data:text/html;base64, PGh0bWw+YmFzZTY0PC9odG1sPg==")); 87 browser(), GURL("data:text/html;base64, PGh0bWw+YmFzZTY0PC9odG1sPg=="));
99 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 3); 88 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 3);
100 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 3); 89 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 3);
101 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 3); 90 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 3);
102 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", 91 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage",
103 5 /* data: */, 3); 92 5 /* data: */, 3);
104 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 3); 93 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 0);
105 histograms.ExpectBucketCount("Navigation.MainFrameScheme.DataUrl.MimeType",
106 NavigationMetricsRecorder::MIME_TYPE_HTML, 2);
107 94
108 // Plain text: 95 // Plain text:
109 RedirectToUrl(web_contents, GURL("data:text/plain, test")); 96 ui_test_utils::NavigateToURL(browser(), GURL("data:text/plain, test"));
110 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 4); 97 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 4);
111 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 4); 98 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 4);
112 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 4); 99 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 4);
113 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", 100 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage",
114 5 /* data: */, 4); 101 5 /* data: */, 4);
115 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 4); 102 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 0);
116 histograms.ExpectBucketCount("Navigation.MainFrameScheme.DataUrl.MimeType",
117 NavigationMetricsRecorder::MIME_TYPE_OTHER, 1);
118 103
119 // Base64 encoded PNG: 104 // Base64 encoded PNG:
120 RedirectToUrl( 105 ui_test_utils::NavigateToURL(
121 web_contents, 106 browser(),
122 GURL("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA" 107 GURL("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA"
123 "AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO" 108 "AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO"
124 "9TXL0Y4OHwAAAABJRU5ErkJggg==")); 109 "9TXL0Y4OHwAAAABJRU5ErkJggg=="));
125 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 5); 110 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 5);
126 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 5); 111 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 5);
127 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 5); 112 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 5);
128 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", 113 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage",
129 5 /* data: */, 5); 114 5 /* data: */, 5);
130 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 5); 115 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 0);
131 histograms.ExpectBucketCount("Navigation.MainFrameScheme.DataUrl.MimeType",
132 NavigationMetricsRecorder::MIME_TYPE_OTHER, 2);
133 } 116 }
134 117
135 } // namespace 118 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698