| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/extension_protocols.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/base64.h" | |
| 10 #include "base/compiler_specific.h" | |
| 11 #include "base/file_util.h" | |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/format_macros.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/memory/weak_ptr.h" | |
| 16 #include "base/message_loop/message_loop.h" | |
| 17 #include "base/metrics/histogram.h" | |
| 18 #include "base/path_service.h" | |
| 19 #include "base/sha1.h" | |
| 20 #include "base/strings/string_number_conversions.h" | |
| 21 #include "base/strings/string_util.h" | |
| 22 #include "base/strings/stringprintf.h" | |
| 23 #include "base/strings/utf_string_conversions.h" | |
| 24 #include "base/threading/sequenced_worker_pool.h" | |
| 25 #include "base/threading/thread_restrictions.h" | |
| 26 #include "base/timer/elapsed_timer.h" | |
| 27 #include "build/build_config.h" | |
| 28 #include "chrome/browser/extensions/extension_renderer_state.h" | |
| 29 #include "chrome/browser/extensions/image_loader.h" | |
| 30 #include "chrome/common/chrome_paths.h" | |
| 31 #include "chrome/common/extensions/manifest_url_handler.h" | |
| 32 #include "chrome/common/url_constants.h" | |
| 33 #include "content/public/browser/browser_thread.h" | |
| 34 #include "content/public/browser/resource_request_info.h" | |
| 35 #include "extensions/browser/info_map.h" | |
| 36 #include "extensions/common/constants.h" | |
| 37 #include "extensions/common/extension.h" | |
| 38 #include "extensions/common/extension_resource.h" | |
| 39 #include "extensions/common/file_util.h" | |
| 40 #include "extensions/common/manifest_handlers/background_info.h" | |
| 41 #include "extensions/common/manifest_handlers/csp_info.h" | |
| 42 #include "extensions/common/manifest_handlers/icons_handler.h" | |
| 43 #include "extensions/common/manifest_handlers/incognito_info.h" | |
| 44 #include "extensions/common/manifest_handlers/shared_module_info.h" | |
| 45 #include "extensions/common/manifest_handlers/web_accessible_resources_info.h" | |
| 46 #include "extensions/common/manifest_handlers/webview_info.h" | |
| 47 #include "grit/component_extension_resources_map.h" | |
| 48 #include "net/base/mime_util.h" | |
| 49 #include "net/base/net_errors.h" | |
| 50 #include "net/http/http_request_headers.h" | |
| 51 #include "net/http/http_response_headers.h" | |
| 52 #include "net/http/http_response_info.h" | |
| 53 #include "net/url_request/url_request_error_job.h" | |
| 54 #include "net/url_request/url_request_file_job.h" | |
| 55 #include "net/url_request/url_request_simple_job.h" | |
| 56 #include "ui/base/resource/resource_bundle.h" | |
| 57 #include "url/url_util.h" | |
| 58 | |
| 59 using content::BrowserThread; | |
| 60 using content::ResourceRequestInfo; | |
| 61 using extensions::Extension; | |
| 62 using extensions::SharedModuleInfo; | |
| 63 | |
| 64 namespace { | |
| 65 | |
| 66 net::HttpResponseHeaders* BuildHttpHeaders( | |
| 67 const std::string& content_security_policy, bool send_cors_header, | |
| 68 const base::Time& last_modified_time) { | |
| 69 std::string raw_headers; | |
| 70 raw_headers.append("HTTP/1.1 200 OK"); | |
| 71 if (!content_security_policy.empty()) { | |
| 72 raw_headers.append(1, '\0'); | |
| 73 raw_headers.append("Content-Security-Policy: "); | |
| 74 raw_headers.append(content_security_policy); | |
| 75 } | |
| 76 | |
| 77 if (send_cors_header) { | |
| 78 raw_headers.append(1, '\0'); | |
| 79 raw_headers.append("Access-Control-Allow-Origin: *"); | |
| 80 } | |
| 81 | |
| 82 if (!last_modified_time.is_null()) { | |
| 83 // Hash the time and make an etag to avoid exposing the exact | |
| 84 // user installation time of the extension. | |
| 85 std::string hash = base::StringPrintf("%" PRId64, | |
| 86 last_modified_time.ToInternalValue()); | |
| 87 hash = base::SHA1HashString(hash); | |
| 88 std::string etag; | |
| 89 base::Base64Encode(hash, &etag); | |
| 90 raw_headers.append(1, '\0'); | |
| 91 raw_headers.append("ETag: \""); | |
| 92 raw_headers.append(etag); | |
| 93 raw_headers.append("\""); | |
| 94 // Also force revalidation. | |
| 95 raw_headers.append(1, '\0'); | |
| 96 raw_headers.append("cache-control: no-cache"); | |
| 97 } | |
| 98 | |
| 99 raw_headers.append(2, '\0'); | |
| 100 return new net::HttpResponseHeaders(raw_headers); | |
| 101 } | |
| 102 | |
| 103 class URLRequestResourceBundleJob : public net::URLRequestSimpleJob { | |
| 104 public: | |
| 105 URLRequestResourceBundleJob(net::URLRequest* request, | |
| 106 net::NetworkDelegate* network_delegate, | |
| 107 const base::FilePath& filename, | |
| 108 int resource_id, | |
| 109 const std::string& content_security_policy, | |
| 110 bool send_cors_header) | |
| 111 : net::URLRequestSimpleJob(request, network_delegate), | |
| 112 filename_(filename), | |
| 113 resource_id_(resource_id), | |
| 114 weak_factory_(this) { | |
| 115 // Leave cache headers out of resource bundle requests. | |
| 116 response_info_.headers = BuildHttpHeaders(content_security_policy, | |
| 117 send_cors_header, | |
| 118 base::Time()); | |
| 119 } | |
| 120 | |
| 121 // Overridden from URLRequestSimpleJob: | |
| 122 virtual int GetData(std::string* mime_type, | |
| 123 std::string* charset, | |
| 124 std::string* data, | |
| 125 const net::CompletionCallback& callback) const OVERRIDE { | |
| 126 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 127 *data = rb.GetRawDataResource(resource_id_).as_string(); | |
| 128 | |
| 129 // Add the Content-Length header now that we know the resource length. | |
| 130 response_info_.headers->AddHeader(base::StringPrintf( | |
| 131 "%s: %s", net::HttpRequestHeaders::kContentLength, | |
| 132 base::UintToString(data->size()).c_str())); | |
| 133 | |
| 134 std::string* read_mime_type = new std::string; | |
| 135 bool posted = base::PostTaskAndReplyWithResult( | |
| 136 BrowserThread::GetBlockingPool(), | |
| 137 FROM_HERE, | |
| 138 base::Bind(&net::GetMimeTypeFromFile, filename_, | |
| 139 base::Unretained(read_mime_type)), | |
| 140 base::Bind(&URLRequestResourceBundleJob::OnMimeTypeRead, | |
| 141 weak_factory_.GetWeakPtr(), | |
| 142 mime_type, charset, data, | |
| 143 base::Owned(read_mime_type), | |
| 144 callback)); | |
| 145 DCHECK(posted); | |
| 146 | |
| 147 return net::ERR_IO_PENDING; | |
| 148 } | |
| 149 | |
| 150 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE { | |
| 151 *info = response_info_; | |
| 152 } | |
| 153 | |
| 154 private: | |
| 155 virtual ~URLRequestResourceBundleJob() { } | |
| 156 | |
| 157 void OnMimeTypeRead(std::string* out_mime_type, | |
| 158 std::string* charset, | |
| 159 std::string* data, | |
| 160 std::string* read_mime_type, | |
| 161 const net::CompletionCallback& callback, | |
| 162 bool read_result) { | |
| 163 *out_mime_type = *read_mime_type; | |
| 164 if (StartsWithASCII(*read_mime_type, "text/", false)) { | |
| 165 // All of our HTML files should be UTF-8 and for other resource types | |
| 166 // (like images), charset doesn't matter. | |
| 167 DCHECK(IsStringUTF8(*data)); | |
| 168 *charset = "utf-8"; | |
| 169 } | |
| 170 int result = read_result ? net::OK : net::ERR_INVALID_URL; | |
| 171 callback.Run(result); | |
| 172 } | |
| 173 | |
| 174 // We need the filename of the resource to determine the mime type. | |
| 175 base::FilePath filename_; | |
| 176 | |
| 177 // The resource bundle id to load. | |
| 178 int resource_id_; | |
| 179 | |
| 180 net::HttpResponseInfo response_info_; | |
| 181 | |
| 182 mutable base::WeakPtrFactory<URLRequestResourceBundleJob> weak_factory_; | |
| 183 }; | |
| 184 | |
| 185 class GeneratedBackgroundPageJob : public net::URLRequestSimpleJob { | |
| 186 public: | |
| 187 GeneratedBackgroundPageJob(net::URLRequest* request, | |
| 188 net::NetworkDelegate* network_delegate, | |
| 189 const scoped_refptr<const Extension> extension, | |
| 190 const std::string& content_security_policy) | |
| 191 : net::URLRequestSimpleJob(request, network_delegate), | |
| 192 extension_(extension) { | |
| 193 const bool send_cors_headers = false; | |
| 194 // Leave cache headers out of generated background page jobs. | |
| 195 response_info_.headers = BuildHttpHeaders(content_security_policy, | |
| 196 send_cors_headers, | |
| 197 base::Time()); | |
| 198 } | |
| 199 | |
| 200 // Overridden from URLRequestSimpleJob: | |
| 201 virtual int GetData(std::string* mime_type, | |
| 202 std::string* charset, | |
| 203 std::string* data, | |
| 204 const net::CompletionCallback& callback) const OVERRIDE { | |
| 205 *mime_type = "text/html"; | |
| 206 *charset = "utf-8"; | |
| 207 | |
| 208 *data = "<!DOCTYPE html>\n<body>\n"; | |
| 209 const std::vector<std::string>& background_scripts = | |
| 210 extensions::BackgroundInfo::GetBackgroundScripts(extension_.get()); | |
| 211 for (size_t i = 0; i < background_scripts.size(); ++i) { | |
| 212 *data += "<script src=\""; | |
| 213 *data += background_scripts[i]; | |
| 214 *data += "\"></script>\n"; | |
| 215 } | |
| 216 | |
| 217 return net::OK; | |
| 218 } | |
| 219 | |
| 220 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE { | |
| 221 *info = response_info_; | |
| 222 } | |
| 223 | |
| 224 private: | |
| 225 virtual ~GeneratedBackgroundPageJob() {} | |
| 226 | |
| 227 scoped_refptr<const Extension> extension_; | |
| 228 net::HttpResponseInfo response_info_; | |
| 229 }; | |
| 230 | |
| 231 base::Time GetFileLastModifiedTime(const base::FilePath& filename) { | |
| 232 if (base::PathExists(filename)) { | |
| 233 base::File::Info info; | |
| 234 if (base::GetFileInfo(filename, &info)) | |
| 235 return info.last_modified; | |
| 236 } | |
| 237 return base::Time(); | |
| 238 } | |
| 239 | |
| 240 base::Time GetFileCreationTime(const base::FilePath& filename) { | |
| 241 if (base::PathExists(filename)) { | |
| 242 base::File::Info info; | |
| 243 if (base::GetFileInfo(filename, &info)) | |
| 244 return info.creation_time; | |
| 245 } | |
| 246 return base::Time(); | |
| 247 } | |
| 248 | |
| 249 void ReadResourceFilePathAndLastModifiedTime( | |
| 250 const extensions::ExtensionResource& resource, | |
| 251 const base::FilePath& directory, | |
| 252 base::FilePath* file_path, | |
| 253 base::Time* last_modified_time) { | |
| 254 *file_path = resource.GetFilePath(); | |
| 255 *last_modified_time = GetFileLastModifiedTime(*file_path); | |
| 256 // While we're here, log the delta between extension directory | |
| 257 // creation time and the resource's last modification time. | |
| 258 base::ElapsedTimer query_timer; | |
| 259 base::Time dir_creation_time = GetFileCreationTime(directory); | |
| 260 UMA_HISTOGRAM_TIMES("Extensions.ResourceDirectoryTimestampQueryLatency", | |
| 261 query_timer.Elapsed()); | |
| 262 int64 delta_seconds = (*last_modified_time - dir_creation_time).InSeconds(); | |
| 263 if (delta_seconds >= 0) { | |
| 264 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ResourceLastModifiedDelta", | |
| 265 delta_seconds, | |
| 266 0, | |
| 267 base::TimeDelta::FromDays(30).InSeconds(), | |
| 268 50); | |
| 269 } else { | |
| 270 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ResourceLastModifiedNegativeDelta", | |
| 271 -delta_seconds, | |
| 272 1, | |
| 273 base::TimeDelta::FromDays(30).InSeconds(), | |
| 274 50); | |
| 275 } | |
| 276 } | |
| 277 | |
| 278 class URLRequestExtensionJob : public net::URLRequestFileJob { | |
| 279 public: | |
| 280 URLRequestExtensionJob(net::URLRequest* request, | |
| 281 net::NetworkDelegate* network_delegate, | |
| 282 const std::string& extension_id, | |
| 283 const base::FilePath& directory_path, | |
| 284 const base::FilePath& relative_path, | |
| 285 const std::string& content_security_policy, | |
| 286 bool send_cors_header, | |
| 287 bool follow_symlinks_anywhere) | |
| 288 : net::URLRequestFileJob( | |
| 289 request, network_delegate, base::FilePath(), | |
| 290 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | |
| 291 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | |
| 292 directory_path_(directory_path), | |
| 293 // TODO(tc): Move all of these files into resources.pak so we don't break | |
| 294 // when updating on Linux. | |
| 295 resource_(extension_id, directory_path, relative_path), | |
| 296 content_security_policy_(content_security_policy), | |
| 297 send_cors_header_(send_cors_header), | |
| 298 weak_factory_(this) { | |
| 299 if (follow_symlinks_anywhere) { | |
| 300 resource_.set_follow_symlinks_anywhere(); | |
| 301 } | |
| 302 } | |
| 303 | |
| 304 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE { | |
| 305 *info = response_info_; | |
| 306 } | |
| 307 | |
| 308 virtual void Start() OVERRIDE { | |
| 309 base::FilePath* read_file_path = new base::FilePath; | |
| 310 base::Time* last_modified_time = new base::Time(); | |
| 311 bool posted = BrowserThread::PostBlockingPoolTaskAndReply( | |
| 312 FROM_HERE, | |
| 313 base::Bind(&ReadResourceFilePathAndLastModifiedTime, resource_, | |
| 314 directory_path_, | |
| 315 base::Unretained(read_file_path), | |
| 316 base::Unretained(last_modified_time)), | |
| 317 base::Bind(&URLRequestExtensionJob::OnFilePathAndLastModifiedTimeRead, | |
| 318 weak_factory_.GetWeakPtr(), | |
| 319 base::Owned(read_file_path), | |
| 320 base::Owned(last_modified_time))); | |
| 321 DCHECK(posted); | |
| 322 } | |
| 323 | |
| 324 private: | |
| 325 virtual ~URLRequestExtensionJob() {} | |
| 326 | |
| 327 void OnFilePathAndLastModifiedTimeRead(base::FilePath* read_file_path, | |
| 328 base::Time* last_modified_time) { | |
| 329 file_path_ = *read_file_path; | |
| 330 response_info_.headers = BuildHttpHeaders( | |
| 331 content_security_policy_, | |
| 332 send_cors_header_, | |
| 333 *last_modified_time); | |
| 334 URLRequestFileJob::Start(); | |
| 335 } | |
| 336 | |
| 337 net::HttpResponseInfo response_info_; | |
| 338 base::FilePath directory_path_; | |
| 339 extensions::ExtensionResource resource_; | |
| 340 std::string content_security_policy_; | |
| 341 bool send_cors_header_; | |
| 342 base::WeakPtrFactory<URLRequestExtensionJob> weak_factory_; | |
| 343 }; | |
| 344 | |
| 345 bool ExtensionCanLoadInIncognito(const ResourceRequestInfo* info, | |
| 346 const std::string& extension_id, | |
| 347 extensions::InfoMap* extension_info_map) { | |
| 348 if (!extension_info_map->IsIncognitoEnabled(extension_id)) | |
| 349 return false; | |
| 350 | |
| 351 // Only allow incognito toplevel navigations to extension resources in | |
| 352 // split mode. In spanning mode, the extension must run in a single process, | |
| 353 // and an incognito tab prevents that. | |
| 354 if (info->GetResourceType() == ResourceType::MAIN_FRAME) { | |
| 355 const Extension* extension = | |
| 356 extension_info_map->extensions().GetByID(extension_id); | |
| 357 return extension && extensions::IncognitoInfo::IsSplitMode(extension); | |
| 358 } | |
| 359 | |
| 360 return true; | |
| 361 } | |
| 362 | |
| 363 // Returns true if an chrome-extension:// resource should be allowed to load. | |
| 364 // TODO(aa): This should be moved into ExtensionResourceRequestPolicy, but we | |
| 365 // first need to find a way to get CanLoadInIncognito state into the renderers. | |
| 366 bool AllowExtensionResourceLoad(net::URLRequest* request, | |
| 367 Profile::ProfileType profile_type, | |
| 368 const Extension* extension, | |
| 369 extensions::InfoMap* extension_info_map) { | |
| 370 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); | |
| 371 | |
| 372 // We have seen crashes where info is NULL: crbug.com/52374. | |
| 373 if (!info) { | |
| 374 LOG(ERROR) << "Allowing load of " << request->url().spec() | |
| 375 << "from unknown origin. Could not find user data for " | |
| 376 << "request."; | |
| 377 return true; | |
| 378 } | |
| 379 | |
| 380 if (profile_type == Profile::INCOGNITO_PROFILE && | |
| 381 !ExtensionCanLoadInIncognito(info, request->url().host(), | |
| 382 extension_info_map)) { | |
| 383 return false; | |
| 384 } | |
| 385 | |
| 386 // The following checks are meant to replicate similar set of checks in the | |
| 387 // renderer process, performed by ResourceRequestPolicy::CanRequestResource. | |
| 388 // These are not exactly equivalent, because we don't have the same bits of | |
| 389 // information. The two checks need to be kept in sync as much as possible, as | |
| 390 // an exploited renderer can bypass the checks in ResourceRequestPolicy. | |
| 391 | |
| 392 // Check if the extension for which this request is made is indeed loaded in | |
| 393 // the process sending the request. If not, we need to explicitly check if | |
| 394 // the resource is explicitly accessible or fits in a set of exception cases. | |
| 395 // Note: This allows a case where two extensions execute in the same renderer | |
| 396 // process to request each other's resources. We can't do a more precise | |
| 397 // check, since the renderer can lie about which extension has made the | |
| 398 // request. | |
| 399 if (extension_info_map->process_map().Contains( | |
| 400 request->url().host(), info->GetChildID())) { | |
| 401 return true; | |
| 402 } | |
| 403 | |
| 404 // Check workers so that importScripts works from extension workers. | |
| 405 if (extension_info_map->worker_process_map().Contains( | |
| 406 request->url().host(), info->GetChildID())) { | |
| 407 return true; | |
| 408 } | |
| 409 | |
| 410 // Extensions with webview: allow loading certain resources by guest renderers | |
| 411 // with privileged partition IDs as specified in the manifest file. | |
| 412 ExtensionRendererState* renderer_state = | |
| 413 ExtensionRendererState::GetInstance(); | |
| 414 ExtensionRendererState::WebViewInfo webview_info; | |
| 415 bool is_guest = renderer_state->GetWebViewInfo(info->GetChildID(), | |
| 416 info->GetRouteID(), | |
| 417 &webview_info); | |
| 418 std::string resource_path = request->url().path(); | |
| 419 if (is_guest && extensions::WebviewInfo::IsResourceWebviewAccessible( | |
| 420 extension, webview_info.partition_id, resource_path)) { | |
| 421 return true; | |
| 422 } | |
| 423 | |
| 424 // If the request is for navigations outside of webviews, then it should be | |
| 425 // allowed. The navigation logic in CrossSiteResourceHandler will properly | |
| 426 // transfer the navigation to a privileged process before it commits. | |
| 427 if (ResourceType::IsFrame(info->GetResourceType()) && !is_guest) | |
| 428 return true; | |
| 429 | |
| 430 if (!content::PageTransitionIsWebTriggerable(info->GetPageTransition())) | |
| 431 return false; | |
| 432 | |
| 433 // The following checks require that we have an actual extension object. If we | |
| 434 // don't have it, allow the request handling to continue with the rest of the | |
| 435 // checks. | |
| 436 if (!extension) | |
| 437 return true; | |
| 438 | |
| 439 // Disallow loading of packaged resources for hosted apps. We don't allow | |
| 440 // hybrid hosted/packaged apps. The one exception is access to icons, since | |
| 441 // some extensions want to be able to do things like create their own | |
| 442 // launchers. | |
| 443 std::string resource_root_relative_path = | |
| 444 request->url().path().empty() ? std::string() | |
| 445 : request->url().path().substr(1); | |
| 446 if (extension->is_hosted_app() && | |
| 447 !extensions::IconsInfo::GetIcons(extension) | |
| 448 .ContainsPath(resource_root_relative_path)) { | |
| 449 LOG(ERROR) << "Denying load of " << request->url().spec() << " from " | |
| 450 << "hosted app."; | |
| 451 return false; | |
| 452 } | |
| 453 | |
| 454 // Extensions with web_accessible_resources: allow loading by regular | |
| 455 // renderers. Since not all subresources are required to be listed in a v2 | |
| 456 // manifest, we must allow all loads if there are any web accessible | |
| 457 // resources. See http://crbug.com/179127. | |
| 458 if (extension->manifest_version() < 2 || | |
| 459 extensions::WebAccessibleResourcesInfo::HasWebAccessibleResources( | |
| 460 extension)) { | |
| 461 return true; | |
| 462 } | |
| 463 | |
| 464 // If there aren't any explicitly marked web accessible resources, the | |
| 465 // load should be allowed only if it is by DevTools. A close approximation is | |
| 466 // checking if the extension contains a DevTools page. | |
| 467 if (extensions::ManifestURL::GetDevToolsPage(extension).is_empty()) | |
| 468 return false; | |
| 469 | |
| 470 return true; | |
| 471 } | |
| 472 | |
| 473 // Returns true if the given URL references an icon in the given extension. | |
| 474 bool URLIsForExtensionIcon(const GURL& url, const Extension* extension) { | |
| 475 DCHECK(url.SchemeIs(extensions::kExtensionScheme)); | |
| 476 | |
| 477 if (!extension) | |
| 478 return false; | |
| 479 | |
| 480 std::string path = url.path(); | |
| 481 DCHECK_EQ(url.host(), extension->id()); | |
| 482 DCHECK(path.length() > 0 && path[0] == '/'); | |
| 483 path = path.substr(1); | |
| 484 return extensions::IconsInfo::GetIcons(extension).ContainsPath(path); | |
| 485 } | |
| 486 | |
| 487 class ExtensionProtocolHandler | |
| 488 : public net::URLRequestJobFactory::ProtocolHandler { | |
| 489 public: | |
| 490 ExtensionProtocolHandler(Profile::ProfileType profile_type, | |
| 491 extensions::InfoMap* extension_info_map) | |
| 492 : profile_type_(profile_type), extension_info_map_(extension_info_map) {} | |
| 493 | |
| 494 virtual ~ExtensionProtocolHandler() {} | |
| 495 | |
| 496 virtual net::URLRequestJob* MaybeCreateJob( | |
| 497 net::URLRequest* request, | |
| 498 net::NetworkDelegate* network_delegate) const OVERRIDE; | |
| 499 | |
| 500 private: | |
| 501 const Profile::ProfileType profile_type_; | |
| 502 extensions::InfoMap* const extension_info_map_; | |
| 503 DISALLOW_COPY_AND_ASSIGN(ExtensionProtocolHandler); | |
| 504 }; | |
| 505 | |
| 506 // Creates URLRequestJobs for extension:// URLs. | |
| 507 net::URLRequestJob* | |
| 508 ExtensionProtocolHandler::MaybeCreateJob( | |
| 509 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | |
| 510 // chrome-extension://extension-id/resource/path.js | |
| 511 std::string extension_id = request->url().host(); | |
| 512 const Extension* extension = | |
| 513 extension_info_map_->extensions().GetByID(extension_id); | |
| 514 | |
| 515 // TODO(mpcomplete): better error code. | |
| 516 if (!AllowExtensionResourceLoad( | |
| 517 request, profile_type_, extension, extension_info_map_)) { | |
| 518 return new net::URLRequestErrorJob( | |
| 519 request, network_delegate, net::ERR_ADDRESS_UNREACHABLE); | |
| 520 } | |
| 521 | |
| 522 base::FilePath directory_path; | |
| 523 if (extension) | |
| 524 directory_path = extension->path(); | |
| 525 if (directory_path.value().empty()) { | |
| 526 const Extension* disabled_extension = | |
| 527 extension_info_map_->disabled_extensions().GetByID(extension_id); | |
| 528 if (URLIsForExtensionIcon(request->url(), disabled_extension)) | |
| 529 directory_path = disabled_extension->path(); | |
| 530 if (directory_path.value().empty()) { | |
| 531 LOG(WARNING) << "Failed to GetPathForExtension: " << extension_id; | |
| 532 return NULL; | |
| 533 } | |
| 534 } | |
| 535 | |
| 536 std::string content_security_policy; | |
| 537 bool send_cors_header = false; | |
| 538 bool follow_symlinks_anywhere = false; | |
| 539 if (extension) { | |
| 540 std::string resource_path = request->url().path(); | |
| 541 content_security_policy = | |
| 542 extensions::CSPInfo::GetResourceContentSecurityPolicy(extension, | |
| 543 resource_path); | |
| 544 if ((extension->manifest_version() >= 2 || | |
| 545 extensions::WebAccessibleResourcesInfo::HasWebAccessibleResources( | |
| 546 extension)) && | |
| 547 extensions::WebAccessibleResourcesInfo::IsResourceWebAccessible( | |
| 548 extension, resource_path)) | |
| 549 send_cors_header = true; | |
| 550 | |
| 551 follow_symlinks_anywhere = | |
| 552 (extension->creation_flags() & Extension::FOLLOW_SYMLINKS_ANYWHERE) | |
| 553 != 0; | |
| 554 } | |
| 555 | |
| 556 std::string path = request->url().path(); | |
| 557 if (path.size() > 1 && | |
| 558 path.substr(1) == extensions::kGeneratedBackgroundPageFilename) { | |
| 559 return new GeneratedBackgroundPageJob( | |
| 560 request, network_delegate, extension, content_security_policy); | |
| 561 } | |
| 562 | |
| 563 base::FilePath resources_path; | |
| 564 base::FilePath relative_path; | |
| 565 // Try to load extension resources from chrome resource file if | |
| 566 // directory_path is a descendant of resources_path. resources_path | |
| 567 // corresponds to src/chrome/browser/resources in source tree. | |
| 568 if (PathService::Get(chrome::DIR_RESOURCES, &resources_path) && | |
| 569 // Since component extension resources are included in | |
| 570 // component_extension_resources.pak file in resources_path, calculate | |
| 571 // extension relative path against resources_path. | |
| 572 resources_path.AppendRelativePath(directory_path, &relative_path)) { | |
| 573 base::FilePath request_path = | |
| 574 extensions::file_util::ExtensionURLToRelativeFilePath(request->url()); | |
| 575 int resource_id; | |
| 576 if (extensions::ImageLoader::IsComponentExtensionResource( | |
| 577 directory_path, request_path, &resource_id)) { | |
| 578 relative_path = relative_path.Append(request_path); | |
| 579 relative_path = relative_path.NormalizePathSeparators(); | |
| 580 return new URLRequestResourceBundleJob( | |
| 581 request, | |
| 582 network_delegate, | |
| 583 relative_path, | |
| 584 resource_id, | |
| 585 content_security_policy, | |
| 586 send_cors_header); | |
| 587 } | |
| 588 } | |
| 589 | |
| 590 relative_path = | |
| 591 extensions::file_util::ExtensionURLToRelativeFilePath(request->url()); | |
| 592 | |
| 593 if (SharedModuleInfo::IsImportedPath(path)) { | |
| 594 std::string new_extension_id; | |
| 595 std::string new_relative_path; | |
| 596 SharedModuleInfo::ParseImportedPath(path, &new_extension_id, | |
| 597 &new_relative_path); | |
| 598 const Extension* new_extension = | |
| 599 extension_info_map_->extensions().GetByID(new_extension_id); | |
| 600 | |
| 601 bool first_party_in_import = false; | |
| 602 // NB: This first_party_for_cookies call is not for security, it is only | |
| 603 // used so an exported extension can limit the visible surface to the | |
| 604 // extension that imports it, more or less constituting its API. | |
| 605 const std::string& first_party_path = | |
| 606 request->first_party_for_cookies().path(); | |
| 607 if (SharedModuleInfo::IsImportedPath(first_party_path)) { | |
| 608 std::string first_party_id; | |
| 609 std::string dummy; | |
| 610 SharedModuleInfo::ParseImportedPath(first_party_path, &first_party_id, | |
| 611 &dummy); | |
| 612 if (first_party_id == new_extension_id) { | |
| 613 first_party_in_import = true; | |
| 614 } | |
| 615 } | |
| 616 | |
| 617 if (SharedModuleInfo::ImportsExtensionById(extension, new_extension_id) && | |
| 618 new_extension && | |
| 619 (first_party_in_import || | |
| 620 SharedModuleInfo::IsExportAllowed(new_extension, new_relative_path))) { | |
| 621 directory_path = new_extension->path(); | |
| 622 extension_id = new_extension_id; | |
| 623 relative_path = base::FilePath::FromUTF8Unsafe(new_relative_path); | |
| 624 } else { | |
| 625 return NULL; | |
| 626 } | |
| 627 } | |
| 628 | |
| 629 return new URLRequestExtensionJob(request, | |
| 630 network_delegate, | |
| 631 extension_id, | |
| 632 directory_path, | |
| 633 relative_path, | |
| 634 content_security_policy, | |
| 635 send_cors_header, | |
| 636 follow_symlinks_anywhere); | |
| 637 } | |
| 638 | |
| 639 } // namespace | |
| 640 | |
| 641 net::URLRequestJobFactory::ProtocolHandler* CreateExtensionProtocolHandler( | |
| 642 Profile::ProfileType profile_type, | |
| 643 extensions::InfoMap* extension_info_map) { | |
| 644 return new ExtensionProtocolHandler(profile_type, extension_info_map); | |
| 645 } | |
| OLD | NEW |