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
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/reporting/reporting_service.h"
#include <optional>
#include <utility>
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/notimplemented.h"
#include "base/time/tick_clock.h"
#include "base/time/time.h"
#include "base/values.h"
#include "net/base/features.h"
#include "net/base/isolation_info.h"
#include "net/http/structured_headers.h"
#include "net/reporting/reporting_browsing_data_remover.h"
#include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_context.h"
#include "net/reporting/reporting_delegate.h"
#include "net/reporting/reporting_delivery_agent.h"
#include "net/reporting/reporting_header_parser.h"
#include "net/reporting/reporting_uploader.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace net {
namespace {
constexpr int kMaxJsonSize = 16 * 1024;
constexpr int kMaxJsonDepth = 5;
// If constructed with a PersistentReportingStore, the first call to any of
// QueueReport(), ProcessHeader(), RemoveBrowsingData(), or
// RemoveAllBrowsingData() on a valid input will trigger a load from the store.
// Tasks are queued pending completion of loading from the store.
class ReportingServiceImpl : public ReportingService {
public:
explicit ReportingServiceImpl(std::unique_ptr<ReportingContext> context)
: context_(std::move(context)) {
if (!context_->IsClientDataPersisted())
initialized_ = true;
}
ReportingServiceImpl(const ReportingServiceImpl&) = delete;
ReportingServiceImpl& operator=(const ReportingServiceImpl&) = delete;
// ReportingService implementation:
~ReportingServiceImpl() override {
if (initialized_)
context_->cache()->Flush();
}
void SetDocumentReportingEndpoints(
const base::UnguessableToken& reporting_source,
const url::Origin& origin,
const IsolationInfo& isolation_info,
const base::flat_map<std::string, std::string>& endpoints) override {
DCHECK(!reporting_source.is_empty());
DoOrBacklogTask(
base::BindOnce(&ReportingServiceImpl::DoSetDocumentReportingEndpoints,
base::Unretained(this), reporting_source, isolation_info,
FixupNetworkAnonymizationKey(
isolation_info.network_anonymization_key()),
origin, std::move(endpoints)));
}
void SetEnterpriseReportingEndpoints(
const base::flat_map<std::string, GURL>& endpoints) override {
if (!base::FeatureList::IsEnabled(
net::features::kReportingApiEnableEnterpriseCookieIssues)) {
return;
}
context_->cache()->SetEnterpriseReportingEndpoints(endpoints);
}
void SendReportsAndRemoveSource(
const base::UnguessableToken& reporting_source) override {
DCHECK(!reporting_source.is_empty());
// Queue expiration of the reporting sources as backlog tasks if the
// reporting service is not yet initialized, so that reports and expirations
// remain well-ordered.
DoOrBacklogTask(
base::BindOnce(&ReportingServiceImpl::DoSendReportsAndRemoveSource,
base::Unretained(this), reporting_source));
}
void QueueReport(
const GURL& url,
const std::optional<base::UnguessableToken>& reporting_source,
const NetworkAnonymizationKey& network_anonymization_key,
const std::string& user_agent,
const std::string& group,
const std::string& type,
base::Value::Dict body,
int depth,
ReportingTargetType target_type) override {
DCHECK(context_);
DCHECK(context_->delegate());
// If |reporting_source| is provided, it must not be empty.
DCHECK(!(reporting_source.has_value() && reporting_source->is_empty()));
if (!context_->delegate()->CanQueueReport(url::Origin::Create(url)))
return;
// Strip username, password, and ref fragment from the URL.
GURL sanitized_url = url.GetAsReferrer();
if (!sanitized_url.is_valid())
return;
base::TimeTicks queued_ticks = context_->tick_clock().NowTicks();
// base::Unretained is safe because the callback is stored in
// |task_backlog_| which will not outlive |this|.
DoOrBacklogTask(
base::BindOnce(&ReportingServiceImpl::DoQueueReport,
base::Unretained(this), reporting_source,
FixupNetworkAnonymizationKey(network_anonymization_key),
std::move(sanitized_url), user_agent, group, type,
std::move(body), depth, queued_ticks, target_type));
}
void ProcessReportToHeader(
const url::Origin& origin,
const NetworkAnonymizationKey& network_anonymization_key,
const std::string& header_string) override {
if (header_string.size() > kMaxJsonSize)
return;
std::optional<base::Value> header_value = base::JSONReader::Read(
"[" + header_string + "]", base::JSON_PARSE_RFC, kMaxJsonDepth);
if (!header_value)
return;
DVLOG(1) << "Received Reporting policy for " << origin;
DoOrBacklogTask(base::BindOnce(
&ReportingServiceImpl::DoProcessReportToHeader, base::Unretained(this),
FixupNetworkAnonymizationKey(network_anonymization_key), origin,
std::move(header_value).value()));
}
void RemoveBrowsingData(
uint64_t data_type_mask,
const base::RepeatingCallback<bool(const url::Origin&)>& origin_filter)
override {
DoOrBacklogTask(base::BindOnce(&ReportingServiceImpl::DoRemoveBrowsingData,
base::Unretained(this), data_type_mask,
origin_filter));
}
void RemoveAllBrowsingData(uint64_t data_type_mask) override {
DoOrBacklogTask(
base::BindOnce(&ReportingServiceImpl::DoRemoveAllBrowsingData,
base::Unretained(this), data_type_mask));
}
void OnShutdown() override {
shut_down_ = true;
context_->OnShutdown();
}
const ReportingPolicy& GetPolicy() const override {
return context_->policy();
}
base::Value StatusAsValue() const override {
base::Value::Dict dict;
dict.Set("reportingEnabled", true);
dict.Set("clients", context_->cache()->GetClientsAsValue());
dict.Set("reports", context_->cache()->GetReportsAsValue());
return base::Value(std::move(dict));
}
std::vector<raw_ptr<const ReportingReport, VectorExperimental>> GetReports()
const override {
std::vector<raw_ptr<const net::ReportingReport, VectorExperimental>>
reports;
context_->cache()->GetReports(&reports);
return reports;
}
base::flat_map<url::Origin, std::vector<ReportingEndpoint>>
GetV1ReportingEndpointsByOrigin() const override {
return context_->cache()->GetV1ReportingEndpointsByOrigin();
}
void AddReportingCacheObserver(ReportingCacheObserver* observer) override {
context_->AddCacheObserver(observer);
}
void RemoveReportingCacheObserver(ReportingCacheObserver* observer) override {
context_->RemoveCacheObserver(observer);
}
ReportingContext* GetContextForTesting() const override {
return context_.get();
}
private:
void DoSendReportsAndRemoveSource(
const base::UnguessableToken& reporting_source) {
context_->delivery_agent()->SendReportsForSource(reporting_source);
context_->cache()->SetExpiredSource(reporting_source);
}
void DoOrBacklogTask(base::OnceClosure task) {
if (shut_down_)
return;
FetchAllClientsFromStoreIfNecessary();
if (!initialized_) {
task_backlog_.push_back(std::move(task));
return;
}
std::move(task).Run();
}
void DoQueueReport(
const std::optional<base::UnguessableToken>& reporting_source,
const NetworkAnonymizationKey& network_anonymization_key,
GURL sanitized_url,
const std::string& user_agent,
const std::string& group,
const std::string& type,
base::Value::Dict body,
int depth,
base::TimeTicks queued_ticks,
ReportingTargetType target_type) {
DCHECK(initialized_);
context_->cache()->AddReport(
reporting_source, network_anonymization_key, sanitized_url, user_agent,
group, type, std::move(body), depth, queued_ticks, target_type);
}
void DoProcessReportToHeader(
const NetworkAnonymizationKey& network_anonymization_key,
const url::Origin& origin,
const base::Value& header_value) {
DCHECK(initialized_);
DCHECK(header_value.is_list());
ReportingHeaderParser::ParseReportToHeader(context_.get(),
network_anonymization_key,
origin, header_value.GetList());
}
void DoSetDocumentReportingEndpoints(
const base::UnguessableToken& reporting_source,
const IsolationInfo& isolation_info,
const NetworkAnonymizationKey& network_anonymization_key,
const url::Origin& origin,
base::flat_map<std::string, std::string> header_value) {
DCHECK(initialized_);
ReportingHeaderParser::ProcessParsedReportingEndpointsHeader(
context_.get(), reporting_source, isolation_info,
network_anonymization_key, origin, std::move(header_value));
}
void DoRemoveBrowsingData(
uint64_t data_type_mask,
const base::RepeatingCallback<bool(const url::Origin&)>& origin_filter) {
DCHECK(initialized_);
ReportingBrowsingDataRemover::RemoveBrowsingData(
context_->cache(), data_type_mask, origin_filter);
}
void DoRemoveAllBrowsingData(uint64_t data_type_mask) {
DCHECK(initialized_);
ReportingBrowsingDataRemover::RemoveAllBrowsingData(context_->cache(),
data_type_mask);
}
void ExecuteBacklog() {
DCHECK(initialized_);
DCHECK(context_);
if (shut_down_)
return;
for (base::OnceClosure& task : task_backlog_) {
std::move(task).Run();
}
task_backlog_.clear();
}
void FetchAllClientsFromStoreIfNecessary() {
if (!context_->IsClientDataPersisted() || started_loading_from_store_)
return;
started_loading_from_store_ = true;
FetchAllClientsFromStore();
}
void FetchAllClientsFromStore() {
DCHECK(context_->IsClientDataPersisted());
DCHECK(!initialized_);
context_->store()->LoadReportingClients(base::BindOnce(
&ReportingServiceImpl::OnClientsLoaded, weak_factory_.GetWeakPtr()));
}
void OnClientsLoaded(
std::vector<ReportingEndpoint> loaded_endpoints,
std::vector<CachedReportingEndpointGroup> loaded_endpoint_groups) {
initialized_ = true;
context_->cache()->AddClientsLoadedFromStore(
std::move(loaded_endpoints), std::move(loaded_endpoint_groups));
ExecuteBacklog();
}
// Returns either |network_anonymization_key| or an empty
// NetworkAnonymizationKey, based on |respect_network_anonymization_key_|.
// Should be used on all NetworkAnonymizationKeys passed in through public API
// calls.
const NetworkAnonymizationKey& FixupNetworkAnonymizationKey(
const NetworkAnonymizationKey& network_anonymization_key) {
if (respect_network_anonymization_key_)
return network_anonymization_key;
return empty_nak_;
}
std::unique_ptr<ReportingContext> context_;
bool shut_down_ = false;
bool started_loading_from_store_ = false;
bool initialized_ = false;
std::vector<base::OnceClosure> task_backlog_;
bool respect_network_anonymization_key_ =
NetworkAnonymizationKey::IsPartitioningEnabled();
// Allows returning a NetworkAnonymizationKey by reference when
// |respect_network_anonymization_key_| is false.
NetworkAnonymizationKey empty_nak_;
base::WeakPtrFactory<ReportingServiceImpl> weak_factory_{this};
};
} // namespace
ReportingService::~ReportingService() = default;
// static
std::unique_ptr<ReportingService> ReportingService::Create(
const ReportingPolicy& policy,
URLRequestContext* request_context,
ReportingCache::PersistentReportingStore* store,
const base::flat_map<std::string, GURL>& enterprise_reporting_endpoints) {
return std::make_unique<ReportingServiceImpl>(ReportingContext::Create(
policy, request_context, store, enterprise_reporting_endpoints));
}
// static
std::unique_ptr<ReportingService> ReportingService::CreateForTesting(
std::unique_ptr<ReportingContext> reporting_context) {
return std::make_unique<ReportingServiceImpl>(std::move(reporting_context));
}
base::Value ReportingService::StatusAsValue() const {
NOTIMPLEMENTED();
return base::Value();
}
} // namespace net
|