| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 "content/common/feature_policy/feature_policy.h" | 5 #include "content/common/feature_policy/feature_policy.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 | 10 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 | 90 |
| 91 void FeaturePolicy::Whitelist::Add(const url::Origin& origin) { | 91 void FeaturePolicy::Whitelist::Add(const url::Origin& origin) { |
| 92 origins_.push_back(origin); | 92 origins_.push_back(origin); |
| 93 } | 93 } |
| 94 | 94 |
| 95 void FeaturePolicy::Whitelist::AddAll() { | 95 void FeaturePolicy::Whitelist::AddAll() { |
| 96 matches_all_origins_ = true; | 96 matches_all_origins_ = true; |
| 97 } | 97 } |
| 98 | 98 |
| 99 bool FeaturePolicy::Whitelist::Contains(const url::Origin& origin) const { | 99 bool FeaturePolicy::Whitelist::Contains(const url::Origin& origin) const { |
| 100 // This does not handle the case where origin is an opaque origin, which is |
| 101 // also supposed to exist in the whitelist. (The identical opaque origins |
| 102 // should match in that case) |
| 103 // TODO(iclelland): Fix that, possibly by having another flag for |
| 104 // 'matches_self', which will explicitly match the policy's origin. |
| 105 // https://crbug.com/690520 |
| 100 if (matches_all_origins_) | 106 if (matches_all_origins_) |
| 101 return true; | 107 return true; |
| 102 for (const auto& targetOrigin : origins_) { | 108 for (const auto& targetOrigin : origins_) { |
| 103 if (targetOrigin.IsSameOriginWith(origin)) | 109 if (targetOrigin.IsSameOriginWith(origin)) |
| 104 return true; | 110 return true; |
| 105 } | 111 } |
| 106 return false; | 112 return false; |
| 107 } | 113 } |
| 108 | 114 |
| 109 // static | 115 // static |
| 110 std::unique_ptr<FeaturePolicy> FeaturePolicy::CreateFromParentPolicy( | 116 std::unique_ptr<FeaturePolicy> FeaturePolicy::CreateFromParentPolicy( |
| 111 const FeaturePolicy* parent_policy, | 117 const FeaturePolicy* parent_policy, |
| 112 const ParsedFeaturePolicyHeader* container_policy, | 118 const ParsedFeaturePolicyHeader& container_policy, |
| 113 const url::Origin& origin) { | 119 const url::Origin& origin) { |
| 114 return CreateFromParentPolicy(parent_policy, container_policy, origin, | 120 return CreateFromParentPolicy(parent_policy, container_policy, origin, |
| 115 GetDefaultFeatureList()); | 121 GetDefaultFeatureList()); |
| 116 } | 122 } |
| 117 | 123 |
| 118 bool FeaturePolicy::IsFeatureEnabledForOrigin( | 124 bool FeaturePolicy::IsFeatureEnabledForOrigin( |
| 119 blink::WebFeaturePolicyFeature feature, | 125 blink::WebFeaturePolicyFeature feature, |
| 120 const url::Origin& origin) const { | 126 const url::Origin& origin) const { |
| 121 DCHECK(base::ContainsKey(feature_list_, feature)); | 127 DCHECK(base::ContainsKey(feature_list_, feature)); |
| 122 const FeaturePolicy::Feature* feature_definition = feature_list_.at(feature); | 128 const FeaturePolicy::Feature* feature_definition = feature_list_.at(feature); |
| 123 DCHECK(base::ContainsKey(inherited_policies_, feature)); | 129 DCHECK(base::ContainsKey(inherited_policies_, feature)); |
| 124 if (!inherited_policies_.at(feature)) | 130 if (!inherited_policies_.at(feature)) |
| 125 return false; | 131 return false; |
| 126 auto whitelist = whitelists_.find(feature); | 132 auto whitelist = whitelists_.find(feature); |
| 127 if (whitelist != whitelists_.end()) | 133 if (whitelist != whitelists_.end()) |
| 128 return whitelist->second->Contains(origin); | 134 return whitelist->second->Contains(origin); |
| 129 if (feature_definition->default_policy == | 135 if (feature_definition->default_policy == |
| 130 FeaturePolicy::FeatureDefault::EnableForAll) { | 136 FeaturePolicy::FeatureDefault::EnableForAll) { |
| 131 return true; | 137 return true; |
| 132 } | 138 } |
| 133 if (feature_definition->default_policy == | 139 if (feature_definition->default_policy == |
| 134 FeaturePolicy::FeatureDefault::EnableForSelf) { | 140 FeaturePolicy::FeatureDefault::EnableForSelf) { |
| 135 return origin_.IsSameOriginWith(origin); | 141 // TODO(iclelland): Remove the pointer equality check once it is possible to |
| 142 // compare opaque origins successfully against themselves. |
| 143 // https://crbug.com/690520 |
| 144 return (&origin_ == &origin) || origin_.IsSameOriginWith(origin); |
| 136 } | 145 } |
| 137 return false; | 146 return false; |
| 138 } | 147 } |
| 139 | 148 |
| 140 bool FeaturePolicy::IsFeatureEnabled( | 149 bool FeaturePolicy::IsFeatureEnabled( |
| 141 blink::WebFeaturePolicyFeature feature) const { | 150 blink::WebFeaturePolicyFeature feature) const { |
| 142 return IsFeatureEnabledForOrigin(feature, origin_); | 151 return IsFeatureEnabledForOrigin(feature, origin_); |
| 143 } | 152 } |
| 144 | 153 |
| 145 void FeaturePolicy::SetHeaderPolicy( | 154 void FeaturePolicy::SetHeaderPolicy( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 160 : origin_(origin), feature_list_(feature_list) {} | 169 : origin_(origin), feature_list_(feature_list) {} |
| 161 | 170 |
| 162 FeaturePolicy::FeaturePolicy(url::Origin origin) | 171 FeaturePolicy::FeaturePolicy(url::Origin origin) |
| 163 : origin_(origin), feature_list_(GetDefaultFeatureList()) {} | 172 : origin_(origin), feature_list_(GetDefaultFeatureList()) {} |
| 164 | 173 |
| 165 FeaturePolicy::~FeaturePolicy() {} | 174 FeaturePolicy::~FeaturePolicy() {} |
| 166 | 175 |
| 167 // static | 176 // static |
| 168 std::unique_ptr<FeaturePolicy> FeaturePolicy::CreateFromParentPolicy( | 177 std::unique_ptr<FeaturePolicy> FeaturePolicy::CreateFromParentPolicy( |
| 169 const FeaturePolicy* parent_policy, | 178 const FeaturePolicy* parent_policy, |
| 170 const ParsedFeaturePolicyHeader* container_policy, | 179 const ParsedFeaturePolicyHeader& container_policy, |
| 171 const url::Origin& origin, | 180 const url::Origin& origin, |
| 172 const FeaturePolicy::FeatureList& features) { | 181 const FeaturePolicy::FeatureList& features) { |
| 173 std::unique_ptr<FeaturePolicy> new_policy = | 182 std::unique_ptr<FeaturePolicy> new_policy = |
| 174 base::WrapUnique(new FeaturePolicy(origin, features)); | 183 base::WrapUnique(new FeaturePolicy(origin, features)); |
| 175 for (const auto& feature : features) { | 184 for (const auto& feature : features) { |
| 176 if (!parent_policy || | 185 if (!parent_policy || |
| 177 parent_policy->IsFeatureEnabledForOrigin(feature.first, origin)) { | 186 parent_policy->IsFeatureEnabledForOrigin(feature.first, origin)) { |
| 178 new_policy->inherited_policies_[feature.first] = true; | 187 new_policy->inherited_policies_[feature.first] = true; |
| 179 } else { | 188 } else { |
| 180 new_policy->inherited_policies_[feature.first] = false; | 189 new_policy->inherited_policies_[feature.first] = false; |
| 181 } | 190 } |
| 182 if (container_policy) | 191 if (!container_policy.empty()) |
| 183 new_policy->AddContainerPolicy(container_policy, parent_policy); | 192 new_policy->AddContainerPolicy(container_policy, parent_policy); |
| 184 } | 193 } |
| 185 return new_policy; | 194 return new_policy; |
| 186 } | 195 } |
| 187 | 196 |
| 188 void FeaturePolicy::AddContainerPolicy( | 197 void FeaturePolicy::AddContainerPolicy( |
| 189 const ParsedFeaturePolicyHeader* container_policy, | 198 const ParsedFeaturePolicyHeader& container_policy, |
| 190 const FeaturePolicy* parent_policy) { | 199 const FeaturePolicy* parent_policy) { |
| 191 DCHECK(container_policy); | |
| 192 DCHECK(parent_policy); | 200 DCHECK(parent_policy); |
| 193 for (const ParsedFeaturePolicyDeclaration& parsed_declaration : | 201 for (const ParsedFeaturePolicyDeclaration& parsed_declaration : |
| 194 *container_policy) { | 202 container_policy) { |
| 195 // If a feature is enabled in the parent frame, and the parent chooses to | 203 // If a feature is enabled in the parent frame, and the parent chooses to |
| 196 // delegate it to the child frame, using the iframe attribute, then the | 204 // delegate it to the child frame, using the iframe attribute, then the |
| 197 // feature should be enabled in the child frame. | 205 // feature should be enabled in the child frame. |
| 198 blink::WebFeaturePolicyFeature feature = | 206 blink::WebFeaturePolicyFeature feature = |
| 199 FeatureForName(parsed_declaration.feature_name, feature_list_); | 207 FeatureForName(parsed_declaration.feature_name, feature_list_); |
| 200 if (feature == blink::WebFeaturePolicyFeature::NotFound) | 208 if (feature == blink::WebFeaturePolicyFeature::NotFound) |
| 201 continue; | 209 continue; |
| 202 if (WhitelistFromDeclaration(parsed_declaration)->Contains(origin_) && | 210 if (WhitelistFromDeclaration(parsed_declaration)->Contains(origin_) && |
| 203 parent_policy->IsFeatureEnabled(feature)) { | 211 parent_policy->IsFeatureEnabled(feature)) { |
| 204 inherited_policies_[feature] = true; | 212 inherited_policies_[feature] = true; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 223 {blink::WebFeaturePolicyFeature::Push, &kPushFeature}, | 231 {blink::WebFeaturePolicyFeature::Push, &kPushFeature}, |
| 224 {blink::WebFeaturePolicyFeature::SyncScript, &kSyncScript}, | 232 {blink::WebFeaturePolicyFeature::SyncScript, &kSyncScript}, |
| 225 {blink::WebFeaturePolicyFeature::SyncXHR, &kSyncXHR}, | 233 {blink::WebFeaturePolicyFeature::SyncXHR, &kSyncXHR}, |
| 226 {blink::WebFeaturePolicyFeature::Usermedia, &kUsermedia}, | 234 {blink::WebFeaturePolicyFeature::Usermedia, &kUsermedia}, |
| 227 {blink::WebFeaturePolicyFeature::Vibrate, &kVibrateFeature}, | 235 {blink::WebFeaturePolicyFeature::Vibrate, &kVibrateFeature}, |
| 228 {blink::WebFeaturePolicyFeature::WebRTC, &kWebRTC}})); | 236 {blink::WebFeaturePolicyFeature::WebRTC, &kWebRTC}})); |
| 229 return default_feature_list; | 237 return default_feature_list; |
| 230 } | 238 } |
| 231 | 239 |
| 232 } // namespace content | 240 } // namespace content |
| OLD | NEW |