1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/feature_policy/feature_policy.h"
#include <algorithm>
#include "third_party/blink/renderer/platform/json/json_values.h"
#include "third_party/blink/renderer/platform/network/http_parsers.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/weborigin/security_origin.h"
#include "third_party/blink/renderer/platform/wtf/ascii_ctype.h"
#include "third_party/blink/renderer/platform/wtf/bit_vector.h"
#include "third_party/blink/renderer/platform/wtf/text/parsing_utilities.h"
#include "third_party/blink/renderer/platform/wtf/text/string_utf8_adaptor.h"
#include "url/gurl.h"
namespace blink {
ParsedFeaturePolicy ParseFeaturePolicyHeader(
const String& policy,
scoped_refptr<const SecurityOrigin> origin,
Vector<String>* messages) {
return ParseFeaturePolicy(policy, origin, nullptr, messages,
GetDefaultFeatureNameMap());
}
ParsedFeaturePolicy ParseFeaturePolicyAttribute(
const String& policy,
scoped_refptr<const SecurityOrigin> self_origin,
scoped_refptr<const SecurityOrigin> src_origin,
Vector<String>* messages) {
return ParseFeaturePolicy(policy, self_origin, src_origin, messages,
GetDefaultFeatureNameMap());
}
ParsedFeaturePolicy ParseFeaturePolicy(
const String& policy,
scoped_refptr<const SecurityOrigin> self_origin,
scoped_refptr<const SecurityOrigin> src_origin,
Vector<String>* messages,
const FeatureNameMap& feature_names) {
ParsedFeaturePolicy allowlists;
BitVector features_specified(
static_cast<int>(mojom::FeaturePolicyFeature::kMaxValue));
// RFC2616, section 4.2 specifies that headers appearing multiple times can be
// combined with a comma. Walk the header string, and parse each comma
// separated chunk as a separate header.
Vector<String> policy_items;
// policy_items = [ policy *( "," [ policy ] ) ]
policy.Split(',', policy_items);
for (const String& item : policy_items) {
Vector<String> entry_list;
// entry_list = [ entry *( ";" [ entry ] ) ]
item.Split(';', entry_list);
for (const String& entry : entry_list) {
// Split removes extra whitespaces by default
// "name value1 value2" or "name".
Vector<String> tokens;
entry.Split(' ', tokens);
// Empty policy. Skip.
if (tokens.IsEmpty())
continue;
if (!feature_names.Contains(tokens[0])) {
if (messages)
messages->push_back("Unrecognized feature: '" + tokens[0] + "'.");
continue;
}
mojom::FeaturePolicyFeature feature = feature_names.at(tokens[0]);
// If a policy has already been specified for the current feature, drop
// the new policy.
if (features_specified.QuickGet(static_cast<int>(feature)))
continue;
ParsedFeaturePolicyDeclaration allowlist;
allowlist.feature = feature;
features_specified.QuickSet(static_cast<int>(feature));
std::vector<url::Origin> origins;
// If a policy entry has no (optional) values (e,g,
// allow="feature_name1; feature_name2 value"), enable the feature for:
// a. |self_origin|, if we are parsing a header policy (i.e.,
// |src_origin| is null);
// b. |src_origin|, if we are parsing an allow attribute (i.e.,
// |src_origin| is not null), |src_origin| is not opaque; or
// c. the opaque origin of the frame, if |src_origin| is opaque.
if (tokens.size() == 1) {
if (!src_origin) {
origins.push_back(self_origin->ToUrlOrigin());
} else if (!src_origin->IsOpaque()) {
origins.push_back(src_origin->ToUrlOrigin());
} else {
allowlist.matches_opaque_src = true;
}
}
for (size_t i = 1; i < tokens.size(); i++) {
if (!tokens[i].ContainsOnlyASCII()) {
messages->push_back("Non-ASCII characters in origin.");
continue;
}
if (EqualIgnoringASCIICase(tokens[i], "'self'")) {
origins.push_back(self_origin->ToUrlOrigin());
} else if (src_origin && EqualIgnoringASCIICase(tokens[i], "'src'")) {
// Only the iframe allow attribute can define |src_origin|.
// When parsing feature policy header, 'src' is disallowed and
// |src_origin| = nullptr.
// If the iframe will have an opaque origin (for example, if it is
// sandboxed, or has a data: URL), then 'src' needs to refer to the
// opaque origin of the frame, which is not known yet. In this case,
// the |matches_opaque_src| flag on the declaration is set, rather
// than adding an origin to the allowlist.
if (src_origin->IsOpaque()) {
allowlist.matches_opaque_src = true;
} else {
origins.push_back(src_origin->ToUrlOrigin());
}
} else if (EqualIgnoringASCIICase(tokens[i], "'none'")) {
continue;
} else if (tokens[i] == "*") {
allowlist.matches_all_origins = true;
break;
} else {
url::Origin target_origin = url::Origin::Create(
GURL(StringUTF8Adaptor(tokens[i]).AsStringPiece()));
if (!target_origin.unique())
origins.push_back(target_origin);
else if (messages)
messages->push_back("Unrecognized origin: '" + tokens[i] + "'.");
}
}
allowlist.origins = origins;
allowlists.push_back(allowlist);
}
}
return allowlists;
}
bool IsFeatureDeclared(mojom::FeaturePolicyFeature feature,
const ParsedFeaturePolicy& policy) {
return std::any_of(policy.begin(), policy.end(),
[feature](const auto& declaration) {
return declaration.feature == feature;
});
}
bool RemoveFeatureIfPresent(mojom::FeaturePolicyFeature feature,
ParsedFeaturePolicy& policy) {
auto new_end = std::remove_if(policy.begin(), policy.end(),
[feature](const auto& declaration) {
return declaration.feature == feature;
});
if (new_end == policy.end())
return false;
policy.erase(new_end, policy.end());
return true;
}
bool DisallowFeatureIfNotPresent(mojom::FeaturePolicyFeature feature,
ParsedFeaturePolicy& policy) {
if (IsFeatureDeclared(feature, policy))
return false;
ParsedFeaturePolicyDeclaration allowlist;
allowlist.feature = feature;
allowlist.matches_all_origins = false;
allowlist.matches_opaque_src = false;
policy.push_back(allowlist);
return true;
}
bool AllowFeatureEverywhereIfNotPresent(mojom::FeaturePolicyFeature feature,
ParsedFeaturePolicy& policy) {
if (IsFeatureDeclared(feature, policy))
return false;
ParsedFeaturePolicyDeclaration allowlist;
allowlist.feature = feature;
allowlist.matches_all_origins = true;
allowlist.matches_opaque_src = true;
policy.push_back(allowlist);
return true;
}
void DisallowFeature(mojom::FeaturePolicyFeature feature,
ParsedFeaturePolicy& policy) {
RemoveFeatureIfPresent(feature, policy);
DisallowFeatureIfNotPresent(feature, policy);
}
void AllowFeatureEverywhere(mojom::FeaturePolicyFeature feature,
ParsedFeaturePolicy& policy) {
RemoveFeatureIfPresent(feature, policy);
AllowFeatureEverywhereIfNotPresent(feature, policy);
}
// This method defines the feature names which will be recognized by the parser
// for the Feature-Policy HTTP header and the <iframe> "allow" attribute, as
// well as the features which will be recognized by the document or iframe
// policy object.
//
// Features which are implemented behind a flag should generally also have the
// same flag controlling whether they are in this map. Note that features which
// are shipping as part of an origin trial should add their feature names to
// this map unconditionally, as the trial token could be added after the HTTP
// header needs to be parsed. This also means that top-level documents which
// simply want to embed another page which uses an origin trial feature, without
// using the feature themselves, can use feature policy to allow use of the
// feature in subframes. (The framed document will still require a valid origin
// trial token to use the feature in this scenario.)
const FeatureNameMap& GetDefaultFeatureNameMap() {
DEFINE_STATIC_LOCAL(FeatureNameMap, default_feature_name_map, ());
if (default_feature_name_map.IsEmpty()) {
default_feature_name_map.Set("autoplay",
mojom::FeaturePolicyFeature::kAutoplay);
default_feature_name_map.Set("camera",
mojom::FeaturePolicyFeature::kCamera);
default_feature_name_map.Set("encrypted-media",
mojom::FeaturePolicyFeature::kEncryptedMedia);
default_feature_name_map.Set("fullscreen",
mojom::FeaturePolicyFeature::kFullscreen);
default_feature_name_map.Set("geolocation",
mojom::FeaturePolicyFeature::kGeolocation);
default_feature_name_map.Set("microphone",
mojom::FeaturePolicyFeature::kMicrophone);
default_feature_name_map.Set("midi",
mojom::FeaturePolicyFeature::kMidiFeature);
default_feature_name_map.Set("speaker",
mojom::FeaturePolicyFeature::kSpeaker);
default_feature_name_map.Set("sync-xhr",
mojom::FeaturePolicyFeature::kSyncXHR);
// Under origin trial: Should be made conditional on WebVR and WebXR
// runtime flags once it is out of trial.
default_feature_name_map.Set("vr", mojom::FeaturePolicyFeature::kWebVr);
if (RuntimeEnabledFeatures::ExperimentalProductivityFeaturesEnabled()) {
default_feature_name_map.Set("animations",
mojom::FeaturePolicyFeature::kAnimations);
default_feature_name_map.Set("document-write",
mojom::FeaturePolicyFeature::kDocumentWrite);
default_feature_name_map.Set(
"image-compression", mojom::FeaturePolicyFeature::kImageCompression);
default_feature_name_map.Set(
"legacy-image-formats",
mojom::FeaturePolicyFeature::kLegacyImageFormats);
default_feature_name_map.Set(
"max-downscaling-image",
mojom::FeaturePolicyFeature::kMaxDownscalingImage);
default_feature_name_map.Set("unsized-media",
mojom::FeaturePolicyFeature::kUnsizedMedia);
default_feature_name_map.Set(
"vertical-scroll", mojom::FeaturePolicyFeature::kVerticalScroll);
default_feature_name_map.Set("sync-script",
mojom::FeaturePolicyFeature::kSyncScript);
}
if (RuntimeEnabledFeatures::PaymentRequestEnabled()) {
default_feature_name_map.Set("payment",
mojom::FeaturePolicyFeature::kPayment);
}
if (RuntimeEnabledFeatures::PictureInPictureAPIEnabled()) {
default_feature_name_map.Set(
"picture-in-picture", mojom::FeaturePolicyFeature::kPictureInPicture);
}
if (RuntimeEnabledFeatures::SensorEnabled()) {
default_feature_name_map.Set("accelerometer",
mojom::FeaturePolicyFeature::kAccelerometer);
default_feature_name_map.Set(
"ambient-light-sensor",
mojom::FeaturePolicyFeature::kAmbientLightSensor);
default_feature_name_map.Set("gyroscope",
mojom::FeaturePolicyFeature::kGyroscope);
default_feature_name_map.Set("magnetometer",
mojom::FeaturePolicyFeature::kMagnetometer);
}
if (RuntimeEnabledFeatures::WebUSBEnabled()) {
default_feature_name_map.Set("usb", mojom::FeaturePolicyFeature::kUsb);
}
}
return default_feature_name_map;
}
const String& GetNameForFeature(mojom::FeaturePolicyFeature feature) {
const static String empty_string;
for (const auto& entry : GetDefaultFeatureNameMap()) {
if (entry.value == feature)
return entry.key;
}
return empty_string;
}
} // namespace blink
|