OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/common/feature_policy/feature_policy.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 |
| 10 namespace content { |
| 11 |
| 12 namespace { |
| 13 |
| 14 // Given a string name, return the matching feature struct, or nullptr if it is |
| 15 // not the name of a policy-controlled feature. |
| 16 blink::WebFeaturePolicyFeature FeatureForName( |
| 17 std::string feature_name, |
| 18 const FeaturePolicy::FeatureList& features) { |
| 19 for (const auto& feature_mapping : features) { |
| 20 if (feature_name == feature_mapping.second->feature_name) |
| 21 return feature_mapping.first; |
| 22 } |
| 23 return blink::WebFeaturePolicyFeature::NotFound; |
| 24 } |
| 25 |
| 26 // Definitions of all features controlled by Feature Policy should appear here. |
| 27 const FeaturePolicy::Feature kDocumentCookie{ |
| 28 "cookie", FeaturePolicy::FeatureDefault::EnableForAll}; |
| 29 const FeaturePolicy::Feature kDocumentDomain{ |
| 30 "domain", FeaturePolicy::FeatureDefault::EnableForAll}; |
| 31 const FeaturePolicy::Feature kDocumentWrite{ |
| 32 "docwrite", FeaturePolicy::FeatureDefault::EnableForAll}; |
| 33 const FeaturePolicy::Feature kFullscreenFeature{ |
| 34 "fullscreen", FeaturePolicy::FeatureDefault::EnableForSelf}; |
| 35 const FeaturePolicy::Feature kGeolocationFeature{ |
| 36 "geolocation", FeaturePolicy::FeatureDefault::EnableForSelf}; |
| 37 const FeaturePolicy::Feature kMidiFeature{ |
| 38 "midi", FeaturePolicy::FeatureDefault::EnableForAll}; |
| 39 const FeaturePolicy::Feature kNotificationsFeature{ |
| 40 "notifications", FeaturePolicy::FeatureDefault::EnableForAll}; |
| 41 const FeaturePolicy::Feature kPaymentFeature{ |
| 42 "payment", FeaturePolicy::FeatureDefault::EnableForSelf}; |
| 43 const FeaturePolicy::Feature kPushFeature{ |
| 44 "push", FeaturePolicy::FeatureDefault::EnableForAll}; |
| 45 const FeaturePolicy::Feature kSyncScript{ |
| 46 "sync-script", FeaturePolicy::FeatureDefault::EnableForAll}; |
| 47 const FeaturePolicy::Feature kSyncXHR{ |
| 48 "sync-xhr", FeaturePolicy::FeatureDefault::EnableForAll}; |
| 49 const FeaturePolicy::Feature kUsermedia{ |
| 50 "usermedia", FeaturePolicy::FeatureDefault::EnableForAll}; |
| 51 const FeaturePolicy::Feature kVibrateFeature{ |
| 52 "vibrate", FeaturePolicy::FeatureDefault::EnableForSelf}; |
| 53 const FeaturePolicy::Feature kWebRTC{ |
| 54 "webrtc", FeaturePolicy::FeatureDefault::EnableForAll}; |
| 55 |
| 56 // Extracts a Whitelist from a FeaturePolicyParsedDeclaration |
| 57 std::unique_ptr<FeaturePolicy::Whitelist> WhitelistFromDeclaration( |
| 58 const FeaturePolicyParsedDeclaration& parsed_declaration) { |
| 59 std::unique_ptr<FeaturePolicy::Whitelist> result = |
| 60 base::WrapUnique(new FeaturePolicy::Whitelist()); |
| 61 if (parsed_declaration.matches_all_origins) |
| 62 result->AddAll(); |
| 63 for (const auto& origin : parsed_declaration.origins) |
| 64 result->Add(origin); |
| 65 return result; |
| 66 } |
| 67 |
| 68 } // namespace |
| 69 |
| 70 FeaturePolicyParsedDeclaration::FeaturePolicyParsedDeclaration() |
| 71 : matches_all_origins(false) {} |
| 72 |
| 73 FeaturePolicyParsedDeclaration::FeaturePolicyParsedDeclaration( |
| 74 std::string feature_name, |
| 75 bool matches_all_origins, |
| 76 std::vector<url::Origin> origins) |
| 77 : feature_name(feature_name), |
| 78 matches_all_origins(matches_all_origins), |
| 79 origins(origins) {} |
| 80 |
| 81 FeaturePolicyParsedDeclaration::FeaturePolicyParsedDeclaration( |
| 82 const FeaturePolicyParsedDeclaration& rhs) = default; |
| 83 |
| 84 FeaturePolicyParsedDeclaration::~FeaturePolicyParsedDeclaration() {} |
| 85 |
| 86 FeaturePolicy::Whitelist::Whitelist() : matches_all_origins_(false) {} |
| 87 FeaturePolicy::Whitelist::~Whitelist() = default; |
| 88 |
| 89 void FeaturePolicy::Whitelist::Add(const url::Origin& origin) { |
| 90 origins_.push_back(origin); |
| 91 } |
| 92 |
| 93 void FeaturePolicy::Whitelist::AddAll() { |
| 94 matches_all_origins_ = true; |
| 95 } |
| 96 |
| 97 bool FeaturePolicy::Whitelist::Contains(const url::Origin& origin) const { |
| 98 if (matches_all_origins_) |
| 99 return true; |
| 100 for (const auto& targetOrigin : origins_) { |
| 101 if (targetOrigin.IsSameOriginWith(origin)) |
| 102 return true; |
| 103 } |
| 104 return false; |
| 105 } |
| 106 |
| 107 // static |
| 108 std::unique_ptr<FeaturePolicy> FeaturePolicy::CreateFromParentPolicy( |
| 109 const FeaturePolicy* parent_policy, |
| 110 url::Origin origin) { |
| 111 return CreateFromParentPolicy(parent_policy, origin, getDefaultFeatureList()); |
| 112 } |
| 113 |
| 114 bool FeaturePolicy::IsFeatureEnabledForOrigin( |
| 115 blink::WebFeaturePolicyFeature feature, |
| 116 url::Origin origin) const { |
| 117 DCHECK(feature_list_.count(feature)); |
| 118 const FeaturePolicy::Feature* feature_definition = feature_list_.at(feature); |
| 119 DCHECK(inherited_policies_.count(feature)); |
| 120 if (!inherited_policies_.at(feature)) { |
| 121 return false; |
| 122 } |
| 123 if (whitelists_.count(feature)) { |
| 124 return whitelists_.at(feature)->Contains(origin); |
| 125 } |
| 126 if (feature_definition->default_policy == |
| 127 FeaturePolicy::FeatureDefault::EnableForAll) { |
| 128 return true; |
| 129 } |
| 130 if (feature_definition->default_policy == |
| 131 FeaturePolicy::FeatureDefault::EnableForSelf) { |
| 132 return origin_.IsSameOriginWith(origin); |
| 133 } |
| 134 return false; |
| 135 } |
| 136 |
| 137 bool FeaturePolicy::IsFeatureEnabled( |
| 138 blink::WebFeaturePolicyFeature feature) const { |
| 139 return IsFeatureEnabledForOrigin(feature, origin_); |
| 140 } |
| 141 |
| 142 void FeaturePolicy::SetHeaderPolicy(const FeaturePolicyHeader& parsed_header) { |
| 143 DCHECK(whitelists_.empty()); |
| 144 for (const FeaturePolicyParsedDeclaration& parsed_declaration : |
| 145 parsed_header) { |
| 146 blink::WebFeaturePolicyFeature feature = |
| 147 FeatureForName(parsed_declaration.feature_name, feature_list_); |
| 148 if (feature == blink::WebFeaturePolicyFeature::NotFound) |
| 149 continue; |
| 150 whitelists_[feature] = WhitelistFromDeclaration(parsed_declaration); |
| 151 } |
| 152 } |
| 153 |
| 154 FeaturePolicy::FeaturePolicy(url::Origin origin, |
| 155 const FeatureList& feature_list) |
| 156 : origin_(origin), feature_list_(feature_list) {} |
| 157 FeaturePolicy::FeaturePolicy(url::Origin origin) |
| 158 : origin_(origin), feature_list_(getDefaultFeatureList()) {} |
| 159 FeaturePolicy::~FeaturePolicy() {} |
| 160 |
| 161 // static |
| 162 std::unique_ptr<FeaturePolicy> FeaturePolicy::CreateFromParentPolicy( |
| 163 const FeaturePolicy* parent_policy, |
| 164 url::Origin origin, |
| 165 const FeaturePolicy::FeatureList& features) { |
| 166 std::unique_ptr<FeaturePolicy> newPolicy = |
| 167 base::WrapUnique(new FeaturePolicy(origin, features)); |
| 168 for (const auto& feature : features) { |
| 169 if (!parent_policy || |
| 170 parent_policy->IsFeatureEnabledForOrigin(feature.first, origin)) { |
| 171 newPolicy->inherited_policies_[feature.first] = true; |
| 172 } else { |
| 173 newPolicy->inherited_policies_[feature.first] = false; |
| 174 } |
| 175 } |
| 176 return newPolicy; |
| 177 } |
| 178 |
| 179 // static |
| 180 const FeaturePolicy::FeatureList& FeaturePolicy::getDefaultFeatureList() { |
| 181 // TODO: See if this should use lazy_instance instead |
| 182 CR_DEFINE_STATIC_LOCAL( |
| 183 FeatureList, defaultFeatureList, |
| 184 ({{blink::WebFeaturePolicyFeature::DocumentCookie, &kDocumentCookie}, |
| 185 {blink::WebFeaturePolicyFeature::DocumentDomain, &kDocumentDomain}, |
| 186 {blink::WebFeaturePolicyFeature::DocumentWrite, &kDocumentWrite}, |
| 187 {blink::WebFeaturePolicyFeature::Fullscreen, &kFullscreenFeature}, |
| 188 {blink::WebFeaturePolicyFeature::Geolocation, &kGeolocationFeature}, |
| 189 {blink::WebFeaturePolicyFeature::MidiFeature, &kMidiFeature}, |
| 190 {blink::WebFeaturePolicyFeature::Notifications, &kNotificationsFeature}, |
| 191 {blink::WebFeaturePolicyFeature::Payment, &kPaymentFeature}, |
| 192 {blink::WebFeaturePolicyFeature::Push, &kPushFeature}, |
| 193 {blink::WebFeaturePolicyFeature::SyncScript, &kSyncScript}, |
| 194 {blink::WebFeaturePolicyFeature::SyncXHR, &kSyncXHR}, |
| 195 {blink::WebFeaturePolicyFeature::Usermedia, &kUsermedia}, |
| 196 {blink::WebFeaturePolicyFeature::Vibrate, &kVibrateFeature}, |
| 197 {blink::WebFeaturePolicyFeature::WebRTC, &kWebRTC}})); |
| 198 return defaultFeatureList; |
| 199 } |
| 200 |
| 201 } // namespace content |
OLD | NEW |