cookie_deletion_info.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2018 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.
  4. #include "net/cookies/cookie_deletion_info.h"
  5. #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
  6. #include "net/cookies/canonical_cookie.h"
  7. #include "net/cookies/cookie_options.h"
  8. namespace net {
  9. namespace {
  10. // Return true if the eTLD+1 of the cookies domain matches any of the strings
  11. // in |match_domains|, false otherwise.
  12. bool DomainMatchesDomains(const net::CanonicalCookie& cookie,
  13. const std::set<std::string>& match_domains) {
  14. if (match_domains.empty())
  15. return false;
  16. // If domain is an IP address it returns an empty string.
  17. std::string effective_domain(
  18. net::registry_controlled_domains::GetDomainAndRegistry(
  19. // GetDomainAndRegistry() is insensitive to leading dots, i.e.
  20. // to host/domain cookie distinctions.
  21. cookie.Domain(),
  22. net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
  23. // If the cookie's domain is is not parsed as belonging to a registry
  24. // (e.g. for IP addresses or internal hostnames) an empty string will be
  25. // returned. In this case, use the domain in the cookie.
  26. if (effective_domain.empty())
  27. effective_domain = cookie.DomainWithoutDot();
  28. return match_domains.count(effective_domain) != 0;
  29. }
  30. } // anonymous namespace
  31. CookieDeletionInfo::TimeRange::TimeRange() = default;
  32. CookieDeletionInfo::TimeRange::TimeRange(const TimeRange& other) = default;
  33. CookieDeletionInfo::TimeRange::TimeRange(base::Time start, base::Time end)
  34. : start_(start), end_(end) {
  35. if (!start.is_null() && !end.is_null())
  36. DCHECK_GE(end, start);
  37. }
  38. CookieDeletionInfo::TimeRange& CookieDeletionInfo::TimeRange::operator=(
  39. const TimeRange& rhs) = default;
  40. bool CookieDeletionInfo::TimeRange::Contains(const base::Time& time) const {
  41. DCHECK(!time.is_null());
  42. if (!start_.is_null() && start_ == end_)
  43. return time == start_;
  44. return (start_.is_null() || start_ <= time) &&
  45. (end_.is_null() || time < end_);
  46. }
  47. void CookieDeletionInfo::TimeRange::SetStart(base::Time value) {
  48. start_ = value;
  49. }
  50. void CookieDeletionInfo::TimeRange::SetEnd(base::Time value) {
  51. end_ = value;
  52. }
  53. CookieDeletionInfo::CookieDeletionInfo()
  54. : CookieDeletionInfo(base::Time(), base::Time()) {}
  55. CookieDeletionInfo::CookieDeletionInfo(base::Time start_time,
  56. base::Time end_time)
  57. : creation_range(start_time, end_time) {}
  58. CookieDeletionInfo::CookieDeletionInfo(CookieDeletionInfo&& other) = default;
  59. CookieDeletionInfo::CookieDeletionInfo(const CookieDeletionInfo& other) =
  60. default;
  61. CookieDeletionInfo::~CookieDeletionInfo() = default;
  62. CookieDeletionInfo& CookieDeletionInfo::operator=(CookieDeletionInfo&& rhs) =
  63. default;
  64. CookieDeletionInfo& CookieDeletionInfo::operator=(
  65. const CookieDeletionInfo& rhs) = default;
  66. bool CookieDeletionInfo::Matches(const CanonicalCookie& cookie,
  67. const CookieAccessParams& params) const {
  68. if (session_control != SessionControl::IGNORE_CONTROL &&
  69. (cookie.IsPersistent() !=
  70. (session_control == SessionControl::PERSISTENT_COOKIES))) {
  71. return false;
  72. }
  73. if (!creation_range.Contains(cookie.CreationDate()))
  74. return false;
  75. if (host.has_value() &&
  76. !(cookie.IsHostCookie() && cookie.IsDomainMatch(host.value()))) {
  77. return false;
  78. }
  79. if (name.has_value() && cookie.Name() != name)
  80. return false;
  81. if (value_for_testing.has_value() &&
  82. value_for_testing.value() != cookie.Value()) {
  83. return false;
  84. }
  85. // |CookieOptions::MakeAllInclusive()| options will make sure that all
  86. // cookies associated with the URL are deleted.
  87. if (url.has_value() &&
  88. !cookie
  89. .IncludeForRequestURL(url.value(), CookieOptions::MakeAllInclusive(),
  90. params)
  91. .status.IsInclude()) {
  92. return false;
  93. }
  94. if (!domains_and_ips_to_delete.empty() &&
  95. !DomainMatchesDomains(cookie, domains_and_ips_to_delete)) {
  96. return false;
  97. }
  98. if (!domains_and_ips_to_ignore.empty() &&
  99. DomainMatchesDomains(cookie, domains_and_ips_to_ignore)) {
  100. return false;
  101. }
  102. if (cookie.IsPartitioned() &&
  103. !cookie_partition_key_collection.Contains(*cookie.PartitionKey())) {
  104. return false;
  105. }
  106. return true;
  107. }
  108. } // namespace net