security_state_tab_helper.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright 2015 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. #include "chrome/browser/ssl/security_state_tab_helper.h"
  5. #include "base/bind.h"
  6. #include "base/metrics/histogram_macros.h"
  7. #include "base/time/time.h"
  8. #include "build/build_config.h"
  9. #if 0
  10. #include "chrome/browser/browser_process.h"
  11. #include "chrome/browser/chromeos/policy/policy_cert_service.h"
  12. #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h"
  13. #include "chrome/browser/profiles/profile.h"
  14. #include "chrome/browser/safe_browsing/safe_browsing_service.h"
  15. #include "chrome/browser/safe_browsing/ui_manager.h"
  16. #endif
  17. #include "components/prefs/pref_service.h"
  18. #include "components/security_state/content/content_utils.h"
  19. #if 0
  20. #include "components/ssl_config/ssl_config_prefs.h"
  21. #endif
  22. #include "content/public/browser/navigation_entry.h"
  23. #include "content/public/browser/navigation_handle.h"
  24. #include "content/public/browser/render_frame_host.h"
  25. #include "content/public/browser/web_contents.h"
  26. #include "content/public/common/origin_util.h"
  27. #include "net/base/net_errors.h"
  28. #include "net/cert/x509_certificate.h"
  29. #include "net/ssl/ssl_cipher_suite_names.h"
  30. #include "net/ssl/ssl_connection_status_flags.h"
  31. #include "third_party/boringssl/src/include/openssl/ssl.h"
  32. #include "ui/base/l10n/l10n_util.h"
  33. DEFINE_WEB_CONTENTS_USER_DATA_KEY(SecurityStateTabHelper);
  34. #if 0
  35. using safe_browsing::SafeBrowsingUIManager;
  36. #endif
  37. SecurityStateTabHelper::SecurityStateTabHelper(
  38. content::WebContents* web_contents)
  39. : content::WebContentsObserver(web_contents),
  40. logged_http_warning_on_current_navigation_(false) {}
  41. SecurityStateTabHelper::~SecurityStateTabHelper() {}
  42. void SecurityStateTabHelper::GetSecurityInfo(
  43. security_state::SecurityInfo* result) const {
  44. security_state::GetSecurityInfo(GetVisibleSecurityState(),
  45. UsedPolicyInstalledCertificate(),
  46. base::Bind(&content::IsOriginSecure), result);
  47. }
  48. void SecurityStateTabHelper::VisibleSecurityStateChanged() {
  49. if (logged_http_warning_on_current_navigation_)
  50. return;
  51. security_state::SecurityInfo security_info;
  52. GetSecurityInfo(&security_info);
  53. if (!security_info.displayed_password_field_on_http &&
  54. !security_info.displayed_credit_card_field_on_http) {
  55. return;
  56. }
  57. DCHECK(time_of_http_warning_on_current_navigation_.is_null());
  58. time_of_http_warning_on_current_navigation_ = base::Time::Now();
  59. std::string warning;
  60. bool warning_is_user_visible = false;
  61. switch (security_info.security_level) {
  62. case security_state::HTTP_SHOW_WARNING:
  63. warning =
  64. "This page includes a password or credit card input in a non-secure "
  65. "context. A warning has been added to the URL bar. For more "
  66. "information, see https://goo.gl/zmWq3m.";
  67. warning_is_user_visible = true;
  68. break;
  69. case security_state::NONE:
  70. case security_state::DANGEROUS:
  71. warning =
  72. "This page includes a password or credit card input in a non-secure "
  73. "context. A warning will be added to the URL bar in Chrome 56 (Jan "
  74. "2017). For more information, see https://goo.gl/zmWq3m.";
  75. break;
  76. default:
  77. return;
  78. }
  79. logged_http_warning_on_current_navigation_ = true;
  80. web_contents()->GetMainFrame()->AddMessageToConsole(
  81. content::CONSOLE_MESSAGE_LEVEL_WARNING, warning);
  82. if (security_info.displayed_credit_card_field_on_http) {
  83. UMA_HISTOGRAM_BOOLEAN(
  84. "Security.HTTPBad.UserWarnedAboutSensitiveInput.CreditCard",
  85. warning_is_user_visible);
  86. }
  87. if (security_info.displayed_password_field_on_http) {
  88. UMA_HISTOGRAM_BOOLEAN(
  89. "Security.HTTPBad.UserWarnedAboutSensitiveInput.Password",
  90. warning_is_user_visible);
  91. }
  92. }
  93. void SecurityStateTabHelper::DidStartNavigation(
  94. content::NavigationHandle* navigation_handle) {
  95. if (time_of_http_warning_on_current_navigation_.is_null() ||
  96. !navigation_handle->IsInMainFrame() ||
  97. navigation_handle->IsSameDocument()) {
  98. return;
  99. }
  100. // Record how quickly a user leaves a site after encountering an
  101. // HTTP-bad warning. A navigation here only counts if it is a
  102. // main-frame, not-same-page navigation, since it aims to measure how
  103. // quickly a user leaves a site after seeing the HTTP warning.
  104. UMA_HISTOGRAM_LONG_TIMES(
  105. "Security.HTTPBad.NavigationStartedAfterUserWarnedAboutSensitiveInput",
  106. base::Time::Now() - time_of_http_warning_on_current_navigation_);
  107. // After recording the histogram, clear the time of the warning. A
  108. // timing histogram will not be recorded again on this page, because
  109. // the time is only set the first time the HTTP-bad warning is shown
  110. // per page.
  111. time_of_http_warning_on_current_navigation_ = base::Time();
  112. }
  113. void SecurityStateTabHelper::DidFinishNavigation(
  114. content::NavigationHandle* navigation_handle) {
  115. if (navigation_handle->IsInMainFrame() &&
  116. !navigation_handle->IsSameDocument()) {
  117. // Only reset the console message flag for main-frame navigations,
  118. // and not for same-page navigations like reference fragments and pushState.
  119. logged_http_warning_on_current_navigation_ = false;
  120. }
  121. }
  122. void SecurityStateTabHelper::WebContentsDestroyed() {
  123. if (time_of_http_warning_on_current_navigation_.is_null()) {
  124. return;
  125. }
  126. // Record how quickly the tab is closed after a user encounters an
  127. // HTTP-bad warning. This histogram will only be recorded if the
  128. // WebContents is destroyed before another navigation begins.
  129. UMA_HISTOGRAM_LONG_TIMES(
  130. "Security.HTTPBad.WebContentsDestroyedAfterUserWarnedAboutSensitiveInput",
  131. base::Time::Now() - time_of_http_warning_on_current_navigation_);
  132. }
  133. bool SecurityStateTabHelper::UsedPolicyInstalledCertificate() const {
  134. #if defined(OS_CHROMEOS)
  135. policy::PolicyCertService* service =
  136. policy::PolicyCertServiceFactory::GetForProfile(
  137. Profile::FromBrowserContext(web_contents()->GetBrowserContext()));
  138. if (service && service->UsedPolicyCertificates())
  139. return true;
  140. #endif
  141. return false;
  142. }
  143. #if 0
  144. security_state::MaliciousContentStatus
  145. SecurityStateTabHelper::GetMaliciousContentStatus() const {
  146. content::NavigationEntry* entry =
  147. web_contents()->GetController().GetVisibleEntry();
  148. if (!entry)
  149. return security_state::MALICIOUS_CONTENT_STATUS_NONE;
  150. safe_browsing::SafeBrowsingService* sb_service =
  151. g_browser_process->safe_browsing_service();
  152. if (!sb_service)
  153. return security_state::MALICIOUS_CONTENT_STATUS_NONE;
  154. scoped_refptr<SafeBrowsingUIManager> sb_ui_manager = sb_service->ui_manager();
  155. safe_browsing::SBThreatType threat_type;
  156. if (sb_ui_manager->IsUrlWhitelistedOrPendingForWebContents(
  157. entry->GetURL(), false, entry, web_contents(), false, &threat_type)) {
  158. switch (threat_type) {
  159. case safe_browsing::SB_THREAT_TYPE_UNUSED:
  160. case safe_browsing::SB_THREAT_TYPE_SAFE:
  161. break;
  162. case safe_browsing::SB_THREAT_TYPE_URL_PHISHING:
  163. case safe_browsing::SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL:
  164. return security_state::MALICIOUS_CONTENT_STATUS_SOCIAL_ENGINEERING;
  165. break;
  166. case safe_browsing::SB_THREAT_TYPE_URL_MALWARE:
  167. case safe_browsing::SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL:
  168. return security_state::MALICIOUS_CONTENT_STATUS_MALWARE;
  169. break;
  170. case safe_browsing::SB_THREAT_TYPE_URL_UNWANTED:
  171. return security_state::MALICIOUS_CONTENT_STATUS_UNWANTED_SOFTWARE;
  172. break;
  173. case safe_browsing::SB_THREAT_TYPE_BINARY_MALWARE_URL:
  174. case safe_browsing::SB_THREAT_TYPE_EXTENSION:
  175. case safe_browsing::SB_THREAT_TYPE_BLACKLISTED_RESOURCE:
  176. case safe_browsing::SB_THREAT_TYPE_API_ABUSE:
  177. // These threat types are not currently associated with
  178. // interstitials, and thus resources with these threat types are
  179. // not ever whitelisted or pending whitelisting.
  180. NOTREACHED();
  181. break;
  182. }
  183. }
  184. return security_state::MALICIOUS_CONTENT_STATUS_NONE;
  185. }
  186. #endif
  187. std::unique_ptr<security_state::VisibleSecurityState>
  188. SecurityStateTabHelper::GetVisibleSecurityState() const {
  189. auto state = security_state::GetVisibleSecurityState(web_contents());
  190. #if 0
  191. // Malware status might already be known even if connection security
  192. // information is still being initialized, thus no need to check for that.
  193. state->malicious_content_status = GetMaliciousContentStatus();
  194. #endif
  195. return state;
  196. }