juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 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/reporting/reporting_header_parser.h" |
| 6 | |
| 7 | #include <string> |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 8 | #include <utility> |
| 9 | #include <vector> |
juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 10 | |
Julia Tuttle | ec467a5f | 2018-02-22 20:22:45 | [diff] [blame] | 11 | #include "base/bind.h" |
juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 12 | #include "base/json/json_reader.h" |
| 13 | #include "base/logging.h" |
juliatuttle | 667c0bb | 2017-07-06 15:17:13 | [diff] [blame] | 14 | #include "base/metrics/histogram_macros.h" |
juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 15 | #include "base/time/time.h" |
| 16 | #include "base/values.h" |
| 17 | #include "net/reporting/reporting_cache.h" |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 18 | #include "net/reporting/reporting_context.h" |
juliatuttle | 58754891 | 2017-05-23 14:17:21 | [diff] [blame] | 19 | #include "net/reporting/reporting_delegate.h" |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame^] | 20 | #include "net/reporting/reporting_endpoint.h" |
juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 21 | |
| 22 | namespace net { |
| 23 | |
| 24 | namespace { |
| 25 | |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 26 | using HeaderEndpointGroupOutcome = |
| 27 | ReportingHeaderParser::HeaderEndpointGroupOutcome; |
| 28 | using HeaderEndpointOutcome = ReportingHeaderParser::HeaderEndpointOutcome; |
| 29 | using HeaderOutcome = ReportingHeaderParser::HeaderOutcome; |
juliatuttle | 667c0bb | 2017-07-06 15:17:13 | [diff] [blame] | 30 | |
| 31 | void RecordHeaderOutcome(HeaderOutcome outcome) { |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 32 | UMA_HISTOGRAM_ENUMERATION(ReportingHeaderParser::kHeaderOutcomeHistogram, |
| 33 | outcome, HeaderOutcome::MAX); |
juliatuttle | 667c0bb | 2017-07-06 15:17:13 | [diff] [blame] | 34 | } |
| 35 | |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 36 | void RecordHeaderEndpointGroupOutcome(HeaderEndpointGroupOutcome outcome) { |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 37 | UMA_HISTOGRAM_ENUMERATION( |
| 38 | ReportingHeaderParser::kHeaderEndpointGroupOutcomeHistogram, outcome, |
| 39 | HeaderEndpointGroupOutcome::MAX); |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 40 | } |
| 41 | |
juliatuttle | 667c0bb | 2017-07-06 15:17:13 | [diff] [blame] | 42 | void RecordHeaderEndpointOutcome(HeaderEndpointOutcome outcome) { |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 43 | UMA_HISTOGRAM_ENUMERATION( |
| 44 | ReportingHeaderParser::kHeaderEndpointOutcomeHistogram, outcome, |
| 45 | HeaderEndpointOutcome::MAX); |
juliatuttle | 667c0bb | 2017-07-06 15:17:13 | [diff] [blame] | 46 | } |
| 47 | |
juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 48 | const char kUrlKey[] = "url"; |
Douglas Creager | bca6442 | 2018-06-18 13:54:42 | [diff] [blame] | 49 | const char kIncludeSubdomainsKey[] = "include_subdomains"; |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 50 | const char kEndpointsKey[] = "endpoints"; |
juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 51 | const char kGroupKey[] = "group"; |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 52 | const char kDefaultGroupName[] = "default"; |
Douglas Creager | bca6442 | 2018-06-18 13:54:42 | [diff] [blame] | 53 | const char kMaxAgeKey[] = "max_age"; |
Julia Tuttle | d56350d | 2017-12-07 19:11:17 | [diff] [blame] | 54 | const char kPriorityKey[] = "priority"; |
| 55 | const char kWeightKey[] = "weight"; |
juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 56 | |
Julia Tuttle | 443a0a68 | 2017-12-04 16:16:26 | [diff] [blame] | 57 | // Processes a single endpoint tuple received in a Report-To header. |
| 58 | // |
| 59 | // |origin| is the origin that sent the Report-To header. |
| 60 | // |
| 61 | // |value| is the parsed JSON value of the endpoint tuple. |
| 62 | // |
| 63 | // |*endpoint_out| will contain the endpoint URL parsed out of the tuple. |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 64 | HeaderEndpointOutcome ProcessEndpoint( |
| 65 | ReportingDelegate* delegate, |
| 66 | const url::Origin& origin, |
| 67 | const base::Value& value, |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame^] | 68 | ReportingEndpoint::EndpointInfo* endpoint_info_out) { |
juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 69 | const base::DictionaryValue* dict = nullptr; |
| 70 | if (!value.GetAsDictionary(&dict)) |
juliatuttle | 667c0bb | 2017-07-06 15:17:13 | [diff] [blame] | 71 | return HeaderEndpointOutcome::DISCARDED_NOT_DICTIONARY; |
juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 72 | DCHECK(dict); |
| 73 | |
| 74 | std::string endpoint_url_string; |
juliatuttle | 667c0bb | 2017-07-06 15:17:13 | [diff] [blame] | 75 | if (!dict->HasKey(kUrlKey)) |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 76 | return HeaderEndpointOutcome::DISCARDED_URL_MISSING; |
juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 77 | if (!dict->GetString(kUrlKey, &endpoint_url_string)) |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 78 | return HeaderEndpointOutcome::DISCARDED_URL_NOT_STRING; |
juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 79 | |
| 80 | GURL endpoint_url(endpoint_url_string); |
| 81 | if (!endpoint_url.is_valid()) |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 82 | return HeaderEndpointOutcome::DISCARDED_URL_INVALID; |
juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 83 | if (!endpoint_url.SchemeIsCryptographic()) |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 84 | return HeaderEndpointOutcome::DISCARDED_URL_INSECURE; |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 85 | endpoint_info_out->url = std::move(endpoint_url); |
juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 86 | |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame^] | 87 | int priority = ReportingEndpoint::EndpointInfo::kDefaultPriority; |
Julia Tuttle | d56350d | 2017-12-07 19:11:17 | [diff] [blame] | 88 | if (dict->HasKey(kPriorityKey) && !dict->GetInteger(kPriorityKey, &priority)) |
| 89 | return HeaderEndpointOutcome::DISCARDED_PRIORITY_NOT_INTEGER; |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 90 | if (priority < 0) |
| 91 | return HeaderEndpointOutcome::DISCARDED_PRIORITY_NEGATIVE; |
| 92 | endpoint_info_out->priority = priority; |
Julia Tuttle | d56350d | 2017-12-07 19:11:17 | [diff] [blame] | 93 | |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame^] | 94 | int weight = ReportingEndpoint::EndpointInfo::kDefaultWeight; |
Julia Tuttle | d56350d | 2017-12-07 19:11:17 | [diff] [blame] | 95 | if (dict->HasKey(kWeightKey) && !dict->GetInteger(kWeightKey, &weight)) |
| 96 | return HeaderEndpointOutcome::DISCARDED_WEIGHT_NOT_INTEGER; |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 97 | if (weight < 0) |
| 98 | return HeaderEndpointOutcome::DISCARDED_WEIGHT_NEGATIVE; |
| 99 | endpoint_info_out->weight = weight; |
juliatuttle | 667c0bb | 2017-07-06 15:17:13 | [diff] [blame] | 100 | |
juliatuttle | 667c0bb | 2017-07-06 15:17:13 | [diff] [blame] | 101 | if (!delegate->CanSetClient(origin, endpoint_url)) |
| 102 | return HeaderEndpointOutcome::SET_REJECTED_BY_DELEGATE; |
| 103 | |
juliatuttle | 667c0bb | 2017-07-06 15:17:13 | [diff] [blame] | 104 | return HeaderEndpointOutcome::SET; |
juliatuttle | 58754891 | 2017-05-23 14:17:21 | [diff] [blame] | 105 | } |
| 106 | |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 107 | // Processes a single endpoint group tuple received in a Report-To header. |
| 108 | // |
| 109 | // |origin| is the origin that sent the Report-To header. |
| 110 | // |
| 111 | // |value| is the parsed JSON value of the endpoint group tuple. |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 112 | HeaderEndpointGroupOutcome ProcessEndpointGroup( |
| 113 | ReportingDelegate* delegate, |
| 114 | ReportingCache* cache, |
| 115 | const url::Origin& origin, |
| 116 | const base::Value& value, |
| 117 | ReportingEndpointGroup* parsed_endpoint_group_out) { |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 118 | const base::DictionaryValue* dict = nullptr; |
| 119 | if (!value.GetAsDictionary(&dict)) |
| 120 | return HeaderEndpointGroupOutcome::DISCARDED_NOT_DICTIONARY; |
| 121 | DCHECK(dict); |
| 122 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 123 | std::string group_name = kDefaultGroupName; |
| 124 | if (dict->HasKey(kGroupKey) && !dict->GetString(kGroupKey, &group_name)) |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 125 | return HeaderEndpointGroupOutcome::DISCARDED_GROUP_NOT_STRING; |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 126 | parsed_endpoint_group_out->name = std::move(group_name); |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 127 | |
| 128 | int ttl_sec = -1; |
| 129 | if (!dict->HasKey(kMaxAgeKey)) |
| 130 | return HeaderEndpointGroupOutcome::DISCARDED_TTL_MISSING; |
| 131 | if (!dict->GetInteger(kMaxAgeKey, &ttl_sec)) |
| 132 | return HeaderEndpointGroupOutcome::DISCARDED_TTL_NOT_INTEGER; |
| 133 | if (ttl_sec < 0) |
| 134 | return HeaderEndpointGroupOutcome::DISCARDED_TTL_NEGATIVE; |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 135 | // max_age: 0 signifies removal of the endpoint group. |
| 136 | if (ttl_sec == 0) { |
| 137 | cache->RemoveEndpointGroup(origin, group_name); |
| 138 | return HeaderEndpointGroupOutcome::REMOVED_TTL_ZERO; |
| 139 | } |
| 140 | parsed_endpoint_group_out->ttl = base::TimeDelta::FromSeconds(ttl_sec); |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 141 | |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 142 | bool subdomains_bool = false; |
| 143 | if (dict->HasKey(kIncludeSubdomainsKey) && |
| 144 | dict->GetBoolean(kIncludeSubdomainsKey, &subdomains_bool) && |
| 145 | subdomains_bool == true) { |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 146 | parsed_endpoint_group_out->include_subdomains = OriginSubdomains::INCLUDE; |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | const base::ListValue* endpoint_list = nullptr; |
| 150 | if (!dict->HasKey(kEndpointsKey)) |
| 151 | return HeaderEndpointGroupOutcome::DISCARDED_ENDPOINTS_MISSING; |
| 152 | if (!dict->GetList(kEndpointsKey, &endpoint_list)) |
| 153 | return HeaderEndpointGroupOutcome::DISCARDED_ENDPOINTS_NOT_LIST; |
| 154 | |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame^] | 155 | std::vector<ReportingEndpoint::EndpointInfo> endpoints; |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 156 | |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 157 | for (size_t i = 0; i < endpoint_list->GetSize(); i++) { |
| 158 | const base::Value* endpoint = nullptr; |
| 159 | bool got_endpoint = endpoint_list->Get(i, &endpoint); |
| 160 | DCHECK(got_endpoint); |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 161 | |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame^] | 162 | ReportingEndpoint::EndpointInfo parsed_endpoint; |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 163 | |
| 164 | HeaderEndpointOutcome outcome = |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 165 | ProcessEndpoint(delegate, origin, *endpoint, &parsed_endpoint); |
| 166 | |
| 167 | if (outcome == HeaderEndpointOutcome::SET) |
| 168 | endpoints.push_back(std::move(parsed_endpoint)); |
| 169 | |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 170 | RecordHeaderEndpointOutcome(outcome); |
| 171 | } |
| 172 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 173 | // Remove the group if it is empty. |
| 174 | if (endpoints.empty()) { |
| 175 | cache->RemoveEndpointGroup(origin, group_name); |
| 176 | return HeaderEndpointGroupOutcome::REMOVED_EMPTY; |
| 177 | } |
| 178 | |
| 179 | parsed_endpoint_group_out->endpoints = std::move(endpoints); |
| 180 | |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 181 | return HeaderEndpointGroupOutcome::PARSED; |
| 182 | } |
| 183 | |
juliatuttle | 58754891 | 2017-05-23 14:17:21 | [diff] [blame] | 184 | } // namespace |
| 185 | |
| 186 | // static |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 187 | const char ReportingHeaderParser::kHeaderOutcomeHistogram[] = |
| 188 | "Net.Reporting.HeaderOutcome"; |
| 189 | |
| 190 | // static |
| 191 | const char ReportingHeaderParser::kHeaderEndpointGroupOutcomeHistogram[] = |
| 192 | "Net.Reporting.HeaderEndpointGroupOutcome"; |
| 193 | |
| 194 | // static |
| 195 | const char ReportingHeaderParser::kHeaderEndpointOutcomeHistogram[] = |
| 196 | "Net.Reporting.HeaderEndpointOutcome"; |
| 197 | |
| 198 | // static |
juliatuttle | 667c0bb | 2017-07-06 15:17:13 | [diff] [blame] | 199 | void ReportingHeaderParser::RecordHeaderDiscardedForNoReportingService() { |
| 200 | RecordHeaderOutcome(HeaderOutcome::DISCARDED_NO_REPORTING_SERVICE); |
| 201 | } |
| 202 | |
| 203 | // static |
| 204 | void ReportingHeaderParser::RecordHeaderDiscardedForInvalidSSLInfo() { |
| 205 | RecordHeaderOutcome(HeaderOutcome::DISCARDED_INVALID_SSL_INFO); |
| 206 | } |
| 207 | |
| 208 | // static |
| 209 | void ReportingHeaderParser::RecordHeaderDiscardedForCertStatusError() { |
| 210 | RecordHeaderOutcome(HeaderOutcome::DISCARDED_CERT_STATUS_ERROR); |
| 211 | } |
| 212 | |
| 213 | // static |
Julia Tuttle | ef19cb5 | 2018-03-16 16:58:35 | [diff] [blame] | 214 | void ReportingHeaderParser::RecordHeaderDiscardedForJsonInvalid() { |
| 215 | RecordHeaderOutcome(HeaderOutcome::DISCARDED_JSON_INVALID); |
| 216 | } |
| 217 | |
| 218 | // static |
| 219 | void ReportingHeaderParser::RecordHeaderDiscardedForJsonTooBig() { |
| 220 | RecordHeaderOutcome(HeaderOutcome::DISCARDED_JSON_TOO_BIG); |
Julia Tuttle | ec467a5f | 2018-02-22 20:22:45 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // static |
juliatuttle | 58754891 | 2017-05-23 14:17:21 | [diff] [blame] | 224 | void ReportingHeaderParser::ParseHeader(ReportingContext* context, |
| 225 | const GURL& url, |
Julia Tuttle | ec467a5f | 2018-02-22 20:22:45 | [diff] [blame] | 226 | std::unique_ptr<base::Value> value) { |
juliatuttle | 58754891 | 2017-05-23 14:17:21 | [diff] [blame] | 227 | DCHECK(url.SchemeIsCryptographic()); |
| 228 | |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 229 | const base::ListValue* group_list = nullptr; |
| 230 | bool is_list = value->GetAsList(&group_list); |
juliatuttle | 58754891 | 2017-05-23 14:17:21 | [diff] [blame] | 231 | DCHECK(is_list); |
| 232 | |
| 233 | ReportingDelegate* delegate = context->delegate(); |
| 234 | ReportingCache* cache = context->cache(); |
Julia Tuttle | 443a0a68 | 2017-12-04 16:16:26 | [diff] [blame] | 235 | |
| 236 | url::Origin origin = url::Origin::Create(url); |
| 237 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 238 | std::vector<ReportingEndpointGroup> parsed_header; |
Julia Tuttle | 443a0a68 | 2017-12-04 16:16:26 | [diff] [blame] | 239 | |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 240 | for (size_t i = 0; i < group_list->GetSize(); i++) { |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 241 | const base::Value* group_value = nullptr; |
| 242 | bool got_group = group_list->Get(i, &group_value); |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 243 | DCHECK(got_group); |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 244 | ReportingEndpointGroup parsed_endpoint_group; |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 245 | HeaderEndpointGroupOutcome outcome = ProcessEndpointGroup( |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 246 | delegate, cache, origin, *group_value, &parsed_endpoint_group); |
Douglas Creager | f0db63a | 2018-02-28 17:50:23 | [diff] [blame] | 247 | RecordHeaderEndpointGroupOutcome(outcome); |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 248 | if (outcome == HeaderEndpointGroupOutcome::PARSED) |
| 249 | parsed_header.push_back(std::move(parsed_endpoint_group)); |
Julia Tuttle | 443a0a68 | 2017-12-04 16:16:26 | [diff] [blame] | 250 | } |
| 251 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 252 | // Remove the client if it has no valid endpoint groups. |
| 253 | if (parsed_header.empty()) { |
| 254 | cache->RemoveClient(origin); |
| 255 | RecordHeaderOutcome(HeaderOutcome::REMOVED_EMPTY); |
| 256 | return; |
juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 257 | } |
Julia Tuttle | efe2fae4 | 2018-03-30 15:22:31 | [diff] [blame] | 258 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 259 | cache->OnParsedHeader(origin, std::move(parsed_header)); |
Julia Tuttle | efe2fae4 | 2018-03-30 15:22:31 | [diff] [blame] | 260 | RecordHeaderOutcome(HeaderOutcome::PARSED); |
juliatuttle | 1690bc6 | 2017-03-29 17:16:02 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | } // namespace net |