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
3 // found in the LICENSE file.
5 #include "extensions/common/file_util.h"
13 #include "base/files/file_enumerator.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/files/scoped_temp_dir.h"
17 #include "base/json/json_file_value_serializer.h"
18 #include "base/logging.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/metrics/field_trial.h"
21 #include "base/metrics/histogram.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "base/threading/thread_restrictions.h"
25 #include "base/time/time.h"
26 #include "extensions/common/constants.h"
27 #include "extensions/common/extension.h"
28 #include "extensions/common/extension_icon_set.h"
29 #include "extensions/common/extension_l10n_util.h"
30 #include "extensions/common/extension_set.h"
31 #include "extensions/common/install_warning.h"
32 #include "extensions/common/manifest.h"
33 #include "extensions/common/manifest_constants.h"
34 #include "extensions/common/manifest_handler.h"
35 #include "extensions/common/manifest_handlers/default_locale_handler.h"
36 #include "extensions/common/manifest_handlers/icons_handler.h"
37 #include "extensions/common/manifest_handlers/shared_module_info.h"
38 #include "grit/extensions_strings.h"
39 #include "net/base/escape.h"
40 #include "ui/base/l10n/l10n_util.h"
43 namespace extensions
{
47 // Returns true if the given file path exists and is not zero-length.
48 bool ValidateFilePath(const base::FilePath
& path
) {
50 if (!base::PathExists(path
) ||
51 !base::GetFileSize(path
, &size
) ||
59 // Returns true if the extension installation should flush all files and the
61 bool UseSafeInstallation() {
62 const char kFieldTrialName
[] = "ExtensionUseSafeInstallation";
63 const char kEnable
[] = "Enable";
64 return base::FieldTrialList::FindFullName(kFieldTrialName
) == kEnable
;
67 enum FlushOneOrAllFiles
{
72 // Flush all files in a directory or just one. When flushing all files, it
73 // makes sure every file is on disk. When flushing one file only, it ensures
74 // all parent directories are on disk.
75 void FlushFilesInDir(const base::FilePath
& path
,
76 FlushOneOrAllFiles one_or_all_files
) {
77 if (!UseSafeInstallation()) {
80 base::FileEnumerator
temp_traversal(path
,
82 base::FileEnumerator::FILES
);
83 for (base::FilePath current
= temp_traversal
.Next(); !current
.empty();
84 current
= temp_traversal
.Next()) {
85 base::File
currentFile(current
,
86 base::File::FLAG_OPEN
| base::File::FLAG_WRITE
);
89 if (one_or_all_files
== ONE_FILE_ONLY
) {
97 const base::FilePath::CharType kTempDirectoryName
[] = FILE_PATH_LITERAL("Temp");
99 base::FilePath
InstallExtension(const base::FilePath
& unpacked_source_dir
,
100 const std::string
& id
,
101 const std::string
& version
,
102 const base::FilePath
& extensions_dir
) {
103 base::FilePath extension_dir
= extensions_dir
.AppendASCII(id
);
104 base::FilePath version_dir
;
106 // Create the extension directory if it doesn't exist already.
107 if (!base::PathExists(extension_dir
)) {
108 if (!base::CreateDirectory(extension_dir
))
109 return base::FilePath();
112 // Get a temp directory on the same file system as the profile.
113 base::FilePath install_temp_dir
= GetInstallTempDir(extensions_dir
);
114 base::ScopedTempDir extension_temp_dir
;
115 if (install_temp_dir
.empty() ||
116 !extension_temp_dir
.CreateUniqueTempDirUnderPath(install_temp_dir
)) {
117 LOG(ERROR
) << "Creating of temp dir under in the profile failed.";
118 return base::FilePath();
120 base::FilePath crx_temp_source
=
121 extension_temp_dir
.path().Append(unpacked_source_dir
.BaseName());
122 if (!base::Move(unpacked_source_dir
, crx_temp_source
)) {
123 LOG(ERROR
) << "Moving extension from : " << unpacked_source_dir
.value()
124 << " to : " << crx_temp_source
.value() << " failed.";
125 return base::FilePath();
128 // Try to find a free directory. There can be legitimate conflicts in the case
129 // of overinstallation of the same version.
130 const int kMaxAttempts
= 100;
131 for (int i
= 0; i
< kMaxAttempts
; ++i
) {
132 base::FilePath candidate
= extension_dir
.AppendASCII(
133 base::StringPrintf("%s_%u", version
.c_str(), i
));
134 if (!base::PathExists(candidate
)) {
135 version_dir
= candidate
;
140 if (version_dir
.empty()) {
141 LOG(ERROR
) << "Could not find a home for extension " << id
<< " with "
142 << "version " << version
<< ".";
143 return base::FilePath();
146 base::TimeTicks start_time
= base::TimeTicks::Now();
148 // Flush the source dir completely before moving to make sure everything is
149 // on disk. Otherwise a sudden power loss could cause the newly installed
150 // extension to be in a corrupted state. Note that empty sub-directories
151 // may still be lost.
152 FlushFilesInDir(crx_temp_source
, ALL_FILES
);
154 // The target version_dir does not exists yet, so base::Move() is using
155 // rename() on POSIX systems. It is atomic in the sense that it will
156 // either complete successfully or in the event of data loss be reverted.
157 if (!base::Move(crx_temp_source
, version_dir
)) {
158 LOG(ERROR
) << "Installing extension from : " << crx_temp_source
.value()
159 << " into : " << version_dir
.value() << " failed.";
160 return base::FilePath();
163 // Flush one file in the new version_dir to make sure the dir move above is
164 // persisted on disk. This is guaranteed on POSIX systems. ExtensionPrefs
165 // is going to be updated with the new version_dir later. In the event of
166 // data loss ExtensionPrefs should be pointing to the previous version which
168 FlushFilesInDir(version_dir
, ONE_FILE_ONLY
);
170 UMA_HISTOGRAM_TIMES("Extensions.FileInstallation",
171 base::TimeTicks::Now() - start_time
);
176 void UninstallExtension(const base::FilePath
& extensions_dir
,
177 const std::string
& id
) {
178 // We don't care about the return value. If this fails (and it can, due to
179 // plugins that aren't unloaded yet), it will get cleaned up by
180 // ExtensionGarbageCollector::GarbageCollectExtensions.
181 base::DeleteFile(extensions_dir
.AppendASCII(id
), true); // recursive.
184 scoped_refptr
<Extension
> LoadExtension(const base::FilePath
& extension_path
,
185 Manifest::Location location
,
187 std::string
* error
) {
188 return LoadExtension(extension_path
, std::string(), location
, flags
, error
);
191 scoped_refptr
<Extension
> LoadExtension(const base::FilePath
& extension_path
,
192 const std::string
& extension_id
,
193 Manifest::Location location
,
195 std::string
* error
) {
196 scoped_ptr
<base::DictionaryValue
> manifest(
197 LoadManifest(extension_path
, error
));
200 if (!extension_l10n_util::LocalizeExtension(
201 extension_path
, manifest
.get(), error
)) {
205 scoped_refptr
<Extension
> extension(Extension::Create(
206 extension_path
, location
, *manifest
, flags
, extension_id
, error
));
207 if (!extension
.get())
210 std::vector
<InstallWarning
> warnings
;
211 if (!ValidateExtension(extension
.get(), error
, &warnings
))
213 extension
->AddInstallWarnings(warnings
);
218 base::DictionaryValue
* LoadManifest(const base::FilePath
& extension_path
,
219 std::string
* error
) {
220 return LoadManifest(extension_path
, kManifestFilename
, error
);
223 base::DictionaryValue
* LoadManifest(
224 const base::FilePath
& extension_path
,
225 const base::FilePath::CharType
* manifest_filename
,
226 std::string
* error
) {
227 base::FilePath manifest_path
= extension_path
.Append(manifest_filename
);
228 if (!base::PathExists(manifest_path
)) {
229 *error
= l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE
);
233 JSONFileValueDeserializer
deserializer(manifest_path
);
234 scoped_ptr
<base::Value
> root(deserializer
.Deserialize(NULL
, error
));
236 if (error
->empty()) {
237 // If |error| is empty, than the file could not be read.
238 // It would be cleaner to have the JSON reader give a specific error
239 // in this case, but other code tests for a file error with
240 // error->empty(). For now, be consistent.
241 *error
= l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE
);
243 *error
= base::StringPrintf(
244 "%s %s", manifest_errors::kManifestParseError
, error
->c_str());
249 if (!root
->IsType(base::Value::TYPE_DICTIONARY
)) {
250 *error
= l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_INVALID
);
254 return static_cast<base::DictionaryValue
*>(root
.release());
257 bool ValidateExtension(const Extension
* extension
,
259 std::vector
<InstallWarning
>* warnings
) {
260 // Ask registered manifest handlers to validate their paths.
261 if (!ManifestHandler::ValidateExtension(extension
, error
, warnings
))
264 // Check children of extension root to see if any of them start with _ and is
265 // not on the reserved list. We only warn, and do not block the loading of the
268 if (!CheckForIllegalFilenames(extension
->path(), &warning
))
269 warnings
->push_back(InstallWarning(warning
));
271 // Check that extensions don't include private key files.
272 std::vector
<base::FilePath
> private_keys
=
273 FindPrivateKeyFiles(extension
->path());
274 if (extension
->creation_flags() & Extension::ERROR_ON_PRIVATE_KEY
) {
275 if (!private_keys
.empty()) {
276 // Only print one of the private keys because l10n_util doesn't have a way
277 // to translate a list of strings.
279 l10n_util::GetStringFUTF8(IDS_EXTENSION_CONTAINS_PRIVATE_KEY
,
280 private_keys
.front().LossyDisplayName());
284 for (size_t i
= 0; i
< private_keys
.size(); ++i
) {
285 warnings
->push_back(InstallWarning(
286 l10n_util::GetStringFUTF8(IDS_EXTENSION_CONTAINS_PRIVATE_KEY
,
287 private_keys
[i
].LossyDisplayName())));
289 // Only warn; don't block loading the extension.
294 std::vector
<base::FilePath
> FindPrivateKeyFiles(
295 const base::FilePath
& extension_dir
) {
296 std::vector
<base::FilePath
> result
;
297 // Pattern matching only works at the root level, so filter manually.
298 base::FileEnumerator
traversal(
299 extension_dir
, /*recursive=*/true, base::FileEnumerator::FILES
);
300 for (base::FilePath current
= traversal
.Next(); !current
.empty();
301 current
= traversal
.Next()) {
302 if (!current
.MatchesExtension(kExtensionKeyFileExtension
))
305 std::string key_contents
;
306 if (!base::ReadFileToString(current
, &key_contents
)) {
307 // If we can't read the file, assume it's not a private key.
310 std::string key_bytes
;
311 if (!Extension::ParsePEMKeyBytes(key_contents
, &key_bytes
)) {
312 // If we can't parse the key, assume it's ok too.
316 result
.push_back(current
);
321 bool CheckForIllegalFilenames(const base::FilePath
& extension_path
,
322 std::string
* error
) {
323 // Reserved underscore names.
324 static const base::FilePath::CharType
* reserved_names
[] = {
325 kLocaleFolder
, kPlatformSpecificFolder
, FILE_PATH_LITERAL("__MACOSX"), };
326 CR_DEFINE_STATIC_LOCAL(
327 std::set
<base::FilePath::StringType
>,
328 reserved_underscore_names
,
329 (reserved_names
, reserved_names
+ arraysize(reserved_names
)));
331 // Enumerate all files and directories in the extension root.
332 // There is a problem when using pattern "_*" with FileEnumerator, so we have
333 // to cheat with find_first_of and match all.
334 const int kFilesAndDirectories
=
335 base::FileEnumerator::DIRECTORIES
| base::FileEnumerator::FILES
;
336 base::FileEnumerator
all_files(extension_path
, false, kFilesAndDirectories
);
339 while (!(file
= all_files
.Next()).empty()) {
340 base::FilePath::StringType filename
= file
.BaseName().value();
341 // Skip all that don't start with "_".
342 if (filename
.find_first_of(FILE_PATH_LITERAL("_")) != 0)
344 if (reserved_underscore_names
.find(filename
) ==
345 reserved_underscore_names
.end()) {
346 *error
= base::StringPrintf(
347 "Cannot load extension with file or directory name %s. "
348 "Filenames starting with \"_\" are reserved for use by the system.",
349 file
.BaseName().AsUTF8Unsafe().c_str());
357 base::FilePath
GetInstallTempDir(const base::FilePath
& extensions_dir
) {
358 // We do file IO in this function, but only when the current profile's
359 // Temp directory has never been used before, or in a rare error case.
360 // Developers are not likely to see these situations often, so do an
361 // explicit thread check.
362 base::ThreadRestrictions::AssertIOAllowed();
364 // Create the temp directory as a sub-directory of the Extensions directory.
365 // This guarantees it is on the same file system as the extension's eventual
367 base::FilePath temp_path
= extensions_dir
.Append(kTempDirectoryName
);
368 if (base::PathExists(temp_path
)) {
369 if (!base::DirectoryExists(temp_path
)) {
370 DLOG(WARNING
) << "Not a directory: " << temp_path
.value();
371 return base::FilePath();
373 if (!base::PathIsWritable(temp_path
)) {
374 DLOG(WARNING
) << "Can't write to path: " << temp_path
.value();
375 return base::FilePath();
377 // This is a directory we can write to.
381 // Directory doesn't exist, so create it.
382 if (!base::CreateDirectory(temp_path
)) {
383 DLOG(WARNING
) << "Couldn't create directory: " << temp_path
.value();
384 return base::FilePath();
389 void DeleteFile(const base::FilePath
& path
, bool recursive
) {
390 base::DeleteFile(path
, recursive
);
393 base::FilePath
ExtensionURLToRelativeFilePath(const GURL
& url
) {
394 std::string url_path
= url
.path();
395 if (url_path
.empty() || url_path
[0] != '/')
396 return base::FilePath();
398 // Drop the leading slashes and convert %-encoded UTF8 to regular UTF8.
399 std::string file_path
= net::UnescapeURLComponent(url_path
,
400 net::UnescapeRule::SPACES
| net::UnescapeRule::URL_SPECIAL_CHARS
);
401 size_t skip
= file_path
.find_first_not_of("/\\");
402 if (skip
!= file_path
.npos
)
403 file_path
= file_path
.substr(skip
);
405 base::FilePath path
= base::FilePath::FromUTF8Unsafe(file_path
);
407 // It's still possible for someone to construct an annoying URL whose path
408 // would still wind up not being considered relative at this point.
409 // For example: chrome-extension://id/c:////foo.html
410 if (path
.IsAbsolute())
411 return base::FilePath();
416 base::FilePath
ExtensionResourceURLToFilePath(const GURL
& url
,
417 const base::FilePath
& root
) {
418 std::string host
= net::UnescapeURLComponent(url
.host(),
419 net::UnescapeRule::SPACES
| net::UnescapeRule::URL_SPECIAL_CHARS
);
421 return base::FilePath();
423 base::FilePath relative_path
= ExtensionURLToRelativeFilePath(url
);
424 if (relative_path
.empty())
425 return base::FilePath();
427 base::FilePath path
= root
.AppendASCII(host
).Append(relative_path
);
428 if (!base::PathExists(path
))
429 return base::FilePath();
430 path
= base::MakeAbsoluteFilePath(path
);
431 if (path
.empty() || !root
.IsParent(path
))
432 return base::FilePath();
436 bool ValidateExtensionIconSet(const ExtensionIconSet
& icon_set
,
437 const Extension
* extension
,
438 int error_message_id
,
439 std::string
* error
) {
440 for (ExtensionIconSet::IconMap::const_iterator iter
= icon_set
.map().begin();
441 iter
!= icon_set
.map().end();
443 const base::FilePath path
=
444 extension
->GetResource(iter
->second
).GetFilePath();
445 if (!ValidateFilePath(path
)) {
446 *error
= l10n_util::GetStringFUTF8(error_message_id
,
447 base::UTF8ToUTF16(iter
->second
));
454 MessageBundle
* LoadMessageBundle(
455 const base::FilePath
& extension_path
,
456 const std::string
& default_locale
,
457 std::string
* error
) {
459 // Load locale information if available.
460 base::FilePath locale_path
= extension_path
.Append(kLocaleFolder
);
461 if (!base::PathExists(locale_path
))
464 std::set
<std::string
> locales
;
465 if (!extension_l10n_util::GetValidLocales(locale_path
, &locales
, error
))
468 if (default_locale
.empty() || locales
.find(default_locale
) == locales
.end()) {
469 *error
= l10n_util::GetStringUTF8(
470 IDS_EXTENSION_LOCALES_NO_DEFAULT_LOCALE_SPECIFIED
);
474 MessageBundle
* message_bundle
=
475 extension_l10n_util::LoadMessageCatalogs(
478 extension_l10n_util::CurrentLocaleOrDefault(),
482 return message_bundle
;
485 MessageBundle::SubstitutionMap
* LoadMessageBundleSubstitutionMap(
486 const base::FilePath
& extension_path
,
487 const std::string
& extension_id
,
488 const std::string
& default_locale
) {
489 MessageBundle::SubstitutionMap
* return_value
=
490 new MessageBundle::SubstitutionMap();
491 if (!default_locale
.empty()) {
492 // Touch disk only if extension is localized.
494 scoped_ptr
<MessageBundle
> bundle(
495 LoadMessageBundle(extension_path
, default_locale
, &error
));
498 *return_value
= *bundle
->dictionary();
501 // Add @@extension_id reserved message here, so it's available to
502 // non-localized extensions too.
503 return_value
->insert(
504 std::make_pair(MessageBundle::kExtensionIdKey
, extension_id
));
509 MessageBundle::SubstitutionMap
* LoadMessageBundleSubstitutionMapWithImports(
510 const std::string
& extension_id
,
511 const ExtensionSet
& extension_set
) {
512 const Extension
* extension
= extension_set
.GetByID(extension_id
);
513 MessageBundle::SubstitutionMap
* return_value
=
514 new MessageBundle::SubstitutionMap();
516 // Add @@extension_id reserved message here, so it's available to
517 // non-localized extensions too.
518 return_value
->insert(
519 std::make_pair(MessageBundle::kExtensionIdKey
, extension_id
));
521 base::FilePath extension_path
;
522 std::string default_locale
;
524 NOTREACHED() << "Missing extension " << extension_id
;
528 // Touch disk only if extension is localized.
529 default_locale
= LocaleInfo::GetDefaultLocale(extension
);
530 if (default_locale
.empty()) {
535 scoped_ptr
<MessageBundle
> bundle(
536 LoadMessageBundle(extension
->path(), default_locale
, &error
));
539 for (auto iter
: *bundle
->dictionary()) {
540 return_value
->insert(std::make_pair(iter
.first
, iter
.second
));
544 auto imports
= extensions::SharedModuleInfo::GetImports(extension
);
545 // Iterate through the imports in reverse. This will allow later imported
546 // modules to override earlier imported modules, as the list order is
547 // maintained from the definition in manifest.json of the imports.
548 for (auto it
= imports
.rbegin(); it
!= imports
.rend(); ++it
) {
549 const extensions::Extension
* imported_extension
=
550 extension_set
.GetByID(it
->extension_id
);
551 if (!imported_extension
) {
552 NOTREACHED() << "Missing shared module " << it
->extension_id
;
555 scoped_ptr
<MessageBundle
> imported_bundle(
556 LoadMessageBundle(imported_extension
->path(), default_locale
, &error
));
558 if (imported_bundle
.get()) {
559 for (auto iter
: *imported_bundle
->dictionary()) {
560 // |insert| only adds new entries, and does not replace entries in
561 // the main extension or previously processed imports.
562 return_value
->insert(std::make_pair(iter
.first
, iter
.second
));
570 base::FilePath
GetVerifiedContentsPath(const base::FilePath
& extension_path
) {
571 return extension_path
.Append(kMetadataFolder
)
572 .Append(kVerifiedContentsFilename
);
574 base::FilePath
GetComputedHashesPath(const base::FilePath
& extension_path
) {
575 return extension_path
.Append(kMetadataFolder
).Append(kComputedHashesFilename
);
578 } // namespace file_util
579 } // namespace extensions