summaryrefslogtreecommitdiff
path: root/chromium/net/reporting/reporting_header_parser.cc
blob: 12f0064a4b817ee7660ac51b5514ff810f10a065 (plain)
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
// 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 <string>
#include <utility>
#include <vector>

#include "base/bind.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/time/time.h"
#include "base/values.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 {

using HeaderEndpointGroupOutcome =
    ReportingHeaderParser::HeaderEndpointGroupOutcome;
using HeaderEndpointOutcome = ReportingHeaderParser::HeaderEndpointOutcome;
using HeaderOutcome = ReportingHeaderParser::HeaderOutcome;

void RecordHeaderOutcome(HeaderOutcome outcome) {
  UMA_HISTOGRAM_ENUMERATION(ReportingHeaderParser::kHeaderOutcomeHistogram,
                            outcome, HeaderOutcome::MAX);
}

void RecordHeaderEndpointGroupOutcome(HeaderEndpointGroupOutcome outcome) {
  UMA_HISTOGRAM_ENUMERATION(
      ReportingHeaderParser::kHeaderEndpointGroupOutcomeHistogram, outcome,
      HeaderEndpointGroupOutcome::MAX);
}

void RecordHeaderEndpointOutcome(HeaderEndpointOutcome outcome) {
  UMA_HISTOGRAM_ENUMERATION(
      ReportingHeaderParser::kHeaderEndpointOutcomeHistogram, outcome,
      HeaderEndpointOutcome::MAX);
}

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 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_out| will contain the endpoint URL parsed out of the tuple.
HeaderEndpointOutcome 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 HeaderEndpointOutcome::DISCARDED_NOT_DICTIONARY;
  DCHECK(dict);

  std::string endpoint_url_string;
  if (!dict->HasKey(kUrlKey))
    return HeaderEndpointOutcome::DISCARDED_URL_MISSING;
  if (!dict->GetString(kUrlKey, &endpoint_url_string))
    return HeaderEndpointOutcome::DISCARDED_URL_NOT_STRING;

  GURL endpoint_url;
  // Support path-absolute-URL string
  if (std::strspn(endpoint_url_string.c_str(), "/") == 1) {
    endpoint_url = group_key.origin.GetURL().Resolve(endpoint_url_string);
  } else {
    endpoint_url = GURL(endpoint_url_string);
  }
  if (!endpoint_url.is_valid())
    return HeaderEndpointOutcome::DISCARDED_URL_INVALID;
  if (!endpoint_url.SchemeIsCryptographic())
    return HeaderEndpointOutcome::DISCARDED_URL_INSECURE;
  endpoint_info_out->url = std::move(endpoint_url);

  int priority = ReportingEndpoint::EndpointInfo::kDefaultPriority;
  if (dict->HasKey(kPriorityKey) && !dict->GetInteger(kPriorityKey, &priority))
    return HeaderEndpointOutcome::DISCARDED_PRIORITY_NOT_INTEGER;
  if (priority < 0)
    return HeaderEndpointOutcome::DISCARDED_PRIORITY_NEGATIVE;
  endpoint_info_out->priority = priority;

  int weight = ReportingEndpoint::EndpointInfo::kDefaultWeight;
  if (dict->HasKey(kWeightKey) && !dict->GetInteger(kWeightKey, &weight))
    return HeaderEndpointOutcome::DISCARDED_WEIGHT_NOT_INTEGER;
  if (weight < 0)
    return HeaderEndpointOutcome::DISCARDED_WEIGHT_NEGATIVE;
  endpoint_info_out->weight = weight;

  if (!delegate->CanSetClient(group_key.origin, endpoint_url))
    return HeaderEndpointOutcome::SET_REJECTED_BY_DELEGATE;

  return HeaderEndpointOutcome::SET;
}

// 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.
HeaderEndpointGroupOutcome 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 HeaderEndpointGroupOutcome::DISCARDED_NOT_DICTIONARY;
  DCHECK(dict);

  std::string group_name = kDefaultGroupName;
  if (dict->HasKey(kGroupKey) && !dict->GetString(kGroupKey, &group_name))
    return HeaderEndpointGroupOutcome::DISCARDED_GROUP_NOT_STRING;
  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 HeaderEndpointGroupOutcome::DISCARDED_TTL_MISSING;
  if (!dict->GetInteger(kMaxAgeKey, &ttl_sec))
    return HeaderEndpointGroupOutcome::DISCARDED_TTL_NOT_INTEGER;
  if (ttl_sec < 0)
    return HeaderEndpointGroupOutcome::DISCARDED_TTL_NEGATIVE;
  // max_age: 0 signifies removal of the endpoint group.
  if (ttl_sec == 0) {
    cache->RemoveEndpointGroup(group_key);
    return HeaderEndpointGroupOutcome::REMOVED_TTL_ZERO;
  }
  parsed_endpoint_group_out->ttl = base::TimeDelta::FromSeconds(ttl_sec);

  bool subdomains_bool = false;
  if (dict->HasKey(kIncludeSubdomainsKey) &&
      dict->GetBoolean(kIncludeSubdomainsKey, &subdomains_bool) &&
      subdomains_bool == true) {
    // 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 HeaderEndpointGroupOutcome::
          DISCARDED_INCLUDE_SUBDOMAINS_NOT_ALLOWED;
    }

    parsed_endpoint_group_out->include_subdomains = OriginSubdomains::INCLUDE;
  }

  const base::ListValue* endpoint_list = nullptr;
  if (!dict->HasKey(kEndpointsKey))
    return HeaderEndpointGroupOutcome::DISCARDED_ENDPOINTS_MISSING;
  if (!dict->GetList(kEndpointsKey, &endpoint_list))
    return HeaderEndpointGroupOutcome::DISCARDED_ENDPOINTS_NOT_LIST;

  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;

    HeaderEndpointOutcome outcome =
        ProcessEndpoint(delegate, group_key, *endpoint, &parsed_endpoint);

    if (outcome == HeaderEndpointOutcome::SET)
      endpoints.push_back(std::move(parsed_endpoint));

    RecordHeaderEndpointOutcome(outcome);
  }

  // Remove the group if it is empty.
  if (endpoints.empty()) {
    cache->RemoveEndpointGroup(group_key);
    return HeaderEndpointGroupOutcome::REMOVED_EMPTY;
  }

  parsed_endpoint_group_out->endpoints = std::move(endpoints);

  return HeaderEndpointGroupOutcome::PARSED;
}

}  // namespace

// static
const char ReportingHeaderParser::kHeaderOutcomeHistogram[] =
    "Net.Reporting.HeaderOutcome";

// static
const char ReportingHeaderParser::kHeaderEndpointGroupOutcomeHistogram[] =
    "Net.Reporting.HeaderEndpointGroupOutcome";

// static
const char ReportingHeaderParser::kHeaderEndpointOutcomeHistogram[] =
    "Net.Reporting.HeaderEndpointOutcome";

// static
void ReportingHeaderParser::RecordHeaderDiscardedForNoReportingService() {
  RecordHeaderOutcome(HeaderOutcome::DISCARDED_NO_REPORTING_SERVICE);
}

// static
void ReportingHeaderParser::RecordHeaderDiscardedForInvalidSSLInfo() {
  RecordHeaderOutcome(HeaderOutcome::DISCARDED_INVALID_SSL_INFO);
}

// static
void ReportingHeaderParser::RecordHeaderDiscardedForCertStatusError() {
  RecordHeaderOutcome(HeaderOutcome::DISCARDED_CERT_STATUS_ERROR);
}

// static
void ReportingHeaderParser::RecordHeaderDiscardedForJsonInvalid() {
  RecordHeaderOutcome(HeaderOutcome::DISCARDED_JSON_INVALID);
}

// static
void ReportingHeaderParser::RecordHeaderDiscardedForJsonTooBig() {
  RecordHeaderOutcome(HeaderOutcome::DISCARDED_JSON_TOO_BIG);
}

// static
void ReportingHeaderParser::ParseHeader(ReportingContext* context,
                                        const GURL& url,
                                        std::unique_ptr<base::Value> value) {
  DCHECK(url.SchemeIsCryptographic());

  const base::ListValue* group_list = nullptr;
  bool is_list = value->GetAsList(&group_list);
  DCHECK(is_list);

  ReportingDelegate* delegate = context->delegate();
  ReportingCache* cache = context->cache();

  url::Origin origin = url::Origin::Create(url);
  NetworkIsolationKey network_isolation_key = NetworkIsolationKey::Todo();

  std::vector<ReportingEndpointGroup> parsed_header;

  for (size_t i = 0; i < group_list->GetSize(); i++) {
    const base::Value* group_value = nullptr;
    bool got_group = group_list->Get(i, &group_value);
    DCHECK(got_group);
    ReportingEndpointGroup parsed_endpoint_group;
    HeaderEndpointGroupOutcome outcome =
        ProcessEndpointGroup(delegate, cache, network_isolation_key, origin,
                             *group_value, &parsed_endpoint_group);
    RecordHeaderEndpointGroupOutcome(outcome);
    if (outcome == HeaderEndpointGroupOutcome::PARSED)
      parsed_header.push_back(std::move(parsed_endpoint_group));
  }

  // Remove the client if it has no valid endpoint groups.
  if (parsed_header.empty()) {
    // TODO(chlily): Pass NIK to cache.
    cache->RemoveClient(NetworkIsolationKey::Todo(), origin);
    RecordHeaderOutcome(HeaderOutcome::REMOVED_EMPTY);
    return;
  }

  // TODO(chlily): Pass NIK to cache.
  cache->OnParsedHeader(origin, std::move(parsed_header));
  RecordHeaderOutcome(HeaderOutcome::PARSED);
}

}  // namespace net