1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/safe_browsing/phishy_interaction_tracker.h"
#include <string>
#include <utility>
#include "base/metrics/statistics_recorder.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "build/build_config.h"
#include "chrome/browser/safe_browsing/chrome_ping_manager_factory.h"
#include "chrome/browser/safe_browsing/chrome_safe_browsing_blocking_page_factory.h"
#include "chrome/browser/safe_browsing/chrome_ui_manager_delegate.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/safe_browsing/content/browser/content_unsafe_resource_util.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "services/network/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/test/base/scoped_testing_local_state.h"
#endif // BUILDFLAG(IS_CHROMEOS)
using content::WebContents;
using safe_browsing::PhishyInteractionTracker;
using testing::_;
using testing::Mock;
using testing::StrictMock;
namespace {
constexpr char kBadURL[] = "https://www.phishing.com";
class MockSafeBrowsingUIManager : public safe_browsing::SafeBrowsingUIManager {
public:
MockSafeBrowsingUIManager()
: SafeBrowsingUIManager(
std::make_unique<
safe_browsing::ChromeSafeBrowsingUIManagerDelegate>(),
std::make_unique<
safe_browsing::ChromeSafeBrowsingBlockingPageFactory>(),
GURL(chrome::kChromeUINewTabURL)) {}
MockSafeBrowsingUIManager(const MockSafeBrowsingUIManager&) = delete;
MockSafeBrowsingUIManager& operator=(const MockSafeBrowsingUIManager&) =
delete;
MOCK_METHOD1(DisplayBlockingPage, void(const UnsafeResource& resource));
bool IsUrlAllowlistedOrPendingForWebContents(
const GURL& url,
content::NavigationEntry* entry,
WebContents* web_contents,
bool allowlist_only,
safe_browsing::SBThreatType* threat_type) override {
*threat_type = safe_browsing::SBThreatType::SB_THREAT_TYPE_URL_PHISHING;
return true;
}
protected:
~MockSafeBrowsingUIManager() override = default;
};
} // namespace
class PhishyInteractionTrackerTest : public ChromeRenderViewHostTestHarness {
public:
typedef security_interstitials::UnsafeResource UnsafeResource;
PhishyInteractionTrackerTest() = default;
~PhishyInteractionTrackerTest() override = default;
void SetUp() override {
browser_process_ = TestingBrowserProcess::GetGlobal();
sb_service_ =
base::MakeRefCounted<safe_browsing::TestSafeBrowsingService>();
sb_service_->SetUseTestUrlLoaderFactory(true);
// Set sb_service before the ChromeRenderViewHostTestHarness::SetUp(),
// because it is needed to construct ping manager.
browser_process_->SetSafeBrowsingService(sb_service_.get());
ChromeRenderViewHostTestHarness::SetUp();
ui_manager_ = new StrictMock<MockSafeBrowsingUIManager>();
phishy_interaction_tracker_ =
base::WrapUnique(new PhishyInteractionTracker(web_contents()));
phishy_interaction_tracker_->SetUIManagerForTesting(ui_manager_.get());
phishy_interaction_tracker_->HandlePageChanged();
}
void TearDown() override {
browser_process_->SetSafeBrowsingService(nullptr);
// Delete the tracker object on the UI thread and release the
// SafeBrowsingService.
sb_service_.reset();
ui_manager_.reset();
phishy_interaction_tracker_.reset();
ChromeRenderViewHostTestHarness::TearDown();
}
Profile* profile() {
return Profile::FromBrowserContext(web_contents()->GetBrowserContext());
}
security_interstitials::UnsafeResource MakeUnsafeResource(const char* url) {
security_interstitials::UnsafeResource resource;
resource.url = GURL(url);
resource.threat_type =
safe_browsing::SBThreatType::SB_THREAT_TYPE_URL_CLIENT_SIDE_PHISHING;
return resource;
}
std::unique_ptr<safe_browsing::ClientSafeBrowsingReportRequest>
GetActualRequest(const network::ResourceRequest& request) {
std::string request_string = GetUploadData(request);
auto actual_request =
std::make_unique<safe_browsing::ClientSafeBrowsingReportRequest>();
actual_request->ParseFromString(request_string);
return actual_request;
}
void TriggerClickEvent() {
blink::WebMouseEvent mouse_event = blink::WebMouseEvent(
blink::WebInputEvent::Type::kMouseDown, gfx::PointF(), gfx::PointF(),
blink::WebPointerProperties::Button::kBack, 0, 0,
base::TimeTicks::Now());
phishy_interaction_tracker_->HandleInputEvent(mouse_event);
}
void TriggerKeyEvent() {
input::NativeWebKeyboardEvent key_event(
blink::WebKeyboardEvent::Type::kChar,
blink::WebInputEvent::kNoModifiers, base::TimeTicks::Now());
phishy_interaction_tracker_->HandleInputEvent(key_event);
}
void TriggerPasteEvent() { phishy_interaction_tracker_->HandlePasteEvent(); }
void SetNullDelayForTest() {
phishy_interaction_tracker_->SetInactivityDelayForTesting(
base::Milliseconds(0));
}
void VerifyInteraction(
const safe_browsing::ClientSafeBrowsingReportRequest& report,
const safe_browsing::ClientSafeBrowsingReportRequest::
PhishySiteInteraction::PhishySiteInteractionType&
expected_interaction_type,
const int& expected_occurrence_count) {
// Find the interaction within the report by comparing
// security_interstitial_interaction.
for (auto interaction : report.phishy_site_interactions()) {
if (interaction.phishy_site_interaction_type() ==
expected_interaction_type) {
EXPECT_EQ(interaction.occurrence_count(), expected_occurrence_count);
if (expected_occurrence_count == 1) {
EXPECT_EQ(interaction.first_interaction_timestamp_msec(),
interaction.last_interaction_timestamp_msec());
} else {
EXPECT_LE(interaction.first_interaction_timestamp_msec(),
interaction.last_interaction_timestamp_msec());
}
break;
}
}
}
void VerifyPhishyInteractionReport(
const safe_browsing::ClientSafeBrowsingReportRequest& report,
int expected_click_occurrences,
int expected_key_occurrences,
int expected_paste_occurrences) {
ASSERT_EQ(report.type(), safe_browsing::ClientSafeBrowsingReportRequest::
PHISHY_SITE_INTERACTIONS);
ASSERT_EQ(report.phishy_site_interactions().size(), 3);
VerifyInteraction(report,
safe_browsing::ClientSafeBrowsingReportRequest::
PhishySiteInteraction::PHISHY_CLICK_EVENT,
expected_click_occurrences);
VerifyInteraction(report,
safe_browsing::ClientSafeBrowsingReportRequest::
PhishySiteInteraction::PHISHY_KEY_EVENT,
expected_key_occurrences);
VerifyInteraction(report,
safe_browsing::ClientSafeBrowsingReportRequest::
PhishySiteInteraction::PHISHY_PASTE_EVENT,
expected_paste_occurrences);
}
protected:
raw_ptr<TestingBrowserProcess> browser_process_;
#if BUILDFLAG(IS_CHROMEOS)
// Local state is needed to construct ProxyConfigService, which is a
// dependency of PingManager on ChromeOS.
ScopedTestingLocalState scoped_testing_local_state_{
TestingBrowserProcess::GetGlobal()};
#endif // BUILDFLAG(IS_CHROMEOS)
scoped_refptr<safe_browsing::TestSafeBrowsingService> sb_service_;
std::unique_ptr<PhishyInteractionTracker> phishy_interaction_tracker_;
scoped_refptr<MockSafeBrowsingUIManager> ui_manager_;
safe_browsing::ChromePingManagerAllowerForTesting allow_ping_manager_;
};
TEST_F(PhishyInteractionTrackerTest, CheckHistogramCountsOnPhishyUserEvents) {
base::HistogramTester histogram_tester_;
security_interstitials::UnsafeResource resource = MakeUnsafeResource(kBadURL);
safe_browsing::SBThreatType threat_type;
EXPECT_TRUE(ui_manager_->IsUrlAllowlistedOrPendingForWebContents(
resource.url, /*entry=*/nullptr,
safe_browsing::unsafe_resource_util::GetWebContentsForResource(resource),
true, &threat_type));
const std::string phishy_interaction_histogram = "SafeBrowsing.PhishySite.";
const int kExpectedClickEventCount = 3;
const int kExpectedKeyEventCount = 5;
const int kExpectedPasteEventCount = 2;
// Trigger kExpectedClickEventCount mouse events.
for (int i = 0; i < kExpectedClickEventCount; ++i) {
TriggerClickEvent();
}
// Trigger kExpectedKeyEventCount key events.
for (int i = 0; i < kExpectedKeyEventCount; ++i) {
TriggerKeyEvent();
}
// Trigger kExpectedPasteEventCount - 1 paste events so we can trigger a
// paste below.
for (int i = 0; i < kExpectedPasteEventCount - 1; ++i) {
TriggerPasteEvent();
}
// Set a null delay so that histograms get logged after this last user event.
SetNullDelayForTest();
TriggerPasteEvent();
base::RunLoop run_loop;
base::StatisticsRecorder::ScopedHistogramSampleObserver observer(
"SafeBrowsing.PhishySite.PasteEventCount", run_loop.QuitClosure());
run_loop.Run();
histogram_tester_.ExpectUniqueSample(
phishy_interaction_histogram + "ClickEventCount",
kExpectedClickEventCount, 1);
histogram_tester_.ExpectUniqueSample(
phishy_interaction_histogram + "KeyEventCount", kExpectedKeyEventCount,
1);
histogram_tester_.ExpectUniqueSample(
phishy_interaction_histogram + "PasteEventCount",
kExpectedPasteEventCount, 1);
}
TEST_F(PhishyInteractionTrackerTest, CheckPhishyUserInteractionClientReport) {
safe_browsing::SetExtendedReportingPrefForTests(profile()->GetPrefs(), true);
const int kExpectedClickEventCount = 4;
const int kExpectedKeyEventCount = 1;
const int kExpectedPasteEventCount = 3;
auto* ping_manager =
safe_browsing::ChromePingManagerFactory::GetForBrowserContext(profile());
network::TestURLLoaderFactory test_url_loader_factory;
base::RunLoop run_loop;
test_url_loader_factory.SetInterceptor(
base::BindLambdaForTesting([&](const network::ResourceRequest& request) {
std::unique_ptr<safe_browsing::ClientSafeBrowsingReportRequest>
actual_request = GetActualRequest(request);
VerifyPhishyInteractionReport(
*actual_request.get(), kExpectedClickEventCount,
kExpectedKeyEventCount, kExpectedPasteEventCount);
run_loop.Quit();
}));
ping_manager->SetURLLoaderFactoryForTesting(
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory));
// Trigger kExpectedClickEventCount mouse events.
for (int i = 0; i < kExpectedClickEventCount; ++i) {
TriggerClickEvent();
}
// Trigger kExpectedKeyEventCount key events.
for (int i = 0; i < kExpectedKeyEventCount; ++i) {
TriggerKeyEvent();
}
// Trigger kExpectedPasteEventCount - 1 paste events so we can trigger a
// paste below.
for (int i = 0; i < kExpectedPasteEventCount - 1; ++i) {
TriggerPasteEvent();
}
// Set a null delay so that histograms get logged after this last user event.
SetNullDelayForTest();
TriggerPasteEvent();
run_loop.Run();
}
|