Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "platform/feature_policy/FeaturePolicy.h" | 5 #include "platform/feature_policy/FeaturePolicy.h" |
| 6 | 6 |
| 7 #include "platform/RuntimeEnabledFeatures.h" | 7 #include "platform/RuntimeEnabledFeatures.h" |
| 8 #include "platform/json/JSONValues.h" | 8 #include "platform/json/JSONValues.h" |
| 9 #include "platform/network/HTTPParsers.h" | 9 #include "platform/network/HTTPParsers.h" |
| 10 #include "platform/weborigin/SecurityOrigin.h" | 10 #include "platform/weborigin/SecurityOrigin.h" |
| 11 #include "platform/wtf/PtrUtil.h" | 11 #include "platform/wtf/PtrUtil.h" |
| 12 | 12 |
| 13 namespace blink { | 13 namespace blink { |
| 14 | 14 |
| 15 namespace { | |
| 16 | |
| 17 void AddAllowFeatureToList( | |
| 18 WebFeaturePolicyFeature feature, | |
| 19 Vector<WebParsedFeaturePolicyDeclaration>& whitelists) { | |
| 20 WebParsedFeaturePolicyDeclaration whitelist; | |
| 21 whitelist.feature = feature; | |
| 22 whitelist.matches_all_origins = true; | |
| 23 whitelists.push_back(whitelist); | |
| 24 } | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 15 WebParsedFeaturePolicy ParseFeaturePolicy(const String& policy, | 28 WebParsedFeaturePolicy ParseFeaturePolicy(const String& policy, |
| 16 RefPtr<SecurityOrigin> origin, | 29 RefPtr<SecurityOrigin> origin, |
| 17 Vector<String>* messages) { | 30 Vector<String>* messages) { |
| 18 return ParseFeaturePolicy(policy, origin, messages, | 31 return ParseFeaturePolicy(policy, origin, messages, |
| 19 GetDefaultFeatureNameMap()); | 32 GetDefaultFeatureNameMap()); |
| 20 } | 33 } |
| 21 | 34 |
| 22 WebParsedFeaturePolicy ParseFeaturePolicy(const String& policy, | 35 WebParsedFeaturePolicy ParseFeaturePolicy(const String& policy, |
| 23 RefPtr<SecurityOrigin> origin, | 36 RefPtr<SecurityOrigin> origin, |
| 24 Vector<String>* messages, | 37 Vector<String>* messages, |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 54 messages->push_back("Whitelist is not an array of strings."); | 67 messages->push_back("Whitelist is not an array of strings."); |
| 55 continue; | 68 continue; |
| 56 } | 69 } |
| 57 | 70 |
| 58 WebParsedFeaturePolicyDeclaration whitelist; | 71 WebParsedFeaturePolicyDeclaration whitelist; |
| 59 whitelist.feature = feature; | 72 whitelist.feature = feature; |
| 60 Vector<WebSecurityOrigin> origins; | 73 Vector<WebSecurityOrigin> origins; |
| 61 String target_string; | 74 String target_string; |
| 62 for (size_t j = 0; j < targets->size(); ++j) { | 75 for (size_t j = 0; j < targets->size(); ++j) { |
| 63 if (targets->at(j)->AsString(&target_string)) { | 76 if (targets->at(j)->AsString(&target_string)) { |
| 64 if (DeprecatedEqualIgnoringCase(target_string, "self")) { | 77 if (EqualIgnoringASCIICase(target_string, "self")) { |
| 65 if (!origin->IsUnique()) | 78 if (!origin->IsUnique()) |
| 66 origins.push_back(origin); | 79 origins.push_back(origin); |
| 67 } else if (target_string == "*") { | 80 } else if (target_string == "*") { |
| 68 whitelist.matches_all_origins = true; | 81 whitelist.matches_all_origins = true; |
| 69 } else { | 82 } else { |
| 70 WebSecurityOrigin target_origin = | 83 WebSecurityOrigin target_origin = |
| 71 WebSecurityOrigin::CreateFromString(target_string); | 84 WebSecurityOrigin::CreateFromString(target_string); |
| 72 if (!target_origin.IsNull() && !target_origin.IsUnique()) | 85 if (!target_origin.IsNull() && !target_origin.IsUnique()) |
| 73 origins.push_back(target_origin); | 86 origins.push_back(target_origin); |
| 74 } | 87 } |
| 75 } else { | 88 } else { |
| 76 if (messages) | 89 if (messages) |
| 77 messages->push_back("Whitelist is not an array of strings."); | 90 messages->push_back("Whitelist is not an array of strings."); |
| 78 } | 91 } |
| 79 } | 92 } |
| 80 whitelist.origins = origins; | 93 whitelist.origins = origins; |
| 81 whitelists.push_back(whitelist); | 94 whitelists.push_back(whitelist); |
| 82 } | 95 } |
| 83 } | 96 } |
| 84 return whitelists; | 97 return whitelists; |
| 85 } | 98 } |
| 86 | 99 |
| 87 // TODO(lunalu): also take information of allowfullscreen and | |
| 88 // allowpaymentrequest into account when constructing the whitelist. | |
| 89 WebParsedFeaturePolicy GetContainerPolicyFromAllowedFeatures( | 100 WebParsedFeaturePolicy GetContainerPolicyFromAllowedFeatures( |
| 90 const WebVector<WebFeaturePolicyFeature>& features, | 101 const WebVector<WebFeaturePolicyFeature>& features, |
| 102 bool allowfullscreen, | |
| 103 bool allowpayment, | |
|
haraken
2017/04/26 02:34:34
This looks a bit too ad-hoc to me. Is there any wa
iclelland
2017/04/26 02:59:47
I see how this could seem like we're requiring ad-
| |
| 91 RefPtr<SecurityOrigin> origin) { | 104 RefPtr<SecurityOrigin> origin) { |
| 92 Vector<WebParsedFeaturePolicyDeclaration> whitelists; | 105 Vector<WebParsedFeaturePolicyDeclaration> whitelists; |
| 106 bool override_payment = false; | |
| 107 bool override_fullscreen = false; | |
| 93 for (const WebFeaturePolicyFeature feature : features) { | 108 for (const WebFeaturePolicyFeature feature : features) { |
| 109 // Container policy should override "allowfullscreen" and | |
| 110 // "allowpaymentrequest" policies. | |
| 111 if (feature == WebFeaturePolicyFeature::kPayment) | |
| 112 override_payment = true; | |
| 113 if (feature == WebFeaturePolicyFeature::kFullscreen) | |
| 114 override_fullscreen = true; | |
| 115 | |
| 94 WebParsedFeaturePolicyDeclaration whitelist; | 116 WebParsedFeaturePolicyDeclaration whitelist; |
| 95 whitelist.feature = feature; | 117 whitelist.feature = feature; |
| 96 whitelist.origins = Vector<WebSecurityOrigin>(1UL, {origin}); | 118 whitelist.origins = Vector<WebSecurityOrigin>(1UL, {origin}); |
| 97 whitelists.push_back(whitelist); | 119 whitelists.push_back(whitelist); |
| 98 } | 120 } |
| 121 // If allowfullscreen attribute is present and no fullscreen policy is set, | |
| 122 // enable the feature for all origins; similarly for allowpaymentrequest. | |
| 123 if (allowpayment && !override_payment) | |
| 124 AddAllowFeatureToList(WebFeaturePolicyFeature::kPayment, whitelists); | |
| 125 if (allowfullscreen && !override_fullscreen) | |
| 126 AddAllowFeatureToList(WebFeaturePolicyFeature::kFullscreen, whitelists); | |
| 127 | |
| 99 return whitelists; | 128 return whitelists; |
| 100 } | 129 } |
| 101 | 130 |
| 102 const FeatureNameMap& GetDefaultFeatureNameMap() { | 131 const FeatureNameMap& GetDefaultFeatureNameMap() { |
| 103 DEFINE_STATIC_LOCAL(FeatureNameMap, default_feature_name_map, ()); | 132 DEFINE_STATIC_LOCAL(FeatureNameMap, default_feature_name_map, ()); |
| 104 if (default_feature_name_map.IsEmpty()) { | 133 if (default_feature_name_map.IsEmpty()) { |
| 105 default_feature_name_map.Set("fullscreen", | 134 default_feature_name_map.Set("fullscreen", |
| 106 WebFeaturePolicyFeature::kFullscreen); | 135 WebFeaturePolicyFeature::kFullscreen); |
| 107 default_feature_name_map.Set("payment", WebFeaturePolicyFeature::kPayment); | 136 default_feature_name_map.Set("payment", WebFeaturePolicyFeature::kPayment); |
| 108 if (RuntimeEnabledFeatures::featurePolicyExperimentalFeaturesEnabled()) { | 137 if (RuntimeEnabledFeatures::featurePolicyExperimentalFeaturesEnabled()) { |
| 109 default_feature_name_map.Set("vibrate", | 138 default_feature_name_map.Set("vibrate", |
| 110 WebFeaturePolicyFeature::kVibrate); | 139 WebFeaturePolicyFeature::kVibrate); |
| 111 default_feature_name_map.Set("camera", WebFeaturePolicyFeature::kCamera); | 140 default_feature_name_map.Set("camera", WebFeaturePolicyFeature::kCamera); |
| 112 default_feature_name_map.Set("eme", WebFeaturePolicyFeature::kEme); | 141 default_feature_name_map.Set("encrypted-media", |
| 142 WebFeaturePolicyFeature::kEme); | |
| 113 default_feature_name_map.Set("microphone", | 143 default_feature_name_map.Set("microphone", |
| 114 WebFeaturePolicyFeature::kMicrophone); | 144 WebFeaturePolicyFeature::kMicrophone); |
| 115 default_feature_name_map.Set("speaker", | 145 default_feature_name_map.Set("speaker", |
| 116 WebFeaturePolicyFeature::kSpeaker); | 146 WebFeaturePolicyFeature::kSpeaker); |
| 117 default_feature_name_map.Set("cookie", | 147 default_feature_name_map.Set("cookie", |
| 118 WebFeaturePolicyFeature::kDocumentCookie); | 148 WebFeaturePolicyFeature::kDocumentCookie); |
| 119 default_feature_name_map.Set("domain", | 149 default_feature_name_map.Set("domain", |
| 120 WebFeaturePolicyFeature::kDocumentDomain); | 150 WebFeaturePolicyFeature::kDocumentDomain); |
| 121 default_feature_name_map.Set("docwrit", | 151 default_feature_name_map.Set("docwrite", |
| 122 WebFeaturePolicyFeature::kDocumentWrite); | 152 WebFeaturePolicyFeature::kDocumentWrite); |
| 123 default_feature_name_map.Set("geolocation", | 153 default_feature_name_map.Set("geolocation", |
| 124 WebFeaturePolicyFeature::kGeolocation); | 154 WebFeaturePolicyFeature::kGeolocation); |
| 125 default_feature_name_map.Set("midi", | 155 default_feature_name_map.Set("midi", |
| 126 WebFeaturePolicyFeature::kMidiFeature); | 156 WebFeaturePolicyFeature::kMidiFeature); |
| 127 default_feature_name_map.Set("notifications", | 157 default_feature_name_map.Set("notifications", |
| 128 WebFeaturePolicyFeature::kNotifications); | 158 WebFeaturePolicyFeature::kNotifications); |
| 129 default_feature_name_map.Set("push", WebFeaturePolicyFeature::kPush); | 159 default_feature_name_map.Set("push", WebFeaturePolicyFeature::kPush); |
| 130 default_feature_name_map.Set("sync-script", | 160 default_feature_name_map.Set("sync-script", |
| 131 WebFeaturePolicyFeature::kSyncScript); | 161 WebFeaturePolicyFeature::kSyncScript); |
| 132 default_feature_name_map.Set("sync-xhr", | 162 default_feature_name_map.Set("sync-xhr", |
| 133 WebFeaturePolicyFeature::kSyncXHR); | 163 WebFeaturePolicyFeature::kSyncXHR); |
| 134 default_feature_name_map.Set("webrtc", WebFeaturePolicyFeature::kWebRTC); | 164 default_feature_name_map.Set("webrtc", WebFeaturePolicyFeature::kWebRTC); |
| 135 } | 165 } |
| 136 } | 166 } |
| 137 return default_feature_name_map; | 167 return default_feature_name_map; |
| 138 } | 168 } |
| 139 | 169 |
| 140 } // namespace blink | 170 } // namespace blink |
| OLD | NEW |