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