OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <vector> |
| 6 |
| 7 #include "content/common/feature_policy/feature_policy.h" |
| 8 #include "content/public/browser/render_frame_host.h" |
| 9 #include "content/public/browser/web_contents.h" |
| 10 #include "content/public/test/test_renderer_host.h" |
| 11 #include "content/test/test_render_frame_host.h" |
| 12 #include "third_party/WebKit/public/platform/WebFeaturePolicyFeature.h" |
| 13 #include "third_party/WebKit/public/web/WebSandboxFlags.h" |
| 14 #include "url/gurl.h" |
| 15 #include "url/origin.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 // Integration tests for feature policy setup and querying through a RFH. These |
| 20 // tests are not meant to cover every edge case as the FeaturePolicy class |
| 21 // itself is tested thoroughly in feature_policy_unittest.cc. Instead they are |
| 22 // meant to ensure that integration with RenderFrameHost works correctly. |
| 23 class RenderFrameHostFeaturePolicyTest |
| 24 : public content::RenderViewHostTestHarness { |
| 25 protected: |
| 26 static constexpr const char* kOrigin1 = "https://google.com"; |
| 27 static constexpr const char* kOrigin2 = "https://maps.google.com"; |
| 28 static constexpr const char* kOrigin3 = "https://example.com"; |
| 29 static constexpr const char* kOrigin4 = "https://test.com"; |
| 30 |
| 31 static const blink::WebFeaturePolicyFeature kDefaultEnabledFeature = |
| 32 blink::WebFeaturePolicyFeature::kDocumentWrite; |
| 33 static const blink::WebFeaturePolicyFeature kDefaultSelfFeature = |
| 34 blink::WebFeaturePolicyFeature::kGeolocation; |
| 35 |
| 36 RenderFrameHost* GetMainRFH(const char* origin) { |
| 37 RenderFrameHost* result = web_contents()->GetMainFrame(); |
| 38 RenderFrameHostTester::For(result)->InitializeRenderFrameIfNeeded(); |
| 39 RenderFrameHostTester::For(result)->SimulateNavigationCommit(GURL(origin)); |
| 40 return result; |
| 41 } |
| 42 |
| 43 RenderFrameHost* AddChildRFH(RenderFrameHost* parent, const char* origin) { |
| 44 RenderFrameHost* result = |
| 45 RenderFrameHostTester::For(parent)->AppendChild(""); |
| 46 RenderFrameHostTester::For(result)->InitializeRenderFrameIfNeeded(); |
| 47 RenderFrameHostTester::For(result)->SimulateNavigationCommit(GURL(origin)); |
| 48 return result; |
| 49 } |
| 50 |
| 51 // The header policy should only be set once on page load, so we refresh the |
| 52 // page to simulate that. |
| 53 void RefreshPageAndSetHeaderPolicy(RenderFrameHost* rfh, |
| 54 blink::WebFeaturePolicyFeature feature, |
| 55 const std::vector<std::string>& origins) { |
| 56 GURL last_url = rfh->GetLastCommittedURL(); |
| 57 RenderFrameHostTester::For(rfh)->SimulateNavigationCommit( |
| 58 GURL("about:blank")); |
| 59 RenderFrameHostTester::For(rfh)->SimulateNavigationCommit(last_url); |
| 60 static_cast<TestRenderFrameHost*>(rfh)->OnDidSetFeaturePolicyHeader( |
| 61 CreateFPHeader(feature, origins)); |
| 62 } |
| 63 |
| 64 void SetContainerPolicy(RenderFrameHost* parent, |
| 65 RenderFrameHost* child, |
| 66 blink::WebFeaturePolicyFeature feature, |
| 67 const std::vector<std::string>& origins) { |
| 68 static_cast<TestRenderFrameHost*>(parent)->OnDidChangeFramePolicy( |
| 69 child->GetRoutingID(), blink::WebSandboxFlags(), |
| 70 CreateFPHeader(feature, origins)); |
| 71 } |
| 72 |
| 73 private: |
| 74 ParsedFeaturePolicyHeader CreateFPHeader( |
| 75 blink::WebFeaturePolicyFeature feature, |
| 76 const std::vector<std::string>& origins) { |
| 77 ParsedFeaturePolicyHeader result(1); |
| 78 result[0].feature = feature; |
| 79 result[0].matches_all_origins = false; |
| 80 for (const std::string& origin : origins) |
| 81 result[0].origins.push_back(url::Origin(GURL(origin))); |
| 82 return result; |
| 83 } |
| 84 }; |
| 85 |
| 86 TEST_F(RenderFrameHostFeaturePolicyTest, DefaultPolicy) { |
| 87 RenderFrameHost* parent = GetMainRFH(kOrigin1); |
| 88 RenderFrameHost* child = AddChildRFH(parent, kOrigin2); |
| 89 |
| 90 EXPECT_TRUE(parent->IsFeatureEnabled(kDefaultEnabledFeature)); |
| 91 EXPECT_TRUE(parent->IsFeatureEnabled(kDefaultSelfFeature)); |
| 92 EXPECT_TRUE(child->IsFeatureEnabled(kDefaultEnabledFeature)); |
| 93 EXPECT_FALSE(child->IsFeatureEnabled(kDefaultSelfFeature)); |
| 94 } |
| 95 |
| 96 TEST_F(RenderFrameHostFeaturePolicyTest, HeaderPolicy) { |
| 97 RenderFrameHost* parent = GetMainRFH(kOrigin1); |
| 98 |
| 99 // Enable the feature for the child in the parent frame. |
| 100 RefreshPageAndSetHeaderPolicy(parent, kDefaultSelfFeature, |
| 101 {kOrigin1, kOrigin2}); |
| 102 |
| 103 // Create the child. |
| 104 RenderFrameHost* child = AddChildRFH(parent, kOrigin2); |
| 105 |
| 106 EXPECT_TRUE(parent->IsFeatureEnabled(kDefaultSelfFeature)); |
| 107 EXPECT_TRUE(child->IsFeatureEnabled(kDefaultSelfFeature)); |
| 108 |
| 109 // Set an empty whitelist in the child to test that the policies combine |
| 110 // correctly. |
| 111 RefreshPageAndSetHeaderPolicy(child, kDefaultSelfFeature, {}); |
| 112 |
| 113 EXPECT_TRUE(parent->IsFeatureEnabled(kDefaultSelfFeature)); |
| 114 EXPECT_FALSE(child->IsFeatureEnabled(kDefaultSelfFeature)); |
| 115 |
| 116 // Re-enable the feature in the child. |
| 117 RefreshPageAndSetHeaderPolicy(child, kDefaultSelfFeature, {kOrigin2}); |
| 118 EXPECT_TRUE(child->IsFeatureEnabled(kDefaultSelfFeature)); |
| 119 |
| 120 // Navigate the child. Check that the feature is disabled. |
| 121 RenderFrameHostTester::For(child)->SimulateNavigationCommit(GURL(kOrigin3)); |
| 122 EXPECT_FALSE(child->IsFeatureEnabled(kDefaultSelfFeature)); |
| 123 } |
| 124 |
| 125 TEST_F(RenderFrameHostFeaturePolicyTest, ContainerPolicy) { |
| 126 RenderFrameHost* parent = GetMainRFH(kOrigin1); |
| 127 RenderFrameHost* child = AddChildRFH(parent, kOrigin2); |
| 128 |
| 129 // Set a container policy on origin 3 to give it the feature. It should not |
| 130 // be enabled because container policy will only take effect after navigation. |
| 131 SetContainerPolicy(parent, child, kDefaultSelfFeature, {kOrigin2, kOrigin3}); |
| 132 EXPECT_FALSE(child->IsFeatureEnabled(kDefaultSelfFeature)); |
| 133 |
| 134 // Navigate the child so that the container policy takes effect. |
| 135 RenderFrameHostTester::For(child)->SimulateNavigationCommit(GURL(kOrigin3)); |
| 136 EXPECT_TRUE(child->IsFeatureEnabled(kDefaultSelfFeature)); |
| 137 |
| 138 // Navigate the child again, the feature should not be enabled. |
| 139 RenderFrameHostTester::For(child)->SimulateNavigationCommit(GURL(kOrigin4)); |
| 140 EXPECT_FALSE(child->IsFeatureEnabled(kDefaultSelfFeature)); |
| 141 } |
| 142 |
| 143 TEST_F(RenderFrameHostFeaturePolicyTest, HeaderAndContainerPolicy) { |
| 144 RenderFrameHost* parent = GetMainRFH(kOrigin1); |
| 145 |
| 146 // Set a header policy and container policy. Check that they both take effect. |
| 147 RefreshPageAndSetHeaderPolicy(parent, kDefaultSelfFeature, |
| 148 {kOrigin1, kOrigin2}); |
| 149 |
| 150 RenderFrameHost* child = AddChildRFH(parent, kOrigin2); |
| 151 SetContainerPolicy(parent, child, kDefaultSelfFeature, {kOrigin3}); |
| 152 |
| 153 // The feature should be enabled in kOrigin2, kOrigin3 but not kOrigin4. |
| 154 EXPECT_TRUE(child->IsFeatureEnabled(kDefaultSelfFeature)); |
| 155 RenderFrameHostTester::For(child)->SimulateNavigationCommit(GURL(kOrigin3)); |
| 156 EXPECT_TRUE(child->IsFeatureEnabled(kDefaultSelfFeature)); |
| 157 RenderFrameHostTester::For(child)->SimulateNavigationCommit(GURL(kOrigin4)); |
| 158 EXPECT_FALSE(child->IsFeatureEnabled(kDefaultSelfFeature)); |
| 159 |
| 160 // Change the header policy to turn off the feature. It should be disabled in |
| 161 // all children. |
| 162 RefreshPageAndSetHeaderPolicy(parent, kDefaultSelfFeature, {}); |
| 163 child = AddChildRFH(parent, kOrigin2); |
| 164 SetContainerPolicy(parent, child, kDefaultSelfFeature, {kOrigin3}); |
| 165 |
| 166 RenderFrameHostTester::For(child)->SimulateNavigationCommit(GURL(kOrigin2)); |
| 167 EXPECT_FALSE(child->IsFeatureEnabled(kDefaultSelfFeature)); |
| 168 RenderFrameHostTester::For(child)->SimulateNavigationCommit(GURL(kOrigin3)); |
| 169 EXPECT_FALSE(child->IsFeatureEnabled(kDefaultSelfFeature)); |
| 170 } |
| 171 |
| 172 } // namespace content |
OLD | NEW |