1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
57d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/common/pepper_permission_util.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <vector>
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/command_line.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/sha1.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_number_conversions.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_tokenizer.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/common/extensions/extension.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/common/extensions/extension_set.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/common/extensions/manifest_handlers/shared_module_info.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "extensions/common/constants.h"
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)using extensions::Extension;
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)using extensions::Manifest;
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace chrome {
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace {
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)std::string HashHost(const std::string& host) {
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const std::string id_hash = base::SHA1HashString(host);
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK_EQ(id_hash.length(), base::kSHA1Length);
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return base::HexEncode(id_hash.c_str(), id_hash.length());
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool HostIsInSet(const std::string& host, const std::set<std::string>& set) {
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return set.count(host) > 0 || set.count(HashHost(host)) > 0;
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool IsExtensionOrSharedModuleWhitelisted(
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const GURL& url,
397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const ExtensionSet* extension_set,
407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::set<std::string>& whitelist) {
417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!url.is_valid() || !url.SchemeIs(extensions::kExtensionScheme))
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const std::string host = url.host();
457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (HostIsInSet(host, whitelist))
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return true;
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Check the modules that are imported by this extension to see if any of them
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // is whitelisted.
507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  const Extension* extension = extension_set ? extension_set->GetByID(host)
517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                             : NULL;
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (extension) {
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    typedef std::vector<extensions::SharedModuleInfo::ImportInfo>
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        ImportInfoVector;
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const ImportInfoVector& imports =
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        extensions::SharedModuleInfo::GetImports(extension);
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    for (ImportInfoVector::const_iterator it = imports.begin();
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)         it != imports.end(); ++it) {
597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      const Extension* imported_extension = extension_set->GetByID(
607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          it->extension_id);
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (imported_extension &&
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          extensions::SharedModuleInfo::IsSharedModule(imported_extension) &&
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          HostIsInSet(it->extension_id, whitelist)) {
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        return true;
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      }
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return false;
707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool IsHostAllowedByCommandLine(const GURL& url,
737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                const ExtensionSet* extension_set,
747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                const char* command_line_switch) {
757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!url.is_valid())
767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return false;
777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const std::string allowed_list =
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      command_line.GetSwitchValueASCII(command_line_switch);
817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (allowed_list.empty())
827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return false;
837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  const std::string host = url.host();
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (allowed_list == "*") {
867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // For now, we only allow packaged and platform apps in this wildcard.
877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (!extension_set || !url.SchemeIs(extensions::kExtensionScheme))
887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return false;
897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const Extension* extension = extension_set->GetByID(host);
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return extension &&
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        (extension->GetType() == Manifest::TYPE_LEGACY_PACKAGED_APP ||
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)         extension->GetType() == Manifest::TYPE_PLATFORM_APP);
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  base::StringTokenizer t(allowed_list, ",");
977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  while (t.GetNext()) {
987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (t.token() == host)
997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return true;
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return false;
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace chrome
106