1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/affiliations/core/browser/affiliation_utils.h"
#include <algorithm>
#include <map>
#include <ostream>
#include <string_view>
#include "base/base64.h"
#include "base/strings/escape.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "components/url_formatter/elide_url.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "url/third_party/mozilla/url_parse.h"
#include "url/url_canon_stdstring.h"
namespace affiliations {
namespace {
// The scheme used for identifying Android applications.
const char kAndroidAppScheme[] = "android";
// Returns a std::string_view corresponding to |component| in |uri|, or the empty
// string in case there is no such component.
std::string_view ComponentString(const std::string& uri,
const url::Component& component) {
if (!component.is_valid()) {
return std::string_view();
}
return std::string_view(uri).substr(component.begin, component.len);
}
// Returns true if the passed ASCII |input| string contains nothing else than
// alphanumeric characters and those in |other_characters|.
bool ContainsOnlyAlphanumericAnd(std::string_view input,
std::string_view other_characters) {
for (char c : input) {
if (!base::IsAsciiAlpha(c) && !base::IsAsciiDigit(c) &&
other_characters.find(c) == std::string_view::npos)
return false;
}
return true;
}
// Canonicalizes a Web facet URI, and returns true if canonicalization was
// successful and produced a valid URI.
bool CanonicalizeWebFacetURI(const std::string& input_uri,
const url::Parsed& input_parsed,
std::string* canonical_uri) {
url::Parsed canonical_parsed;
url::StdStringCanonOutput canonical_output(canonical_uri);
bool canonicalization_succeeded = url::CanonicalizeStandardURL(
input_uri.c_str(), input_parsed,
url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, nullptr,
&canonical_output, &canonical_parsed);
canonical_output.Complete();
if (canonicalization_succeeded && canonical_parsed.host.is_nonempty() &&
!canonical_parsed.username.is_valid() &&
!canonical_parsed.password.is_valid() &&
ComponentString(*canonical_uri, canonical_parsed.path) == "/" &&
!canonical_parsed.query.is_valid() && !canonical_parsed.ref.is_valid()) {
// Get rid of the trailing slash added by url::CanonicalizeStandardURL().
DCHECK_EQ((size_t)canonical_parsed.path.begin, canonical_uri->size() - 1);
canonical_uri->erase(canonical_parsed.path.begin,
canonical_parsed.path.len);
return true;
}
return false;
}
// Adds padding until the length of the base64-encoded |data| becomes a multiple
// of 4, and returns true if the thusly obtained |data| is now correctly padded,
// i.e., there are at most 2 padding characters ('=') at the very end.
bool CanonicalizeBase64Padding(std::string* data) {
while (data->size() % 4u != 0u)
data->push_back('=');
size_t first_padding = data->find_first_of('=');
return first_padding == std::string::npos ||
(data->size() - first_padding <= 2u &&
data->find_first_not_of('=', first_padding) == std::string::npos);
}
// Canonicalizes the username component in an Android facet URI (containing the
// certificate hash), and returns true if canonicalization was successful and
// produced a valid non-empty component.
bool CanonicalizeHashComponent(std::string_view input_hash,
url::CanonOutput* canonical_output) {
// Characters other than alphanumeric that are used in the "URL and filename
// safe" base64 alphabet; plus the padding ('=').
const char kBase64NonAlphanumericChars[] = "-_=";
std::string base64_encoded_hash =
base::UnescapeBinaryURLComponent(input_hash);
if (!base64_encoded_hash.empty() &&
CanonicalizeBase64Padding(&base64_encoded_hash) &&
ContainsOnlyAlphanumericAnd(base64_encoded_hash,
kBase64NonAlphanumericChars)) {
canonical_output->Append(base64_encoded_hash);
canonical_output->push_back('@');
return true;
}
return false;
}
// Canonicalizes the host component in an Android facet URI (containing the
// package name), and returns true if canonicalization was successful and
// produced a valid non-empty component.
bool CanonicalizePackageNameComponent(
std::string_view input_package_name,
url::CanonOutput* canonical_output) {
// Characters other than alphanumeric that are permitted in the package names.
const char kPackageNameNonAlphanumericChars[] = "._";
std::string package_name =
base::UnescapeBinaryURLComponent(input_package_name);
// TODO(engedy): We might want to use a regex to check this more throughly.
if (!package_name.empty() &&
ContainsOnlyAlphanumericAnd(package_name,
kPackageNameNonAlphanumericChars)) {
canonical_output->Append(package_name);
return true;
}
return false;
}
// Canonicalizes an Android facet URI, and returns true if canonicalization was
// successful and produced a valid URI.
bool CanonicalizeAndroidFacetURI(const std::string& input_uri,
const url::Parsed& input_parsed,
std::string* canonical_uri) {
url::StdStringCanonOutput canonical_output(canonical_uri);
url::Component unused;
bool success = url::CanonicalizeScheme(
input_parsed.scheme.as_string_view_on(input_uri.c_str()),
&canonical_output, &unused);
canonical_output.push_back('/');
canonical_output.push_back('/');
// We cannot use url::CanonicalizeUserInfo as that would percent encode the
// base64 padding characters ('=').
success &= CanonicalizeHashComponent(
ComponentString(input_uri, input_parsed.username), &canonical_output);
// We cannot use url::CanonicalizeHost as that would convert the package name
// to lower case, but the package name is case sensitive.
success &= CanonicalizePackageNameComponent(
ComponentString(input_uri, input_parsed.host), &canonical_output);
canonical_output.Complete();
return success && input_parsed.password.is_empty() &&
(input_parsed.path.is_empty() ||
ComponentString(input_uri, input_parsed.path) == "/") &&
input_parsed.port.is_empty() && !input_parsed.query.is_valid() &&
!input_parsed.ref.is_valid();
}
// Computes the canonicalized form of |uri| into |canonical_uri|, and returns
// true if canonicalization was successful and produced a valid URI.
bool ParseAndCanonicalizeFacetURI(const std::string& input_uri,
std::string* canonical_uri) {
DCHECK(canonical_uri);
canonical_uri->clear();
canonical_uri->reserve(input_uri.size() + 32);
url::Parsed input_parsed = url::ParseStandardURL(input_uri);
std::string_view scheme = ComponentString(input_uri, input_parsed.scheme);
if (base::EqualsCaseInsensitiveASCII(scheme, url::kHttpsScheme)) {
return CanonicalizeWebFacetURI(input_uri, input_parsed, canonical_uri);
}
if (base::EqualsCaseInsensitiveASCII(scheme, kAndroidAppScheme)) {
return CanonicalizeAndroidFacetURI(input_uri, input_parsed, canonical_uri);
}
*canonical_uri = input_uri;
return false;
}
// Extracts and sorts the facet URIs of the given affiliated facets. This is
// used to determine whether two equivalence classes are equal.
std::vector<FacetURI> ExtractAndSortFacetURIs(const AffiliatedFacets& facets) {
std::vector<FacetURI> uris;
uris.reserve(facets.size());
std::ranges::transform(facets, std::back_inserter(uris), &Facet::uri);
std::sort(uris.begin(), uris.end());
return uris;
}
// Appends a new level to the |main_domain| from |full_domain|.
// |main_domain| must be a suffix of |full_domain|.
void IncreaseDomainLevel(const std::string& full_domain,
std::string& main_domain) {
DCHECK_GT(full_domain.size(), main_domain.size());
auto starting_pos = full_domain.rbegin() + main_domain.size();
// Verify that we are at '.' and move to the next character.
DCHECK_EQ(*starting_pos, '.');
starting_pos++;
// Find next '.' from |starting_pos|
auto ending_pos = std::find(starting_pos, full_domain.rend(), '.');
main_domain = std::string(ending_pos.base(), full_domain.end());
}
// An implementation of the disjoint-set data structure
// (https://en.wikipedia.org/wiki/Disjoint-set_data_structure). This
// implementation uses the path compression and union by rank optimizations,
// achieving near-constant runtime on all operations.
//
// This data structure allows to keep track of disjoin sets. Constructor accepts
// number of elements and initially each element represent an individual set.
// Later by calling MergeSets corresponding sets are merged together.
// Example usage:
// DisjointSet disjoint_set(5);
// disjoint_set.GetDisjointSets(); // Returns {{0}, {1}, {2}, {3}, {4}}
// disjoint_set.MergeSets(0, 2);
// disjoint_set.GetDisjointSets(); // Returns {{0, 2}, {1}, {3}, {4}}
// disjoint_set.MergeSets(2, 4);
// disjoint_set.GetDisjointSets(); // Returns {{0, 2, 4}, {1}, {3}}
class DisjointSet {
public:
explicit DisjointSet(size_t size) : parent_id_(size), ranks_(size, 0) {
for (size_t i = 0; i < size; i++) {
parent_id_[i] = i;
}
}
// Merges two sets based on their rank. Set with higher rank becomes a parent
// for another set.
void MergeSets(int set1, int set2) {
set1 = GetRoot(set1);
set2 = GetRoot(set2);
if (set1 == set2) {
return;
}
// Update parent based on rank.
if (ranks_[set1] > ranks_[set2]) {
parent_id_[set2] = set1;
} else {
parent_id_[set1] = set2;
// if ranks were equal increment by one new root's rank.
if (ranks_[set1] == ranks_[set2]) {
ranks_[set2]++;
}
}
}
// Returns disjoin sets after merging. It's guarantee that the result will
// hold all elements.
std::vector<std::vector<int>> GetDisjointSets() {
std::vector<std::vector<int>> disjoint_sets(parent_id_.size());
for (size_t i = 0; i < parent_id_.size(); i++) {
// Append all elements to the root.
int root = GetRoot(i);
disjoint_sets[root].push_back(i);
}
// Clear empty sets.
std::erase_if(disjoint_sets, [](const auto& set) { return set.empty(); });
return disjoint_sets;
}
private:
// Returns root for a given element.
int GetRoot(int index) {
if (index == parent_id_[index]) {
return index;
}
// To speed up future lookups flatten the tree along the way.
return parent_id_[index] = GetRoot(parent_id_[index]);
}
// Vector where element at i'th position holds a parent for i.
std::vector<int> parent_id_;
// Upper bound depth of a tree for i'th element.
std::vector<size_t> ranks_;
};
} // namespace
// FacetURI -------------------------------------------------------------------
FacetURI::FacetURI() = default;
// static
FacetURI FacetURI::FromPotentiallyInvalidSpec(const std::string& spec) {
std::string canonical_spec;
bool is_valid = ParseAndCanonicalizeFacetURI(spec, &canonical_spec);
return FacetURI(canonical_spec, is_valid);
}
// static
FacetURI FacetURI::FromCanonicalSpec(const std::string& canonical_spec) {
return FacetURI(canonical_spec, true);
}
bool FacetURI::IsValidWebFacetURI() const {
return scheme() == url::kHttpsScheme;
}
bool FacetURI::IsValidAndroidFacetURI() const {
return scheme() == kAndroidAppScheme;
}
std::string FacetURI::scheme() const {
return is_valid()
? std::string(ComponentString(canonical_spec_, parsed_.scheme))
: "";
}
std::string FacetURI::android_package_name() const {
if (!IsValidAndroidFacetURI())
return "";
return std::string(ComponentString(canonical_spec_, parsed_.host));
}
std::string FacetURI::GetAndroidPackageDisplayName() const {
CHECK(IsValidAndroidFacetURI());
std::vector<std::string> parts = base::SplitString(
android_package_name(), ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
std::reverse(parts.begin(), parts.end());
return base::JoinString(parts, ".");
}
FacetURI::FacetURI(const std::string& canonical_spec, bool is_valid)
: is_valid_(is_valid), canonical_spec_(canonical_spec) {
// TODO(engedy): Refactor code in order to avoid to avoid parsing the URL
// twice.
parsed_ = url::ParseStandardURL(canonical_spec_);
}
// Facet
Facet::Facet(FacetURI uri,
FacetBrandingInfo branding_info,
GURL change_password_url,
std::string main_domain)
: uri(std::move(uri)),
branding_info(std::move(branding_info)),
change_password_url(std::move(change_password_url)),
main_domain(std::move(main_domain)) {}
Facet::~Facet() = default;
Facet::Facet(const Facet& other) = default;
Facet::Facet(Facet&& other) = default;
Facet& Facet::operator=(const Facet& other) = default;
Facet& Facet::operator=(Facet&& other) = default;
// GroupedFacets
GroupedFacets::GroupedFacets() = default;
GroupedFacets::~GroupedFacets() = default;
GroupedFacets::GroupedFacets(const GroupedFacets& other) = default;
GroupedFacets::GroupedFacets(GroupedFacets&& other) = default;
GroupedFacets& GroupedFacets::operator=(const GroupedFacets& other) = default;
GroupedFacets& GroupedFacets::operator=(GroupedFacets&& other) = default;
std::vector<GroupedFacets> MergeRelatedGroups(
const base::flat_set<std::string>& psl_extensions,
const std::vector<GroupedFacets>& groups) {
DisjointSet unions(groups.size());
std::map<std::string, int> main_domain_to_group;
for (size_t i = 0; i < groups.size(); i++) {
for (auto& facet : groups[i].facets) {
if (facet.uri.IsValidAndroidFacetURI()) {
continue;
}
// If domain is empty - compute it manually.
std::string main_domain =
facet.main_domain.empty()
? GetExtendedTopLevelDomain(
GURL(facet.uri.potentially_invalid_spec()), psl_extensions)
: facet.main_domain;
if (main_domain.empty()) {
continue;
}
auto it = main_domain_to_group.find(main_domain);
if (it == main_domain_to_group.end()) {
main_domain_to_group[main_domain] = i;
continue;
}
unions.MergeSets(i, it->second);
}
}
std::vector<GroupedFacets> result;
for (const auto& merged_groups : unions.GetDisjointSets()) {
GroupedFacets group;
for (int group_id : merged_groups) {
// Move all the elements into a new vector.
group.facets.insert(group.facets.end(), groups[group_id].facets.begin(),
groups[group_id].facets.end());
// Use non-empty name for a combined group.
if (!groups[group_id].branding_info.icon_url.is_empty()) {
group.branding_info = groups[group_id].branding_info;
}
}
result.push_back(std::move(group));
}
return result;
}
// AffiliatedFacetsWithUpdateTime ---------------------------------------------
AffiliatedFacetsWithUpdateTime::AffiliatedFacetsWithUpdateTime() = default;
AffiliatedFacetsWithUpdateTime::AffiliatedFacetsWithUpdateTime(
const AffiliatedFacetsWithUpdateTime& other) = default;
AffiliatedFacetsWithUpdateTime::AffiliatedFacetsWithUpdateTime(
AffiliatedFacetsWithUpdateTime&& other) = default;
AffiliatedFacetsWithUpdateTime& AffiliatedFacetsWithUpdateTime::operator=(
const AffiliatedFacetsWithUpdateTime& other) = default;
AffiliatedFacetsWithUpdateTime& AffiliatedFacetsWithUpdateTime::operator=(
AffiliatedFacetsWithUpdateTime&& other) = default;
AffiliatedFacetsWithUpdateTime::~AffiliatedFacetsWithUpdateTime() = default;
// Helpers --------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const FacetURI& facet_uri) {
return os << facet_uri.potentially_invalid_spec();
}
bool operator==(const GroupedFacets& lhs, const GroupedFacets& rhs) {
if (!std::ranges::is_permutation(lhs.facets, rhs.facets)) {
return false;
}
return lhs.branding_info == rhs.branding_info;
}
bool AreEquivalenceClassesEqual(const AffiliatedFacets& a,
const AffiliatedFacets& b) {
return a.size() == b.size() &&
ExtractAndSortFacetURIs(a) == ExtractAndSortFacetURIs(b);
}
bool IsValidAndroidFacetURI(const std::string& url) {
FacetURI facet = FacetURI::FromPotentiallyInvalidSpec(url);
return facet.IsValidAndroidFacetURI();
}
std::string GetExtendedTopLevelDomain(
const GURL& url,
const base::flat_set<std::string>& psl_extensions) {
std::string main_domain =
net::registry_controlled_domains::GetDomainAndRegistry(
url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
if (main_domain.empty()) {
return main_domain;
}
std::string full_domain = url.host();
// Something went wrong, and it shouldn't happen. Return early in this case to
// avoid undefined behaviour.
if (!base::EndsWith(full_domain, main_domain)) {
return main_domain;
}
// If a domain is contained within the PSL extension list, an additional
// subdomain is added to that domain. This is done until the domain is not
// contained within the PSL extension list or fully shown. For multi-level
// extension, this approach only works if all sublevels are included in the
// PSL extension list.
while (main_domain != full_domain && psl_extensions.contains(main_domain)) {
IncreaseDomainLevel(full_domain, main_domain);
}
return main_domain;
}
bool IsExtendedPublicSuffixDomainMatch(
const GURL& url1,
const GURL& url2,
const base::flat_set<std::string>& psl_extensions) {
if (!url1.is_valid() || !url2.is_valid()) {
return false;
}
std::string domain1(GetExtendedTopLevelDomain(url1, psl_extensions));
std::string domain2(GetExtendedTopLevelDomain(url2, psl_extensions));
if (domain1.empty() || domain2.empty()) {
return false;
}
return domain1 == domain2;
}
} // namespace affiliations
|