forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreporting_header_parser.cc
349 lines (298 loc) · 12.1 KB
/
reporting_header_parser.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
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
// Copyright 2017 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/reporting/reporting_header_parser.h"
#include <cstring>
#include <string>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/check.h"
#include "base/feature_list.h"
#include "base/json/json_reader.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "net/base/features.h"
#include "net/base/network_isolation_key.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_context.h"
#include "net/reporting/reporting_delegate.h"
#include "net/reporting/reporting_endpoint.h"
namespace net {
namespace {
const char kUrlKey[] = "url";
const char kIncludeSubdomainsKey[] = "include_subdomains";
const char kEndpointsKey[] = "endpoints";
const char kGroupKey[] = "group";
const char kDefaultGroupName[] = "default";
const char kMaxAgeKey[] = "max_age";
const char kPriorityKey[] = "priority";
const char kWeightKey[] = "weight";
// Processes a single endpoint url string parsed from header.
//
// |endpoint_url_string| is the string value of the endpoint URL.
// |header_origin_url| is the origin URL that sent the header.
//
// |endpoint_url_out| is the endpoint URL parsed out of the string.
// Returns true on success or false if url was invalid.
bool ProcessEndpointURLString(const std::string& endpoint_url_string,
const url::Origin& header_origin,
GURL& endpoint_url_out) {
// Support path-absolute-URL string with exactly one leading "/"
if (std::strspn(endpoint_url_string.c_str(), "/") == 1) {
endpoint_url_out = header_origin.GetURL().Resolve(endpoint_url_string);
} else {
endpoint_url_out = GURL(endpoint_url_string);
}
if (!endpoint_url_out.is_valid())
return false;
if (!endpoint_url_out.SchemeIsCryptographic())
return false;
return true;
}
// Processes a single endpoint tuple received in a Report-To header.
//
// |origin| is the origin that sent the Report-To header.
//
// |value| is the parsed JSON value of the endpoint tuple.
//
// |*endpoint_info_out| will contain the endpoint URL parsed out of the tuple.
// Returns true on success or false if endpoint was discarded.
bool ProcessEndpoint(ReportingDelegate* delegate,
const ReportingEndpointGroupKey& group_key,
const base::Value& value,
ReportingEndpoint::EndpointInfo* endpoint_info_out) {
const base::DictionaryValue* dict = nullptr;
if (!value.GetAsDictionary(&dict))
return false;
DCHECK(dict);
std::string endpoint_url_string;
if (!dict->HasKey(kUrlKey))
return false;
if (!dict->GetString(kUrlKey, &endpoint_url_string))
return false;
GURL endpoint_url;
if (!ProcessEndpointURLString(endpoint_url_string, group_key.origin,
endpoint_url)) {
return false;
}
endpoint_info_out->url = std::move(endpoint_url);
int priority = ReportingEndpoint::EndpointInfo::kDefaultPriority;
if (dict->HasKey(kPriorityKey) && !dict->GetInteger(kPriorityKey, &priority))
return false;
if (priority < 0)
return false;
endpoint_info_out->priority = priority;
int weight = ReportingEndpoint::EndpointInfo::kDefaultWeight;
if (dict->HasKey(kWeightKey) && !dict->GetInteger(kWeightKey, &weight))
return false;
if (weight < 0)
return false;
endpoint_info_out->weight = weight;
return delegate->CanSetClient(group_key.origin, endpoint_info_out->url);
}
// Processes a single endpoint group tuple received in a Report-To header.
//
// |origin| is the origin that sent the Report-To header.
//
// |value| is the parsed JSON value of the endpoint group tuple.
// Returns true on successfully adding a non-empty group, or false if endpoint
// group was discarded or processed as a deletion.
bool ProcessEndpointGroup(ReportingDelegate* delegate,
ReportingCache* cache,
const NetworkIsolationKey& network_isolation_key,
const url::Origin& origin,
const base::Value& value,
ReportingEndpointGroup* parsed_endpoint_group_out) {
const base::DictionaryValue* dict = nullptr;
if (!value.GetAsDictionary(&dict))
return false;
DCHECK(dict);
std::string group_name = kDefaultGroupName;
if (dict->HasKey(kGroupKey) && !dict->GetString(kGroupKey, &group_name))
return false;
ReportingEndpointGroupKey group_key(network_isolation_key, origin,
group_name);
parsed_endpoint_group_out->group_key = group_key;
int ttl_sec = -1;
if (!dict->HasKey(kMaxAgeKey))
return false;
if (!dict->GetInteger(kMaxAgeKey, &ttl_sec))
return false;
if (ttl_sec < 0)
return false;
// max_age: 0 signifies removal of the endpoint group.
if (ttl_sec == 0) {
cache->RemoveEndpointGroup(group_key);
return false;
}
parsed_endpoint_group_out->ttl = base::TimeDelta::FromSeconds(ttl_sec);
absl::optional<bool> subdomains_bool =
dict->FindBoolKey(kIncludeSubdomainsKey);
if (subdomains_bool && subdomains_bool.value()) {
// Disallow eTLDs from setting include_subdomains endpoint groups.
if (registry_controlled_domains::GetRegistryLength(
origin.GetURL(),
registry_controlled_domains::INCLUDE_UNKNOWN_REGISTRIES,
registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES) == 0) {
return false;
}
parsed_endpoint_group_out->include_subdomains = OriginSubdomains::INCLUDE;
}
const base::ListValue* endpoint_list = nullptr;
if (!dict->HasKey(kEndpointsKey))
return false;
if (!dict->GetList(kEndpointsKey, &endpoint_list))
return false;
std::vector<ReportingEndpoint::EndpointInfo> endpoints;
for (size_t i = 0; i < endpoint_list->GetSize(); i++) {
const base::Value* endpoint = nullptr;
bool got_endpoint = endpoint_list->Get(i, &endpoint);
DCHECK(got_endpoint);
ReportingEndpoint::EndpointInfo parsed_endpoint;
if (ProcessEndpoint(delegate, group_key, *endpoint, &parsed_endpoint))
endpoints.push_back(std::move(parsed_endpoint));
}
// Remove the group if it is empty.
if (endpoints.empty()) {
cache->RemoveEndpointGroup(group_key);
return false;
}
parsed_endpoint_group_out->endpoints = std::move(endpoints);
return true;
}
// Processes a single endpoint tuple received in a Reporting-Endpoints header.
//
// |group_key| is the key for the endpoint group this endpoint belongs.
// |endpoint_url_string| is the endpoint url as received in the header.
//
// |endpoint_info_out| is the endpoint info parsed out of the value.
bool ProcessEndpoint(ReportingDelegate* delegate,
const ReportingEndpointGroupKey& group_key,
const std::string& endpoint_url_string,
ReportingEndpoint::EndpointInfo& endpoint_info_out) {
if (endpoint_url_string.empty())
return false;
GURL endpoint_url;
if (!ProcessEndpointURLString(endpoint_url_string, group_key.origin,
endpoint_url)) {
return false;
}
endpoint_info_out.url = std::move(endpoint_url);
// Reporting-Endpoints endpoint doesn't have prioirty/weight so set to
// default.
endpoint_info_out.priority =
ReportingEndpoint::EndpointInfo::kDefaultPriority;
endpoint_info_out.weight = ReportingEndpoint::EndpointInfo::kDefaultWeight;
return delegate->CanSetClient(group_key.origin, endpoint_info_out.url);
}
// Process a single endpoint received in a Reporting-Endpoints header.
bool ProcessV1Endpoint(ReportingDelegate* delegate,
ReportingCache* cache,
const base::UnguessableToken& reporting_source,
const NetworkIsolationKey& network_isolation_key,
const url::Origin& origin,
const std::string& endpoint_name,
const std::string& endpoint_url_string,
ReportingEndpoint& parsed_endpoint_out) {
DCHECK(!reporting_source.is_empty());
ReportingEndpointGroupKey group_key(network_isolation_key, reporting_source,
origin, endpoint_name);
parsed_endpoint_out.group_key = group_key;
ReportingEndpoint::EndpointInfo parsed_endpoint;
if (!ProcessEndpoint(delegate, group_key, endpoint_url_string,
parsed_endpoint)) {
return false;
}
parsed_endpoint_out.info = std::move(parsed_endpoint);
return true;
}
} // namespace
absl::optional<base::flat_map<std::string, std::string>>
ParseReportingEndpoints(const std::string& header) {
absl::optional<structured_headers::Dictionary> header_dict =
structured_headers::ParseDictionary(header);
if (!header_dict) {
return absl::nullopt;
}
base::flat_map<std::string, std::string> parsed_header;
for (const structured_headers::DictionaryMember& entry : *header_dict) {
if (entry.second.member_is_inner_list ||
!entry.second.member.front().item.is_string()) {
return absl::nullopt;
}
const std::string& endpoint_url_string =
entry.second.member.front().item.GetString();
parsed_header[entry.first] = endpoint_url_string;
}
return parsed_header;
}
// static
void ReportingHeaderParser::RecordReportingHeaderType(
ReportingHeaderType header_type) {
base::UmaHistogramEnumeration("Net.Reporting.HeaderType", header_type);
}
// static
void ReportingHeaderParser::ParseReportToHeader(
ReportingContext* context,
const NetworkIsolationKey& network_isolation_key,
const GURL& url,
std::unique_ptr<base::Value> value) {
DCHECK(url.SchemeIsCryptographic());
DCHECK(value->is_list());
ReportingDelegate* delegate = context->delegate();
ReportingCache* cache = context->cache();
url::Origin origin = url::Origin::Create(url);
std::vector<ReportingEndpointGroup> parsed_header;
for (size_t i = 0; i < value->GetList().size(); i++) {
const base::Value& group_value = value->GetList()[i];
ReportingEndpointGroup parsed_endpoint_group;
if (ProcessEndpointGroup(delegate, cache, network_isolation_key, origin,
group_value, &parsed_endpoint_group)) {
parsed_header.push_back(std::move(parsed_endpoint_group));
}
}
if (parsed_header.empty() && value->GetList().size() > 0) {
RecordReportingHeaderType(ReportingHeaderType::kReportToInvalid);
}
// Remove the client if it has no valid endpoint groups.
if (parsed_header.empty()) {
cache->RemoveClient(network_isolation_key, origin);
return;
}
RecordReportingHeaderType(ReportingHeaderType::kReportTo);
cache->OnParsedHeader(network_isolation_key, origin,
std::move(parsed_header));
}
// static
void ReportingHeaderParser::ProcessParsedReportingEndpointsHeader(
ReportingContext* context,
const base::UnguessableToken& reporting_source,
const NetworkIsolationKey& network_isolation_key,
const url::Origin& origin,
base::flat_map<std::string, std::string> header) {
DCHECK(base::FeatureList::IsEnabled(net::features::kDocumentReporting));
DCHECK(GURL::SchemeIsCryptographic(origin.scheme()));
DCHECK(!reporting_source.is_empty());
ReportingDelegate* delegate = context->delegate();
ReportingCache* cache = context->cache();
std::vector<ReportingEndpoint> parsed_header;
for (const auto& member : header) {
ReportingEndpoint parsed_endpoint;
if (ProcessV1Endpoint(delegate, cache, reporting_source,
network_isolation_key, origin, member.first,
member.second, parsed_endpoint)) {
parsed_header.push_back(std::move(parsed_endpoint));
}
}
// Remove the client if it has no valid endpoint groups.
if (parsed_header.empty()) {
cache->RemoveClient(network_isolation_key, origin);
return;
}
cache->OnParsedReportingEndpointsHeader(reporting_source,
std::move(parsed_header));
}
} // namespace net