Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(629)

Side by Side Diff: components/data_reduction_proxy/browser/data_reduction_proxy_tamper_detect.cc

Issue 338483002: Chrome Participated Tamper Detect (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
179 if (*has_chrome_proxy_via_header)
180 return !has_intermediary;
181 return false;
182 }
183
184 void DataReductionProxyTamperDetection::ReportViaHeaderTamperedUMA(
185 bool has_chrome_proxy) const {
186 // The Via header of the data reduction proxy is missing.
187 if (!has_chrome_proxy) {
188 REPORT_TAMPER_DETECTION_UMA(
189 is_secure_scheme_,
190 "DataReductionProxy.HTTPSHeaderTampered_Via_Missing",
191 "DataReductionProxy.HTTPHeaderTampered_Via_Missing",
192 carrier_id_);
193 return;
194 }
195
196 REPORT_TAMPER_DETECTION_UMA(
197 is_secure_scheme_,
198 "DataReductionProxy.HTTPSHeaderTampered_Via",
199 "DataReductionProxy.HTTPHeaderTampered_Via",
200 carrier_id_);
201 }
202
203 // Checks whether values of a predefined list of headers have been modified. At
204 // the data reduction proxy side, it constructs a canonical representation of
205 // values of a list headers. The fingerprint is constructed as follows:
206 // 1) for each header, gets the string representation of its values (same to
207 // ValuesToSortedString);
208 // 2) concatenates all header's string representation with a ";" delimiter,
209 // respect to the order of the header list;
210 // 3) calculates the MD5 hash value of above concatenated string;
211 // 4) appends the header names to the fingerprint, with a delimiter "|".
212 // The constructed fingerprint looks like:
213 // [hashed_fingerprint]|header_name1|header_namer2:...
214 //
215 // To check whether such fingerprint matches the response that the Chromium
216 // client receives, the Chromium client firstly extracts the header names. For
217 // each header, gets its string representation (by ValuesToSortedString),
218 // concatenates them and calculates the MD5 hash value. Compares such hash
219 // value to the fingerprint received from the data reduction proxy.
220 bool DataReductionProxyTamperDetection::AreOtherHeadersTampered(
221 const std::string& fingerprint) const {
222 std::string received_fingerprint;
223 DCHECK(fingerprint.size());
224
225 // "|" delimiter would not occur in base64 as well as header names.
226 net::HttpUtil::ValuesIterator it(fingerprint.begin(),
227 fingerprint.end(), '|');
228
229 // The first value from fingerprint is the base64 encoded fingerprint; the
230 // following values are the header names included in fingerprint calculation.
231 // Make sure there is [base64fingerprint] and it can be decoded.
232 if (!(it.GetNext() &&
233 base::Base64Decode(it.value(), &received_fingerprint))) {
234 NOTREACHED();
235 return true;
236 }
237
238 std::string header_values;
239 // Enumerates the list of headers.
240 while (it.GetNext()) {
241 // Gets values of one header.
242 std::vector<std::string> response_header_values =
243 GetHeaderValues(response_headers_, it.value());
244 // Sorts the values and concatenate them, with delimiter ";". ";" would not
245 // occur in header values,
246 header_values += ValuesToSortedString(&response_header_values) + ";";
247 }
248
249 // Calculates the MD5 hash of the concatenated string.
250 std::string actual_fingerprint = GetMD5(header_values);
251
252 return received_fingerprint != actual_fingerprint;
253 }
254
255 void DataReductionProxyTamperDetection::ReportOtherHeadersTamperedUMA() const {
256 REPORT_TAMPER_DETECTION_UMA(
257 is_secure_scheme_,
258 "DataReductionProxy.HTTPSHeaderTampered_OtherHeaders",
259 "DataReductionProxy.HTTPHeaderTampered_OtherHeaders",
260 carrier_id_);
261 }
262
263 // Checks whether the Content-Length value is different from what the data
264 // reduction proxy sends. Reports it as modified only if Content-Length can be
265 // decoded as an integer at both ends and such two numbers are not equal.
266 bool DataReductionProxyTamperDetection::IsContentLengthHeaderTampered(
267 const std::string& fingerprint) const {
268 int received_content_length_fingerprint, actual_content_length;
269 // If Content-Length value from data reduction proxy does not exist or it
270 // cannot be converted to an integer, abort.
271 if (base::StringToInt(fingerprint, &received_content_length_fingerprint)) {
272 std::string actual_content_length_string;
273 // If there is no Content-Length header received, abort.
274 if (response_headers_->GetNormalizedHeader("Content-Length",
275 &actual_content_length_string)) {
276 // If the Content-Length value cannot be converted to integer, abort.
277 if (!base::StringToInt(actual_content_length_string,
278 &actual_content_length)) {
279 return false;
280 }
281
282 return received_content_length_fingerprint != actual_content_length;
283 }
284 }
285 return false;
286 }
287
288 void DataReductionProxyTamperDetection::ReportContentLengthHeaderTamperedUMA()
289 const {
290 // Gets MIME type of the response and reports to UMA histograms separately.
291 // Divides MIME types into 4 groups: JavaScript, CSS, Images, and others.
292 REPORT_TAMPER_DETECTION_UMA(
293 is_secure_scheme_,
294 "DataReductionProxy.HTTPSHeaderTampered_ContentLength",
295 "DataReductionProxy.HTTPHeaderTampered_ContentLength",
296 carrier_id_);
297
298 // Gets MIME type.
299 std::string mime_type;
300 response_headers_->GetMimeType(&mime_type);
301
302 // Reports tampered JavaScript.
303 if (mime_type.compare("text/javascript") == 0 ||
304 mime_type.compare("application/x-javascript") == 0 ||
305 mime_type.compare("application/javascript") == 0) {
306 REPORT_TAMPER_DETECTION_UMA(
307 is_secure_scheme_,
308 "DataReductionProxy.HTTPSHeaderTampered_ContentLength_JS",
309 "DataReductionProxy.HTTPHeaderTampered_ContentLength_JS",
310 carrier_id_);
311 }
312 // Reports tampered CSSs.
313 else if (mime_type.compare("text/css") == 0) {
314 REPORT_TAMPER_DETECTION_UMA(
315 is_secure_scheme_,
316 "DataReductionProxy.HTTPSHeaderTampered_ContentLength_CSS",
317 "DataReductionProxy.HTTPHeaderTampered_ContentLength_CSS",
318 carrier_id_);
319 }
320 // Reports tampered images.
321 else if (mime_type.find("image/") == 0) {
322 REPORT_TAMPER_DETECTION_UMA(
323 is_secure_scheme_,
324 "DataReductionProxy.HTTPSHeaderTampered_ContentLength_Image",
325 "DataReductionProxy.HTTPHeaderTampered_ContentLength_Image",
326 carrier_id_);
327 }
328 // Reports tampered other MIME types.
329 else {
330 REPORT_TAMPER_DETECTION_UMA(
331 is_secure_scheme_,
332 "DataReductionProxy.HTTPSHeaderTampered_ContentLength_Other",
333 "DataReductionProxy.HTTPHeaderTampered_ContentLength_Other",
334 carrier_id_);
335 }
336 }
337
338 DataReductionProxyTamperDetection::FingerprintCode
339 DataReductionProxyTamperDetection::GetFingerprintCode(
340 const std::string& fingerprint_name) {
341 std::map<std::string, FingerprintCode>::iterator it =
342 fingerprint_name_code_map_.find(fingerprint_name);
343
344 if (it != fingerprint_name_code_map_.end())
345 return it->second;
346 return NONEXIST;
347 }
348
349 // Removes the Chrome-Proxy header's fingerprint (action name
350 // |kFingerprintChromeProxy|) from its values vector.
351 void DataReductionProxyTamperDetection::RemoveChromeProxyFingerprint(
352 std::vector<std::string>* values) {
353 DCHECK(values);
354 if (!values) return;
355
356 std::string chrome_proxy_fingerprint_prefix = std::string(
357 kChromeProxyActionFingerprintChromeProxy) + "=";
358
359 for (size_t i = 0; i < values->size(); ++i) {
360 if ((*values)[i].find(chrome_proxy_fingerprint_prefix) == 0) {
361 values->erase(values->begin() + i);
362 break;
363 }
364 }
365 }
366
367 // We construct a canonical representation of the header so that reordered
368 // header values will produce the same fingerprint. The fingerprint is
369 // constructed as follows:
370 // 1) sorts the values;
371 // 2) concatenates sorted values with a "," delimiter.
372 std::string DataReductionProxyTamperDetection::ValuesToSortedString(
373 std::vector<std::string>* values) {
374 std::string concatenated_values;
375 DCHECK(values);
376 if (!values) return "";
377
378 std::sort(values->begin(), values->end());
379 for (size_t i = 0; i < values->size(); ++i) {
380 // Concatenates with delimiter ",".
381 concatenated_values += (*values)[i] + ",";
382 }
383 return concatenated_values;
384 }
385
386 std::string DataReductionProxyTamperDetection::GetMD5(
387 const std::string &input) {
388 base::MD5Digest digest;
389 base::MD5Sum(input.c_str(), input.size(), &digest);
390 return std::string((char*)digest.a, ARRAYSIZE_UNSAFE(digest.a));
391 }
392
393 std::vector<std::string> DataReductionProxyTamperDetection::GetHeaderValues(
394 const net::HttpResponseHeaders* headers, const std::string& header_name) {
395 std::vector<std::string> values;
396 std::string value;
397 void* iter = NULL;
398 while (headers->EnumerateHeader(&iter, header_name, &value)) {
399 values.push_back(value);
400 }
401 return values;
402 }
403
404 } // namespace data_reduction_proxy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698