Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "components/security_state/core/security_state.h" | 5 #include "components/security_state/core/security_state.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> | |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
| 14 #include "base/test/scoped_command_line.h" | |
| 15 #include "components/security_state/core/switches.h" | |
| 13 #include "net/cert/x509_certificate.h" | 16 #include "net/cert/x509_certificate.h" |
| 14 #include "net/ssl/ssl_cipher_suite_names.h" | 17 #include "net/ssl/ssl_cipher_suite_names.h" |
| 15 #include "net/ssl/ssl_connection_status_flags.h" | 18 #include "net/ssl/ssl_connection_status_flags.h" |
| 16 #include "net/test/cert_test_util.h" | 19 #include "net/test/cert_test_util.h" |
| 17 #include "net/test/test_certificate_data.h" | 20 #include "net/test/test_certificate_data.h" |
| 18 #include "net/test/test_data_directory.h" | 21 #include "net/test/test_data_directory.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 23 |
| 21 namespace security_state { | 24 namespace security_state { |
| 22 | 25 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 42 cert_(net::ImportCertFromFile(net::GetTestCertsDirectory(), | 45 cert_(net::ImportCertFromFile(net::GetTestCertsDirectory(), |
| 43 "sha1_2016.pem")), | 46 "sha1_2016.pem")), |
| 44 connection_status_(net::SSL_CONNECTION_VERSION_TLS1_2 | 47 connection_status_(net::SSL_CONNECTION_VERSION_TLS1_2 |
| 45 << net::SSL_CONNECTION_VERSION_SHIFT), | 48 << net::SSL_CONNECTION_VERSION_SHIFT), |
| 46 cert_status_(net::CERT_STATUS_SHA1_SIGNATURE_PRESENT), | 49 cert_status_(net::CERT_STATUS_SHA1_SIGNATURE_PRESENT), |
| 47 displayed_mixed_content_(false), | 50 displayed_mixed_content_(false), |
| 48 contained_mixed_form_(false), | 51 contained_mixed_form_(false), |
| 49 ran_mixed_content_(false), | 52 ran_mixed_content_(false), |
| 50 malicious_content_status_(MALICIOUS_CONTENT_STATUS_NONE), | 53 malicious_content_status_(MALICIOUS_CONTENT_STATUS_NONE), |
| 51 displayed_password_field_on_http_(false), | 54 displayed_password_field_on_http_(false), |
| 52 displayed_credit_card_field_on_http_(false) {} | 55 displayed_credit_card_field_on_http_(false), |
| 56 is_incognito_(false) {} | |
| 53 virtual ~TestSecurityStateHelper() {} | 57 virtual ~TestSecurityStateHelper() {} |
| 54 | 58 |
| 55 void SetCertificate(scoped_refptr<net::X509Certificate> cert) { | 59 void SetCertificate(scoped_refptr<net::X509Certificate> cert) { |
| 56 cert_ = std::move(cert); | 60 cert_ = std::move(cert); |
| 57 } | 61 } |
| 58 void set_connection_status(int connection_status) { | 62 void set_connection_status(int connection_status) { |
| 59 connection_status_ = connection_status; | 63 connection_status_ = connection_status; |
| 60 } | 64 } |
| 61 void SetCipherSuite(uint16_t ciphersuite) { | 65 void SetCipherSuite(uint16_t ciphersuite) { |
| 62 net::SSLConnectionStatusSetCipherSuite(ciphersuite, &connection_status_); | 66 net::SSLConnectionStatusSetCipherSuite(ciphersuite, &connection_status_); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 78 malicious_content_status_ = malicious_content_status; | 82 malicious_content_status_ = malicious_content_status; |
| 79 } | 83 } |
| 80 void set_displayed_password_field_on_http( | 84 void set_displayed_password_field_on_http( |
| 81 bool displayed_password_field_on_http) { | 85 bool displayed_password_field_on_http) { |
| 82 displayed_password_field_on_http_ = displayed_password_field_on_http; | 86 displayed_password_field_on_http_ = displayed_password_field_on_http; |
| 83 } | 87 } |
| 84 void set_displayed_credit_card_field_on_http( | 88 void set_displayed_credit_card_field_on_http( |
| 85 bool displayed_credit_card_field_on_http) { | 89 bool displayed_credit_card_field_on_http) { |
| 86 displayed_credit_card_field_on_http_ = displayed_credit_card_field_on_http; | 90 displayed_credit_card_field_on_http_ = displayed_credit_card_field_on_http; |
| 87 } | 91 } |
| 92 void set_is_incognito(bool is_incognito) { is_incognito_ = is_incognito; } | |
| 88 | 93 |
| 89 void SetUrl(const GURL& url) { url_ = url; } | 94 void SetUrl(const GURL& url) { url_ = url; } |
| 90 | 95 |
| 91 std::unique_ptr<VisibleSecurityState> GetVisibleSecurityState() const { | 96 std::unique_ptr<VisibleSecurityState> GetVisibleSecurityState() const { |
| 92 auto state = base::MakeUnique<VisibleSecurityState>(); | 97 auto state = base::MakeUnique<VisibleSecurityState>(); |
| 93 state->connection_info_initialized = true; | 98 state->connection_info_initialized = true; |
| 94 state->url = url_; | 99 state->url = url_; |
| 95 state->certificate = cert_; | 100 state->certificate = cert_; |
| 96 state->cert_status = cert_status_; | 101 state->cert_status = cert_status_; |
| 97 state->connection_status = connection_status_; | 102 state->connection_status = connection_status_; |
| 98 state->security_bits = 256; | 103 state->security_bits = 256; |
| 99 state->displayed_mixed_content = displayed_mixed_content_; | 104 state->displayed_mixed_content = displayed_mixed_content_; |
| 100 state->contained_mixed_form = contained_mixed_form_; | 105 state->contained_mixed_form = contained_mixed_form_; |
| 101 state->ran_mixed_content = ran_mixed_content_; | 106 state->ran_mixed_content = ran_mixed_content_; |
| 102 state->malicious_content_status = malicious_content_status_; | 107 state->malicious_content_status = malicious_content_status_; |
| 103 state->displayed_password_field_on_http = displayed_password_field_on_http_; | 108 state->displayed_password_field_on_http = displayed_password_field_on_http_; |
| 104 state->displayed_credit_card_field_on_http = | 109 state->displayed_credit_card_field_on_http = |
| 105 displayed_credit_card_field_on_http_; | 110 displayed_credit_card_field_on_http_; |
| 111 state->is_incognito = is_incognito_; | |
| 106 return state; | 112 return state; |
| 107 } | 113 } |
| 108 | 114 |
| 109 void GetSecurityInfo(SecurityInfo* security_info) const { | 115 void GetSecurityInfo(SecurityInfo* security_info) const { |
| 110 security_state::GetSecurityInfo( | 116 security_state::GetSecurityInfo( |
| 111 GetVisibleSecurityState(), | 117 GetVisibleSecurityState(), |
| 112 false /* used policy installed certificate */, | 118 false /* used policy installed certificate */, |
| 113 base::Bind(&IsOriginSecure), security_info); | 119 base::Bind(&IsOriginSecure), security_info); |
| 114 } | 120 } |
| 115 | 121 |
| 116 private: | 122 private: |
| 117 GURL url_; | 123 GURL url_; |
| 118 scoped_refptr<net::X509Certificate> cert_; | 124 scoped_refptr<net::X509Certificate> cert_; |
| 119 int connection_status_; | 125 int connection_status_; |
| 120 net::CertStatus cert_status_; | 126 net::CertStatus cert_status_; |
| 121 bool displayed_mixed_content_; | 127 bool displayed_mixed_content_; |
| 122 bool contained_mixed_form_; | 128 bool contained_mixed_form_; |
| 123 bool ran_mixed_content_; | 129 bool ran_mixed_content_; |
| 124 MaliciousContentStatus malicious_content_status_; | 130 MaliciousContentStatus malicious_content_status_; |
| 125 bool displayed_password_field_on_http_; | 131 bool displayed_password_field_on_http_; |
| 126 bool displayed_credit_card_field_on_http_; | 132 bool displayed_credit_card_field_on_http_; |
| 133 bool is_incognito_; | |
| 127 }; | 134 }; |
| 128 | 135 |
| 129 } // namespace | 136 } // namespace |
| 130 | 137 |
| 131 // Tests that SHA1-signed certificates, when not allowed by policy, downgrade | 138 // Tests that SHA1-signed certificates, when not allowed by policy, downgrade |
| 132 // the security state of the page to DANGEROUS. | 139 // the security state of the page to DANGEROUS. |
| 133 TEST(SecurityStateTest, SHA1Blocked) { | 140 TEST(SecurityStateTest, SHA1Blocked) { |
| 134 TestSecurityStateHelper helper; | 141 TestSecurityStateHelper helper; |
| 135 helper.AddCertStatus(net::CERT_STATUS_WEAK_SIGNATURE_ALGORITHM); | 142 helper.AddCertStatus(net::CERT_STATUS_WEAK_SIGNATURE_ALGORITHM); |
| 136 helper.AddCertStatus(net::CERT_STATUS_SHA1_SIGNATURE_PRESENT); | 143 helper.AddCertStatus(net::CERT_STATUS_SHA1_SIGNATURE_PRESENT); |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 346 TestSecurityStateHelper helper; | 353 TestSecurityStateHelper helper; |
| 347 helper.SetUrl(GURL(url)); | 354 helper.SetUrl(GURL(url)); |
| 348 SecurityInfo security_info; | 355 SecurityInfo security_info; |
| 349 helper.GetSecurityInfo(&security_info); | 356 helper.GetSecurityInfo(&security_info); |
| 350 EXPECT_FALSE(security_info.displayed_password_field_on_http); | 357 EXPECT_FALSE(security_info.displayed_password_field_on_http); |
| 351 EXPECT_FALSE(security_info.displayed_credit_card_field_on_http); | 358 EXPECT_FALSE(security_info.displayed_credit_card_field_on_http); |
| 352 EXPECT_EQ(NONE, security_info.security_level); | 359 EXPECT_EQ(NONE, security_info.security_level); |
| 353 } | 360 } |
| 354 } | 361 } |
| 355 | 362 |
| 363 // Tests that |is_incognito| is set only when the corresponding | |
| 364 // VisibleSecurityState flag is set. | |
| 365 TEST(SecurityStateTest, IncognitoFlagPropagates) { | |
| 366 TestSecurityStateHelper helper; | |
| 367 helper.SetUrl(GURL(kHttpUrl)); | |
| 368 SecurityInfo security_info; | |
| 369 helper.GetSecurityInfo(&security_info); | |
| 370 EXPECT_FALSE(security_info.is_incognito); | |
| 371 | |
| 372 helper.set_is_incognito(true); | |
| 373 helper.GetSecurityInfo(&security_info); | |
| 374 EXPECT_TRUE(security_info.is_incognito); | |
| 375 } | |
| 376 | |
| 356 // Tests that SSL.MarkHttpAsStatus histogram is updated when security state is | 377 // Tests that SSL.MarkHttpAsStatus histogram is updated when security state is |
| 357 // computed for a page. | 378 // computed for a page. |
| 358 TEST(SecurityStateTest, MarkHttpAsStatusHistogram) { | 379 TEST(SecurityStateTest, MarkHttpAsStatusHistogram) { |
| 359 const char* kHistogramName = "SSL.MarkHttpAsStatus"; | 380 const char* kHistogramName = "SSL.MarkHttpAsStatus"; |
| 360 base::HistogramTester histograms; | 381 base::HistogramTester histograms; |
| 361 TestSecurityStateHelper helper; | 382 TestSecurityStateHelper helper; |
| 362 helper.SetUrl(GURL(kHttpUrl)); | 383 helper.SetUrl(GURL(kHttpUrl)); |
| 363 | 384 |
| 364 // Ensure histogram recorded correctly when a non-secure password input is | 385 // Ensure histogram recorded correctly when a non-secure password input is |
| 365 // found on the page. | 386 // found on the page. |
| 366 helper.set_displayed_password_field_on_http(true); | 387 helper.set_displayed_password_field_on_http(true); |
| 367 SecurityInfo security_info; | 388 SecurityInfo security_info; |
| 368 histograms.ExpectTotalCount(kHistogramName, 0); | 389 histograms.ExpectTotalCount(kHistogramName, 0); |
| 369 helper.GetSecurityInfo(&security_info); | 390 helper.GetSecurityInfo(&security_info); |
| 370 histograms.ExpectUniqueSample(kHistogramName, 2 /* HTTP_SHOW_WARNING */, 1); | 391 histograms.ExpectUniqueSample( |
| 392 kHistogramName, 2 /* HTTP_SHOW_WARNING_ON_SENSITIVE_FIELDS */, 1); | |
| 371 | 393 |
| 372 // Ensure histogram recorded correctly even without a password input. | 394 // Ensure histogram recorded correctly even without a password input. |
| 373 helper.set_displayed_password_field_on_http(false); | 395 helper.set_displayed_password_field_on_http(false); |
| 374 helper.GetSecurityInfo(&security_info); | 396 helper.GetSecurityInfo(&security_info); |
| 375 histograms.ExpectUniqueSample(kHistogramName, 2 /* HTTP_SHOW_WARNING */, 2); | 397 histograms.ExpectUniqueSample( |
| 398 kHistogramName, 2 /* HTTP_SHOW_WARNING_ON_SENSITIVE_FIELDS */, 2); | |
| 399 | |
| 400 { | |
| 401 // Test the "non-secure-while-incognito" configuration. | |
| 402 base::test::ScopedCommandLine scoped_command_line; | |
| 403 scoped_command_line.GetProcessCommandLine()->AppendSwitchASCII( | |
| 404 security_state::switches::kMarkHttpAs, | |
| 405 security_state::switches::kMarkHttpAsNonSecureWhileIncognito); | |
| 406 | |
| 407 base::HistogramTester histograms; | |
| 408 TestSecurityStateHelper helper; | |
| 409 helper.SetUrl(GURL(kHttpUrl)); | |
| 410 | |
| 411 // Ensure histogram recorded correctly when the Incognito flag is present. | |
| 412 helper.set_is_incognito(true); | |
| 413 SecurityInfo security_info; | |
| 414 histograms.ExpectTotalCount(kHistogramName, 0); | |
| 415 helper.GetSecurityInfo(&security_info); | |
| 416 histograms.ExpectUniqueSample(kHistogramName, | |
| 417 4 /* NON_SECURE_WHILE_INCOGNITO */, 1); | |
| 418 | |
| 419 // Ensure histogram recorded correctly even without the Incognito flag. | |
| 420 helper.set_displayed_password_field_on_http(false); | |
|
estark
2017/06/09 05:09:38
set_is_incognito?
elawrence
2017/06/13 15:31:36
Gah, fixed.
| |
| 421 helper.GetSecurityInfo(&security_info); | |
| 422 histograms.ExpectUniqueSample(kHistogramName, | |
| 423 4 /* NON_SECURE_WHILE_INCOGNITO */, 2); | |
| 424 } | |
| 425 | |
| 426 { | |
| 427 // Test the "non-secure-while-incognito-or-editing" configuration. | |
| 428 base::test::ScopedCommandLine scoped_command_line; | |
| 429 scoped_command_line.GetProcessCommandLine()->AppendSwitchASCII( | |
| 430 security_state::switches::kMarkHttpAs, | |
| 431 security_state::switches::kMarkHttpAsNonSecureWhileIncognitoOrEditing); | |
| 432 | |
| 433 base::HistogramTester histograms; | |
| 434 TestSecurityStateHelper helper; | |
| 435 helper.SetUrl(GURL(kHttpUrl)); | |
| 436 | |
| 437 // Ensure histogram recorded correctly when the Incognito flag is present. | |
| 438 helper.set_is_incognito(true); | |
| 439 SecurityInfo security_info; | |
| 440 histograms.ExpectTotalCount(kHistogramName, 0); | |
| 441 helper.GetSecurityInfo(&security_info); | |
| 442 histograms.ExpectUniqueSample( | |
| 443 kHistogramName, 5 /* NON_SECURE_WHILE_INCOGNITO_OR_EDITING */, 1); | |
| 444 | |
| 445 // Ensure histogram recorded correctly even without the Incognito flag. | |
| 446 helper.set_displayed_password_field_on_http(false); | |
|
estark
2017/06/09 05:09:38
ditto
elawrence
2017/06/13 15:31:36
Gah, fixed.
| |
| 447 helper.GetSecurityInfo(&security_info); | |
| 448 histograms.ExpectUniqueSample( | |
| 449 kHistogramName, 5 /* NON_SECURE_WHILE_INCOGNITO_OR_EDITING */, 2); | |
| 450 } | |
| 376 } | 451 } |
| 377 | 452 |
| 378 TEST(SecurityStateTest, DetectSubjectAltName) { | 453 TEST(SecurityStateTest, DetectSubjectAltName) { |
| 379 TestSecurityStateHelper helper; | 454 TestSecurityStateHelper helper; |
| 380 | 455 |
| 381 // Ensure subjectAltName is detected as present when the cert includes it. | 456 // Ensure subjectAltName is detected as present when the cert includes it. |
| 382 SecurityInfo san_security_info; | 457 SecurityInfo san_security_info; |
| 383 helper.GetSecurityInfo(&san_security_info); | 458 helper.GetSecurityInfo(&san_security_info); |
| 384 EXPECT_FALSE(san_security_info.cert_missing_subject_alt_name); | 459 EXPECT_FALSE(san_security_info.cert_missing_subject_alt_name); |
| 385 | 460 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 414 helper.set_ran_mixed_content(true); | 489 helper.set_ran_mixed_content(true); |
| 415 SecurityInfo mixed_form_and_active_security_info; | 490 SecurityInfo mixed_form_and_active_security_info; |
| 416 helper.GetSecurityInfo(&mixed_form_and_active_security_info); | 491 helper.GetSecurityInfo(&mixed_form_and_active_security_info); |
| 417 EXPECT_TRUE(mixed_form_and_active_security_info.contained_mixed_form); | 492 EXPECT_TRUE(mixed_form_and_active_security_info.contained_mixed_form); |
| 418 EXPECT_EQ(CONTENT_STATUS_RAN, | 493 EXPECT_EQ(CONTENT_STATUS_RAN, |
| 419 mixed_form_and_active_security_info.mixed_content_status); | 494 mixed_form_and_active_security_info.mixed_content_status); |
| 420 EXPECT_EQ(DANGEROUS, mixed_form_and_active_security_info.security_level); | 495 EXPECT_EQ(DANGEROUS, mixed_form_and_active_security_info.security_level); |
| 421 } | 496 } |
| 422 | 497 |
| 423 } // namespace security_state | 498 } // namespace security_state |
| OLD | NEW |