OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "extensions/browser/content_verifier.h" | 5 #include "extensions/browser/content_verifier.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/common/content_switches.h" |
13 #include "extensions/browser/content_hash_fetcher.h" | 14 #include "extensions/browser/content_hash_fetcher.h" |
14 #include "extensions/browser/content_hash_reader.h" | 15 #include "extensions/browser/content_hash_reader.h" |
15 #include "extensions/browser/content_verifier_delegate.h" | 16 #include "extensions/browser/content_verifier_delegate.h" |
16 #include "extensions/browser/extension_registry.h" | 17 #include "extensions/browser/extension_registry.h" |
| 18 #include "extensions/common/constants.h" |
| 19 #include "extensions/common/extension_l10n_util.h" |
17 #include "extensions/common/switches.h" | 20 #include "extensions/common/switches.h" |
18 | 21 |
19 namespace { | 22 namespace { |
20 | 23 |
21 const char kExperimentName[] = "ExtensionContentVerification"; | 24 const char kExperimentName[] = "ExtensionContentVerification"; |
22 | 25 |
23 } // namespace | 26 } // namespace |
24 | 27 |
25 namespace extensions { | 28 namespace extensions { |
26 | 29 |
(...skipping 15 matching lines...) Expand all Loading... |
42 | 45 |
43 void ContentVerifier::Shutdown() { | 46 void ContentVerifier::Shutdown() { |
44 fetcher_.reset(); | 47 fetcher_.reset(); |
45 delegate_.reset(); | 48 delegate_.reset(); |
46 } | 49 } |
47 | 50 |
48 ContentVerifyJob* ContentVerifier::CreateJobFor( | 51 ContentVerifyJob* ContentVerifier::CreateJobFor( |
49 const std::string& extension_id, | 52 const std::string& extension_id, |
50 const base::FilePath& extension_root, | 53 const base::FilePath& extension_root, |
51 const base::FilePath& relative_path) { | 54 const base::FilePath& relative_path) { |
52 if (!delegate_) | 55 if (mode_ < BOOTSTRAP || !delegate_) |
53 return NULL; | 56 return NULL; |
54 | 57 |
55 ExtensionRegistry* registry = ExtensionRegistry::Get(context_); | 58 ExtensionRegistry* registry = ExtensionRegistry::Get(context_); |
56 const Extension* extension = | 59 const Extension* extension = |
57 registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING); | 60 registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING); |
58 | 61 |
59 if (!extension || !delegate_->ShouldBeVerified(*extension) || | 62 if (!extension || !extension->version() || |
60 !extension->version()) | 63 !delegate_->ShouldBeVerified(*extension)) |
61 return NULL; | 64 return NULL; |
62 | 65 |
| 66 // Images used in the browser get transcoded during install, so skip checking |
| 67 // them for now. TODO(asargent) - see if we can cache this list for a given |
| 68 // extension id/version pair. |
| 69 std::set<base::FilePath> browser_images = |
| 70 delegate_->GetBrowserImagePaths(extension); |
| 71 if (ContainsKey(browser_images, relative_path)) |
| 72 return NULL; |
| 73 |
| 74 base::FilePath locales_dir = extension_root.Append(kLocaleFolder); |
| 75 base::FilePath full_path = extension_root.Append(relative_path); |
| 76 if (locales_dir.IsParent(full_path)) { |
| 77 // TODO(asargent) - see if we can cache this list to avoid having to fetch |
| 78 // it every time. Maybe it can never change at runtime? (Or if it can, |
| 79 // maybe there is an event we can listen for to know to drop our cache). |
| 80 std::set<std::string> all_locales; |
| 81 extension_l10n_util::GetAllLocales(&all_locales); |
| 82 // Since message catalogs get transcoded during installation, we want to |
| 83 // ignore only those paths that the localization transcoding *did* ignore. |
| 84 if (!extension_l10n_util::ShouldSkipValidation( |
| 85 locales_dir, full_path, all_locales)) |
| 86 return NULL; |
| 87 } |
| 88 |
| 89 // TODO(asargent) - we can probably get some good performance wins by having |
| 90 // a cache of ContentHashReader's that we hold onto past the end of each job. |
63 return new ContentVerifyJob( | 91 return new ContentVerifyJob( |
64 new ContentHashReader(extension_id, | 92 new ContentHashReader(extension_id, |
65 *extension->version(), | 93 *extension->version(), |
66 extension_root, | 94 extension_root, |
67 relative_path, | 95 relative_path, |
68 delegate_->PublicKey()), | 96 delegate_->PublicKey()), |
69 base::Bind(&ContentVerifier::VerifyFailed, this, extension->id())); | 97 base::Bind(&ContentVerifier::VerifyFailed, this, extension->id())); |
70 } | 98 } |
71 | 99 |
72 void ContentVerifier::VerifyFailed(const std::string& extension_id, | 100 void ContentVerifier::VerifyFailed(const std::string& extension_id, |
(...skipping 18 matching lines...) Expand all Loading... |
91 registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING); | 119 registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING); |
92 if (extension) | 120 if (extension) |
93 fetcher_->DoFetch(extension); | 121 fetcher_->DoFetch(extension); |
94 return; | 122 return; |
95 } | 123 } |
96 delegate_->VerifyFailed(extension_id); | 124 delegate_->VerifyFailed(extension_id); |
97 } | 125 } |
98 | 126 |
99 // static | 127 // static |
100 ContentVerifier::Mode ContentVerifier::GetMode() { | 128 ContentVerifier::Mode ContentVerifier::GetMode() { |
| 129 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 130 |
101 Mode experiment_value = NONE; | 131 Mode experiment_value = NONE; |
102 const std::string group = base::FieldTrialList::FindFullName(kExperimentName); | 132 const std::string group = base::FieldTrialList::FindFullName(kExperimentName); |
103 if (group == "EnforceStrict") | 133 if (group == "EnforceStrict") |
104 experiment_value = ENFORCE_STRICT; | 134 experiment_value = ENFORCE_STRICT; |
105 else if (group == "Enforce") | 135 else if (group == "Enforce") |
106 experiment_value = ENFORCE; | 136 experiment_value = ENFORCE; |
107 else if (group == "Bootstrap") | 137 else if (group == "Bootstrap") |
108 experiment_value = BOOTSTRAP; | 138 experiment_value = BOOTSTRAP; |
109 | 139 |
| 140 // The field trial value that normally comes from the server can be |
| 141 // overridden on the command line, which we don't want to allow since malware |
| 142 // can set chrome command line flags. There isn't currently a way to find out |
| 143 // what the server-provided value is in this case, so we conservatively |
| 144 // default to the strictest mode if we detect our experiment name being |
| 145 // overridden. |
| 146 if (command_line->HasSwitch(::switches::kForceFieldTrials)) { |
| 147 std::string forced_trials = |
| 148 command_line->GetSwitchValueASCII(::switches::kForceFieldTrials); |
| 149 if (forced_trials.find(kExperimentName) != std::string::npos) |
| 150 experiment_value = ENFORCE_STRICT; |
| 151 } |
| 152 |
110 Mode cmdline_value = NONE; | 153 Mode cmdline_value = NONE; |
111 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
112 if (command_line->HasSwitch(switches::kExtensionContentVerification)) { | 154 if (command_line->HasSwitch(switches::kExtensionContentVerification)) { |
113 std::string switch_value = command_line->GetSwitchValueASCII( | 155 std::string switch_value = command_line->GetSwitchValueASCII( |
114 switches::kExtensionContentVerification); | 156 switches::kExtensionContentVerification); |
115 if (switch_value == switches::kExtensionContentVerificationBootstrap) | 157 if (switch_value == switches::kExtensionContentVerificationBootstrap) |
116 cmdline_value = BOOTSTRAP; | 158 cmdline_value = BOOTSTRAP; |
117 else if (switch_value == switches::kExtensionContentVerificationEnforce) | 159 else if (switch_value == switches::kExtensionContentVerificationEnforce) |
118 cmdline_value = ENFORCE; | 160 cmdline_value = ENFORCE; |
119 else if (switch_value == | 161 else if (switch_value == |
120 switches::kExtensionContentVerificationEnforceStrict) | 162 switches::kExtensionContentVerificationEnforceStrict) |
121 cmdline_value = ENFORCE_STRICT; | 163 cmdline_value = ENFORCE_STRICT; |
122 else | 164 else |
123 // If no value was provided (or the wrong one), just default to enforce. | 165 // If no value was provided (or the wrong one), just default to enforce. |
124 cmdline_value = ENFORCE; | 166 cmdline_value = ENFORCE; |
125 } | 167 } |
126 | 168 |
127 // We don't want to allow the command-line flags to eg disable enforcement if | 169 // We don't want to allow the command-line flags to eg disable enforcement if |
128 // the experiment group says it should be on, or malware may just modify the | 170 // the experiment group says it should be on, or malware may just modify the |
129 // command line flags. So return the more restrictive of the 2 values. | 171 // command line flags. So return the more restrictive of the 2 values. |
130 return std::max(experiment_value, cmdline_value); | 172 return std::max(experiment_value, cmdline_value); |
131 } | 173 } |
132 | 174 |
133 } // namespace extensions | 175 } // namespace extensions |
OLD | NEW |