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

Side by Side Diff: components/security_state/content/content_utils.cc

Issue 2448943002: Refactor SecurityStateModel/Clients for simplicity and reusability. (Closed)
Patch Set: update comments. Created 4 years, 1 month 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ssl/chrome_security_state_model_client.h" 5 #include "components/security_state/content/content_utils.h"
6 6
7 #include <string>
7 #include <vector> 8 #include <vector>
8 9
9 #include "base/command_line.h" 10 #include "base/memory/ptr_util.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/metrics/histogram_macros.h"
12 #include "base/strings/string16.h" 11 #include "base/strings/string16.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "build/build_config.h" 14 #include "components/strings/grit/components_chromium_strings.h"
15 #include "chrome/browser/browser_process.h" 15 #include "components/strings/grit/components_strings.h"
16 #include "chrome/browser/chromeos/policy/policy_cert_service.h"
17 #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
20 #include "chrome/browser/safe_browsing/ui_manager.h"
21 #include "chrome/grit/chromium_strings.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "content/public/browser/navigation_entry.h" 16 #include "content/public/browser/navigation_entry.h"
24 #include "content/public/browser/navigation_handle.h"
25 #include "content/public/browser/render_frame_host.h"
26 #include "content/public/browser/security_style_explanation.h" 17 #include "content/public/browser/security_style_explanation.h"
27 #include "content/public/browser/security_style_explanations.h" 18 #include "content/public/browser/security_style_explanations.h"
28 #include "content/public/browser/ssl_status.h" 19 #include "content/public/browser/ssl_status.h"
29 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
30 #include "content/public/common/origin_util.h" 21 #include "content/public/common/content_client.h"
31 #include "net/base/net_errors.h" 22 #include "net/base/net_errors.h"
32 #include "net/cert/x509_certificate.h" 23 #include "net/cert/x509_certificate.h"
33 #include "net/ssl/ssl_cipher_suite_names.h" 24 #include "net/ssl/ssl_cipher_suite_names.h"
34 #include "net/ssl/ssl_connection_status_flags.h" 25 #include "net/ssl/ssl_connection_status_flags.h"
35 #include "third_party/boringssl/src/include/openssl/ssl.h" 26 #include "third_party/boringssl/src/include/openssl/ssl.h"
36 #include "ui/base/l10n/l10n_util.h" 27 #include "ui/base/l10n/l10n_util.h"
37 28
38 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeSecurityStateModelClient); 29 namespace security_state_content_utils {
estark 2016/11/03 04:38:45 Per blundell's comment, these should be in the sam
Eric Seckler 2016/11/03 17:01:06 Done.
39 30
40 using safe_browsing::SafeBrowsingUIManager;
41 using security_state::SecurityStateModel; 31 using security_state::SecurityStateModel;
42 32
43 namespace { 33 namespace {
44 34
45 // Note: This is a lossy operation. Not all of the policies that can be 35 // Note: This is a lossy operation. Not all of the policies that can be
46 // expressed by a SecurityLevel (a //chrome concept) can be expressed by 36 // expressed by a SecurityLevel can be expressed by a blink::WebSecurityStyle.
47 // a blink::WebSecurityStyle.
48 blink::WebSecurityStyle SecurityLevelToSecurityStyle( 37 blink::WebSecurityStyle SecurityLevelToSecurityStyle(
49 SecurityStateModel::SecurityLevel security_level) { 38 SecurityStateModel::SecurityLevel security_level) {
50 switch (security_level) { 39 switch (security_level) {
51 case SecurityStateModel::NONE: 40 case SecurityStateModel::NONE:
52 case SecurityStateModel::HTTP_SHOW_WARNING: 41 case SecurityStateModel::HTTP_SHOW_WARNING:
53 return blink::WebSecurityStyleUnauthenticated; 42 return blink::WebSecurityStyleUnauthenticated;
54 case SecurityStateModel::SECURITY_WARNING: 43 case SecurityStateModel::SECURITY_WARNING:
55 case SecurityStateModel::SECURE_WITH_POLICY_INSTALLED_CERT: 44 case SecurityStateModel::SECURE_WITH_POLICY_INSTALLED_CERT:
56 return blink::WebSecurityStyleWarning; 45 return blink::WebSecurityStyleWarning;
57 case SecurityStateModel::EV_SECURE: 46 case SecurityStateModel::EV_SECURE:
58 case SecurityStateModel::SECURE: 47 case SecurityStateModel::SECURE:
59 return blink::WebSecurityStyleAuthenticated; 48 return blink::WebSecurityStyleAuthenticated;
60 case SecurityStateModel::DANGEROUS: 49 case SecurityStateModel::DANGEROUS:
61 return blink::WebSecurityStyleAuthenticationBroken; 50 return blink::WebSecurityStyleAuthenticationBroken;
62 } 51 }
63 52
64 NOTREACHED(); 53 NOTREACHED();
65 return blink::WebSecurityStyleUnknown; 54 return blink::WebSecurityStyleUnknown;
66 } 55 }
67 56
68 void AddConnectionExplanation( 57 void AddConnectionExplanation(
69 const security_state::SecurityStateModel::SecurityInfo& security_info, 58 const SecurityStateModel::SecurityInfo& security_info,
70 content::SecurityStyleExplanations* security_style_explanations) { 59 content::SecurityStyleExplanations* security_style_explanations) {
71
72 // Avoid showing TLS details when we couldn't even establish a TLS connection 60 // Avoid showing TLS details when we couldn't even establish a TLS connection
73 // (e.g. for net errors) or if there was no real connection (some tests). We 61 // (e.g. for net errors) or if there was no real connection (some tests). We
74 // check the |connection_status| to see if there was a connection. 62 // check the |connection_status| to see if there was a connection.
75 if (security_info.connection_status == 0) { 63 if (security_info.connection_status == 0) {
76 return; 64 return;
77 } 65 }
78 66
79 int ssl_version = 67 int ssl_version =
80 net::SSLConnectionStatusToVersion(security_info.connection_status); 68 net::SSLConnectionStatusToVersion(security_info.connection_status);
81 const char* protocol; 69 const char* protocol;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 description_replacements.push_back(cipher_name); 129 description_replacements.push_back(cipher_name);
142 130
143 security_style_explanations->info_explanations.push_back( 131 security_style_explanations->info_explanations.push_back(
144 content::SecurityStyleExplanation( 132 content::SecurityStyleExplanation(
145 l10n_util::GetStringUTF8(IDS_OBSOLETE_SSL_SUMMARY), 133 l10n_util::GetStringUTF8(IDS_OBSOLETE_SSL_SUMMARY),
146 base::UTF16ToUTF8( 134 base::UTF16ToUTF8(
147 l10n_util::GetStringFUTF16(IDS_OBSOLETE_SSL_DESCRIPTION, 135 l10n_util::GetStringFUTF16(IDS_OBSOLETE_SSL_DESCRIPTION,
148 description_replacements, nullptr)))); 136 description_replacements, nullptr))));
149 } 137 }
150 138
151 // Check to see whether the security state should be downgraded to reflect
152 // a Safe Browsing verdict.
153 void CheckSafeBrowsingStatus(content::NavigationEntry* entry,
154 content::WebContents* web_contents,
155 SecurityStateModel::VisibleSecurityState* state) {
156 safe_browsing::SafeBrowsingService* sb_service =
157 g_browser_process->safe_browsing_service();
158 if (!sb_service)
159 return;
160 scoped_refptr<SafeBrowsingUIManager> sb_ui_manager = sb_service->ui_manager();
161 if (sb_ui_manager->IsUrlWhitelistedOrPendingForWebContents(
162 entry->GetURL(), false, entry, web_contents, false)) {
163 state->fails_malware_check = true;
164 }
165 }
166
167 } // namespace 139 } // namespace
168 140
169 ChromeSecurityStateModelClient::ChromeSecurityStateModelClient( 141 std::unique_ptr<SecurityStateModel::VisibleSecurityState>
170 content::WebContents* web_contents) 142 GetVisibleSecurityState(content::WebContents* web_contents) {
171 : content::WebContentsObserver(web_contents), 143 auto state = base::MakeUnique<SecurityStateModel::VisibleSecurityState>();
172 web_contents_(web_contents), 144
173 security_state_model_(new SecurityStateModel()), 145 content::NavigationEntry* entry =
174 logged_http_warning_on_current_navigation_(false) { 146 web_contents->GetController().GetVisibleEntry();
175 security_state_model_->SetClient(this); 147 if (!entry || !entry->GetSSL().initialized)
148 return state;
149
150 state->connection_info_initialized = true;
151 state->url = entry->GetURL();
152 const content::SSLStatus& ssl = entry->GetSSL();
153 state->certificate = ssl.certificate;
154 state->cert_status = ssl.cert_status;
155 state->connection_status = ssl.connection_status;
156 state->key_exchange_group = ssl.key_exchange_group;
157 state->security_bits = ssl.security_bits;
158 state->pkp_bypassed = ssl.pkp_bypassed;
159 state->sct_verify_statuses.clear();
160 state->sct_verify_statuses.insert(state->sct_verify_statuses.begin(),
161 ssl.sct_statuses.begin(),
162 ssl.sct_statuses.end());
163 state->displayed_mixed_content =
164 !!(ssl.content_status & content::SSLStatus::DISPLAYED_INSECURE_CONTENT);
165 state->ran_mixed_content =
166 !!(ssl.content_status & content::SSLStatus::RAN_INSECURE_CONTENT);
167 state->displayed_content_with_cert_errors =
168 !!(ssl.content_status &
169 content::SSLStatus::DISPLAYED_CONTENT_WITH_CERT_ERRORS);
170 state->ran_content_with_cert_errors =
171 !!(ssl.content_status & content::SSLStatus::RAN_CONTENT_WITH_CERT_ERRORS);
172 state->displayed_password_field_on_http =
173 !!(ssl.content_status &
174 content::SSLStatus::DISPLAYED_PASSWORD_FIELD_ON_HTTP);
175 state->displayed_credit_card_field_on_http =
176 !!(ssl.content_status &
177 content::SSLStatus::DISPLAYED_CREDIT_CARD_FIELD_ON_HTTP);
178
179 return state;
176 } 180 }
177 181
178 ChromeSecurityStateModelClient::~ChromeSecurityStateModelClient() {} 182 blink::WebSecurityStyle GetSecurityStyle(
179 183 const SecurityStateModel::SecurityInfo& security_info,
180 // static
181 blink::WebSecurityStyle ChromeSecurityStateModelClient::GetSecurityStyle(
182 const security_state::SecurityStateModel::SecurityInfo& security_info,
183 content::SecurityStyleExplanations* security_style_explanations) { 184 content::SecurityStyleExplanations* security_style_explanations) {
184 const blink::WebSecurityStyle security_style = 185 const blink::WebSecurityStyle security_style =
185 SecurityLevelToSecurityStyle(security_info.security_level); 186 SecurityLevelToSecurityStyle(security_info.security_level);
186 187
187 if (security_info.security_level == 188 if (security_info.security_level == SecurityStateModel::HTTP_SHOW_WARNING) {
188 security_state::SecurityStateModel::HTTP_SHOW_WARNING) {
189 // If the HTTP_SHOW_WARNING field trial is in use, display an 189 // If the HTTP_SHOW_WARNING field trial is in use, display an
190 // unauthenticated explanation explaining why the omnibox warning is 190 // unauthenticated explanation explaining why the omnibox warning is
191 // present. 191 // present.
192 security_style_explanations->unauthenticated_explanations.push_back( 192 security_style_explanations->unauthenticated_explanations.push_back(
193 content::SecurityStyleExplanation( 193 content::SecurityStyleExplanation(
194 l10n_util::GetStringUTF8(IDS_PRIVATE_USER_DATA_INPUT), 194 l10n_util::GetStringUTF8(IDS_PRIVATE_USER_DATA_INPUT),
195 l10n_util::GetStringUTF8(IDS_PRIVATE_USER_DATA_INPUT_DESCRIPTION))); 195 l10n_util::GetStringUTF8(IDS_PRIVATE_USER_DATA_INPUT_DESCRIPTION)));
196 } else if (security_info.security_level == 196 } else if (security_info.security_level == SecurityStateModel::NONE &&
197 security_state::SecurityStateModel::NONE &&
198 security_info.displayed_private_user_data_input_on_http) { 197 security_info.displayed_private_user_data_input_on_http) {
199 // If the HTTP_SHOW_WARNING field trial isn't in use yet, display an 198 // If the HTTP_SHOW_WARNING field trial isn't in use yet, display an
200 // informational note that the omnibox will contain a warning for 199 // informational note that the omnibox will contain a warning for
201 // this site in a future version of Chrome. 200 // this site in a future version of Chrome.
202 security_style_explanations->info_explanations.push_back( 201 security_style_explanations->info_explanations.push_back(
203 content::SecurityStyleExplanation( 202 content::SecurityStyleExplanation(
204 l10n_util::GetStringUTF8(IDS_PRIVATE_USER_DATA_INPUT), 203 l10n_util::GetStringUTF8(IDS_PRIVATE_USER_DATA_INPUT),
205 l10n_util::GetStringUTF8( 204 l10n_util::GetStringUTF8(
206 IDS_PRIVATE_USER_DATA_INPUT_FUTURE_DESCRIPTION))); 205 IDS_PRIVATE_USER_DATA_INPUT_FUTURE_DESCRIPTION)));
207 } 206 }
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 if (security_info.pkp_bypassed) { 314 if (security_info.pkp_bypassed) {
316 security_style_explanations->info_explanations.push_back( 315 security_style_explanations->info_explanations.push_back(
317 content::SecurityStyleExplanation( 316 content::SecurityStyleExplanation(
318 "Public-Key Pinning Bypassed", 317 "Public-Key Pinning Bypassed",
319 "Public-key pinning was bypassed by a local root certificate.")); 318 "Public-key pinning was bypassed by a local root certificate."));
320 } 319 }
321 320
322 return security_style; 321 return security_style;
323 } 322 }
324 323
325 void ChromeSecurityStateModelClient::GetSecurityInfo( 324 } // namespace security_state
326 SecurityStateModel::SecurityInfo* result) const {
327 security_state_model_->GetSecurityInfo(result);
328 }
329
330 void ChromeSecurityStateModelClient::VisibleSecurityStateChanged() {
331 if (logged_http_warning_on_current_navigation_)
332 return;
333
334 security_state::SecurityStateModel::SecurityInfo security_info;
335 GetSecurityInfo(&security_info);
336 if (!security_info.displayed_private_user_data_input_on_http)
337 return;
338
339 std::string warning;
340 bool warning_is_user_visible = false;
341 switch (security_info.security_level) {
342 case security_state::SecurityStateModel::HTTP_SHOW_WARNING:
343 warning =
344 "This page includes a password or credit card input in a non-secure "
345 "context. A warning has been added to the URL bar. For more "
346 "information, see https://goo.gl/zmWq3m.";
347 warning_is_user_visible = true;
348 break;
349 case security_state::SecurityStateModel::NONE:
350 case security_state::SecurityStateModel::DANGEROUS:
351 warning =
352 "This page includes a password or credit card input in a non-secure "
353 "context. A warning will be added to the URL bar in Chrome 56 (Jan "
354 "2017). For more information, see https://goo.gl/zmWq3m.";
355 break;
356 default:
357 return;
358 }
359
360 logged_http_warning_on_current_navigation_ = true;
361 web_contents_->GetMainFrame()->AddMessageToConsole(
362 content::CONSOLE_MESSAGE_LEVEL_WARNING, warning);
363 UMA_HISTOGRAM_BOOLEAN("Security.HTTPBad.UserWarnedAboutSensitiveInput",
364 warning_is_user_visible);
365 }
366
367 void ChromeSecurityStateModelClient::DidFinishNavigation(
368 content::NavigationHandle* navigation_handle) {
369 if (navigation_handle->IsInMainFrame() && !navigation_handle->IsSamePage()) {
370 // Only reset the console message flag for main-frame navigations,
371 // and not for same-page navigations like reference fragments and pushState.
372 logged_http_warning_on_current_navigation_ = false;
373 }
374 }
375
376 bool ChromeSecurityStateModelClient::UsedPolicyInstalledCertificate() {
377 #if defined(OS_CHROMEOS)
378 policy::PolicyCertService* service =
379 policy::PolicyCertServiceFactory::GetForProfile(
380 Profile::FromBrowserContext(web_contents_->GetBrowserContext()));
381 if (service && service->UsedPolicyCertificates())
382 return true;
383 #endif
384 return false;
385 }
386
387 bool ChromeSecurityStateModelClient::IsOriginSecure(const GURL& url) {
388 return content::IsOriginSecure(url);
389 }
390
391 void ChromeSecurityStateModelClient::GetVisibleSecurityState(
392 SecurityStateModel::VisibleSecurityState* state) {
393 content::NavigationEntry* entry =
394 web_contents_->GetController().GetVisibleEntry();
395 if (!entry) {
396 *state = SecurityStateModel::VisibleSecurityState();
397 return;
398 }
399
400 if (!entry->GetSSL().initialized) {
401 *state = SecurityStateModel::VisibleSecurityState();
402 // Connection security information is still being initialized, but malware
403 // status might already be known.
404 CheckSafeBrowsingStatus(entry, web_contents_, state);
405 return;
406 }
407
408 state->connection_info_initialized = true;
409 state->url = entry->GetURL();
410 const content::SSLStatus& ssl = entry->GetSSL();
411 state->certificate = ssl.certificate;
412 state->cert_status = ssl.cert_status;
413 state->connection_status = ssl.connection_status;
414 state->key_exchange_group = ssl.key_exchange_group;
415 state->security_bits = ssl.security_bits;
416 state->pkp_bypassed = ssl.pkp_bypassed;
417 state->sct_verify_statuses.clear();
418 state->sct_verify_statuses.insert(state->sct_verify_statuses.begin(),
419 ssl.sct_statuses.begin(),
420 ssl.sct_statuses.end());
421 state->displayed_mixed_content =
422 !!(ssl.content_status & content::SSLStatus::DISPLAYED_INSECURE_CONTENT);
423 state->ran_mixed_content =
424 !!(ssl.content_status & content::SSLStatus::RAN_INSECURE_CONTENT);
425 state->displayed_content_with_cert_errors =
426 !!(ssl.content_status &
427 content::SSLStatus::DISPLAYED_CONTENT_WITH_CERT_ERRORS);
428 state->ran_content_with_cert_errors =
429 !!(ssl.content_status & content::SSLStatus::RAN_CONTENT_WITH_CERT_ERRORS);
430 state->displayed_password_field_on_http =
431 !!(ssl.content_status &
432 content::SSLStatus::DISPLAYED_PASSWORD_FIELD_ON_HTTP);
433 state->displayed_credit_card_field_on_http =
434 !!(ssl.content_status &
435 content::SSLStatus::DISPLAYED_CREDIT_CARD_FIELD_ON_HTTP);
436
437 CheckSafeBrowsingStatus(entry, web_contents_, state);
438 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698