Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 "components/data_reduction_proxy/browser/data_reduction_proxy_tamper_de tect.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <cstring> | |
| 9 | |
| 10 #include "base/base64.h" | |
| 11 #include "base/md5.h" | |
| 12 #include "base/metrics/histogram.h" | |
| 13 #include "base/metrics/sparse_histogram.h" | |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "components/data_reduction_proxy/common/data_reduction_proxy_headers.h" | |
| 16 #include "net/android/network_library.h" | |
| 17 #include "net/http/http_response_headers.h" | |
| 18 #include "net/http/http_util.h" | |
| 19 | |
| 20 // Macro for UMA reporting. Depending on |scheme_is_https|, first reports to | |
| 21 // histogram events |https_histogram| or |http_histogram| by |carrier_id|; then | |
| 22 // reports total counts to |https_histogram|_Total or |http_histogram|_Total. | |
| 23 #define REPORT_TAMPER_DETECTION_UMA(scheme_is_https, http_histogram, https_histo gram, carrier_id) \ | |
| 24 do { \ | |
| 25 if (scheme_is_https) { \ | |
| 26 UMA_HISTOGRAM_SPARSE_SLOWLY(https_histogram, carrier_id); \ | |
| 27 UMA_HISTOGRAM_COUNTS(https_histogram "_Total", 1); \ | |
| 28 } else { \ | |
| 29 UMA_HISTOGRAM_SPARSE_SLOWLY(http_histogram, carrier_id); \ | |
| 30 UMA_HISTOGRAM_COUNTS(http_histogram "_Total", 1); \ | |
| 31 }\ | |
| 32 } while (0) | |
| 33 | |
| 34 namespace data_reduction_proxy { | |
| 35 | |
| 36 // static | |
| 37 void DataReductionProxyTamperDetection::DetectAndReport( | |
| 38 const net::HttpResponseHeaders* headers, | |
| 39 const bool is_secure_scheme) { | |
| 40 DCHECK(headers); | |
| 41 if (!headers) | |
| 42 return; | |
| 43 | |
| 44 // If the fingerprint of the Chrome-Proxy header is absent, abort tamper | |
| 45 // detection. | |
| 46 std::string chrome_proxy_fingerprint; | |
| 47 if (!GetDataReductionProxyActionValue( | |
| 48 headers, | |
| 49 kChromeProxyActionFingerprintChromeProxy, | |
| 50 &chrome_proxy_fingerprint)) | |
| 51 return; | |
| 52 | |
| 53 // Gets the Chrome-Proxy header values. | |
| 54 std::vector<std::string> chrome_proxy_header_values = | |
| 55 GetHeaderValues(headers, "Chrome-Proxy"); | |
| 56 | |
| 57 // Removes header's fingerprint for generating the fingerprint of received | |
| 58 // Chrome-Proxy header later. | |
| 59 RemoveChromeProxyFingerprint(&chrome_proxy_header_values); | |
| 60 | |
| 61 // Get carrier ID. | |
| 62 unsigned carrier_id = 0; | |
| 63 #if defined(OS_ANDROID) | |
| 64 base::StringToUint(net::android::GetTelephonyNetworkOperator(), &carrier_id); | |
| 65 #endif | |
| 66 | |
| 67 DataReductionProxyTamperDetection tamper_detection( | |
| 68 headers, | |
| 69 is_secure_scheme, | |
| 70 carrier_id, | |
| 71 &chrome_proxy_header_values); | |
| 72 | |
| 73 // Checks if the Chrome-Proxy header has been tampered with. | |
| 74 if (tamper_detection.IsChromeProxyHeaderTampered(chrome_proxy_fingerprint)) { | |
| 75 tamper_detection.ReportChromeProxyHeaderTamperedUMA(); | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 // Since the Chrome-Proxy header has not been tampered with, reports the | |
| 80 // number of responses that other fingerprints will be checked. | |
| 81 REPORT_TAMPER_DETECTION_UMA( | |
| 82 is_secure_scheme, | |
| 83 "DataReductionProxy.HTTPSHeaderTamperDetection", | |
| 84 "DataReductionProxy.HTTPHeaderTamperDetection", | |
| 85 carrier_id); | |
| 86 | |
| 87 std::map<std::string, FingerprintCode>::iterator i; | |
| 88 for (i = tamper_detection.fingerprint_name_code_map_.begin(); | |
| 89 i != tamper_detection.fingerprint_name_code_map_.end(); ++i) { | |
| 90 std::string fingerprint; | |
| 91 if (!GetDataReductionProxyActionValue( | |
| 92 headers, i->first, &fingerprint)) { | |
| 93 continue; | |
| 94 } | |
| 95 | |
| 96 switch (i->second) { | |
| 97 case VIA: | |
| 98 bool has_chrome_proxy_via_header; | |
| 99 if (tamper_detection.IsViaHeaderTampered( | |
| 100 fingerprint, &has_chrome_proxy_via_header)) | |
| 101 tamper_detection.ReportViaHeaderTamperedUMA( | |
| 102 has_chrome_proxy_via_header); | |
| 103 break; | |
| 104 case OTHERHEADERS: | |
| 105 if (tamper_detection.AreOtherHeadersTampered(fingerprint)) | |
| 106 tamper_detection.ReportOtherHeadersTamperedUMA(); | |
| 107 break; | |
| 108 case CONTENTLENGTH: | |
| 109 if (tamper_detection.IsContentLengthHeaderTampered(fingerprint)) | |
| 110 tamper_detection.ReportContentLengthHeaderTamperedUMA(); | |
| 111 break; | |
| 112 default: | |
| 113 NOTREACHED(); | |
| 114 break; | |
| 115 } | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 // Constructor initializes the map of fingerprint name to code. | |
| 120 DataReductionProxyTamperDetection::DataReductionProxyTamperDetection( | |
| 121 const net::HttpResponseHeaders* headers, | |
| 122 const bool is_secure, | |
| 123 const unsigned carrier_id, | |
| 124 std::vector<std::string>* values) | |
| 125 : response_headers_(headers), | |
| 126 is_secure_scheme_(is_secure), | |
| 127 carrier_id_(carrier_id), | |
| 128 clean_chrome_proxy_header_values_(values) { | |
| 129 DCHECK(headers); | |
| 130 fingerprint_name_code_map_ = std::map<std::string, FingerprintCode>(); | |
| 131 fingerprint_name_code_map_ | |
| 132 [kChromeProxyActionFingerprintVia] = VIA; | |
| 133 fingerprint_name_code_map_ | |
| 134 [kChromeProxyActionFingerprintOtherHeaders] = OTHERHEADERS; | |
| 135 fingerprint_name_code_map_ | |
| 136 [kChromeProxyActionFingerprintContentLength] = CONTENTLENGTH; | |
| 137 }; | |
| 138 | |
| 139 DataReductionProxyTamperDetection::~DataReductionProxyTamperDetection() {}; | |
| 140 | |
| 141 // Checks whether the Chrome-Proxy header has been tampered with. |fingerprint| | |
| 142 // is the fingerprint received from the data reduction proxy, which is Base64 | |
| 143 // encoded. Decodes it first. Then calculates the fingerprint of received | |
| 144 // Chrome-Proxy header, and compares the two to see whether they are equal or | |
| 145 // not. Note that |clean_chrome_proxy_header_values_| holds the values of the | |
| 146 // Chrome-Proxy header with its own fingerprint removed, so it's the correct | |
| 147 // values to calculate fingerprint of received Chrome-Proxy header. | |
| 148 bool DataReductionProxyTamperDetection::IsChromeProxyHeaderTampered( | |
| 149 const std::string& fingerprint) const { | |
| 150 std::string received_fingerprint; | |
| 151 if (!base::Base64Decode(fingerprint, &received_fingerprint)) | |
| 152 return true; | |
| 153 // Calculates the MD5 hash value of Chrome-Proxy. | |
| 154 std::string actual_fingerprint = GetMD5( | |
| 155 ValuesToSortedString(clean_chrome_proxy_header_values_)); | |
| 156 | |
| 157 return received_fingerprint != actual_fingerprint; | |
| 158 } | |
| 159 | |
| 160 void DataReductionProxyTamperDetection::ReportChromeProxyHeaderTamperedUMA() | |
| 161 const { | |
| 162 REPORT_TAMPER_DETECTION_UMA( | |
| 163 is_secure_scheme_, | |
| 164 "DataReductionProxy.HTTPSHeaderTampered_ChromeProxy", | |
| 165 "DataReductionProxy.HTTPHeaderTampered_ChromeProxy", | |
| 166 carrier_id_); | |
| 167 } | |
| 168 | |
| 169 // Checks whether there are other proxies/middleboxes' name after the data | |
| 170 // reduction proxy's name in Via header. |has_chrome_proxy_via_header| marks | |
| 171 // that whether the data reduction proxy's Via header occurs or not. | |
| 172 bool DataReductionProxyTamperDetection::IsViaHeaderTampered( | |
| 173 const std::string& fingerprint, bool* has_chrome_proxy_via_header) const { | |
| 174 bool has_intermediary; | |
| 175 *has_chrome_proxy_via_header = HasDataReductionProxyViaHeader( | |
| 176 response_headers_, | |
| 177 &has_intermediary); | |
| 178 return !has_intermediary; | |
| 179 } | |
| 180 | |
| 181 void DataReductionProxyTamperDetection::ReportViaHeaderTamperedUMA( | |
| 182 bool has_chrome_proxy) const { | |
| 183 // The Via header of the data reduction proxy is missing. | |
| 184 if (!has_chrome_proxy) { | |
| 185 REPORT_TAMPER_DETECTION_UMA( | |
| 186 is_secure_scheme_, | |
| 187 "DataReductionProxy.HTTPSHeaderTampered_Via_Missing", | |
| 188 "DataReductionProxy.HTTPHeaderTampered_Via_Missing", | |
| 189 carrier_id_); | |
| 190 return; | |
| 191 } | |
| 192 | |
| 193 REPORT_TAMPER_DETECTION_UMA( | |
| 194 is_secure_scheme_, | |
| 195 "DataReductionProxy.HTTPSHeaderTampered_Via", | |
| 196 "DataReductionProxy.HTTPHeaderTampered_Via", | |
| 197 carrier_id_); | |
| 198 } | |
| 199 | |
| 200 // Checks whether values of a predefined list of headers have been modified. At | |
| 201 // the data reduction proxy side, it constructs a canonical representation of | |
| 202 // values of a list headers. The fingerprint is constructed as follows: | |
| 203 // 1) for each header, gets the string representation of its values (same to | |
| 204 // ValuesToSortedString); | |
| 205 // 2) concatenates all header's string representation with a ";" delimiter, | |
| 206 // respect to the order of the header list; | |
| 207 // 3) calculates the MD5 hash value of above concatenated string; | |
| 208 // 4) appends the header names to the fingerprint, with a delimiter "|". | |
| 209 // The constructed fingerprint looks like: | |
| 210 // [hashed_fingerprint]|header_name1|header_namer2:... | |
| 211 // | |
| 212 // To check whether such fingerprint matches the response that the Chromium | |
| 213 // client receives, the Chromium client firstly extracts the header names. For | |
| 214 // each header, gets its string representation (by ValuesToSortedString), | |
| 215 // concatenates them and calculates the MD5 hash value. Compares such hash | |
| 216 // value to the fingerprint received from the data reduction proxy. | |
| 217 bool DataReductionProxyTamperDetection::AreOtherHeadersTampered( | |
| 218 const std::string& fingerprint) const { | |
| 219 std::string received_fingerprint; | |
| 220 DCHECK(fingerprint.size()); | |
| 221 | |
| 222 // "|" delimiter would not occur in base64 as well as header names. | |
| 223 net::HttpUtil::ValuesIterator it(fingerprint.begin(), | |
| 224 fingerprint.end(), '|'); | |
| 225 | |
| 226 // The first value from fingerprint is the base64 encoded fingerprint; the | |
| 227 // following values are the header names included in fingerprint calculation. | |
| 228 // Make sure there is [base64fingerprint] and it can be decoded. | |
| 229 if (!(it.GetNext() && | |
| 230 base::Base64Decode(it.value(), &received_fingerprint))) { | |
| 231 NOTREACHED(); | |
| 232 return true; | |
| 233 } | |
| 234 | |
| 235 std::string header_values; | |
| 236 // Enumerates the list of headers. | |
| 237 while (it.GetNext()) { | |
| 238 // Gets values of one header. | |
| 239 std::vector<std::string> response_header_values = | |
| 240 GetHeaderValues(response_headers_, it.value()); | |
| 241 // Sorts the values and concatenate them, with delimiter ";". ";" would not | |
| 242 // occur in header values, | |
| 243 header_values += ValuesToSortedString(&response_header_values) + ";"; | |
| 244 } | |
| 245 | |
| 246 // Calculates the MD5 hash of the concatenated string. | |
| 247 std::string actual_fingerprint = GetMD5(header_values); | |
| 248 | |
| 249 return received_fingerprint != actual_fingerprint; | |
| 250 } | |
| 251 | |
| 252 void DataReductionProxyTamperDetection::ReportOtherHeadersTamperedUMA() const { | |
| 253 REPORT_TAMPER_DETECTION_UMA( | |
| 254 is_secure_scheme_, | |
| 255 "DataReductionProxy.HTTPSHeaderTampered_OtherHeaders", | |
| 256 "DataReductionProxy.HTTPHeaderTampered_OtherHeaders", | |
| 257 carrier_id_); | |
| 258 } | |
| 259 | |
| 260 // Checks whether the Content-Length value is different from what the data | |
| 261 // reduction proxy sends. Reports it as modified only if Content-Length can be | |
| 262 // decoded as an integer at both ends and such two numbers are not equal. | |
| 263 bool DataReductionProxyTamperDetection::IsContentLengthHeaderTampered( | |
| 264 const std::string& fingerprint) const { | |
| 265 int received_content_length_fingerprint, actual_content_length; | |
| 266 // If Content-Length value from data reduction proxy does not exist or it | |
| 267 // cannot be converted to an integer, abort. | |
| 268 if (base::StringToInt(fingerprint, &received_content_length_fingerprint)) { | |
| 269 std::string actual_content_length_string; | |
| 270 // If there is no Content-Length header received, abort. | |
| 271 if (response_headers_->GetNormalizedHeader("Content-Length", | |
| 272 &actual_content_length_string)) { | |
| 273 // If the Content-Length value cannot be converted to integer, abort. | |
| 274 if (!base::StringToInt(actual_content_length_string, | |
| 275 &actual_content_length)) { | |
| 276 return false; | |
| 277 } | |
| 278 | |
| 279 return received_content_length_fingerprint != actual_content_length; | |
| 280 } | |
| 281 } | |
| 282 return false; | |
| 283 } | |
| 284 | |
| 285 void DataReductionProxyTamperDetection::ReportContentLengthHeaderTamperedUMA() | |
| 286 const { | |
| 287 // Gets MIME type of the response and reports to UMA histograms separately. | |
| 288 // Divides MIME types into 4 groups: JavaScript, CSS, Images, and others. | |
| 289 REPORT_TAMPER_DETECTION_UMA( | |
| 290 is_secure_scheme_, | |
| 291 "DataReductionProxy.HTTPSHeaderTampered_ContentLength", | |
| 292 "DataReductionProxy.HTTPHeaderTampered_ContentLength", | |
| 293 carrier_id_); | |
| 294 | |
| 295 // Gets MIME type. | |
| 296 std::string mime_type; | |
| 297 response_headers_->GetMimeType(&mime_type); | |
| 298 | |
| 299 // Reports tampered JavaScript. | |
| 300 if (mime_type.compare("text/javascript") == 0 || | |
| 301 mime_type.compare("application/x-javascript") == 0 || | |
| 302 mime_type.compare("application/javascript") == 0) { | |
| 303 REPORT_TAMPER_DETECTION_UMA( | |
| 304 is_secure_scheme_, | |
| 305 "DataReductionProxy.HTTPSHeaderTampered_ContentLength_JS", | |
| 306 "DataReductionProxy.HTTPHeaderTampered_ContentLength_JS", | |
| 307 carrier_id_); | |
| 308 } | |
| 309 // Reports tampered CSSs. | |
| 310 else if (mime_type.compare("text/css") == 0) { | |
| 311 REPORT_TAMPER_DETECTION_UMA( | |
| 312 is_secure_scheme_, | |
| 313 "DataReductionProxy.HTTPSHeaderTampered_ContentLength_CSS", | |
| 314 "DataReductionProxy.HTTPHeaderTampered_ContentLength_CSS", | |
| 315 carrier_id_); | |
| 316 } | |
| 317 // Reports tampered images. | |
| 318 else if (mime_type.find("image/") == 0) { | |
| 319 REPORT_TAMPER_DETECTION_UMA( | |
| 320 is_secure_scheme_, | |
| 321 "DataReductionProxy.HTTPSHeaderTampered_ContentLength_Image", | |
| 322 "DataReductionProxy.HTTPHeaderTampered_ContentLength_Image", | |
| 323 carrier_id_); | |
| 324 } | |
| 325 // Reports tampered other MIME types. | |
| 326 else { | |
| 327 REPORT_TAMPER_DETECTION_UMA( | |
| 328 is_secure_scheme_, | |
| 329 "DataReductionProxy.HTTPSHeaderTampered_ContentLength_Other", | |
| 330 "DataReductionProxy.HTTPHeaderTampered_ContentLength_Other", | |
| 331 carrier_id_); | |
| 332 } | |
| 333 } | |
| 334 | |
| 335 DataReductionProxyTamperDetection::FingerprintCode | |
| 336 DataReductionProxyTamperDetection::GetFingerprintCode( | |
| 337 const std::string& fingerprint_name) { | |
| 338 std::map<std::string, FingerprintCode>::iterator it = | |
| 339 fingerprint_name_code_map_.find(fingerprint_name); | |
| 340 | |
| 341 if (it != fingerprint_name_code_map_.end()) | |
| 342 return it->second; | |
| 343 return NONEXIST; | |
| 344 } | |
| 345 | |
| 346 // Removes the Chrome-Proxy header's fingerprint (action name | |
| 347 // |kFingerprintChromeProxy|) from its values vector. | |
| 348 void DataReductionProxyTamperDetection::RemoveChromeProxyFingerprint( | |
| 349 std::vector<std::string>* values) { | |
| 350 DCHECK(values); | |
| 351 if (!values) return; | |
| 352 | |
| 353 std::string chrome_proxy_fingerprint_prefix = std::string( | |
| 354 kChromeProxyActionFingerprintChromeProxy) + "="; | |
| 355 | |
| 356 for (size_t i = 0; i < values->size(); ++i) { | |
| 357 if ((*values)[i].find(chrome_proxy_fingerprint_prefix) == 0) { | |
| 358 values->erase(values->begin() + i); | |
| 359 break; | |
| 360 } | |
| 361 } | |
| 362 } | |
| 363 | |
| 364 // We construct a canonical representation of the header so that reordered | |
| 365 // header values will produce the same fingerprint. The fingerprint is | |
| 366 // constructed as follows: | |
| 367 // 1) sorts the values; | |
| 368 // 2) concatenates sorted values with a "," delimiter. | |
| 369 std::string DataReductionProxyTamperDetection::ValuesToSortedString( | |
| 370 std::vector<std::string>* values) { | |
| 371 std::string concatenated_values; | |
| 372 DCHECK(values); | |
| 373 if (!values) return ""; | |
| 374 | |
| 375 std::sort(values->begin(), values->end()); | |
| 376 for (size_t i = 0; i < values->size(); ++i) { | |
| 377 // Concatenates with delimiter ",". | |
| 378 concatenated_values += (*values)[i] + ","; | |
| 379 } | |
| 380 return concatenated_values; | |
| 381 } | |
| 382 | |
| 383 std::string DataReductionProxyTamperDetection::GetMD5( | |
| 384 const std::string &input) { | |
| 385 base::MD5Digest digest; | |
| 386 base::MD5Sum(input.c_str(), input.size(), &digest); | |
| 387 return std::string((char*)digest.a, ARRAYSIZE_UNSAFE(digest.a)); | |
| 388 } | |
| 389 | |
| 390 std::vector<std::string> DataReductionProxyTamperDetection::GetHeaderValues( | |
|
bolian
2014/07/21 23:43:04
Should this belong to components/data_reduction_pr
xingx
2014/07/22 17:10:59
I think it would only be used in tamper detection:
| |
| 391 const net::HttpResponseHeaders* headers, const std::string& header_name) { | |
| 392 std::vector<std::string> values; | |
| 393 std::string value; | |
| 394 void* iter = NULL; | |
| 395 while (headers->EnumerateHeader(&iter, header_name, &value)) { | |
| 396 values.push_back(value); | |
| 397 } | |
| 398 return values; | |
| 399 } | |
| 400 | |
| 401 } // namespace data_reduction_proxy | |
| OLD | NEW |