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.
5 // This file implements the tamper detection logic, which detects whether
6 // there are middleboxes and whether they are tampering with the response
7 // which may break correct communication and data transfer between the Chromium
8 // client and the data reduction proxy.
10 // At a high level, the tamper detection process works in two steps:
11 // 1. The data reduction proxy selects a fraction of responses to analyze,
12 // generates a series of fingerprints for each, and appends them to the
13 // Chrome-Proxy response headers;
14 // 2. The client re-generate the fingerprints using the same method as the
15 // proxy, compares them to the fingerprints in the response, and generates
16 // UMA. A response is considered to have been tampered with if the
17 // fingerprints do not match.
19 // Four fingerprints are generated by the data reduction proxy:
20 // 1. Fingerprint of the Chrome-Proxy header, which is designed to check
21 // whether the Chrome-Proxy header has been modified or not;
22 // 2. Fingerprint of the Via header, which is designed to check whether there
23 // are middleboxes between the Chromium client and the data reduction proxy;
24 // 3. Fingerprint of a list of headers, which is designed to check whether the
25 // values of a list of headers (list is defined by the data reduction proxy)
26 // have been modified or deleted;
27 // 4. Fingerprint of the Content-Length header, which is designed to check
28 // whether the response body has been modified or not (the code assumes that
29 // different Content-Length values indicate different response bodies).
31 // On the client side, the fingerprint of the Chrome-Proxy header will be
32 // checked first. If the fingerprint indicates that the Chrome-Proxy header has
33 // not been modified, then the other fingerprints will be considered to be
34 // reliable and will be checked next; if not, then it's possible that the other
35 // fingerprints have been tampered with and thus they will not be checked.
36 // If middlebox removes all the fingerprints then such tampering will not be
39 // Detected tampering information will be reported to UMA. In general, for each
40 // fingerprint, the client reports the number of responses that have been
41 // tampered with for different carriers. For the fingerprint of the
42 // Content-Length header, which indicates whether the response body has been
43 // modified or not, the reports of tampering are separated by MIME type of the
46 #ifndef COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_TAMPER_DETECTION_H_
47 #define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_TAMPER_DETECTION_H_
53 #include "net/proxy/proxy_service.h"
56 class HttpResponseHeaders
;
59 namespace data_reduction_proxy
{
61 // Detects if the response sent by the data reduction proxy has been modified
62 // by intermediaries on the Web.
63 class DataReductionProxyTamperDetection
{
65 // Checks if the response contains tamper detection fingerprints added by the
66 // data reduction proxy, and determines if the response had been tampered
67 // with if so. Results are reported to UMA. HTTP and HTTPS traffic are
68 // reported separately, specified by |scheme_is_https|. Returns true if
69 // the response has been tampered with.
70 static bool DetectAndReport(const net::HttpResponseHeaders
* headers
,
71 bool scheme_is_https
);
73 // Tamper detection checks |response_headers|. Histogram events are reported
74 // by |carrier_id|; |scheme_is_https| determines which histogram to report
75 // (HTTP and HTTPS are reported separately).
76 DataReductionProxyTamperDetection(
77 const net::HttpResponseHeaders
* response_headers
,
81 virtual ~DataReductionProxyTamperDetection();
84 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyTamperDetectionTest
,
85 TestFingerprintCommon
);
86 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyTamperDetectionTest
,
88 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyTamperDetectionTest
,
90 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyTamperDetectionTest
,
92 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyTamperDetectionTest
,
94 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyTamperDetectionTest
,
96 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyTamperDetectionTest
,
97 ValuesToSortedString
);
98 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyTamperDetectionTest
,
100 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyTamperDetectionTest
,
103 // Returns the result of validating Chrome-Proxy header.
104 bool ValidateChromeProxyHeader(const std::string
& fingerprint
) const;
106 // Reports UMA for tampering of the Chrome-Proxy header.
107 void ReportUMAforChromeProxyHeaderValidation() const;
109 // Returns the result of validating the Via header. |has_chrome_proxy|
110 // indicates that the data reduction proxy's Via header occurs or not.
111 bool ValidateViaHeader(const std::string
& fingerprint
,
112 bool* has_chrome_proxy_via_header
) const;
114 // Reports UMA for tampering of the Via header.
115 void ReportUMAforViaHeaderValidation(bool has_chrome_proxy_via_header
) const;
117 // Returns the result of validating a list of headers.
118 bool ValidateOtherHeaders(const std::string
& fingerprint
) const;
120 // Reports UMA for tampering of values of the list of headers.
121 void ReportUMAforOtherHeadersValidation() const;
123 // Returns the result of validating the Content-Length header.
124 bool ValidateContentLengthHeader(const std::string
& fingerprint
) const;
126 // Reports UMA for tampering of the Content-Length header.
127 void ReportUMAforContentLengthHeaderValidation() const;
129 // Returns a string representation of |values|.
130 static std::string
ValuesToSortedString(std::vector
<std::string
>* values
);
132 // Returns raw MD5 hash value for a given string |input|. It is different to
133 // base::MD5String which is base16 encoded.
134 static void GetMD5(const std::string
& input
, std::string
* output
);
136 // Returns all the values of |header_name| of the response |headers| as a
137 // vector. This function is used for values that need to be sorted later.
138 static std::vector
<std::string
> GetHeaderValues(
139 const net::HttpResponseHeaders
* headers
,
140 const std::string
& header_name
);
142 // Pointer to response headers.
143 const net::HttpResponseHeaders
* response_headers_
;
145 // If true, the connection to the data reduction proxy is over HTTPS;
146 const bool scheme_is_https_
;
148 // Carrier ID: the numeric name of the current registered operator.
149 const unsigned carrier_id_
;
151 DISALLOW_COPY_AND_ASSIGN(DataReductionProxyTamperDetection
);
154 } // namespace data_reduction_proxy
155 #endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_TAMPER_DETECTION_H_