chromium/chrome/browser/safe_browsing/download_protection/file_analyzer.cc

// Copyright 2018 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/download_protection/file_analyzer.h"

#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#include "chrome/browser/file_util_service.h"
#include "chrome/browser/safe_browsing/download_protection/download_protection_util.h"
#include "chrome/common/safe_browsing/archive_analyzer_results.h"
#include "chrome/common/safe_browsing/download_type_util.h"
#include "components/safe_browsing/content/common/file_type_policies.h"
#include "components/safe_browsing/core/common/features.h"
#include "content/public/browser/browser_thread.h"
#include "url/gurl.h"

namespace safe_browsing {

namespace {

BrowserThread;

FileAnalyzer::Results ExtractFileFeatures(
    scoped_refptr<BinaryFeatureExtractor> binary_feature_extractor,
    base::FilePath file_path) {}

}  // namespace

FileAnalyzer::Results::Results() = default;
FileAnalyzer::Results::~Results() {}
FileAnalyzer::Results::Results(const FileAnalyzer::Results& other) = default;

FileAnalyzer::FileAnalyzer(
    scoped_refptr<BinaryFeatureExtractor> binary_feature_extractor)
    :{}

FileAnalyzer::~FileAnalyzer() {}

void FileAnalyzer::Start(const base::FilePath& target_path,
                         const base::FilePath& tmp_path,
                         base::optional_ref<const std::string> password,
                         base::OnceCallback<void(Results)> callback) {}

void FileAnalyzer::StartExtractFileFeatures() {}

void FileAnalyzer::OnFileAnalysisFinished(FileAnalyzer::Results results) {}

void FileAnalyzer::StartExtractZipFeatures() {}

void FileAnalyzer::OnZipAnalysisFinished(
    const ArchiveAnalyzerResults& archive_results) {}

void FileAnalyzer::StartExtractRarFeatures() {}

void FileAnalyzer::OnRarAnalysisFinished(
    const ArchiveAnalyzerResults& archive_results) {}

#if BUILDFLAG(IS_MAC)
// This is called for .DMGs and other files that can be parsed by
// SandboxedDMGAnalyzer.
void FileAnalyzer::StartExtractDmgFeatures() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  // Directly use 'dmg' extension since download file may not have any
  // extension, but has still been deemed a DMG through file type sniffing.
  dmg_analyzer_ = SandboxedDMGAnalyzer::CreateAnalyzer(
      tmp_path_,
      FileTypePolicies::GetInstance()->GetMaxFileSizeToAnalyze("dmg"),
      base::BindRepeating(&FileAnalyzer::OnDmgAnalysisFinished,
                          weakptr_factory_.GetWeakPtr()),
      LaunchFileUtilService());
  dmg_analyzer_->Start();
}

void FileAnalyzer::ExtractFileOrDmgFeatures(
    bool download_file_has_koly_signature) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  if (download_file_has_koly_signature) {
    StartExtractDmgFeatures();
  } else {
    StartExtractFileFeatures();
  }
}

void FileAnalyzer::OnDmgAnalysisFinished(
    const safe_browsing::ArchiveAnalyzerResults& archive_results) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  base::UmaHistogramEnumeration("SBClientDownload.DmgArchiveAnalysisResult",
                                archive_results.analysis_result);
  LogAnalysisDurationWithAndWithoutSuffix("Dmg");

  if (archive_results.signature_blob.size() > 0) {
    results_.disk_image_signature =
        std::vector<uint8_t>(archive_results.signature_blob);
  }

  results_.detached_code_signatures.CopyFrom(
      archive_results.detached_code_signatures);

  // Even if !results.success, some of the DMG may have been parsed.
  results_.archived_executable = archive_results.has_executable;
  results_.archived_archive = archive_results.has_archive;
  results_.archived_binaries =
      SelectArchiveEntries(archive_results.archived_binary);

  if (archive_results.success) {
    results_.type = ClientDownloadRequest::MAC_EXECUTABLE;
  } else {
    results_.type = ClientDownloadRequest::MAC_ARCHIVE_FAILED_PARSING;
  }

  if (archive_results.success) {
    results_.archive_summary.set_parser_status(
        ClientDownloadRequest::ArchiveSummary::VALID);
  } else if (archive_results.analysis_result ==
             ArchiveAnalysisResult::kTimeout) {
    results_.archive_summary.set_parser_status(
        ClientDownloadRequest::ArchiveSummary::PARSER_TIMED_OUT);
  } else if (archive_results.analysis_result ==
             ArchiveAnalysisResult::kTooLarge) {
    results_.archive_summary.set_parser_status(
        ClientDownloadRequest::ArchiveSummary::TOO_LARGE);
  }

  results_.archive_summary.set_is_encrypted(
      archive_results.encryption_info.is_encrypted);
  results_.encryption_info = archive_results.encryption_info;

  std::move(callback_).Run(std::move(results_));
}
#endif  // BUILDFLAG(IS_MAC)

void FileAnalyzer::StartExtractSevenZipFeatures() {}

void FileAnalyzer::OnSevenZipAnalysisFinished(
    const ArchiveAnalyzerResults& archive_results) {}

void FileAnalyzer::LogAnalysisDurationWithAndWithoutSuffix(
    const std::string& suffix) {}

}  // namespace safe_browsing