OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/pepper_permission_util.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "base/sha1.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_tokenizer.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/extensions/extension_system.h" |
| 15 #include "chrome/browser/google/google_util.h" |
| 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/common/extensions/extension.h" |
| 18 #include "chrome/common/extensions/extension_set.h" |
| 19 #include "chrome/common/extensions/manifest_handlers/shared_module_info.h" |
| 20 #include "extensions/common/constants.h" |
| 21 |
| 22 using extensions::Extension; |
| 23 using extensions::Manifest; |
| 24 |
| 25 namespace chrome { |
| 26 |
| 27 namespace { |
| 28 |
| 29 std::string HashHost(const std::string& host) { |
| 30 const std::string id_hash = base::SHA1HashString(host); |
| 31 DCHECK_EQ(id_hash.length(), base::kSHA1Length); |
| 32 return base::HexEncode(id_hash.c_str(), id_hash.length()); |
| 33 } |
| 34 |
| 35 bool HostIsInSet(const std::string& host, const std::set<std::string>& set) { |
| 36 return set.count(host) > 0 || set.count(HashHost(host)) > 0; |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
| 41 bool IsExtensionOrSharedModuleWhitelisted( |
| 42 Profile* profile, |
| 43 const GURL& url, |
| 44 const std::set<std::string>& whitelist, |
| 45 const char* command_line_switch) { |
| 46 if (!url.is_valid()) |
| 47 return false; |
| 48 |
| 49 const std::string host = url.host(); |
| 50 if (url.SchemeIs(extensions::kExtensionScheme) && |
| 51 HostIsInSet(host, whitelist)) { |
| 52 return true; |
| 53 } |
| 54 |
| 55 const Extension* extension = NULL; |
| 56 ExtensionService* extension_service = !profile ? NULL : |
| 57 extensions::ExtensionSystem::Get(profile)->extension_service(); |
| 58 if (extension_service) { |
| 59 extension = extension_service->extensions()-> |
| 60 GetExtensionOrAppByURL(ExtensionURLInfo(url)); |
| 61 } |
| 62 |
| 63 // Check the modules that are imported by this extension to see if any of them |
| 64 // is whitelisted. |
| 65 if (extension) { |
| 66 typedef std::vector<extensions::SharedModuleInfo::ImportInfo> |
| 67 ImportInfoVector; |
| 68 const ImportInfoVector& imports = |
| 69 extensions::SharedModuleInfo::GetImports(extension); |
| 70 for (ImportInfoVector::const_iterator it = imports.begin(); |
| 71 it != imports.end(); ++it) { |
| 72 const Extension* imported_extension = extension_service-> |
| 73 GetExtensionById(it->extension_id, false); |
| 74 if (imported_extension && |
| 75 extensions::SharedModuleInfo::IsSharedModule(imported_extension) && |
| 76 HostIsInSet(it->extension_id, whitelist)) { |
| 77 return true; |
| 78 } |
| 79 } |
| 80 } |
| 81 |
| 82 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 83 const std::string allowed_list = |
| 84 command_line.GetSwitchValueASCII(command_line_switch); |
| 85 if (allowed_list == "*") { |
| 86 // The wildcard allows socket API only for packaged and platform apps. |
| 87 return extension && |
| 88 (extension->GetType() == Manifest::TYPE_LEGACY_PACKAGED_APP || |
| 89 extension->GetType() == Manifest::TYPE_PLATFORM_APP); |
| 90 } |
| 91 |
| 92 if (!allowed_list.empty()) { |
| 93 base::StringTokenizer t(allowed_list, ","); |
| 94 while (t.GetNext()) { |
| 95 if (t.token() == host) |
| 96 return true; |
| 97 } |
| 98 } |
| 99 |
| 100 return false; |
| 101 } |
| 102 |
| 103 } // namespace chrome |
OLD | NEW |