| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/strings/pattern.h" | 6 #include "base/strings/pattern.h" |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/test/histogram_tester.h" | 9 #include "base/test/histogram_tester.h" |
| 10 #include "content/public/common/content_switches.h" | 10 #include "content/public/common/content_switches.h" |
| 11 #include "content/public/common/resource_type.h" | 11 #include "content/public/common/resource_type.h" |
| 12 #include "content/public/test/browser_test_utils.h" | 12 #include "content/public/test/browser_test_utils.h" |
| 13 #include "content/public/test/content_browser_test.h" | 13 #include "content/public/test/content_browser_test.h" |
| 14 #include "content/public/test/content_browser_test_utils.h" | 14 #include "content/public/test/content_browser_test_utils.h" |
| 15 #include "content/shell/browser/shell.h" | 15 #include "content/shell/browser/shell.h" |
| 16 #include "net/test/spawned_test_server/spawned_test_server.h" | 16 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 | 20 |
| 20 // These tests simulate exploited renderer processes, which can fetch arbitrary | 21 // These tests simulate exploited renderer processes, which can fetch arbitrary |
| 21 // resources from other websites, not constrained by the Same Origin Policy. We | 22 // resources from other websites, not constrained by the Same Origin Policy. We |
| 22 // are trying to verify that the renderer cannot fetch any cross-site document | 23 // are trying to verify that the renderer cannot fetch any cross-site document |
| 23 // responses even when the Same Origin Policy is turned off inside the renderer. | 24 // responses even when the Same Origin Policy is turned off inside the renderer. |
| 24 class SiteIsolationStatsGathererBrowserTest : public ContentBrowserTest { | 25 class SiteIsolationStatsGathererBrowserTest : public ContentBrowserTest { |
| 25 public: | 26 public: |
| 26 SiteIsolationStatsGathererBrowserTest() {} | 27 SiteIsolationStatsGathererBrowserTest() {} |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 } else if (base::MatchPattern(resource_name, "xml*")) { | 66 } else if (base::MatchPattern(resource_name, "xml*")) { |
| 66 bucket += ".XML"; | 67 bucket += ".XML"; |
| 67 } | 68 } |
| 68 } else { | 69 } else { |
| 69 FAIL(); | 70 FAIL(); |
| 70 } | 71 } |
| 71 FetchHistogramsFromChildProcesses(); | 72 FetchHistogramsFromChildProcesses(); |
| 72 | 73 |
| 73 // A few histograms are incremented unconditionally. | 74 // A few histograms are incremented unconditionally. |
| 74 histograms.ExpectUniqueSample("SiteIsolation.AllResponses", 1, 1); | 75 histograms.ExpectUniqueSample("SiteIsolation.AllResponses", 1, 1); |
| 75 histograms.ExpectTotalCount("SiteIsolation.XSD.DataLength", 1); | 76 base::HistogramTester::CountsMap expected_metrics; |
| 76 histograms.ExpectUniqueSample("SiteIsolation.XSD.MimeType", mime_type, 1); | 77 expected_metrics["SiteIsolation.XSD.DataLength"] = 1; |
| 78 expected_metrics["SiteIsolation.XSD.MimeType"] = 1; |
| 77 | 79 |
| 78 // Inspect the appropriate conditionally-incremented histogram[s]. | 80 // Determine the appropriate conditionally-incremented histograms. |
| 79 std::set<std::string> expected_metrics; | 81 std::string base = "SiteIsolation.XSD." + bucket; |
| 80 std::string base_metric = "SiteIsolation.XSD." + bucket; | |
| 81 base_metric += should_be_blocked ? ".Blocked" : ".NotBlocked"; | |
| 82 expected_metrics.insert(base_metric); | |
| 83 if (should_be_blocked) { | 82 if (should_be_blocked) { |
| 84 expected_metrics.insert(base_metric + ".RenderableStatusCode"); | 83 expected_metrics[base + ".Blocked"] = 1; |
| 85 } else if (base::MatchPattern(resource_name, "*js.*")) { | 84 expected_metrics[base + ".Blocked.RenderableStatusCode"] = 1; |
| 86 expected_metrics.insert(base_metric + ".MaybeJS"); | 85 } else { |
| 87 } | 86 expected_metrics[base + ".NotBlocked"] = 1; |
| 88 | 87 if (base::MatchPattern(resource_name, "*js.*")) { |
| 89 for (std::string metric : expected_metrics) { | 88 expected_metrics[base + ".NotBlocked.MaybeJS"] = 1; |
| 90 if (base::MatchPattern(metric, "*.RenderableStatusCode")) { | |
| 91 histograms.ExpectUniqueSample(metric, RESOURCE_TYPE_XHR, 1); | |
| 92 } else { | |
| 93 histograms.ExpectUniqueSample(metric, 1, 1); | |
| 94 } | 89 } |
| 95 } | 90 } |
| 96 | 91 |
| 97 // Make sure no other conditionally-incremented histograms were touched. | 92 // Make sure that the expected metrics, and only those metrics, were |
| 98 const char* all_metrics[] = { | 93 // incremented. |
| 99 "SiteIsolation.XSD.HTML.Blocked", | 94 EXPECT_THAT(histograms.GetTotalCountsForPrefix("SiteIsolation.XSD."), |
| 100 "SiteIsolation.XSD.HTML.Blocked.NonRenderableStatusCode", | 95 testing::ContainerEq(expected_metrics)) |
| 101 "SiteIsolation.XSD.HTML.Blocked.RenderableStatusCode", | 96 << "For resource_name=" << resource_name |
| 102 "SiteIsolation.XSD.HTML.NoSniffBlocked", | 97 << ", should_be_blocked=" << should_be_blocked; |
| 103 "SiteIsolation.XSD.HTML.NoSniffBlocked.NonRenderableStatusCode", | |
| 104 "SiteIsolation.XSD.HTML.NoSniffBlocked.RenderableStatusCode", | |
| 105 "SiteIsolation.XSD.HTML.NotBlocked", | |
| 106 "SiteIsolation.XSD.HTML.NotBlocked.MaybeJS", | |
| 107 "SiteIsolation.XSD.JSON.Blocked", | |
| 108 "SiteIsolation.XSD.JSON.Blocked.NonRenderableStatusCode", | |
| 109 "SiteIsolation.XSD.JSON.Blocked.RenderableStatusCode", | |
| 110 "SiteIsolation.XSD.JSON.NoSniffBlocked", | |
| 111 "SiteIsolation.XSD.JSON.NoSniffBlocked.NonRenderableStatusCode", | |
| 112 "SiteIsolation.XSD.JSON.NoSniffBlocked.RenderableStatusCode", | |
| 113 "SiteIsolation.XSD.JSON.NotBlocked", | |
| 114 "SiteIsolation.XSD.JSON.NotBlocked.MaybeJS", | |
| 115 "SiteIsolation.XSD.Plain.HTML.Blocked", | |
| 116 "SiteIsolation.XSD.Plain.HTML.Blocked.NonRenderableStatusCode", | |
| 117 "SiteIsolation.XSD.Plain.HTML.Blocked.RenderableStatusCode", | |
| 118 "SiteIsolation.XSD.Plain.JSON.Blocked", | |
| 119 "SiteIsolation.XSD.Plain.JSON.Blocked.NonRenderableStatusCode", | |
| 120 "SiteIsolation.XSD.Plain.JSON.Blocked.RenderableStatusCode", | |
| 121 "SiteIsolation.XSD.Plain.NoSniffBlocked", | |
| 122 "SiteIsolation.XSD.Plain.NoSniffBlocked.NonRenderableStatusCode", | |
| 123 "SiteIsolation.XSD.Plain.NoSniffBlocked.RenderableStatusCode", | |
| 124 "SiteIsolation.XSD.Plain.NotBlocked", | |
| 125 "SiteIsolation.XSD.Plain.NotBlocked.MaybeJS", | |
| 126 "SiteIsolation.XSD.Plain.XML.Blocked", | |
| 127 "SiteIsolation.XSD.Plain.XML.Blocked.NonRenderableStatusCode", | |
| 128 "SiteIsolation.XSD.Plain.XML.Blocked.RenderableStatusCode", | |
| 129 "SiteIsolation.XSD.XML.Blocked", | |
| 130 "SiteIsolation.XSD.XML.Blocked.NonRenderableStatusCode", | |
| 131 "SiteIsolation.XSD.XML.Blocked.RenderableStatusCode", | |
| 132 "SiteIsolation.XSD.XML.NoSniffBlocked", | |
| 133 "SiteIsolation.XSD.XML.NoSniffBlocked.NonRenderableStatusCode", | |
| 134 "SiteIsolation.XSD.XML.NoSniffBlocked.RenderableStatusCode", | |
| 135 "SiteIsolation.XSD.XML.NotBlocked", | |
| 136 "SiteIsolation.XSD.XML.NotBlocked.MaybeJS"}; | |
| 137 | 98 |
| 138 for (const char* metric : all_metrics) { | 99 EXPECT_THAT(histograms.GetAllSamples("SiteIsolation.XSD.MimeType"), |
| 139 if (!expected_metrics.count(metric)) { | 100 testing::ElementsAre(base::Bucket(mime_type, 1))) |
| 140 histograms.ExpectTotalCount(metric, 0); | 101 << "The wrong mime type bucket was incremented."; |
| 141 } | 102 if (should_be_blocked) { |
| 103 static_assert(13 == RESOURCE_TYPE_XHR, "Histogram enums mustn't change."); |
| 104 EXPECT_THAT( |
| 105 histograms.GetAllSamples(base + ".Blocked.RenderableStatusCode"), |
| 106 testing::ElementsAre(base::Bucket(RESOURCE_TYPE_XHR, 1))) |
| 107 << "The wrong RenderableStatusCode bucket was incremented."; |
| 142 } | 108 } |
| 143 } | 109 } |
| 144 | 110 |
| 145 private: | 111 private: |
| 146 DISALLOW_COPY_AND_ASSIGN(SiteIsolationStatsGathererBrowserTest); | 112 DISALLOW_COPY_AND_ASSIGN(SiteIsolationStatsGathererBrowserTest); |
| 147 }; | 113 }; |
| 148 | 114 |
| 149 // TODO(dsjang): we cannot run these tests on Android since SetUpCommandLine() | 115 // TODO(dsjang): we cannot run these tests on Android since SetUpCommandLine() |
| 150 // is executed before the I/O thread is created on Android. After this bug | 116 // is executed before the I/O thread is created on Android. After this bug |
| 151 // (crbug.com/278425) is resolved, we can enable this test case on Android. | 117 // (crbug.com/278425) is resolved, we can enable this test case on Android. |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 // length is different from what's described in "content-length" for such | 212 // length is different from what's described in "content-length" for such |
| 247 // different targets. | 213 // different targets. |
| 248 | 214 |
| 249 // TODO(nick): Split up these cases, and add positive assertions here about | 215 // TODO(nick): Split up these cases, and add positive assertions here about |
| 250 // what actually happens in these various resource-block cases. | 216 // what actually happens in these various resource-block cases. |
| 251 GURL foo("http://foo.com/files/cross_site_document_request_target.html"); | 217 GURL foo("http://foo.com/files/cross_site_document_request_target.html"); |
| 252 NavigateToURL(shell(), foo); | 218 NavigateToURL(shell(), foo); |
| 253 } | 219 } |
| 254 | 220 |
| 255 } // namespace content | 221 } // namespace content |
| OLD | NEW |