1 // Copyright (c) 2012 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 #include "chrome/renderer/security_filter_peer.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/stringprintf.h"
11 #include "chrome/grit/generated_resources.h"
12 #include "content/public/child/fixed_received_data.h"
13 #include "net/base/net_errors.h"
14 #include "net/http/http_response_headers.h"
15 #include "ui/base/l10n/l10n_util.h"
17 SecurityFilterPeer::SecurityFilterPeer(content::RequestPeer
* peer
)
18 : original_peer_(peer
) {
21 SecurityFilterPeer::~SecurityFilterPeer() {
26 SecurityFilterPeer::CreateSecurityFilterPeerForDeniedRequest(
27 content::ResourceType resource_type
,
28 content::RequestPeer
* peer
,
30 // Create a filter for SSL and CERT errors.
32 case net::ERR_SSL_PROTOCOL_ERROR
:
33 case net::ERR_CERT_COMMON_NAME_INVALID
:
34 case net::ERR_CERT_DATE_INVALID
:
35 case net::ERR_CERT_AUTHORITY_INVALID
:
36 case net::ERR_CERT_CONTAINS_ERRORS
:
37 case net::ERR_CERT_NO_REVOCATION_MECHANISM
:
38 case net::ERR_CERT_UNABLE_TO_CHECK_REVOCATION
:
39 case net::ERR_CERT_REVOKED
:
40 case net::ERR_CERT_INVALID
:
41 case net::ERR_CERT_WEAK_SIGNATURE_ALGORITHM
:
42 case net::ERR_CERT_WEAK_KEY
:
43 case net::ERR_CERT_NAME_CONSTRAINT_VIOLATION
:
44 case net::ERR_INSECURE_RESPONSE
:
45 case net::ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN
:
46 if (content::IsResourceTypeFrame(resource_type
))
47 return CreateSecurityFilterPeerForFrame(peer
, os_error
);
48 // Any other content is entirely filtered-out.
49 return new ReplaceContentPeer(peer
, std::string(), std::string());
51 // For other errors, we use our normal error handling.
57 SecurityFilterPeer
* SecurityFilterPeer::CreateSecurityFilterPeerForFrame(
58 content::RequestPeer
* peer
,
60 // TODO(jcampan): use a different message when getting a phishing/malware
62 std::string html
= base::StringPrintf(
63 "<html><meta charset='UTF-8'>"
64 "<body style='background-color:#990000;color:white;'>"
66 l10n_util::GetStringUTF8(IDS_UNSAFE_FRAME_MESSAGE
).c_str());
67 return new ReplaceContentPeer(peer
, "text/html", html
);
70 void SecurityFilterPeer::OnUploadProgress(uint64 position
, uint64 size
) {
71 original_peer_
->OnUploadProgress(position
, size
);
74 bool SecurityFilterPeer::OnReceivedRedirect(
75 const net::RedirectInfo
& redirect_info
,
76 const content::ResourceResponseInfo
& info
) {
81 void SecurityFilterPeer::OnReceivedResponse(
82 const content::ResourceResponseInfo
& info
) {
86 void SecurityFilterPeer::OnReceivedData(scoped_ptr
<ReceivedData
> data
) {
90 void SecurityFilterPeer::OnCompletedRequest(
92 bool was_ignored_by_handler
,
93 bool stale_copy_in_cache
,
94 const std::string
& security_info
,
95 const base::TimeTicks
& completion_time
,
96 int64 total_transfer_size
) {
101 void ProcessResponseInfo(const content::ResourceResponseInfo
& info_in
,
102 content::ResourceResponseInfo
* info_out
,
103 const std::string
& mime_type
) {
106 info_out
->mime_type
= mime_type
;
107 // Let's create our own HTTP headers.
108 std::string raw_headers
;
109 raw_headers
.append("HTTP/1.1 200 OK");
110 raw_headers
.push_back('\0');
111 // Don't cache the data we are serving, it is not the real data for that URL
112 // (if the filtered resource were to make it into the WebCore cache, then the
113 // same URL loaded in a safe scenario would still return the filtered
115 raw_headers
.append("cache-control: no-cache");
116 raw_headers
.push_back('\0');
117 if (!mime_type
.empty()) {
118 raw_headers
.append("content-type: ");
119 raw_headers
.append(mime_type
);
120 raw_headers
.push_back('\0');
122 raw_headers
.push_back('\0');
123 net::HttpResponseHeaders
* new_headers
=
124 new net::HttpResponseHeaders(raw_headers
);
125 info_out
->headers
= new_headers
;
128 ////////////////////////////////////////////////////////////////////////////////
131 BufferedPeer::BufferedPeer(content::RequestPeer
* peer
,
132 const std::string
& mime_type
)
133 : SecurityFilterPeer(peer
), mime_type_(mime_type
) {}
135 BufferedPeer::~BufferedPeer() {
138 void BufferedPeer::OnReceivedResponse(
139 const content::ResourceResponseInfo
& info
) {
140 ProcessResponseInfo(info
, &response_info_
, mime_type_
);
143 void BufferedPeer::OnReceivedData(scoped_ptr
<ReceivedData
> data
) {
144 data_
.append(data
->payload(), data
->length());
147 void BufferedPeer::OnCompletedRequest(int error_code
,
148 bool was_ignored_by_handler
,
149 bool stale_copy_in_cache
,
150 const std::string
& security_info
,
151 const base::TimeTicks
& completion_time
,
152 int64 total_transfer_size
) {
153 // Make sure we delete ourselves at the end of this call.
154 scoped_ptr
<BufferedPeer
> this_deleter(this);
156 // Give sub-classes a chance at altering the data.
157 if (error_code
!= net::OK
|| !DataReady()) {
158 // Pretend we failed to load the resource.
159 original_peer_
->OnReceivedResponse(response_info_
);
160 original_peer_
->OnCompletedRequest(net::ERR_ABORTED
, false,
162 security_info
, completion_time
,
163 total_transfer_size
);
167 original_peer_
->OnReceivedResponse(response_info_
);
169 original_peer_
->OnReceivedData(make_scoped_ptr(
170 new content::FixedReceivedData(data_
.data(), data_
.size(), -1)));
171 original_peer_
->OnCompletedRequest(error_code
, was_ignored_by_handler
,
172 stale_copy_in_cache
, security_info
,
173 completion_time
, total_transfer_size
);
176 ////////////////////////////////////////////////////////////////////////////////
177 // ReplaceContentPeer
179 ReplaceContentPeer::ReplaceContentPeer(content::RequestPeer
* peer
,
180 const std::string
& mime_type
,
181 const std::string
& data
)
182 : SecurityFilterPeer(peer
),
183 mime_type_(mime_type
),
186 ReplaceContentPeer::~ReplaceContentPeer() {
189 void ReplaceContentPeer::OnReceivedResponse(
190 const content::ResourceResponseInfo
& info
) {
191 // Ignore this, we'll serve some alternate content in OnCompletedRequest.
194 void ReplaceContentPeer::OnReceivedData(scoped_ptr
<ReceivedData
> data
) {
195 // Ignore this, we'll serve some alternate content in OnCompletedRequest.
198 void ReplaceContentPeer::OnCompletedRequest(
200 bool was_ignored_by_handler
,
201 bool stale_copy_in_cache
,
202 const std::string
& security_info
,
203 const base::TimeTicks
& completion_time
,
204 int64 total_transfer_size
) {
205 content::ResourceResponseInfo info
;
206 ProcessResponseInfo(info
, &info
, mime_type_
);
207 info
.security_info
= security_info
;
208 info
.content_length
= static_cast<int>(data_
.size());
209 original_peer_
->OnReceivedResponse(info
);
211 original_peer_
->OnReceivedData(make_scoped_ptr(
212 new content::FixedReceivedData(data_
.data(), data_
.size(), -1)));
213 original_peer_
->OnCompletedRequest(net::OK
,
218 total_transfer_size
);
220 // The request processing is complete, we must delete ourselves.