1 // Copyright (c) 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/extension.h"
7 #include "base/base64.h"
8 #include "base/basictypes.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/i18n/rtl.h"
12 #include "base/logging.h"
13 #include "base/memory/singleton.h"
14 #include "base/stl_util.h"
15 #include "base/strings/string16.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_piece.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/values.h"
22 #include "base/version.h"
23 #include "components/crx_file/id_util.h"
24 #include "content/public/common/url_constants.h"
25 #include "extensions/common/constants.h"
26 #include "extensions/common/error_utils.h"
27 #include "extensions/common/manifest.h"
28 #include "extensions/common/manifest_constants.h"
29 #include "extensions/common/manifest_handler.h"
30 #include "extensions/common/manifest_handlers/permissions_parser.h"
31 #include "extensions/common/permissions/permission_set.h"
32 #include "extensions/common/permissions/permissions_data.h"
33 #include "extensions/common/permissions/permissions_info.h"
34 #include "extensions/common/switches.h"
35 #include "extensions/common/url_pattern.h"
36 #include "net/base/filename_util.h"
37 #include "url/url_util.h"
39 namespace extensions
{
41 namespace keys
= manifest_keys
;
42 namespace values
= manifest_values
;
43 namespace errors
= manifest_errors
;
47 const int kModernManifestVersion
= 2;
48 const int kPEMOutputColumns
= 64;
51 const char kKeyBeginHeaderMarker
[] = "-----BEGIN";
52 const char kKeyBeginFooterMarker
[] = "-----END";
53 const char kKeyInfoEndMarker
[] = "KEY-----";
54 const char kPublic
[] = "PUBLIC";
55 const char kPrivate
[] = "PRIVATE";
57 bool ContainsReservedCharacters(const base::FilePath
& path
) {
58 // We should disallow backslash '\\' as file path separator even on Windows,
59 // because the backslash is not regarded as file path separator on Linux/Mac.
60 // Extensions are cross-platform.
61 // Since FilePath uses backslash '\\' as file path separator on Windows, so we
62 // need to check manually.
63 if (path
.value().find('\\') != path
.value().npos
)
65 return !net::IsSafePortableRelativePath(path
);
70 const int Extension::kInitFromValueFlagBits
= 13;
72 const char Extension::kMimeType
[] = "application/x-chrome-extension";
74 const int Extension::kValidWebExtentSchemes
=
75 URLPattern::SCHEME_HTTP
| URLPattern::SCHEME_HTTPS
;
77 const int Extension::kValidHostPermissionSchemes
= URLPattern::SCHEME_CHROMEUI
|
78 URLPattern::SCHEME_HTTP
|
79 URLPattern::SCHEME_HTTPS
|
80 URLPattern::SCHEME_FILE
|
81 URLPattern::SCHEME_FTP
;
88 scoped_refptr
<Extension
> Extension::Create(const base::FilePath
& path
,
89 Manifest::Location location
,
90 const base::DictionaryValue
& value
,
92 std::string
* utf8_error
) {
93 return Extension::Create(path
,
97 std::string(), // ID is ignored if empty.
101 // TODO(sungguk): Continue removing std::string errors and replacing
102 // with base::string16. See http://crbug.com/71980.
103 scoped_refptr
<Extension
> Extension::Create(const base::FilePath
& path
,
104 Manifest::Location location
,
105 const base::DictionaryValue
& value
,
107 const std::string
& explicit_id
,
108 std::string
* utf8_error
) {
110 base::string16 error
;
111 scoped_ptr
<extensions::Manifest
> manifest(
112 new extensions::Manifest(
113 location
, scoped_ptr
<base::DictionaryValue
>(value
.DeepCopy())));
115 if (!InitExtensionID(manifest
.get(), path
, explicit_id
, flags
, &error
)) {
116 *utf8_error
= base::UTF16ToUTF8(error
);
120 std::vector
<InstallWarning
> install_warnings
;
121 if (!manifest
->ValidateManifest(utf8_error
, &install_warnings
)) {
125 scoped_refptr
<Extension
> extension
= new Extension(path
, manifest
.Pass());
126 extension
->install_warnings_
.swap(install_warnings
);
128 if (!extension
->InitFromValue(flags
, &error
)) {
129 *utf8_error
= base::UTF16ToUTF8(error
);
136 Manifest::Type
Extension::GetType() const {
137 return converted_from_user_script() ?
138 Manifest::TYPE_USER_SCRIPT
: manifest_
->type();
142 GURL
Extension::GetResourceURL(const GURL
& extension_url
,
143 const std::string
& relative_path
) {
144 DCHECK(extension_url
.SchemeIs(extensions::kExtensionScheme
));
145 DCHECK_EQ("/", extension_url
.path());
147 std::string path
= relative_path
;
149 // If the relative path starts with "/", it is "absolute" relative to the
150 // extension base directory, but extension_url is already specified to refer
151 // to that base directory, so strip the leading "/" if present.
152 if (relative_path
.size() > 0 && relative_path
[0] == '/')
153 path
= relative_path
.substr(1);
155 GURL ret_val
= GURL(extension_url
.spec() + path
);
156 DCHECK(StartsWithASCII(ret_val
.spec(), extension_url
.spec(), false));
161 bool Extension::ResourceMatches(const URLPatternSet
& pattern_set
,
162 const std::string
& resource
) const {
163 return pattern_set
.MatchesURL(extension_url_
.Resolve(resource
));
166 ExtensionResource
Extension::GetResource(
167 const std::string
& relative_path
) const {
168 std::string new_path
= relative_path
;
169 // We have some legacy data where resources have leading slashes.
170 // See: http://crbug.com/121164
171 if (!new_path
.empty() && new_path
.at(0) == '/')
172 new_path
.erase(0, 1);
173 base::FilePath relative_file_path
= base::FilePath::FromUTF8Unsafe(new_path
);
174 if (ContainsReservedCharacters(relative_file_path
))
175 return ExtensionResource();
176 ExtensionResource
r(id(), path(), relative_file_path
);
177 if ((creation_flags() & Extension::FOLLOW_SYMLINKS_ANYWHERE
)) {
178 r
.set_follow_symlinks_anywhere();
183 ExtensionResource
Extension::GetResource(
184 const base::FilePath
& relative_file_path
) const {
185 if (ContainsReservedCharacters(relative_file_path
))
186 return ExtensionResource();
187 ExtensionResource
r(id(), path(), relative_file_path
);
188 if ((creation_flags() & Extension::FOLLOW_SYMLINKS_ANYWHERE
)) {
189 r
.set_follow_symlinks_anywhere();
194 // TODO(rafaelw): Move ParsePEMKeyBytes, ProducePEM & FormatPEMForOutput to a
195 // util class in base:
196 // http://code.google.com/p/chromium/issues/detail?id=13572
198 bool Extension::ParsePEMKeyBytes(const std::string
& input
,
199 std::string
* output
) {
203 if (input
.length() == 0)
206 std::string working
= input
;
207 if (StartsWithASCII(working
, kKeyBeginHeaderMarker
, true)) {
208 working
= base::CollapseWhitespaceASCII(working
, true);
209 size_t header_pos
= working
.find(kKeyInfoEndMarker
,
210 sizeof(kKeyBeginHeaderMarker
) - 1);
211 if (header_pos
== std::string::npos
)
213 size_t start_pos
= header_pos
+ sizeof(kKeyInfoEndMarker
) - 1;
214 size_t end_pos
= working
.rfind(kKeyBeginFooterMarker
);
215 if (end_pos
== std::string::npos
)
217 if (start_pos
>= end_pos
)
220 working
= working
.substr(start_pos
, end_pos
- start_pos
);
221 if (working
.length() == 0)
225 return base::Base64Decode(working
, output
);
229 bool Extension::ProducePEM(const std::string
& input
, std::string
* output
) {
233 base::Base64Encode(input
, output
);
238 bool Extension::FormatPEMForFileOutput(const std::string
& input
,
242 if (input
.length() == 0)
245 output
->append(kKeyBeginHeaderMarker
);
247 output
->append(is_public
? kPublic
: kPrivate
);
249 output
->append(kKeyInfoEndMarker
);
250 output
->append("\n");
251 for (size_t i
= 0; i
< input
.length(); ) {
252 int slice
= std::min
<int>(input
.length() - i
, kPEMOutputColumns
);
253 output
->append(input
.substr(i
, slice
));
254 output
->append("\n");
257 output
->append(kKeyBeginFooterMarker
);
259 output
->append(is_public
? kPublic
: kPrivate
);
261 output
->append(kKeyInfoEndMarker
);
262 output
->append("\n");
268 GURL
Extension::GetBaseURLFromExtensionId(const std::string
& extension_id
) {
269 return GURL(std::string(extensions::kExtensionScheme
) +
270 url::kStandardSchemeSeparator
+ extension_id
+ "/");
273 bool Extension::ShowConfigureContextMenus() const {
274 // Don't show context menu for component extensions. We might want to show
275 // options for component extension button but now there is no component
276 // extension with options. All other menu items like uninstall have
277 // no sense for component extensions.
278 return location() != Manifest::COMPONENT
&&
279 location() != Manifest::EXTERNAL_COMPONENT
;
282 bool Extension::OverlapsWithOrigin(const GURL
& origin
) const {
286 if (web_extent().is_empty())
289 // Note: patterns and extents ignore port numbers.
290 URLPattern
origin_only_pattern(kValidWebExtentSchemes
);
291 if (!origin_only_pattern
.SetScheme(origin
.scheme()))
293 origin_only_pattern
.SetHost(origin
.host());
294 origin_only_pattern
.SetPath("/*");
296 URLPatternSet origin_only_pattern_list
;
297 origin_only_pattern_list
.AddPattern(origin_only_pattern
);
299 return web_extent().OverlapsWith(origin_only_pattern_list
);
302 bool Extension::RequiresSortOrdinal() const {
303 return is_app() && (display_in_launcher_
|| display_in_new_tab_page_
);
306 bool Extension::ShouldDisplayInAppLauncher() const {
307 // Only apps should be displayed in the launcher.
308 return is_app() && display_in_launcher_
;
311 bool Extension::ShouldDisplayInNewTabPage() const {
312 // Only apps should be displayed on the NTP.
313 return is_app() && display_in_new_tab_page_
;
316 bool Extension::ShouldDisplayInExtensionSettings() const {
317 // Don't show for themes since the settings UI isn't really useful for them.
321 // Don't show component extensions and invisible apps.
322 if (ShouldNotBeVisible())
325 // Always show unpacked extensions and apps.
326 if (Manifest::IsUnpackedLocation(location()))
329 // Unless they are unpacked, never show hosted apps. Note: We intentionally
330 // show packaged apps and platform apps because there are some pieces of
331 // functionality that are only available in chrome://extensions/ but which
332 // are needed for packaged and platform apps. For example, inspecting
333 // background pages. See http://crbug.com/116134.
340 bool Extension::ShouldNotBeVisible() const {
341 // Don't show component extensions because they are only extensions as an
342 // implementation detail of Chrome.
343 if (extensions::Manifest::IsComponentLocation(location()) &&
344 !CommandLine::ForCurrentProcess()->HasSwitch(
345 switches::kShowComponentExtensionOptions
)) {
349 // Always show unpacked extensions and apps.
350 if (Manifest::IsUnpackedLocation(location()))
353 // Don't show apps that aren't visible in either launcher or ntp.
354 if (is_app() && !ShouldDisplayInAppLauncher() && !ShouldDisplayInNewTabPage())
360 Extension::ManifestData
* Extension::GetManifestData(const std::string
& key
)
362 DCHECK(finished_parsing_manifest_
|| thread_checker_
.CalledOnValidThread());
363 ManifestDataMap::const_iterator iter
= manifest_data_
.find(key
);
364 if (iter
!= manifest_data_
.end())
365 return iter
->second
.get();
369 void Extension::SetManifestData(const std::string
& key
,
370 Extension::ManifestData
* data
) {
371 DCHECK(!finished_parsing_manifest_
&& thread_checker_
.CalledOnValidThread());
372 manifest_data_
[key
] = linked_ptr
<ManifestData
>(data
);
375 Manifest::Location
Extension::location() const {
376 return manifest_
->location();
379 const std::string
& Extension::id() const {
380 return manifest_
->extension_id();
383 const std::string
Extension::VersionString() const {
384 return version()->GetString();
387 void Extension::AddInstallWarning(const InstallWarning
& new_warning
) {
388 install_warnings_
.push_back(new_warning
);
391 void Extension::AddInstallWarnings(
392 const std::vector
<InstallWarning
>& new_warnings
) {
393 install_warnings_
.insert(install_warnings_
.end(),
394 new_warnings
.begin(), new_warnings
.end());
397 bool Extension::is_app() const {
398 return manifest()->is_app();
401 bool Extension::is_platform_app() const {
402 return manifest()->is_platform_app();
405 bool Extension::is_hosted_app() const {
406 return manifest()->is_hosted_app();
409 bool Extension::is_legacy_packaged_app() const {
410 return manifest()->is_legacy_packaged_app();
413 bool Extension::is_extension() const {
414 return manifest()->is_extension();
417 bool Extension::is_shared_module() const {
418 return manifest()->is_shared_module();
421 bool Extension::is_theme() const {
422 return manifest()->is_theme();
425 bool Extension::can_be_incognito_enabled() const {
426 // Only component platform apps are supported in incognito.
427 return !is_platform_app() || location() == Manifest::COMPONENT
;
430 void Extension::AddWebExtentPattern(const URLPattern
& pattern
) {
431 // Bookmark apps are permissionless.
435 extent_
.AddPattern(pattern
);
439 bool Extension::InitExtensionID(extensions::Manifest
* manifest
,
440 const base::FilePath
& path
,
441 const std::string
& explicit_id
,
443 base::string16
* error
) {
444 if (!explicit_id
.empty()) {
445 manifest
->set_extension_id(explicit_id
);
449 if (manifest
->HasKey(keys::kPublicKey
)) {
450 std::string public_key
;
451 std::string public_key_bytes
;
452 if (!manifest
->GetString(keys::kPublicKey
, &public_key
) ||
453 !ParsePEMKeyBytes(public_key
, &public_key_bytes
)) {
454 *error
= base::ASCIIToUTF16(errors::kInvalidKey
);
457 std::string extension_id
= crx_file::id_util::GenerateId(public_key_bytes
);
458 manifest
->set_extension_id(extension_id
);
462 if (creation_flags
& REQUIRE_KEY
) {
463 *error
= base::ASCIIToUTF16(errors::kInvalidKey
);
466 // If there is a path, we generate the ID from it. This is useful for
467 // development mode, because it keeps the ID stable across restarts and
468 // reloading the extension.
469 std::string extension_id
= crx_file::id_util::GenerateIdForPath(path
);
470 if (extension_id
.empty()) {
471 NOTREACHED() << "Could not create ID from path.";
474 manifest
->set_extension_id(extension_id
);
479 Extension::Extension(const base::FilePath
& path
,
480 scoped_ptr
<extensions::Manifest
> manifest
)
481 : manifest_version_(0),
482 converted_from_user_script_(false),
483 manifest_(manifest
.release()),
484 finished_parsing_manifest_(false),
485 display_in_launcher_(true),
486 display_in_new_tab_page_(true),
487 wants_file_access_(false),
489 DCHECK(path
.empty() || path
.IsAbsolute());
490 path_
= crx_file::id_util::MaybeNormalizePath(path
);
493 Extension::~Extension() {
496 bool Extension::InitFromValue(int flags
, base::string16
* error
) {
499 creation_flags_
= flags
;
501 // Important to load manifest version first because many other features
502 // depend on its value.
503 if (!LoadManifestVersion(error
))
506 if (!LoadRequiredFeatures(error
))
509 // We don't need to validate because InitExtensionID already did that.
510 manifest_
->GetString(keys::kPublicKey
, &public_key_
);
512 extension_url_
= Extension::GetBaseURLFromExtensionId(id());
514 // Load App settings. LoadExtent at least has to be done before
515 // ParsePermissions(), because the valid permissions depend on what type of
517 if (is_app() && !LoadAppFeatures(error
))
520 permissions_parser_
.reset(new PermissionsParser());
521 if (!permissions_parser_
->Parse(this, error
))
524 if (manifest_
->HasKey(keys::kConvertedFromUserScript
)) {
525 manifest_
->GetBoolean(keys::kConvertedFromUserScript
,
526 &converted_from_user_script_
);
529 if (!LoadSharedFeatures(error
))
532 permissions_parser_
->Finalize(this);
533 permissions_parser_
.reset();
535 finished_parsing_manifest_
= true;
537 permissions_data_
.reset(new PermissionsData(this));
542 bool Extension::LoadRequiredFeatures(base::string16
* error
) {
543 if (!LoadName(error
) ||
549 bool Extension::LoadName(base::string16
* error
) {
550 base::string16 localized_name
;
551 if (!manifest_
->GetString(keys::kName
, &localized_name
)) {
552 *error
= base::ASCIIToUTF16(errors::kInvalidName
);
555 non_localized_name_
= base::UTF16ToUTF8(localized_name
);
556 base::i18n::AdjustStringForLocaleDirection(&localized_name
);
557 name_
= base::UTF16ToUTF8(localized_name
);
561 bool Extension::LoadVersion(base::string16
* error
) {
562 std::string version_str
;
563 if (!manifest_
->GetString(keys::kVersion
, &version_str
)) {
564 *error
= base::ASCIIToUTF16(errors::kInvalidVersion
);
567 version_
.reset(new Version(version_str
));
568 if (!version_
->IsValid() || version_
->components().size() > 4) {
569 *error
= base::ASCIIToUTF16(errors::kInvalidVersion
);
575 bool Extension::LoadAppFeatures(base::string16
* error
) {
576 if (!LoadExtent(keys::kWebURLs
, &extent_
,
577 errors::kInvalidWebURLs
, errors::kInvalidWebURL
, error
)) {
580 if (manifest_
->HasKey(keys::kDisplayInLauncher
) &&
581 !manifest_
->GetBoolean(keys::kDisplayInLauncher
, &display_in_launcher_
)) {
582 *error
= base::ASCIIToUTF16(errors::kInvalidDisplayInLauncher
);
585 if (manifest_
->HasKey(keys::kDisplayInNewTabPage
)) {
586 if (!manifest_
->GetBoolean(keys::kDisplayInNewTabPage
,
587 &display_in_new_tab_page_
)) {
588 *error
= base::ASCIIToUTF16(errors::kInvalidDisplayInNewTabPage
);
592 // Inherit default from display_in_launcher property.
593 display_in_new_tab_page_
= display_in_launcher_
;
598 bool Extension::LoadExtent(const char* key
,
599 URLPatternSet
* extent
,
600 const char* list_error
,
601 const char* value_error
,
602 base::string16
* error
) {
603 const base::Value
* temp_pattern_value
= NULL
;
604 if (!manifest_
->Get(key
, &temp_pattern_value
))
607 const base::ListValue
* pattern_list
= NULL
;
608 if (!temp_pattern_value
->GetAsList(&pattern_list
)) {
609 *error
= base::ASCIIToUTF16(list_error
);
613 for (size_t i
= 0; i
< pattern_list
->GetSize(); ++i
) {
614 std::string pattern_string
;
615 if (!pattern_list
->GetString(i
, &pattern_string
)) {
616 *error
= ErrorUtils::FormatErrorMessageUTF16(value_error
,
617 base::UintToString(i
),
618 errors::kExpectString
);
622 URLPattern
pattern(kValidWebExtentSchemes
);
623 URLPattern::ParseResult parse_result
= pattern
.Parse(pattern_string
);
624 if (parse_result
== URLPattern::PARSE_ERROR_EMPTY_PATH
) {
625 pattern_string
+= "/";
626 parse_result
= pattern
.Parse(pattern_string
);
629 if (parse_result
!= URLPattern::PARSE_SUCCESS
) {
630 *error
= ErrorUtils::FormatErrorMessageUTF16(
632 base::UintToString(i
),
633 URLPattern::GetParseResultString(parse_result
));
637 // Do not allow authors to claim "<all_urls>".
638 if (pattern
.match_all_urls()) {
639 *error
= ErrorUtils::FormatErrorMessageUTF16(
641 base::UintToString(i
),
642 errors::kCannotClaimAllURLsInExtent
);
646 // Do not allow authors to claim "*" for host.
647 if (pattern
.host().empty()) {
648 *error
= ErrorUtils::FormatErrorMessageUTF16(
650 base::UintToString(i
),
651 errors::kCannotClaimAllHostsInExtent
);
655 // We do not allow authors to put wildcards in their paths. Instead, we
656 // imply one at the end.
657 if (pattern
.path().find('*') != std::string::npos
) {
658 *error
= ErrorUtils::FormatErrorMessageUTF16(
660 base::UintToString(i
),
661 errors::kNoWildCardsInPaths
);
664 pattern
.SetPath(pattern
.path() + '*');
666 extent
->AddPattern(pattern
);
672 bool Extension::LoadSharedFeatures(base::string16
* error
) {
673 if (!LoadDescription(error
) ||
674 !ManifestHandler::ParseExtension(this, error
) ||
675 !LoadShortName(error
))
681 bool Extension::LoadDescription(base::string16
* error
) {
682 if (manifest_
->HasKey(keys::kDescription
) &&
683 !manifest_
->GetString(keys::kDescription
, &description_
)) {
684 *error
= base::ASCIIToUTF16(errors::kInvalidDescription
);
690 bool Extension::LoadManifestVersion(base::string16
* error
) {
691 // Get the original value out of the dictionary so that we can validate it
693 if (manifest_
->value()->HasKey(keys::kManifestVersion
)) {
694 int manifest_version
= 1;
695 if (!manifest_
->GetInteger(keys::kManifestVersion
, &manifest_version
) ||
696 manifest_version
< 1) {
697 *error
= base::ASCIIToUTF16(errors::kInvalidManifestVersion
);
702 manifest_version_
= manifest_
->GetManifestVersion();
703 if (manifest_version_
< kModernManifestVersion
&&
704 ((creation_flags_
& REQUIRE_MODERN_MANIFEST_VERSION
&&
705 !CommandLine::ForCurrentProcess()->HasSwitch(
706 switches::kAllowLegacyExtensionManifests
)) ||
707 GetType() == Manifest::TYPE_PLATFORM_APP
)) {
708 *error
= ErrorUtils::FormatErrorMessageUTF16(
709 errors::kInvalidManifestVersionOld
,
710 base::IntToString(kModernManifestVersion
),
711 is_platform_app() ? "apps" : "extensions");
718 bool Extension::LoadShortName(base::string16
* error
) {
719 if (manifest_
->HasKey(keys::kShortName
)) {
720 base::string16 localized_short_name
;
721 if (!manifest_
->GetString(keys::kShortName
, &localized_short_name
) ||
722 localized_short_name
.empty()) {
723 *error
= base::ASCIIToUTF16(errors::kInvalidShortName
);
727 base::i18n::AdjustStringForLocaleDirection(&localized_short_name
);
728 short_name_
= base::UTF16ToUTF8(localized_short_name
);
735 ExtensionInfo::ExtensionInfo(const base::DictionaryValue
* manifest
,
736 const std::string
& id
,
737 const base::FilePath
& path
,
738 Manifest::Location location
)
740 extension_path(path
),
741 extension_location(location
) {
743 extension_manifest
.reset(manifest
->DeepCopy());
746 ExtensionInfo::~ExtensionInfo() {}
748 InstalledExtensionInfo::InstalledExtensionInfo(
749 const Extension
* extension
,
752 const std::string
& old_name
)
753 : extension(extension
),
754 is_update(is_update
),
755 from_ephemeral(from_ephemeral
),
756 old_name(old_name
) {}
758 UnloadedExtensionInfo::UnloadedExtensionInfo(
759 const Extension
* extension
,
760 UnloadedExtensionInfo::Reason reason
)
762 extension(extension
) {}
764 UpdatedExtensionPermissionsInfo::UpdatedExtensionPermissionsInfo(
765 const Extension
* extension
,
766 const PermissionSet
* permissions
,
769 extension(extension
),
770 permissions(permissions
) {}
772 } // namespace extensions