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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
|
// Copyright 2022 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/enterprise/connectors/analysis/file_transfer_analysis_delegate.h"
#include <numeric>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
#include "chrome/browser/ash/file_manager/volume_manager.h"
#include "chrome/browser/enterprise/connectors/analysis/files_request_handler.h"
#include "chrome/browser/enterprise/connectors/analysis/source_destination_matcher_ash.h"
#include "chrome/browser/enterprise/connectors/connectors_service.h"
#include "chrome/browser/extensions/api/safe_browsing_private/safe_browsing_private_event_router.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/safe_browsing/cloud_content_scanning/binary_upload_service.h"
#include "chrome/browser/safe_browsing/cloud_content_scanning/deep_scanning_utils.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "components/enterprise/connectors/core/analysis_settings.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "storage/browser/file_system/file_system_context.h"
#include "storage/browser/file_system/file_system_operation.h"
#include "storage/browser/file_system/file_system_operation_runner.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/browser/file_system/recursive_operation_delegate.h"
using safe_browsing::BinaryUploadService;
namespace {
enterprise_connectors::FileTransferAnalysisDelegate::
FileTransferAnalysisDelegateFactory&
GetFactoryStorage() {
static base::NoDestructor<
enterprise_connectors::FileTransferAnalysisDelegate::
FileTransferAnalysisDelegateFactory>
factory;
return *factory;
}
// GetFileURLsDelegate is used to get the `FileSystemURL`s of all files lying
// within `root`. A vector of these urls is passed to `callback`. If `root` is
// a file, the vector will only contain `root`. If `root` is a directory all
// files lying in that directory or any descended subdirectory are passed to
// `callback`.
class GetFileURLsDelegate final : public storage::RecursiveOperationDelegate {
public:
using FileURLsCallback =
base::OnceCallback<void(std::vector<storage::FileSystemURL>)>;
GetFileURLsDelegate(storage::FileSystemContext* file_system_context,
const storage::FileSystemURL& root,
FileURLsCallback callback)
: RecursiveOperationDelegate(file_system_context),
root_(root),
callback_(std::move(callback)) {}
GetFileURLsDelegate(const GetFileURLsDelegate&) = delete;
GetFileURLsDelegate& operator=(const GetFileURLsDelegate&) = delete;
~GetFileURLsDelegate() override = default;
// RecursiveOperationDelegate:
void Run() override { NOTREACHED(); }
void RunRecursively() override {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
StartRecursiveOperation(root_,
storage::FileSystemOperation::ERROR_BEHAVIOR_ABORT,
base::BindOnce(&GetFileURLsDelegate::Completed,
weak_ptr_factory_.GetWeakPtr()));
}
void ProcessFile(const storage::FileSystemURL& url,
StatusCallback callback) override {
if (error_url_.is_valid() && error_url_ == url) {
std::move(callback).Run(base::File::FILE_ERROR_FAILED);
return;
}
file_system_context()->operation_runner()->GetMetadata(
url, {storage::FileSystemOperation::GetMetadataField::kIsDirectory},
base::BindOnce(&GetFileURLsDelegate::OnGetMetadata,
weak_ptr_factory_.GetWeakPtr(), url,
std::move(callback)));
}
void ProcessDirectory(const storage::FileSystemURL& url,
StatusCallback callback) override {
std::move(callback).Run(base::File::FILE_OK);
}
void PostProcessDirectory(const storage::FileSystemURL& url,
StatusCallback callback) override {
std::move(callback).Run(base::File::FILE_OK);
}
base::WeakPtr<storage::RecursiveOperationDelegate> AsWeakPtr() override {
return weak_ptr_factory_.GetWeakPtr();
}
private:
void OnGetMetadata(const storage::FileSystemURL& url,
StatusCallback callback,
base::File::Error result,
const base::File::Info& file_info) {
if (result != base::File::FILE_OK) {
std::move(callback).Run(result);
return;
}
if (file_info.is_directory) {
std::move(callback).Run(base::File::FILE_ERROR_NOT_A_FILE);
return;
}
file_urls_.push_back(url);
std::move(callback).Run(base::File::FILE_OK);
}
void Completed(base::File::Error result) {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(std::move(callback_), std::move(file_urls_)));
}
storage::FileSystemURL root_;
FileURLsCallback callback_;
storage::FileSystemURL error_url_;
std::vector<storage::FileSystemURL> file_urls_;
base::WeakPtrFactory<GetFileURLsDelegate> weak_ptr_factory_{this};
};
bool IsInSameFileSystem(Profile* profile,
storage::FileSystemURL source_url,
storage::FileSystemURL destination_url) {
// Cheap check: source file system url.
if (!source_url.IsInSameFileSystem(destination_url)) {
return false;
}
// For some URLs FileSystemURL's IsInSameFileSystem function returns false
// positives. Which `volume_manager` is able to properly determine.
file_manager::VolumeManager* const volume_manager =
file_manager::VolumeManager::Get(profile);
base::WeakPtr<file_manager::Volume> source_volume =
volume_manager->FindVolumeFromPath(source_url.path());
base::WeakPtr<file_manager::Volume> destination_volume =
volume_manager->FindVolumeFromPath(destination_url.path());
if (!source_volume || !destination_volume) {
// The source or destination volume don't exist, so we trust the
// FileSystemURL response, i.e., they lie in the same file system.
return true;
}
// If both volumes exist, we check whether their ID is the same.
return source_volume->volume_id() == destination_volume->volume_id();
}
} // namespace
namespace enterprise_connectors {
// static
FileTransferAnalysisDelegate::FileTransferAnalysisResult
FileTransferAnalysisDelegate::FileTransferAnalysisResult::Allowed() {
return FileTransferAnalysisResult(
Verdict::ALLOWED, /*final_result=*/std::nullopt, /*tag=*/std::string());
}
// static
FileTransferAnalysisDelegate::FileTransferAnalysisResult
FileTransferAnalysisDelegate::FileTransferAnalysisResult::Blocked(
FinalContentAnalysisResult final_result,
const std::string& tag) {
return FileTransferAnalysisResult(Verdict::BLOCKED, final_result, tag);
}
// static
FileTransferAnalysisDelegate::FileTransferAnalysisResult
FileTransferAnalysisDelegate::FileTransferAnalysisResult::Unknown() {
return FileTransferAnalysisResult(
Verdict::UNKNOWN, /*final_result=*/std::nullopt, /*tag=*/std::string());
}
const std::string&
FileTransferAnalysisDelegate::FileTransferAnalysisResult::tag() const {
return tag_;
}
const std::optional<FinalContentAnalysisResult>
FileTransferAnalysisDelegate::FileTransferAnalysisResult::final_result() const {
return final_result_;
}
FileTransferAnalysisDelegate::FileTransferAnalysisResult::
FileTransferAnalysisResult(
Verdict verdict,
std::optional<FinalContentAnalysisResult> final_result,
const std::string& tag)
: verdict_(verdict), final_result_(final_result), tag_(tag) {}
FileTransferAnalysisDelegate::FileTransferAnalysisResult::
~FileTransferAnalysisResult() = default;
FileTransferAnalysisDelegate::FileTransferAnalysisResult::
FileTransferAnalysisResult(const FileTransferAnalysisResult& other) =
default;
FileTransferAnalysisDelegate::FileTransferAnalysisResult&
FileTransferAnalysisDelegate::FileTransferAnalysisResult::operator=(
FileTransferAnalysisResult&& other) = default;
bool FileTransferAnalysisDelegate::FileTransferAnalysisResult::IsAllowed()
const {
return verdict_ == Verdict::ALLOWED;
}
bool FileTransferAnalysisDelegate::FileTransferAnalysisResult::IsBlocked()
const {
return verdict_ == Verdict::BLOCKED;
}
bool FileTransferAnalysisDelegate::FileTransferAnalysisResult::IsUnknown()
const {
return verdict_ == Verdict::UNKNOWN;
}
// static
std::unique_ptr<FileTransferAnalysisDelegate>
FileTransferAnalysisDelegate::Create(
safe_browsing::DeepScanAccessPoint access_point,
storage::FileSystemURL source_url,
storage::FileSystemURL destination_url,
Profile* profile,
storage::FileSystemContext* file_system_context,
AnalysisSettings settings) {
if (GetFactoryStorage().is_null()) {
// This code path is always reached outside of tests.
return base::WrapUnique(
new enterprise_connectors::FileTransferAnalysisDelegate(
access_point, source_url, destination_url, profile,
file_system_context, std::move(settings)));
} else {
// Only in tests, GetFactoryStorage() can be set and this code path can be
// reached.
// Pass `idx` in addition to the constructor parameters to make testing
// easier.
return GetFactoryStorage().Run(access_point, source_url, destination_url,
profile, file_system_context,
std::move(settings));
}
}
// static
void FileTransferAnalysisDelegate::SetFactoryForTesting(
FileTransferAnalysisDelegateFactory factory) {
GetFactoryStorage() = factory;
}
// static
std::vector<std::optional<AnalysisSettings>>
FileTransferAnalysisDelegate::IsEnabledVec(
Profile* profile,
const std::vector<storage::FileSystemURL>& source_urls,
storage::FileSystemURL destination_url) {
DCHECK(profile);
auto* service =
enterprise_connectors::ConnectorsServiceFactory::GetForBrowserContext(
profile);
// If the corresponding Connector policy isn't set, don't perform scans.
if (!service ||
!service->IsConnectorEnabled(enterprise_connectors::FILE_TRANSFER)) {
// Return an empty vector.
return {};
}
std::vector<std::optional<AnalysisSettings>> settings(source_urls.size());
bool at_least_one_enabled = false;
for (size_t i = 0; i < source_urls.size(); ++i) {
if (IsInSameFileSystem(profile, source_urls[i], destination_url)) {
// Scanning is disabled for transfers on the same file system.
continue;
}
settings[i] = service->GetAnalysisSettings(
source_urls[i], destination_url, enterprise_connectors::FILE_TRANSFER);
at_least_one_enabled |= settings[i].has_value();
}
if (!at_least_one_enabled) {
// Return an empty vector.
return {};
}
return settings;
}
FileTransferAnalysisDelegate::FileTransferAnalysisResult
FileTransferAnalysisDelegate::GetAnalysisResultAfterScan(
storage::FileSystemURL url) {
// Should only be called for blocking scans.
DCHECK_EQ(settings_.block_until_verdict, BlockUntilVerdict::kBlock);
DCHECK_EQ(results_.size(), scanning_urls_.size());
for (size_t i = 0; i < scanning_urls_.size(); ++i) {
if (scanning_urls_[i] == url) {
if (results_[i].complies ||
(warning_is_bypassed_ &&
results_[i].final_result == FinalContentAnalysisResult::WARNING)) {
return FileTransferAnalysisResult::Allowed();
}
return FileTransferAnalysisResult::Blocked(results_[i].final_result,
results_[i].tag);
}
}
return FileTransferAnalysisResult::Unknown();
}
std::vector<storage::FileSystemURL>
FileTransferAnalysisDelegate::GetWarnedFiles() const {
// Should only be called for blocking scans.
DCHECK_EQ(settings_.block_until_verdict, BlockUntilVerdict::kBlock);
DCHECK_EQ(results_.size(), scanning_urls_.size());
std::vector<storage::FileSystemURL> warned_files;
for (size_t i = 0; i < scanning_urls_.size(); ++i) {
if (!results_[i].complies &&
results_[i].final_result == FinalContentAnalysisResult::WARNING) {
warned_files.push_back(scanning_urls_[i]);
}
}
return warned_files;
}
void FileTransferAnalysisDelegate::UploadData(
base::OnceClosure completion_callback) {
callback_ = std::move(completion_callback);
DCHECK(!callback_.is_null());
// This will start aggregating the needed file urls and pass them to
// OnGotFileSourceURLs.
// The usage of the WeakPtr is only safe if `get_file_urls_delegate_` is
// deleted on the IOThread.
content::GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&storage::RecursiveOperationDelegate::RunRecursively,
get_file_urls_delegate_->AsWeakPtr()));
}
FileTransferAnalysisDelegate::FileTransferAnalysisDelegate(
safe_browsing::DeepScanAccessPoint access_point,
storage::FileSystemURL source_url,
storage::FileSystemURL destination_url,
Profile* profile,
storage::FileSystemContext* file_system_context,
AnalysisSettings settings)
: settings_{std::move(settings)},
profile_{profile},
access_point_{access_point},
source_url_(std::move(source_url)),
destination_url_{std::move(destination_url)} {
DCHECK(profile);
// For blocking scans, scanning is performed before the copy/move and
// thus scanning should be performed on the source.
// For non-blocking report-only scans, scanning is performed after the
// copy/move and thus scanning should be performed on the destination.
auto scanning_url = settings_.block_until_verdict == BlockUntilVerdict::kBlock
? source_url_
: destination_url_;
get_file_urls_delegate_ = std::make_unique<GetFileURLsDelegate>(
file_system_context, scanning_url,
base::BindOnce(&FileTransferAnalysisDelegate::OnGotFileURLs,
weak_ptr_factory_.GetWeakPtr()));
}
void FileTransferAnalysisDelegate::BypassWarnings(
std::optional<std::u16string> user_justification) {
if (!warned_file_indices_.empty()) {
request_handler_->ReportWarningBypass(user_justification);
warning_is_bypassed_ = true;
}
}
void FileTransferAnalysisDelegate::Cancel(bool warning) {
// TODO(crbug.com/1340313)
}
std::optional<std::u16string> FileTransferAnalysisDelegate::GetCustomMessage(
const std::string& tag) const {
auto it = settings_.tags.find(tag);
if (it == settings_.tags.end()) {
return std::nullopt;
}
const std::u16string& message = it->second.custom_message.message;
if (message.empty()) {
return std::nullopt;
}
return message;
}
std::optional<GURL> FileTransferAnalysisDelegate::GetCustomLearnMoreUrl(
const std::string& tag) const {
auto it = settings_.tags.find(tag);
if (it == settings_.tags.end()) {
return std::nullopt;
}
const GURL& learn_more_url = it->second.custom_message.learn_more_url;
if (!learn_more_url.is_valid()) {
return std::nullopt;
}
return learn_more_url;
}
bool FileTransferAnalysisDelegate::BypassRequiresJustification(
const std::string& tag) const {
auto it = settings_.tags.find(tag);
if (it == settings_.tags.end()) {
return false;
}
return it->second.requires_justification;
}
FilesRequestHandler*
FileTransferAnalysisDelegate::GetFilesRequestHandlerForTesting() {
return request_handler_.get();
}
const AnalysisSettings& FileTransferAnalysisDelegate::settings() const {
return settings_;
}
signin::IdentityManager* FileTransferAnalysisDelegate::identity_manager() const {
return IdentityManagerFactory::GetForProfile(profile_);
}
int FileTransferAnalysisDelegate::user_action_requests_count() const {
return scanning_urls_.size();
}
std::string FileTransferAnalysisDelegate::tab_title() const {
return "";
}
std::string FileTransferAnalysisDelegate::user_action_id() const {
return "";
}
std::string FileTransferAnalysisDelegate::email() const {
return GetProfileEmail(profile_);
}
std::string FileTransferAnalysisDelegate::url() const {
return "";
}
const GURL& FileTransferAnalysisDelegate::tab_url() const {
return GURL::EmptyGURL();
}
ContentAnalysisRequest::Reason FileTransferAnalysisDelegate::reason() const {
return ContentAnalysisRequest::UNKNOWN;
}
google::protobuf::RepeatedPtrField<::safe_browsing::ReferrerChainEntry>
FileTransferAnalysisDelegate::referrer_chain() const {
return google::protobuf::RepeatedPtrField<
::safe_browsing::ReferrerChainEntry>();
}
google::protobuf::RepeatedPtrField<std::string>
FileTransferAnalysisDelegate::frame_url_chain() const {
return {};
}
void FileTransferAnalysisDelegate::OnGotFileURLs(
std::vector<storage::FileSystemURL> scanning_urls) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
scanning_urls_ = std::move(scanning_urls);
if (scanning_urls_.empty()) {
ContentAnalysisCompleted(std::vector<RequestHandlerResult>());
return;
}
std::vector<base::FilePath> paths;
for (const storage::FileSystemURL& url : scanning_urls_) {
paths.push_back(url.path());
}
request_handler_ = FilesRequestHandler::Create(
this,
safe_browsing::BinaryUploadService::GetForProfile(profile_, settings_),
profile_, GURL{},
SourceDestinationMatcherAsh::GetVolumeDescriptionFromPath(
profile_, source_url_.path()),
SourceDestinationMatcherAsh::GetVolumeDescriptionFromPath(
profile_, destination_url_.path()),
/*content_transfer_method=*/std::string(), access_point_,
std::move(paths),
base::BindOnce(&FileTransferAnalysisDelegate::ContentAnalysisCompleted,
weak_ptr_factory_.GetWeakPtr()));
request_handler_->UploadData();
}
FileTransferAnalysisDelegate::~FileTransferAnalysisDelegate() {
if (get_file_urls_delegate_) {
// To ensure that there are no race conditions, we post the deletion of
// `get_file_urls_delegate_` to the IO thread.
content::GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
[](std::unique_ptr<storage::RecursiveOperationDelegate> delegate) {
// Do nothing.
// At the end of this task `get_file_urls_delegate_`
// will be deleted.
},
std::move(get_file_urls_delegate_)));
}
}
void FileTransferAnalysisDelegate::ContentAnalysisCompleted(
std::vector<RequestHandlerResult> results) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
results_ = std::move(results);
// Don't show warning here, as we use multiple FileTransferAnalysisDelegate's
// and only want to show one warning.
for (size_t index = 0; index < results_.size(); ++index) {
FinalContentAnalysisResult result = results_[index].final_result;
if (result == FinalContentAnalysisResult::WARNING) {
warned_file_indices_.push_back(index);
}
}
DCHECK(!callback_.is_null());
std::move(callback_).Run();
}
} // namespace enterprise_connectors
|