#include "components/safe_browsing/core/browser/hashprefix_realtime/hash_realtime_service.h"
#include "base/base64url.h"
#include "base/containers/contains.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/escape.h"
#include "base/strings/stringprintf.h"
#include "base/task/sequenced_task_runner.h"
#include "components/safe_browsing/core/browser/hashprefix_realtime/ohttp_key_service.h"
#include "components/safe_browsing/core/browser/verdict_cache_manager.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/safe_browsing/core/common/hashprefix_realtime/hash_realtime_utils.h"
#include "components/safe_browsing/core/common/proto/safebrowsingv5.pb.h"
#include "components/safe_browsing/core/common/utils.h"
#include "google_apis/google_api_keys.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/mojom/oblivious_http_request.mojom.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
namespace safe_browsing {
namespace {
const size_t kNumFailuresToEnforceBackoff = …;
const size_t kMinBackOffResetDurationInSeconds = …;
const size_t kMaxBackOffResetDurationInSeconds = …;
const size_t kLookupTimeoutDurationInSeconds = …;
void LogThreatInfoSize(int num_full_hash_matches, bool is_source_local_cache) { … }
SBThreatType MapFullHashDetailToSbThreatType(
const V5::FullHash::FullHashDetail& detail) { … }
class ObliviousHttpClient : public network::mojom::ObliviousHttpClient { … };
}
HashRealTimeService::HashRealTimeService(
base::RepeatingCallback<network::mojom::NetworkContext*()>
get_network_context,
VerdictCacheManager* cache_manager,
OhttpKeyService* ohttp_key_service,
WebUIDelegate* webui_delegate)
: … { … }
HashRealTimeService::~HashRealTimeService() = default;
bool HashRealTimeService::CanCheckUrl(const GURL& url) { … }
HashRealTimeService::SBThreatInfo::SBThreatInfo(SBThreatType threat_type,
int num_full_hash_matches)
: … { … }
HashRealTimeService::SBThreatInfo HashRealTimeService::DetermineSBThreatInfo(
const GURL& url,
const std::vector<V5::FullHash>& result_full_hashes) { … }
int HashRealTimeService::GetThreatSeverity(
const V5::FullHash::FullHashDetail& detail) { … }
bool HashRealTimeService::IsHashDetailMoreSevere(
const V5::FullHash::FullHashDetail& detail,
int baseline_severity) { … }
std::set<std::string> HashRealTimeService::GetHashPrefixesSet(
const GURL& url) const { … }
void HashRealTimeService::SearchCache(
std::set<std::string> hash_prefixes,
std::vector<std::string>* out_missing_hash_prefixes,
std::vector<V5::FullHash>* out_cached_full_hashes) const { … }
void HashRealTimeService::StartLookup(
const GURL& url,
HPRTLookupResponseCallback response_callback,
scoped_refptr<base::SequencedTaskRunner> callback_task_runner) { … }
void HashRealTimeService::StartLookupInternal(
const GURL& url,
std::unique_ptr<LookupCompleter> lookup_completer) { … }
void HashRealTimeService::OnGetOhttpKey(
std::unique_ptr<V5::SearchHashesRequest> request,
const GURL& url,
const std::vector<std::string>& hash_prefixes_in_request,
std::vector<V5::FullHash> result_full_hashes,
base::TimeTicks request_start_time,
std::unique_ptr<LookupCompleter> lookup_completer,
std::optional<std::string> key) { … }
void HashRealTimeService::OnOhttpComplete(
const GURL& url,
const std::vector<std::string>& hash_prefixes_in_request,
std::vector<V5::FullHash> result_full_hashes,
base::TimeTicks request_start_time,
std::unique_ptr<LookupCompleter> lookup_completer,
std::string ohttp_key,
std::optional<int> webui_delegate_token,
const std::optional<std::string>& response_body,
int net_error,
int response_code,
scoped_refptr<net::HttpResponseHeaders> headers,
bool ohttp_client_destructed_early) { … }
void HashRealTimeService::OnURLLoaderComplete(
const GURL& url,
const std::vector<std::string>& hash_prefixes_in_request,
std::vector<V5::FullHash> result_full_hashes,
base::TimeTicks request_start_time,
std::unique_ptr<LookupCompleter> lookup_completer,
std::unique_ptr<std::string> response_body,
int net_error,
int response_code,
std::optional<int> webui_delegate_token,
bool ohttp_client_destructed_early) { … }
base::expected<std::unique_ptr<V5::SearchHashesResponse>,
HashRealTimeService::OperationOutcome>
HashRealTimeService::ParseResponseAndUpdateBackoff(
int net_error,
int response_code,
std::unique_ptr<std::string> response_body,
const std::vector<std::string>& requested_hash_prefixes) const { … }
void HashRealTimeService::RemoveUnmatchedFullHashes(
std::unique_ptr<V5::SearchHashesResponse>& response,
const std::vector<std::string>& requested_hash_prefixes) const { … }
void HashRealTimeService::RemoveFullHashDetailsWithInvalidEnums(
std::unique_ptr<V5::SearchHashesResponse>& response) const { … }
base::expected<std::unique_ptr<V5::SearchHashesResponse>,
HashRealTimeService::OperationOutcome>
HashRealTimeService::ParseResponse(
int net_error,
int response_code,
std::unique_ptr<std::string> response_body,
const std::vector<std::string>& requested_hash_prefixes) const { … }
std::string HashRealTimeService::GetResourceUrl(
V5::SearchHashesRequest* request) const { … }
void HashRealTimeService::Shutdown() { … }
base::WeakPtr<HashRealTimeService> HashRealTimeService::GetWeakPtr() { … }
net::NetworkTrafficAnnotationTag
HashRealTimeService::GetTrafficAnnotationTagForOhttp() const { … }
HashRealTimeService::LookupCompleter::LookupCompleter(
HPRTLookupResponseCallback response_callback,
scoped_refptr<base::SequencedTaskRunner> response_callback_task_runner)
: … { … }
HashRealTimeService::LookupCompleter::~LookupCompleter() = default;
void HashRealTimeService::LookupCompleter::CompleteLookup(
bool is_lookup_successful,
std::optional<SBThreatType> sb_threat_type,
OperationOutcome operation_outcome) { … }
}