forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie_deletion_info.cc
141 lines (108 loc) · 4.32 KB
/
cookie_deletion_info.cc
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/cookies/cookie_deletion_info.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/cookies/cookie_options.h"
namespace net {
namespace {
// Return true if the eTLD+1 of the cookies domain matches any of the strings
// in |match_domains|, false otherwise.
bool DomainMatchesDomains(const net::CanonicalCookie& cookie,
const std::set<std::string>& match_domains) {
if (match_domains.empty())
return false;
// If domain is an IP address it returns an empty string.
std::string effective_domain(
net::registry_controlled_domains::GetDomainAndRegistry(
// GetDomainAndRegistry() is insensitive to leading dots, i.e.
// to host/domain cookie distinctions.
cookie.Domain(),
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
// If the cookie's domain is is not parsed as belonging to a registry
// (e.g. for IP addresses or internal hostnames) an empty string will be
// returned. In this case, use the domain in the cookie.
if (effective_domain.empty()) {
if (cookie.IsDomainCookie())
effective_domain = cookie.Domain().substr(1);
else
effective_domain = cookie.Domain();
}
return match_domains.count(effective_domain) != 0;
}
} // anonymous namespace
CookieDeletionInfo::TimeRange::TimeRange() = default;
CookieDeletionInfo::TimeRange::TimeRange(const TimeRange& other) = default;
CookieDeletionInfo::TimeRange::TimeRange(base::Time start, base::Time end)
: start_(start), end_(end) {
if (!start.is_null() && !end.is_null())
DCHECK_GE(end, start);
}
CookieDeletionInfo::TimeRange& CookieDeletionInfo::TimeRange::operator=(
const TimeRange& rhs) = default;
bool CookieDeletionInfo::TimeRange::Contains(const base::Time& time) const {
DCHECK(!time.is_null());
if (!start_.is_null() && start_ == end_)
return time == start_;
return (start_.is_null() || start_ <= time) &&
(end_.is_null() || time < end_);
}
void CookieDeletionInfo::TimeRange::SetStart(base::Time value) {
start_ = value;
}
void CookieDeletionInfo::TimeRange::SetEnd(base::Time value) {
end_ = value;
}
CookieDeletionInfo::CookieDeletionInfo()
: CookieDeletionInfo(base::Time(), base::Time()) {}
CookieDeletionInfo::CookieDeletionInfo(base::Time start_time,
base::Time end_time)
: creation_range(start_time, end_time) {
// Options to use for deletion of cookies associated with
// a particular URL. These options will make sure that all
// cookies associated with the URL are deleted.
cookie_options.set_include_httponly();
cookie_options.set_same_site_cookie_mode(
net::CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX);
}
CookieDeletionInfo::CookieDeletionInfo(CookieDeletionInfo&& other) = default;
CookieDeletionInfo::CookieDeletionInfo(const CookieDeletionInfo& other) =
default;
CookieDeletionInfo::~CookieDeletionInfo() = default;
CookieDeletionInfo& CookieDeletionInfo::operator=(CookieDeletionInfo&& rhs) =
default;
CookieDeletionInfo& CookieDeletionInfo::operator=(
const CookieDeletionInfo& rhs) = default;
bool CookieDeletionInfo::Matches(const CanonicalCookie& cookie) const {
if (session_control != SessionControl::IGNORE_CONTROL &&
(cookie.IsPersistent() !=
(session_control == SessionControl::PERSISTENT_COOKIES))) {
return false;
}
if (!creation_range.Contains(cookie.CreationDate()))
return false;
if (host.has_value() &&
!(cookie.IsHostCookie() && cookie.IsDomainMatch(host.value()))) {
return false;
}
if (name.has_value() && cookie.Name() != name)
return false;
if (value_for_testing.has_value() &&
value_for_testing.value() != cookie.Value()) {
return false;
}
if (url.has_value() &&
!cookie.IncludeForRequestURL(url.value(), cookie_options)) {
return false;
}
if (!domains_and_ips_to_delete.empty() &&
!DomainMatchesDomains(cookie, domains_and_ips_to_delete)) {
return false;
}
if (!domains_and_ips_to_ignore.empty() &&
DomainMatchesDomains(cookie, domains_and_ips_to_ignore)) {
return false;
}
return true;
}
} // namespace net