Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: third_party/WebKit/Source/platform/feature_policy/FeaturePolicy.h

Issue 2254533002: [FeaturePolicy] Initial implementation of Feature Policy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fp-flag
Patch Set: Addressing comments from PS#16 Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #ifndef FeaturePolicy_h
6 #define FeaturePolicy_h
7
8 #include "platform/PlatformExport.h"
9 #include "platform/weborigin/SecurityOrigin.h"
10 #include "wtf/RefPtr.h"
11 #include "wtf/Vector.h"
12 #include "wtf/text/WTFString.h"
13
14 #include <memory>
15
16 namespace blink {
17
18 // Feature Policy is a mechanism for controlling the availability of web
19 // platform features in a frame, including all embedded frames. It can be used
20 // to remove features, automatically refuse API permission requests, or modify
21 // the behaviour of features. (The specific changes which are made depend on the
22 // feature; see the specification for details).
23 //
24 // Policies can be defined in the HTTP header stream, with the |Feature-Policy|
25 // HTTP header, or can be set by |enable| and |disable| attributes on the iframe
26 // element which embeds the document.
27 //
28 // See https://wicg.github.io/FeaturePolicy/
29 //
30 // Key concepts:
31 //
32 // Features
33 // --------
34 // Features which can be controlled by policy are defined as instances of the
35 // FeaturePoliicy::Feature struct. The features are referenced by pointer, so
36 // only a single instance of each feature should be defined. The features which
37 // are declared in the feature policy specification are all defined in
38 // |FeaturePolicy.cpp|.
39 //
40 // Whitelists
41 // ----------
42 // Policies are defined as a mapping of feaure names to whitelists. Whitelists
43 // are collections of origins, although two special terms can be used when
44 // declaring them:
45 // "self" refers to the orgin of the frame which is declaring the policy.
46 // "*" refers to all origins; any origin will match a whitelist which contains
47 // it.
48 //
49 // Defaults
50 // --------
51 // Each defined feature has a default policy, which determines whether the
52 // feature is available when no policy has been declared, ans determines how the
raymes 2016/10/24 06:51:45 nit: and
53 // feature is inherited across origin boundaries.
54 //
55 // If the default policy is in effect for a frame, then it controls how the
raymes 2016/10/24 06:51:45 nit: double space
56 // feature is inherited by any cross-origin iframes embedded by the frame. (See
57 // the comments below in FeaturePolicy::DefaultPolicy for specifics)
58 //
59 // Policy Inheritance
60 // ------------------
61 // Policies in effect for a frame are inherited by any child frames it embeds.
62 // Unless another policy is declared in the child, all same-origin children will
63 // receive the same set of enables features as the parent frame. Whether or not
64 // features are inherited by cross-origin iframes without an explicit policy is
65 // determined by the feature's default policy. (Again, see the comments in
66 // FeaturePolicy::DefaultPolicy for details)
raymes 2016/10/24 06:51:45 I find this section a bit confusing, because inher
67
68 class PLATFORM_EXPORT FeaturePolicy final {
69 public:
70 // Represents a collection of origins which make up a whitelist in a feature
71 // policy. This collection may be set to match every origin (corresponding to
72 // the "*" syntax in the policy string, in which case the contains() method
73 // will always return true.
74 class Whitelist final {
75 public:
76 Whitelist();
77
78 // Adds a single origin to the whitelist.
79 void add(RefPtr<SecurityOrigin>);
80
81 // Adds all origins to the whitelist.
82 void addAll();
83
84 // Returns true if the given origin has been added to the whitelist.
85 bool contains(const SecurityOrigin&) const;
86 String toString();
87
88 private:
89 bool m_matchesAllOrigins;
90 Vector<RefPtr<SecurityOrigin>> m_origins;
91 };
92
93 // The FeaturePolicy::FeatureDefault enum defines the default enable state for
94 // a feature when neither it nor any parent frame have declared an explicit
95 // policy. The three possibilities map directly to Feature Policy Whitelist
96 // semantics.
97 enum class FeatureDefault {
98 // Equivalent to []. If this default policy is in effect for a frame, then
99 // the feature will not be enabled for that frame or any of its children.
100 DisableForAll,
101
102 // Equivalent to ["self"]. If this default policy is in effect for a frame,
103 // then the feature will be enabled for that frame, and any same-origin
104 // child frames, but not for any cross-origin child frames.
105 EnableForSelf,
106
107 // Equivalent to ["*"]. If in effect for a frame, then the feature is
108 // enabled for that frame and all of its children.
109 EnableForAll
110 };
111
112 // The FeaturePolicy::Feature struct is used to define all features under
113 // control of Feature Policy. There should only be one instance of this struct
114 // for any given feature (declared below).
115 struct Feature {
116 // The name of the feature, as it should appear in a policy string
117 const char* featureName;
118
119 // Controls whether the feature should be available in the platform by
120 // default, in the absence of any declared policy.
121 FeatureDefault defaultPolicy;
122 };
123
124 using FeatureList = const Vector<const FeaturePolicy::Feature*>;
125
126 static FeaturePolicy* createFromParentPolicy(const FeaturePolicy* parent,
127 RefPtr<SecurityOrigin>);
128
129 // Sets the declared policy from the Feature-Policy HTTP header. If the header
130 // cannot be parsed, errors will be appended to the |messages| vector.
131 void setHeaderPolicy(const String&, Vector<String>& messages);
132
133 // Returns whether or not the given feature is enabled by this policy.
134 bool isFeatureEnabledForOrigin(const Feature*, const SecurityOrigin&) const;
135
136 // Returns whether or not the given feature is enabled for the frame that owns
137 // the policy.
138 bool isFeatureEnabled(const Feature*) const;
139
140 // Returns the list of features which can be controlled by Feature Policy.
141 static FeatureList& getDefaultFeatureList();
142
143 String toString();
144
145 private:
146 friend class FeaturePolicyTest;
147
148 FeaturePolicy(RefPtr<SecurityOrigin>, FeatureList& features);
149
150 static FeaturePolicy* createFromParentPolicy(const FeaturePolicy* parent,
151 RefPtr<SecurityOrigin>,
152 FeatureList& features);
153
154 // Parses a policy string into a set of whitelists for features.
155 HashMap<const Feature*, std::unique_ptr<Whitelist>> parse(const String&);
156
157 RefPtr<SecurityOrigin> m_origin;
158
159 // Records whether or not each feature was enabled for this frame by its
160 // parent frame.
161 // TODO(iclelland): Generate, instead of this map, a set of bool flags, one
162 // for each feature, as all features are supposed to be represented here.
163 HashMap<const Feature*, bool> m_inheritedFeatures;
164
165 // Map of feature names to declared whitelists. Any feature which is missing
166 // from this map should use the inherited policy.
167 HashMap<const Feature*, std::unique_ptr<Whitelist>> m_headerWhitelists;
168
169 // Contains the set of all features which can be controlled by this policy.
170 FeatureList& m_features;
171
172 DISALLOW_COPY_AND_ASSIGN(FeaturePolicy);
173 };
174
175 // Declarations for all features currently under control of the Feature Policy
176 // mechanism should be placed here.
177 extern const PLATFORM_EXPORT FeaturePolicy::Feature kDocumentCookie;
178 extern const PLATFORM_EXPORT FeaturePolicy::Feature kDocumentDomain;
179 extern const PLATFORM_EXPORT FeaturePolicy::Feature kDocumentWrite;
180 extern const PLATFORM_EXPORT FeaturePolicy::Feature kGeolocationFeature;
181 extern const PLATFORM_EXPORT FeaturePolicy::Feature kMidiFeature;
182 extern const PLATFORM_EXPORT FeaturePolicy::Feature kNotificationsFeature;
183 extern const PLATFORM_EXPORT FeaturePolicy::Feature kPaymentFeature;
184 extern const PLATFORM_EXPORT FeaturePolicy::Feature kPushFeature;
185 extern const PLATFORM_EXPORT FeaturePolicy::Feature kSyncScript;
186 extern const PLATFORM_EXPORT FeaturePolicy::Feature kSyncXHR;
187 extern const PLATFORM_EXPORT FeaturePolicy::Feature kUsermedia;
188 extern const PLATFORM_EXPORT FeaturePolicy::Feature kVibrateFeature;
189 extern const PLATFORM_EXPORT FeaturePolicy::Feature kWebRTC;
190
191 } // namespace blink
192
193 #endif // FeaturePolicy_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698