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
3 // found in the LICENSE file.
5 #include "extensions/common/manifest_handlers/externally_connectable.h"
9 #include "base/stl_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/crx_file/id_util.h"
12 #include "extensions/common/api/extensions_manifest_types.h"
13 #include "extensions/common/error_utils.h"
14 #include "extensions/common/manifest_constants.h"
15 #include "extensions/common/manifest_handlers/permissions_parser.h"
16 #include "extensions/common/permissions/api_permission_set.h"
17 #include "extensions/common/url_pattern.h"
18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
21 namespace rcd
= net::registry_controlled_domains
;
23 namespace extensions
{
25 namespace externally_connectable_errors
{
26 const char kErrorInvalidMatchPattern
[] = "Invalid match pattern '*'";
27 const char kErrorInvalidId
[] = "Invalid ID '*'";
28 const char kErrorNothingSpecified
[] =
29 "'externally_connectable' specifies neither 'matches' nor 'ids'; "
30 "nothing will be able to connect";
31 const char kErrorTopLevelDomainsNotAllowed
[] =
32 "\"*\" is an effective top level domain for which wildcard subdomains such "
33 "as \"*\" are not allowed";
34 const char kErrorWildcardHostsNotAllowed
[] =
35 "Wildcard domain patterns such as \"*\" are not allowed";
36 } // namespace externally_connectable_errors
38 namespace keys
= extensions::manifest_keys
;
39 namespace errors
= externally_connectable_errors
;
40 using core_api::extensions_manifest_types::ExternallyConnectable
;
44 const char kAllIds
[] = "*";
47 std::vector
<T
> Sorted(const std::vector
<T
>& in
) {
48 std::vector
<T
> out
= in
;
49 std::sort(out
.begin(), out
.end());
55 ExternallyConnectableHandler::ExternallyConnectableHandler() {
58 ExternallyConnectableHandler::~ExternallyConnectableHandler() {
61 bool ExternallyConnectableHandler::Parse(Extension
* extension
,
62 base::string16
* error
) {
63 const base::Value
* externally_connectable
= NULL
;
64 CHECK(extension
->manifest()->Get(keys::kExternallyConnectable
,
65 &externally_connectable
));
66 std::vector
<InstallWarning
> install_warnings
;
67 scoped_ptr
<ExternallyConnectableInfo
> info
=
68 ExternallyConnectableInfo::FromValue(*externally_connectable
,
73 if (!info
->matches
.is_empty()) {
74 PermissionsParser::AddAPIPermission(extension
,
75 APIPermission::kWebConnectable
);
77 extension
->AddInstallWarnings(install_warnings
);
78 extension
->SetManifestData(keys::kExternallyConnectable
, info
.release());
82 const std::vector
<std::string
> ExternallyConnectableHandler::Keys() const {
83 return SingleKey(keys::kExternallyConnectable
);
87 ExternallyConnectableInfo
* ExternallyConnectableInfo::Get(
88 const Extension
* extension
) {
89 return static_cast<ExternallyConnectableInfo
*>(
90 extension
->GetManifestData(keys::kExternallyConnectable
));
94 scoped_ptr
<ExternallyConnectableInfo
> ExternallyConnectableInfo::FromValue(
95 const base::Value
& value
,
96 std::vector
<InstallWarning
>* install_warnings
,
97 base::string16
* error
) {
98 scoped_ptr
<ExternallyConnectable
> externally_connectable
=
99 ExternallyConnectable::FromValue(value
, error
);
100 if (!externally_connectable
)
101 return scoped_ptr
<ExternallyConnectableInfo
>();
103 URLPatternSet matches
;
105 if (externally_connectable
->matches
) {
106 for (std::vector
<std::string
>::iterator it
=
107 externally_connectable
->matches
->begin();
108 it
!= externally_connectable
->matches
->end();
110 // Safe to use SCHEME_ALL here; externally_connectable gives a page ->
111 // extension communication path, not the other way.
112 URLPattern
pattern(URLPattern::SCHEME_ALL
);
113 if (pattern
.Parse(*it
) != URLPattern::PARSE_SUCCESS
) {
114 *error
= ErrorUtils::FormatErrorMessageUTF16(
115 errors::kErrorInvalidMatchPattern
, *it
);
116 return scoped_ptr
<ExternallyConnectableInfo
>();
119 // Wildcard hosts are not allowed.
120 if (pattern
.host().empty()) {
121 // Warning not error for forwards compatibility.
122 install_warnings
->push_back(
123 InstallWarning(ErrorUtils::FormatErrorMessage(
124 errors::kErrorWildcardHostsNotAllowed
, *it
),
125 keys::kExternallyConnectable
,
130 // Wildcards on subdomains of a TLD are not allowed.
131 size_t registry_length
= rcd::GetRegistryLength(
133 // This means that things that look like TLDs - the foobar in
134 // http://google.foobar - count as TLDs.
135 rcd::INCLUDE_UNKNOWN_REGISTRIES
,
136 // This means that effective TLDs like appspot.com count as TLDs;
137 // codereview.appspot.com and evil.appspot.com are different.
138 rcd::INCLUDE_PRIVATE_REGISTRIES
);
140 if (registry_length
== std::string::npos
) {
141 // The URL parsing combined with host().empty() should have caught this.
143 *error
= ErrorUtils::FormatErrorMessageUTF16(
144 errors::kErrorInvalidMatchPattern
, *it
);
145 return scoped_ptr
<ExternallyConnectableInfo
>();
148 // Broad match patterns like "*.com", "*.co.uk", and even "*.appspot.com"
149 // are not allowed. However just "appspot.com" is ok.
150 if (registry_length
== 0 && pattern
.match_subdomains()) {
151 // Warning not error for forwards compatibility.
152 install_warnings
->push_back(
153 InstallWarning(ErrorUtils::FormatErrorMessage(
154 errors::kErrorTopLevelDomainsNotAllowed
,
155 pattern
.host().c_str(),
157 keys::kExternallyConnectable
,
162 matches
.AddPattern(pattern
);
166 std::vector
<std::string
> ids
;
167 bool all_ids
= false;
169 if (externally_connectable
->ids
) {
170 for (std::vector
<std::string
>::iterator it
=
171 externally_connectable
->ids
->begin();
172 it
!= externally_connectable
->ids
->end();
174 if (*it
== kAllIds
) {
176 } else if (crx_file::id_util::IdIsValid(*it
)) {
180 ErrorUtils::FormatErrorMessageUTF16(errors::kErrorInvalidId
, *it
);
181 return scoped_ptr
<ExternallyConnectableInfo
>();
186 if (!externally_connectable
->matches
&& !externally_connectable
->ids
) {
187 install_warnings
->push_back(InstallWarning(errors::kErrorNothingSpecified
,
188 keys::kExternallyConnectable
));
191 bool accepts_tls_channel_id
=
192 externally_connectable
->accepts_tls_channel_id
.get() &&
193 *externally_connectable
->accepts_tls_channel_id
;
194 return make_scoped_ptr(new ExternallyConnectableInfo(
195 matches
, ids
, all_ids
, accepts_tls_channel_id
));
198 ExternallyConnectableInfo::~ExternallyConnectableInfo() {
201 ExternallyConnectableInfo::ExternallyConnectableInfo(
202 const URLPatternSet
& matches
,
203 const std::vector
<std::string
>& ids
,
205 bool accepts_tls_channel_id
)
209 accepts_tls_channel_id(accepts_tls_channel_id
) {
212 bool ExternallyConnectableInfo::IdCanConnect(const std::string
& id
) {
215 DCHECK(base::STLIsSorted(ids
));
216 return std::binary_search(ids
.begin(), ids
.end(), id
);
219 } // namespace extensions