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

Side by Side Diff: chrome/browser/extensions/api/declarative_webrequest/webrequest_action.h

Issue 14358004: Almost all actions in Declarative Web Request require all_urls host permissions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Now with error messages Created 7 years, 8 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_ACTION_H _ 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_ACTION_H _
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_ACTION_H _ 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_ACTION_H _
7 7
8 #include <list> 8 #include <list>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 } 44 }
45 45
46 namespace extensions { 46 namespace extensions {
47 47
48 typedef linked_ptr<extension_web_request_api_helpers::EventResponseDelta> 48 typedef linked_ptr<extension_web_request_api_helpers::EventResponseDelta>
49 LinkedPtrEventResponseDelta; 49 LinkedPtrEventResponseDelta;
50 50
51 // Base class for all WebRequestActions of the declarative Web Request API. 51 // Base class for all WebRequestActions of the declarative Web Request API.
52 class WebRequestAction { 52 class WebRequestAction {
53 public: 53 public:
54 // Type identifiers for concrete WebRequestActions. 54 // Type identifiers for concrete WebRequestActions. If you add a new type,
55 // also update |action_names| in WebRequestActionFactory, update the
56 // unittest WebRequestActionTest.GetName, and add a
57 // WebRequestActionWithThreadsTest.Permission* unittest.
55 enum Type { 58 enum Type {
56 ACTION_CANCEL_REQUEST, 59 ACTION_CANCEL_REQUEST,
57 ACTION_REDIRECT_REQUEST, 60 ACTION_REDIRECT_REQUEST,
58 ACTION_REDIRECT_TO_TRANSPARENT_IMAGE, 61 ACTION_REDIRECT_TO_TRANSPARENT_IMAGE,
59 ACTION_REDIRECT_TO_EMPTY_DOCUMENT, 62 ACTION_REDIRECT_TO_EMPTY_DOCUMENT,
60 ACTION_REDIRECT_BY_REGEX_DOCUMENT, 63 ACTION_REDIRECT_BY_REGEX_DOCUMENT,
61 ACTION_SET_REQUEST_HEADER, 64 ACTION_SET_REQUEST_HEADER,
62 ACTION_REMOVE_REQUEST_HEADER, 65 ACTION_REMOVE_REQUEST_HEADER,
63 ACTION_ADD_RESPONSE_HEADER, 66 ACTION_ADD_RESPONSE_HEADER,
64 ACTION_REMOVE_RESPONSE_HEADER, 67 ACTION_REMOVE_RESPONSE_HEADER,
65 ACTION_IGNORE_RULES, 68 ACTION_IGNORE_RULES,
66 ACTION_MODIFY_REQUEST_COOKIE, 69 ACTION_MODIFY_REQUEST_COOKIE,
67 ACTION_MODIFY_RESPONSE_COOKIE, 70 ACTION_MODIFY_RESPONSE_COOKIE,
68 ACTION_SEND_MESSAGE_TO_EXTENSION, 71 ACTION_SEND_MESSAGE_TO_EXTENSION,
69 }; 72 };
70 73
71 // Strategies for checking host permissions. 74 // Strategies for checking host permissions.
72 enum HostPermissionsStrategy { 75 enum HostPermissionsStrategy {
73 STRATEGY_NONE, // Do not check host permissions. 76 STRATEGY_NONE = 1 << 0, // Do not check host permissions.
Matt Perry 2013/04/25 19:59:21 Can you have multiple strategies? I don't see how.
vabr (Chromium) 2013/04/26 09:58:52 Not for one action. But during host permissions ch
74 STRATEGY_DEFAULT, // Check host permissions in HasPermission, 77 STRATEGY_DEFAULT = 1 << 1, // Check for host permissions for all URLs
75 // before creating the delta. 78 // before creating the delta.
76 STRATEGY_ALLOW_SAME_DOMAIN, // Skip host permission checks if the request 79 STRATEGY_HOST = 1 << 2, // Check that host permissions match the URL
77 // URL and new URL have the same domain. 80 // of the request.
78 // Do these checks in DeltaHasPermission,
79 // after creating the delta.
80 }; 81 };
81 82
82 // Information necessary to decide how to apply a WebRequestAction 83 // Information necessary to decide how to apply a WebRequestAction
83 // inside a matching rule. 84 // inside a matching rule.
84 struct ApplyInfo { 85 struct ApplyInfo {
85 const ExtensionInfoMap* extension_info_map; 86 const ExtensionInfoMap* extension_info_map;
86 const WebRequestData& request_data; 87 const WebRequestData& request_data;
87 bool crosses_incognito; 88 bool crosses_incognito;
88 // Modified by each applied action: 89 // Modified by each applied action:
89 std::list<LinkedPtrEventResponseDelta>* deltas; 90 std::list<LinkedPtrEventResponseDelta>* deltas;
90 std::set<std::string>* ignored_tags; 91 std::set<std::string>* ignored_tags;
91 }; 92 };
92 93
93 WebRequestAction();
94 virtual ~WebRequestAction(); 94 virtual ~WebRequestAction();
95 95
96 // Returns a bit vector representing extensions::RequestStage. The bit vector 96 // Returns a bit vector representing extensions::RequestStage. The bit vector
97 // contains a 1 for each request stage during which the condition can be 97 // contains a 1 for each request stage during which the condition can be
98 // tested. 98 // tested.
99 virtual int GetStages() const = 0; 99 virtual int GetStages() const = 0;
vabr (Chromium) 2013/04/25 19:08:03 In the same way permissions_strategy() went from a
100 100
101 virtual Type GetType() const = 0; 101 virtual Type GetType() const = 0;
102 102
103 // Return the JavaScript type name corresponding to GetType(). If there are
104 // more names, they are returned separated by a colon.
105 const std::string& GetName() const;
106
103 // Returns the minimum priority of rules that may be evaluated after 107 // Returns the minimum priority of rules that may be evaluated after
104 // this rule. Defaults to MIN_INT. 108 // this rule. Defaults to MIN_INT.
105 virtual int GetMinimumPriority() const; 109 virtual int GetMinimumPriority() const;
106 110
107 // Returns whether host permissions checks depend on the resulting delta 111 HostPermissionsStrategy host_permissions_strategy() const {
108 // and therefore must be checked in DeltaHasPermission, after the delta 112 return host_permissions_strategy_;
109 // is created, rather than in HasPermission, before it is created. 113 }
110 // Defaults to STRATEGY_DEFAULT.
111 virtual HostPermissionsStrategy GetHostPermissionsStrategy() const;
112 114
113 // Returns whether the specified extension has permission to execute this 115 // Returns whether the specified extension has permission to execute this
114 // action on |request|. Checks the host permission if the host permissions 116 // action on |request|. Checks the host permission if the host permissions
115 // strategy is STRATEGY_DEFAULT. 117 // strategy is STRATEGY_DEFAULT.
116 // |extension_info_map| may only be NULL for during testing, in which case 118 // |extension_info_map| may only be NULL for during testing, in which case
117 // host permissions are ignored. |crosses_incognito| specifies 119 // host permissions are ignored. |crosses_incognito| specifies
118 // whether the request comes from a different profile than |extension_id| 120 // whether the request comes from a different profile than |extension_id|
119 // but was processed because the extension is in spanning mode. 121 // but was processed because the extension is in spanning mode.
120 virtual bool HasPermission(const ExtensionInfoMap* extension_info_map, 122 virtual bool HasPermission(const ExtensionInfoMap* extension_info_map,
121 const std::string& extension_id, 123 const std::string& extension_id,
122 const net::URLRequest* request, 124 const net::URLRequest* request,
123 bool crosses_incognito) const; 125 bool crosses_incognito) const;
124 126
125 // Returns whether the specified extension has permission to modify the
126 // |request| with this |delta|. This check is in addition to HasPermission;
127 // if either fails, the request will not be modified. Unlike HasPermission,
128 // it runs after the change is created, so it can use the full information
129 // about what the change would be. Checks the host permission if the strategy
130 // is STRATEGY_ALLOW_SAME_DOMAIN.
131 virtual bool DeltaHasPermission(
132 const ExtensionInfoMap* extension_info_map,
133 const std::string& extension_id,
134 const net::URLRequest* request,
135 bool crosses_incognito,
136 const LinkedPtrEventResponseDelta& delta) const;
137
138 // Factory method that instantiates a concrete WebRequestAction 127 // Factory method that instantiates a concrete WebRequestAction
139 // implementation according to |json_action|, the representation of the 128 // implementation according to |json_action|, the representation of the
140 // WebRequestAction as received from the extension API. 129 // WebRequestAction as received from the extension API.
141 // Sets |error| and returns NULL in case of a semantic error that cannot 130 // Sets |error| and returns NULL in case of a semantic error that cannot
142 // be caught by schema validation. Sets |bad_message| and returns NULL 131 // be caught by schema validation. Sets |bad_message| and returns NULL
143 // in case the input is syntactically unexpected. 132 // in case the input is syntactically unexpected.
144 static scoped_ptr<WebRequestAction> Create(const base::Value& json_action, 133 static scoped_ptr<WebRequestAction> Create(const base::Value& json_action,
145 std::string* error, 134 std::string* error,
146 bool* bad_message); 135 bool* bad_message);
147 136
148 // Returns a description of the modification to the request caused by 137 // Returns a description of the modification to the request caused by
149 // this action. 138 // this action.
150 virtual LinkedPtrEventResponseDelta CreateDelta( 139 virtual LinkedPtrEventResponseDelta CreateDelta(
151 const WebRequestData& request_data, 140 const WebRequestData& request_data,
152 const std::string& extension_id, 141 const std::string& extension_id,
153 const base::Time& extension_install_time) const = 0; 142 const base::Time& extension_install_time) const = 0;
154 143
155 // Applies this action to a request, recording the results into 144 // Applies this action to a request, recording the results into
156 // apply_info.deltas. 145 // apply_info.deltas.
157 void Apply(const std::string& extension_id, 146 void Apply(const std::string& extension_id,
158 base::Time extension_install_time, 147 base::Time extension_install_time,
159 ApplyInfo* apply_info) const; 148 ApplyInfo* apply_info) const;
149
150 protected:
151 explicit WebRequestAction(HostPermissionsStrategy strategy);
152
153 private:
154 // Defaults to STRATEGY_DEFAULT.
155 const HostPermissionsStrategy host_permissions_strategy_;
160 }; 156 };
161 157
162 typedef DeclarativeActionSet<WebRequestAction> WebRequestActionSet; 158 typedef DeclarativeActionSet<WebRequestAction> WebRequestActionSet;
163 159
164 // 160 //
165 // The following are concrete actions. 161 // The following are concrete actions.
166 // 162 //
167 163
168 // Action that instructs to cancel a network request. 164 // Action that instructs to cancel a network request.
169 class WebRequestCancelAction : public WebRequestAction { 165 class WebRequestCancelAction : public WebRequestAction {
170 public: 166 public:
171 WebRequestCancelAction(); 167 WebRequestCancelAction();
172 virtual ~WebRequestCancelAction(); 168 virtual ~WebRequestCancelAction();
173 169
174 // Implementation of WebRequestAction: 170 // Implementation of WebRequestAction:
175 virtual int GetStages() const OVERRIDE; 171 virtual int GetStages() const OVERRIDE;
176 virtual Type GetType() const OVERRIDE; 172 virtual Type GetType() const OVERRIDE;
177 virtual HostPermissionsStrategy GetHostPermissionsStrategy() const OVERRIDE;
178 virtual LinkedPtrEventResponseDelta CreateDelta( 173 virtual LinkedPtrEventResponseDelta CreateDelta(
179 const WebRequestData& request_data, 174 const WebRequestData& request_data,
180 const std::string& extension_id, 175 const std::string& extension_id,
181 const base::Time& extension_install_time) const OVERRIDE; 176 const base::Time& extension_install_time) const OVERRIDE;
182 177
183 private: 178 private:
184 DISALLOW_COPY_AND_ASSIGN(WebRequestCancelAction); 179 DISALLOW_COPY_AND_ASSIGN(WebRequestCancelAction);
185 }; 180 };
186 181
187 // Action that instructs to redirect a network request. 182 // Action that instructs to redirect a network request.
188 class WebRequestRedirectAction : public WebRequestAction { 183 class WebRequestRedirectAction : public WebRequestAction {
189 public: 184 public:
190 explicit WebRequestRedirectAction(const GURL& redirect_url); 185 explicit WebRequestRedirectAction(const GURL& redirect_url);
191 virtual ~WebRequestRedirectAction(); 186 virtual ~WebRequestRedirectAction();
192 187
193 // Implementation of WebRequestAction: 188 // Implementation of WebRequestAction:
194 virtual int GetStages() const OVERRIDE; 189 virtual int GetStages() const OVERRIDE;
195 virtual Type GetType() const OVERRIDE; 190 virtual Type GetType() const OVERRIDE;
196 virtual HostPermissionsStrategy GetHostPermissionsStrategy() const OVERRIDE;
197 virtual LinkedPtrEventResponseDelta CreateDelta( 191 virtual LinkedPtrEventResponseDelta CreateDelta(
198 const WebRequestData& request_data, 192 const WebRequestData& request_data,
199 const std::string& extension_id, 193 const std::string& extension_id,
200 const base::Time& extension_install_time) const OVERRIDE; 194 const base::Time& extension_install_time) const OVERRIDE;
201 195
202 private: 196 private:
203 GURL redirect_url_; // Target to which the request shall be redirected. 197 GURL redirect_url_; // Target to which the request shall be redirected.
204 198
205 DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectAction); 199 DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectAction);
206 }; 200 };
207 201
208 // Action that instructs to redirect a network request to a transparent image. 202 // Action that instructs to redirect a network request to a transparent image.
209 class WebRequestRedirectToTransparentImageAction : public WebRequestAction { 203 class WebRequestRedirectToTransparentImageAction : public WebRequestAction {
210 public: 204 public:
211 WebRequestRedirectToTransparentImageAction(); 205 WebRequestRedirectToTransparentImageAction();
212 virtual ~WebRequestRedirectToTransparentImageAction(); 206 virtual ~WebRequestRedirectToTransparentImageAction();
213 207
214 // Implementation of WebRequestAction: 208 // Implementation of WebRequestAction:
215 virtual int GetStages() const OVERRIDE; 209 virtual int GetStages() const OVERRIDE;
216 virtual Type GetType() const OVERRIDE; 210 virtual Type GetType() const OVERRIDE;
217 virtual HostPermissionsStrategy GetHostPermissionsStrategy() const OVERRIDE;
218 virtual LinkedPtrEventResponseDelta CreateDelta( 211 virtual LinkedPtrEventResponseDelta CreateDelta(
219 const WebRequestData& request_data, 212 const WebRequestData& request_data,
220 const std::string& extension_id, 213 const std::string& extension_id,
221 const base::Time& extension_install_time) const OVERRIDE; 214 const base::Time& extension_install_time) const OVERRIDE;
222 215
223 private: 216 private:
224 DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectToTransparentImageAction); 217 DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectToTransparentImageAction);
225 }; 218 };
226 219
227 220
228 // Action that instructs to redirect a network request to an empty document. 221 // Action that instructs to redirect a network request to an empty document.
229 class WebRequestRedirectToEmptyDocumentAction : public WebRequestAction { 222 class WebRequestRedirectToEmptyDocumentAction : public WebRequestAction {
230 public: 223 public:
231 WebRequestRedirectToEmptyDocumentAction(); 224 WebRequestRedirectToEmptyDocumentAction();
232 virtual ~WebRequestRedirectToEmptyDocumentAction(); 225 virtual ~WebRequestRedirectToEmptyDocumentAction();
233 226
234 // Implementation of WebRequestAction: 227 // Implementation of WebRequestAction:
235 virtual int GetStages() const OVERRIDE; 228 virtual int GetStages() const OVERRIDE;
236 virtual Type GetType() const OVERRIDE; 229 virtual Type GetType() const OVERRIDE;
237 virtual HostPermissionsStrategy GetHostPermissionsStrategy() const OVERRIDE;
238 virtual LinkedPtrEventResponseDelta CreateDelta( 230 virtual LinkedPtrEventResponseDelta CreateDelta(
239 const WebRequestData& request_data, 231 const WebRequestData& request_data,
240 const std::string& extension_id, 232 const std::string& extension_id,
241 const base::Time& extension_install_time) const OVERRIDE; 233 const base::Time& extension_install_time) const OVERRIDE;
242 234
243 private: 235 private:
244 DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectToEmptyDocumentAction); 236 DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectToEmptyDocumentAction);
245 }; 237 };
246 238
247 // Action that instructs to redirect a network request. 239 // Action that instructs to redirect a network request.
248 class WebRequestRedirectByRegExAction : public WebRequestAction { 240 class WebRequestRedirectByRegExAction : public WebRequestAction {
249 public: 241 public:
250 // The |to_pattern| has to be passed in RE2 syntax with the exception that 242 // The |to_pattern| has to be passed in RE2 syntax with the exception that
251 // capture groups are referenced in Perl style ($1, $2, ...). 243 // capture groups are referenced in Perl style ($1, $2, ...).
252 explicit WebRequestRedirectByRegExAction(scoped_ptr<re2::RE2> from_pattern, 244 explicit WebRequestRedirectByRegExAction(scoped_ptr<re2::RE2> from_pattern,
253 const std::string& to_pattern); 245 const std::string& to_pattern);
254 virtual ~WebRequestRedirectByRegExAction(); 246 virtual ~WebRequestRedirectByRegExAction();
255 247
256 // Conversion of capture group styles between Perl style ($1, $2, ...) and 248 // Conversion of capture group styles between Perl style ($1, $2, ...) and
257 // RE2 (\1, \2, ...). 249 // RE2 (\1, \2, ...).
258 static std::string PerlToRe2Style(const std::string& perl); 250 static std::string PerlToRe2Style(const std::string& perl);
259 251
260 // Implementation of WebRequestAction: 252 // Implementation of WebRequestAction:
261 virtual int GetStages() const OVERRIDE; 253 virtual int GetStages() const OVERRIDE;
262 virtual Type GetType() const OVERRIDE; 254 virtual Type GetType() const OVERRIDE;
263 virtual HostPermissionsStrategy GetHostPermissionsStrategy() const OVERRIDE;
264 virtual LinkedPtrEventResponseDelta CreateDelta( 255 virtual LinkedPtrEventResponseDelta CreateDelta(
265 const WebRequestData& request_data, 256 const WebRequestData& request_data,
266 const std::string& extension_id, 257 const std::string& extension_id,
267 const base::Time& extension_install_time) const OVERRIDE; 258 const base::Time& extension_install_time) const OVERRIDE;
268 259
269 private: 260 private:
270 scoped_ptr<re2::RE2> from_pattern_; 261 scoped_ptr<re2::RE2> from_pattern_;
271 std::string to_pattern_; 262 std::string to_pattern_;
272 263
273 DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectByRegExAction); 264 DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectByRegExAction);
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 class WebRequestIgnoreRulesAction : public WebRequestAction { 352 class WebRequestIgnoreRulesAction : public WebRequestAction {
362 public: 353 public:
363 explicit WebRequestIgnoreRulesAction(int minimum_priority, 354 explicit WebRequestIgnoreRulesAction(int minimum_priority,
364 const std::string& ignore_tag); 355 const std::string& ignore_tag);
365 virtual ~WebRequestIgnoreRulesAction(); 356 virtual ~WebRequestIgnoreRulesAction();
366 357
367 // Implementation of WebRequestAction: 358 // Implementation of WebRequestAction:
368 virtual int GetStages() const OVERRIDE; 359 virtual int GetStages() const OVERRIDE;
369 virtual Type GetType() const OVERRIDE; 360 virtual Type GetType() const OVERRIDE;
370 virtual int GetMinimumPriority() const OVERRIDE; 361 virtual int GetMinimumPriority() const OVERRIDE;
371 virtual HostPermissionsStrategy GetHostPermissionsStrategy() const OVERRIDE;
372 virtual LinkedPtrEventResponseDelta CreateDelta( 362 virtual LinkedPtrEventResponseDelta CreateDelta(
373 const WebRequestData& request_data, 363 const WebRequestData& request_data,
374 const std::string& extension_id, 364 const std::string& extension_id,
375 const base::Time& extension_install_time) const OVERRIDE; 365 const base::Time& extension_install_time) const OVERRIDE;
376 const std::string& ignore_tag() const { return ignore_tag_; } 366 const std::string& ignore_tag() const { return ignore_tag_; }
377 367
378 private: 368 private:
379 int minimum_priority_; 369 int minimum_priority_;
380 // Rules are ignored if they have a tag matching |ignore_tag_| and 370 // Rules are ignored if they have a tag matching |ignore_tag_| and
381 // |ignore_tag_| is non-empty. 371 // |ignore_tag_| is non-empty.
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 const base::Time& extension_install_time) const OVERRIDE; 435 const base::Time& extension_install_time) const OVERRIDE;
446 436
447 private: 437 private:
448 std::string message_; 438 std::string message_;
449 DISALLOW_COPY_AND_ASSIGN(WebRequestSendMessageToExtensionAction); 439 DISALLOW_COPY_AND_ASSIGN(WebRequestSendMessageToExtensionAction);
450 }; 440 };
451 441
452 } // namespace extensions 442 } // namespace extensions
453 443
454 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_ACTIO N_H_ 444 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_ACTIO N_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698