blob: 822c77f02626985275027009175c884f096d51b7 [file] [log] [blame]
[email protected]93f72062013-05-29 20:29:401// Copyright 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
[email protected]9d450492013-06-13 23:08:375#include "chrome/common/pepper_permission_util.h"
[email protected]93f72062013-05-29 20:29:406
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"
[email protected]93f72062013-05-29 20:29:4013#include "extensions/common/constants.h"
[email protected]e4452d32013-11-15 23:07:4114#include "extensions/common/extension.h"
[email protected]289c44b2013-12-17 03:26:5715#include "extensions/common/extension_set.h"
[email protected]301116c62013-11-26 10:37:4516#include "extensions/common/manifest_handlers/shared_module_info.h"
[email protected]93f72062013-05-29 20:29:4017
18using extensions::Extension;
19using extensions::Manifest;
[email protected]b0b1fbc2014-05-10 19:58:2920using extensions::SharedModuleInfo;
[email protected]93f72062013-05-29 20:29:4021
22namespace chrome {
23
24namespace {
25
26std::string HashHost(const std::string& host) {
27 const std::string id_hash = base::SHA1HashString(host);
28 DCHECK_EQ(id_hash.length(), base::kSHA1Length);
29 return base::HexEncode(id_hash.c_str(), id_hash.length());
30}
31
32bool HostIsInSet(const std::string& host, const std::set<std::string>& set) {
33 return set.count(host) > 0 || set.count(HashHost(host)) > 0;
34}
35
36} // namespace
37
38bool IsExtensionOrSharedModuleWhitelisted(
[email protected]93f72062013-05-29 20:29:4039 const GURL& url,
[email protected]289c44b2013-12-17 03:26:5740 const extensions::ExtensionSet* extension_set,
[email protected]dda54822013-06-15 01:26:3941 const std::set<std::string>& whitelist) {
42 if (!url.is_valid() || !url.SchemeIs(extensions::kExtensionScheme))
[email protected]93f72062013-05-29 20:29:4043 return false;
44
[email protected]dda54822013-06-15 01:26:3945 const std::string host = url.host();
46 if (HostIsInSet(host, whitelist))
[email protected]93f72062013-05-29 20:29:4047 return true;
[email protected]93f72062013-05-29 20:29:4048
[email protected]93f72062013-05-29 20:29:4049 // Check the modules that are imported by this extension to see if any of them
50 // is whitelisted.
[email protected]9d450492013-06-13 23:08:3751 const Extension* extension = extension_set ? extension_set->GetByID(host)
52 : NULL;
[email protected]b0b1fbc2014-05-10 19:58:2953 if (!extension)
54 return false;
55
56 typedef std::vector<SharedModuleInfo::ImportInfo> ImportInfoVector;
57 const ImportInfoVector& imports = SharedModuleInfo::GetImports(extension);
58 for (ImportInfoVector::const_iterator it = imports.begin();
59 it != imports.end();
60 ++it) {
61 const Extension* imported_extension =
62 extension_set->GetByID(it->extension_id);
63 if (imported_extension &&
64 SharedModuleInfo::IsSharedModule(imported_extension) &&
[email protected]b0b1fbc2014-05-10 19:58:2965 HostIsInSet(it->extension_id, whitelist)) {
66 return true;
[email protected]93f72062013-05-29 20:29:4067 }
68 }
69
[email protected]dda54822013-06-15 01:26:3970 return false;
71}
72
73bool IsHostAllowedByCommandLine(const GURL& url,
[email protected]289c44b2013-12-17 03:26:5774 const extensions::ExtensionSet* extension_set,
[email protected]dda54822013-06-15 01:26:3975 const char* command_line_switch) {
76 if (!url.is_valid())
77 return false;
78
avi79bf9132014-12-25 17:48:0579 const base::CommandLine& command_line =
80 *base::CommandLine::ForCurrentProcess();
[email protected]93f72062013-05-29 20:29:4081 const std::string allowed_list =
82 command_line.GetSwitchValueASCII(command_line_switch);
[email protected]dda54822013-06-15 01:26:3983 if (allowed_list.empty())
84 return false;
85
86 const std::string host = url.host();
[email protected]93f72062013-05-29 20:29:4087 if (allowed_list == "*") {
[email protected]9d450492013-06-13 23:08:3788 // For now, we only allow packaged and platform apps in this wildcard.
[email protected]dda54822013-06-15 01:26:3989 if (!extension_set || !url.SchemeIs(extensions::kExtensionScheme))
90 return false;
91
92 const Extension* extension = extension_set->GetByID(host);
[email protected]93f72062013-05-29 20:29:4093 return extension &&
94 (extension->GetType() == Manifest::TYPE_LEGACY_PACKAGED_APP ||
95 extension->GetType() == Manifest::TYPE_PLATFORM_APP);
96 }
97
[email protected]dda54822013-06-15 01:26:3998 base::StringTokenizer t(allowed_list, ",");
99 while (t.GetNext()) {
100 if (t.token() == host)
101 return true;
[email protected]93f72062013-05-29 20:29:40102 }
103
104 return false;
105}
106
107} // namespace chrome