|
| 1 | +// Copyright 2024 The Chromium Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "chrome/browser/safe_browsing/external_app_redirect_checking.h" |
| 6 | + |
| 7 | +#include "base/json/values_util.h" |
| 8 | +#include "chrome/browser/browser_process.h" |
| 9 | +#include "chrome/browser/profiles/profile.h" |
| 10 | +#include "chrome/browser/safe_browsing/chrome_user_population_helper.h" |
| 11 | +#include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager_factory.h" |
| 12 | +#include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 13 | +#include "components/prefs/pref_service.h" |
| 14 | +#include "components/safe_browsing/content/browser/safe_browsing_navigation_observer_manager.h" |
| 15 | +#include "components/safe_browsing/core/browser/db/database_manager.h" |
| 16 | +#include "components/safe_browsing/core/common/proto/csd.pb.h" |
| 17 | +#include "components/safe_browsing/core/common/safe_browsing_prefs.h" |
| 18 | +#include "content/public/browser/web_contents.h" |
| 19 | +#include "services/preferences/public/cpp/dictionary_value_update.h" |
| 20 | +#include "services/preferences/public/cpp/scoped_pref_update.h" |
| 21 | + |
| 22 | +namespace safe_browsing { |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +constexpr base::TimeDelta kRecentVisitThreshold = base::Days(30); |
| 27 | + |
| 28 | +// Keys in base::Value::Dict cannot contain periods. |
| 29 | +std::string SanitizeAppName(std::string_view app_name) { |
| 30 | + std::string sanitized; |
| 31 | + sanitized.reserve(app_name.size()); |
| 32 | + for (char c : app_name) { |
| 33 | + if (c != '.') { |
| 34 | + sanitized += c; |
| 35 | + } else { |
| 36 | + sanitized += '_'; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return sanitized; |
| 41 | +} |
| 42 | + |
| 43 | +bool HasRecentAppVisit(PrefService& prefs, std::string_view app_name) { |
| 44 | + const base::Value::Dict& timestamps = |
| 45 | + prefs.GetDict(prefs::kExternalAppRedirectTimestamps); |
| 46 | + std::optional<base::Time> app_timestamp = |
| 47 | + base::ValueToTime(timestamps.Find(SanitizeAppName(app_name))); |
| 48 | + if (!app_timestamp) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + return base::Time::Now() - *app_timestamp <= kRecentVisitThreshold; |
| 52 | +} |
| 53 | + |
| 54 | +void OnAllowlistCheckComplete( |
| 55 | + base::OnceCallback<void(bool)> callback, |
| 56 | + bool is_allowlisted, |
| 57 | + std::optional<SafeBrowsingDatabaseManager:: |
| 58 | + HighConfidenceAllowlistCheckLoggingDetails>) { |
| 59 | + std::move(callback).Run(!is_allowlisted); |
| 60 | +} |
| 61 | + |
| 62 | +} // namespace |
| 63 | + |
| 64 | +void ShouldReportExternalAppRedirect( |
| 65 | + scoped_refptr<SafeBrowsingDatabaseManager> database_manager, |
| 66 | + content::WebContents* web_contents, |
| 67 | + std::string_view app_name, |
| 68 | + base::OnceCallback<void(bool)> callback) { |
| 69 | + if (!base::FeatureList::IsEnabled(kExternalAppRedirectTelemetry)) { |
| 70 | + // Always run `callback` asynchronously, to make calling code simpler. |
| 71 | + base::SequencedTaskRunner::GetCurrentDefault()->PostTask( |
| 72 | + FROM_HERE, base::BindOnce(std::move(callback), false)); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + Profile* profile = |
| 77 | + Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 78 | + if (profile->IsOffTheRecord()) { |
| 79 | + base::SequencedTaskRunner::GetCurrentDefault()->PostTask( |
| 80 | + FROM_HERE, base::BindOnce(std::move(callback), false)); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + PrefService& prefs = *profile->GetPrefs(); |
| 85 | + if (!IsEnhancedProtectionEnabled(prefs)) { |
| 86 | + base::SequencedTaskRunner::GetCurrentDefault()->PostTask( |
| 87 | + FROM_HERE, base::BindOnce(std::move(callback), false)); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + if (app_name.empty() || HasRecentAppVisit(prefs, app_name)) { |
| 92 | + base::SequencedTaskRunner::GetCurrentDefault()->PostTask( |
| 93 | + FROM_HERE, base::BindOnce(std::move(callback), false)); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + database_manager->CheckUrlForHighConfidenceAllowlist( |
| 98 | + web_contents->GetLastCommittedURL(), |
| 99 | + base::BindOnce(&OnAllowlistCheckComplete, std::move(callback))); |
| 100 | +} |
| 101 | + |
| 102 | +void LogExternalAppRedirectTimestamp(PrefService& prefs, |
| 103 | + std::string_view app_name) { |
| 104 | + if (app_name.empty()) { |
| 105 | + return; |
| 106 | + } |
| 107 | + prefs::ScopedDictionaryPrefUpdate update( |
| 108 | + &prefs, prefs::kExternalAppRedirectTimestamps); |
| 109 | + update->Set(SanitizeAppName(app_name), base::TimeToValue(base::Time::Now())); |
| 110 | +} |
| 111 | + |
| 112 | +void CleanupExternalAppRedirectTimestamps(PrefService& prefs) { |
| 113 | + std::vector<std::string> to_remove; |
| 114 | + for (const auto [app_name, timestamp_value] : |
| 115 | + prefs.GetDict(prefs::kExternalAppRedirectTimestamps)) { |
| 116 | + std::optional<base::Time> timestamp = base::ValueToTime(timestamp_value); |
| 117 | + if (!timestamp || base::Time::Now() - *timestamp >= kRecentVisitThreshold) { |
| 118 | + to_remove.push_back(app_name); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + prefs::ScopedDictionaryPrefUpdate update( |
| 123 | + &prefs, prefs::kExternalAppRedirectTimestamps); |
| 124 | + for (const std::string& key : to_remove) { |
| 125 | + update->Remove(key); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +std::unique_ptr<ClientSafeBrowsingReportRequest> MakeExternalAppRedirectReport( |
| 130 | + content::WebContents* web_contents, |
| 131 | + std::string_view uri) { |
| 132 | + constexpr int kReferrerChainUserGestureLimit = 2; |
| 133 | + |
| 134 | + if (!web_contents) { |
| 135 | + return nullptr; |
| 136 | + } |
| 137 | + |
| 138 | + auto report = std::make_unique<ClientSafeBrowsingReportRequest>(); |
| 139 | + |
| 140 | + report->set_type(ClientSafeBrowsingReportRequest::EXTERNAL_APP_REDIRECT); |
| 141 | + report->set_url(std::string(uri)); |
| 142 | + report->set_page_url(web_contents->GetLastCommittedURL().spec()); |
| 143 | + *report->mutable_population() = GetUserPopulationForProfile( |
| 144 | + Profile::FromBrowserContext(web_contents->GetBrowserContext())); |
| 145 | + auto* navigation_observer_manager = |
| 146 | + SafeBrowsingNavigationObserverManagerFactory::GetForBrowserContext( |
| 147 | + web_contents->GetBrowserContext()); |
| 148 | + |
| 149 | + navigation_observer_manager->IdentifyReferrerChainByRenderFrameHost( |
| 150 | + web_contents->GetPrimaryMainFrame(), kReferrerChainUserGestureLimit, |
| 151 | + report->mutable_referrer_chain()); |
| 152 | + return report; |
| 153 | +} |
| 154 | + |
| 155 | +} // namespace safe_browsing |
0 commit comments