OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/common/file_util.h" | 5 #include "extensions/common/file_util.h" |
6 | 6 |
7 #include <map> | |
7 #include <set> | 8 #include <set> |
8 #include <string> | 9 #include <string> |
9 #include <utility> | 10 #include <utility> |
11 #include <vector> | |
10 | 12 |
11 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/files/file_enumerator.h" | |
12 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/files/scoped_temp_dir.h" | |
17 #include "base/json/json_file_value_serializer.h" | |
18 #include "base/logging.h" | |
13 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
20 #include "base/strings/stringprintf.h" | |
14 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
22 #include "base/threading/thread_restrictions.h" | |
15 #include "extensions/common/constants.h" | 23 #include "extensions/common/constants.h" |
16 #include "extensions/common/extension.h" | 24 #include "extensions/common/extension.h" |
17 #include "extensions/common/extension_icon_set.h" | 25 #include "extensions/common/extension_icon_set.h" |
18 #include "extensions/common/extension_l10n_util.h" | 26 #include "extensions/common/extension_l10n_util.h" |
27 #include "extensions/common/install_warning.h" | |
28 #include "extensions/common/manifest.h" | |
29 #include "extensions/common/manifest_constants.h" | |
30 #include "extensions/common/manifest_handler.h" | |
31 #include "extensions/common/manifest_handlers/icons_handler.h" | |
19 #include "extensions/common/message_bundle.h" | 32 #include "extensions/common/message_bundle.h" |
20 #include "grit/generated_resources.h" | 33 #include "grit/generated_resources.h" |
21 #include "net/base/escape.h" | 34 #include "net/base/escape.h" |
22 #include "ui/base/l10n/l10n_util.h" | 35 #include "ui/base/l10n/l10n_util.h" |
23 #include "url/gurl.h" | 36 #include "url/gurl.h" |
24 | 37 |
25 namespace extensions { | 38 namespace extensions { |
26 namespace file_util { | 39 namespace file_util { |
27 namespace { | 40 namespace { |
28 | 41 |
29 // Returns true if the given file path exists and is not zero-length. | 42 // Returns true if the given file path exists and is not zero-length. |
30 bool ValidateFilePath(const base::FilePath& path) { | 43 bool ValidateFilePath(const base::FilePath& path) { |
31 int64 size = 0; | 44 int64 size = 0; |
32 if (!base::PathExists(path) || | 45 if (!base::PathExists(path) || |
33 !base::GetFileSize(path, &size) || | 46 !base::GetFileSize(path, &size) || |
34 size == 0) { | 47 size == 0) { |
35 return false; | 48 return false; |
36 } | 49 } |
37 | 50 |
38 return true; | 51 return true; |
39 } | 52 } |
40 | 53 |
41 } // namespace | 54 } // namespace |
42 | 55 |
56 const base::FilePath::CharType kTempDirectoryName[] = FILE_PATH_LITERAL("Temp"); | |
James Cook
2014/04/12 00:03:22
This code is also unchanged except for moving it i
| |
57 | |
58 base::FilePath InstallExtension(const base::FilePath& unpacked_source_dir, | |
59 const std::string& id, | |
60 const std::string& version, | |
61 const base::FilePath& extensions_dir) { | |
62 base::FilePath extension_dir = extensions_dir.AppendASCII(id); | |
63 base::FilePath version_dir; | |
64 | |
65 // Create the extension directory if it doesn't exist already. | |
66 if (!base::PathExists(extension_dir)) { | |
67 if (!base::CreateDirectory(extension_dir)) | |
68 return base::FilePath(); | |
69 } | |
70 | |
71 // Get a temp directory on the same file system as the profile. | |
72 base::FilePath install_temp_dir = GetInstallTempDir(extensions_dir); | |
73 base::ScopedTempDir extension_temp_dir; | |
74 if (install_temp_dir.empty() || | |
75 !extension_temp_dir.CreateUniqueTempDirUnderPath(install_temp_dir)) { | |
76 LOG(ERROR) << "Creating of temp dir under in the profile failed."; | |
77 return base::FilePath(); | |
78 } | |
79 base::FilePath crx_temp_source = | |
80 extension_temp_dir.path().Append(unpacked_source_dir.BaseName()); | |
81 if (!base::Move(unpacked_source_dir, crx_temp_source)) { | |
82 LOG(ERROR) << "Moving extension from : " << unpacked_source_dir.value() | |
83 << " to : " << crx_temp_source.value() << " failed."; | |
84 return base::FilePath(); | |
85 } | |
86 | |
87 // Try to find a free directory. There can be legitimate conflicts in the case | |
88 // of overinstallation of the same version. | |
89 const int kMaxAttempts = 100; | |
90 for (int i = 0; i < kMaxAttempts; ++i) { | |
91 base::FilePath candidate = extension_dir.AppendASCII( | |
92 base::StringPrintf("%s_%u", version.c_str(), i)); | |
93 if (!base::PathExists(candidate)) { | |
94 version_dir = candidate; | |
95 break; | |
96 } | |
97 } | |
98 | |
99 if (version_dir.empty()) { | |
100 LOG(ERROR) << "Could not find a home for extension " << id << " with " | |
101 << "version " << version << "."; | |
102 return base::FilePath(); | |
103 } | |
104 | |
105 if (!base::Move(crx_temp_source, version_dir)) { | |
106 LOG(ERROR) << "Installing extension from : " << crx_temp_source.value() | |
107 << " into : " << version_dir.value() << " failed."; | |
108 return base::FilePath(); | |
109 } | |
110 | |
111 return version_dir; | |
112 } | |
113 | |
114 void UninstallExtension(const base::FilePath& extensions_dir, | |
115 const std::string& id) { | |
116 // We don't care about the return value. If this fails (and it can, due to | |
117 // plugins that aren't unloaded yet), it will get cleaned up by | |
118 // ExtensionGarbageCollector::GarbageCollectExtensions. | |
119 base::DeleteFile(extensions_dir.AppendASCII(id), true); // recursive. | |
120 } | |
121 | |
122 scoped_refptr<Extension> LoadExtension(const base::FilePath& extension_path, | |
123 Manifest::Location location, | |
124 int flags, | |
125 std::string* error) { | |
126 return LoadExtension(extension_path, std::string(), location, flags, error); | |
127 } | |
128 | |
129 scoped_refptr<Extension> LoadExtension(const base::FilePath& extension_path, | |
130 const std::string& extension_id, | |
131 Manifest::Location location, | |
132 int flags, | |
133 std::string* error) { | |
134 scoped_ptr<base::DictionaryValue> manifest( | |
135 LoadManifest(extension_path, error)); | |
136 if (!manifest.get()) | |
137 return NULL; | |
138 if (!extension_l10n_util::LocalizeExtension( | |
139 extension_path, manifest.get(), error)) { | |
140 return NULL; | |
141 } | |
142 | |
143 scoped_refptr<Extension> extension(Extension::Create( | |
144 extension_path, location, *manifest, flags, extension_id, error)); | |
145 if (!extension.get()) | |
146 return NULL; | |
147 | |
148 std::vector<InstallWarning> warnings; | |
149 if (!ValidateExtension(extension.get(), error, &warnings)) | |
150 return NULL; | |
151 extension->AddInstallWarnings(warnings); | |
152 | |
153 return extension; | |
154 } | |
155 | |
156 base::DictionaryValue* LoadManifest(const base::FilePath& extension_path, | |
157 std::string* error) { | |
158 base::FilePath manifest_path = extension_path.Append(kManifestFilename); | |
159 if (!base::PathExists(manifest_path)) { | |
160 *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); | |
161 return NULL; | |
162 } | |
163 | |
164 JSONFileValueSerializer serializer(manifest_path); | |
165 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, error)); | |
166 if (!root.get()) { | |
167 if (error->empty()) { | |
168 // If |error| is empty, than the file could not be read. | |
169 // It would be cleaner to have the JSON reader give a specific error | |
170 // in this case, but other code tests for a file error with | |
171 // error->empty(). For now, be consistent. | |
172 *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); | |
173 } else { | |
174 *error = base::StringPrintf( | |
175 "%s %s", manifest_errors::kManifestParseError, error->c_str()); | |
176 } | |
177 return NULL; | |
178 } | |
179 | |
180 if (!root->IsType(base::Value::TYPE_DICTIONARY)) { | |
181 *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_INVALID); | |
182 return NULL; | |
183 } | |
184 | |
185 return static_cast<base::DictionaryValue*>(root.release()); | |
186 } | |
187 | |
188 bool ValidateExtension(const Extension* extension, | |
189 std::string* error, | |
190 std::vector<InstallWarning>* warnings) { | |
191 // Ask registered manifest handlers to validate their paths. | |
192 if (!ManifestHandler::ValidateExtension(extension, error, warnings)) | |
193 return false; | |
194 | |
195 // Check children of extension root to see if any of them start with _ and is | |
196 // not on the reserved list. We only warn, and do not block the loading of the | |
197 // extension. | |
198 std::string warning; | |
199 if (!CheckForIllegalFilenames(extension->path(), &warning)) | |
200 warnings->push_back(InstallWarning(warning)); | |
201 | |
202 // Check that extensions don't include private key files. | |
203 std::vector<base::FilePath> private_keys = | |
204 FindPrivateKeyFiles(extension->path()); | |
205 if (extension->creation_flags() & Extension::ERROR_ON_PRIVATE_KEY) { | |
206 if (!private_keys.empty()) { | |
207 // Only print one of the private keys because l10n_util doesn't have a way | |
208 // to translate a list of strings. | |
209 *error = | |
210 l10n_util::GetStringFUTF8(IDS_EXTENSION_CONTAINS_PRIVATE_KEY, | |
211 private_keys.front().LossyDisplayName()); | |
212 return false; | |
213 } | |
214 } else { | |
215 for (size_t i = 0; i < private_keys.size(); ++i) { | |
216 warnings->push_back(InstallWarning( | |
217 l10n_util::GetStringFUTF8(IDS_EXTENSION_CONTAINS_PRIVATE_KEY, | |
218 private_keys[i].LossyDisplayName()))); | |
219 } | |
220 // Only warn; don't block loading the extension. | |
221 } | |
222 return true; | |
223 } | |
224 | |
225 std::vector<base::FilePath> FindPrivateKeyFiles( | |
226 const base::FilePath& extension_dir) { | |
227 std::vector<base::FilePath> result; | |
228 // Pattern matching only works at the root level, so filter manually. | |
229 base::FileEnumerator traversal( | |
230 extension_dir, /*recursive=*/true, base::FileEnumerator::FILES); | |
231 for (base::FilePath current = traversal.Next(); !current.empty(); | |
232 current = traversal.Next()) { | |
233 if (!current.MatchesExtension(kExtensionKeyFileExtension)) | |
234 continue; | |
235 | |
236 std::string key_contents; | |
237 if (!base::ReadFileToString(current, &key_contents)) { | |
238 // If we can't read the file, assume it's not a private key. | |
239 continue; | |
240 } | |
241 std::string key_bytes; | |
242 if (!Extension::ParsePEMKeyBytes(key_contents, &key_bytes)) { | |
243 // If we can't parse the key, assume it's ok too. | |
244 continue; | |
245 } | |
246 | |
247 result.push_back(current); | |
248 } | |
249 return result; | |
250 } | |
251 | |
252 bool CheckForIllegalFilenames(const base::FilePath& extension_path, | |
253 std::string* error) { | |
254 // Reserved underscore names. | |
255 static const base::FilePath::CharType* reserved_names[] = { | |
256 kLocaleFolder, kPlatformSpecificFolder, FILE_PATH_LITERAL("__MACOSX"), }; | |
257 CR_DEFINE_STATIC_LOCAL( | |
258 std::set<base::FilePath::StringType>, | |
259 reserved_underscore_names, | |
260 (reserved_names, reserved_names + arraysize(reserved_names))); | |
261 | |
262 // Enumerate all files and directories in the extension root. | |
263 // There is a problem when using pattern "_*" with FileEnumerator, so we have | |
264 // to cheat with find_first_of and match all. | |
265 const int kFilesAndDirectories = | |
266 base::FileEnumerator::DIRECTORIES | base::FileEnumerator::FILES; | |
267 base::FileEnumerator all_files(extension_path, false, kFilesAndDirectories); | |
268 | |
269 base::FilePath file; | |
270 while (!(file = all_files.Next()).empty()) { | |
271 base::FilePath::StringType filename = file.BaseName().value(); | |
272 // Skip all that don't start with "_". | |
273 if (filename.find_first_of(FILE_PATH_LITERAL("_")) != 0) | |
274 continue; | |
275 if (reserved_underscore_names.find(filename) == | |
276 reserved_underscore_names.end()) { | |
277 *error = base::StringPrintf( | |
278 "Cannot load extension with file or directory name %s. " | |
279 "Filenames starting with \"_\" are reserved for use by the system.", | |
280 file.BaseName().AsUTF8Unsafe().c_str()); | |
281 return false; | |
282 } | |
283 } | |
284 | |
285 return true; | |
286 } | |
287 | |
288 base::FilePath GetInstallTempDir(const base::FilePath& extensions_dir) { | |
289 // We do file IO in this function, but only when the current profile's | |
290 // Temp directory has never been used before, or in a rare error case. | |
291 // Developers are not likely to see these situations often, so do an | |
292 // explicit thread check. | |
293 base::ThreadRestrictions::AssertIOAllowed(); | |
294 | |
295 // Create the temp directory as a sub-directory of the Extensions directory. | |
296 // This guarantees it is on the same file system as the extension's eventual | |
297 // install target. | |
298 base::FilePath temp_path = extensions_dir.Append(kTempDirectoryName); | |
299 if (base::PathExists(temp_path)) { | |
300 if (!base::DirectoryExists(temp_path)) { | |
301 DLOG(WARNING) << "Not a directory: " << temp_path.value(); | |
302 return base::FilePath(); | |
303 } | |
304 if (!base::PathIsWritable(temp_path)) { | |
305 DLOG(WARNING) << "Can't write to path: " << temp_path.value(); | |
306 return base::FilePath(); | |
307 } | |
308 // This is a directory we can write to. | |
309 return temp_path; | |
310 } | |
311 | |
312 // Directory doesn't exist, so create it. | |
313 if (!base::CreateDirectory(temp_path)) { | |
314 DLOG(WARNING) << "Couldn't create directory: " << temp_path.value(); | |
315 return base::FilePath(); | |
316 } | |
317 return temp_path; | |
318 } | |
319 | |
320 void DeleteFile(const base::FilePath& path, bool recursive) { | |
321 base::DeleteFile(path, recursive); | |
322 } | |
323 | |
43 base::FilePath ExtensionURLToRelativeFilePath(const GURL& url) { | 324 base::FilePath ExtensionURLToRelativeFilePath(const GURL& url) { |
44 std::string url_path = url.path(); | 325 std::string url_path = url.path(); |
45 if (url_path.empty() || url_path[0] != '/') | 326 if (url_path.empty() || url_path[0] != '/') |
46 return base::FilePath(); | 327 return base::FilePath(); |
47 | 328 |
48 // Drop the leading slashes and convert %-encoded UTF8 to regular UTF8. | 329 // Drop the leading slashes and convert %-encoded UTF8 to regular UTF8. |
49 std::string file_path = net::UnescapeURLComponent(url_path, | 330 std::string file_path = net::UnescapeURLComponent(url_path, |
50 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS); | 331 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS); |
51 size_t skip = file_path.find_first_not_of("/\\"); | 332 size_t skip = file_path.find_first_not_of("/\\"); |
52 if (skip != file_path.npos) | 333 if (skip != file_path.npos) |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 // Add @@extension_id reserved message here, so it's available to | 432 // Add @@extension_id reserved message here, so it's available to |
152 // non-localized extensions too. | 433 // non-localized extensions too. |
153 return_value->insert( | 434 return_value->insert( |
154 std::make_pair(MessageBundle::kExtensionIdKey, extension_id)); | 435 std::make_pair(MessageBundle::kExtensionIdKey, extension_id)); |
155 | 436 |
156 return return_value; | 437 return return_value; |
157 } | 438 } |
158 | 439 |
159 } // namespace file_util | 440 } // namespace file_util |
160 } // namespace extensions | 441 } // namespace extensions |
OLD | NEW |