File: archive_analyzer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (167 lines) | stat: -rw-r--r-- 5,453 bytes parent folder | download | duplicates (6)
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
// 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/utility/safe_browsing/archive_analyzer.h"

#include "base/feature_list.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "chrome/common/safe_browsing/archive_analyzer_results.h"
#include "chrome/utility/safe_browsing/seven_zip_analyzer.h"
#include "chrome/utility/safe_browsing/zip_analyzer.h"
#include "components/safe_browsing/content/common/proto/download_file_types.pb.h"
#include "components/safe_browsing/core/common/features.h"

#if BUILDFLAG(IS_MAC)
#include "chrome/utility/safe_browsing/mac/dmg_analyzer.h"
#endif

#if USE_UNRAR
#include "chrome/utility/safe_browsing/rar_analyzer.h"
#endif

namespace safe_browsing {

// static
std::unique_ptr<ArchiveAnalyzer> ArchiveAnalyzer::CreateForArchiveType(
    DownloadFileType_InspectionType file_type) {
  if (file_type == DownloadFileType::ZIP) {
    return std::make_unique<ZipAnalyzer>();
  } else if (file_type == DownloadFileType::SEVEN_ZIP) {
    return std::make_unique<SevenZipAnalyzer>();
  }

#if BUILDFLAG(IS_MAC)
  if (file_type == DownloadFileType::DMG) {
    return std::make_unique<dmg::DMGAnalyzer>();
  }
#endif

#if USE_UNRAR
  if (file_type == DownloadFileType::RAR) {
    return std::make_unique<RarAnalyzer>();
  }
#endif

  return nullptr;
}

ArchiveAnalyzer::ArchiveAnalyzer() = default;
ArchiveAnalyzer::~ArchiveAnalyzer() = default;

void ArchiveAnalyzer::Analyze(
    base::File archive_file,
    base::FilePath relative_path,
    const std::optional<std::string>& password,
    FinishedAnalysisCallback finished_analysis_callback,
    GetTempFileCallback get_temp_file_callback,
    ArchiveAnalyzerResults* results) {
  archive_file_ = std::move(archive_file);
  root_path_ = relative_path;
  password_ = password;
  finished_analysis_callback_ = std::move(finished_analysis_callback);
  get_temp_file_callback_ = std::move(get_temp_file_callback);
  results_ = results;

  Init();
}

void ArchiveAnalyzer::SetResultsForTesting(ArchiveAnalyzerResults* results) {
  results_ = results;
}

void ArchiveAnalyzer::SetFinishedCallbackForTesting(
    FinishedAnalysisCallback callback) {
  finished_analysis_callback_ = std::move(callback);
}

base::File& ArchiveAnalyzer::GetArchiveFile() {
  return archive_file_;
}
void ArchiveAnalyzer::GetTempFile(
    base::OnceCallback<void(base::File)> callback) {
  get_temp_file_callback_.Run(std::move(callback));
}
const base::FilePath& ArchiveAnalyzer::GetRootPath() const {
  return root_path_;
}

bool ArchiveAnalyzer::UpdateResultsForEntry(base::File entry,
                                            base::FilePath path,
                                            int file_length,
                                            bool is_encrypted,
                                            bool is_directory,
                                            bool contents_valid) {
  if (!is_encrypted) {
    nested_analyzer_ = ArchiveAnalyzer::CreateForArchiveType(GetFileType(path));
    if (nested_analyzer_) {
      // Archive analyzers expect to start at the beginning of the
      // archive, but we may be at the end.
      entry.Seek(base::File::FROM_BEGIN, 0);
      nested_analyzer_->Analyze(
          entry.Duplicate(), path, password(),
          base::BindOnce(&ArchiveAnalyzer::NestedAnalysisFinished, GetWeakPtr(),
                         entry.Duplicate(), path, file_length),
          get_temp_file_callback_, results_);
      return false;
    }
  }

  if (is_directory) {
    results_->directory_count++;
  } else {
    results_->file_count++;
  }

  UpdateArchiveAnalyzerResultsWithFile(path, &entry, file_length, is_encrypted,
                                       is_directory, contents_valid,
                                       IsTopLevelArchive(), results_);
  return true;
}

void ArchiveAnalyzer::InitComplete(ArchiveAnalysisResult result) {
  if (result != ArchiveAnalysisResult::kValid) {
    results_->success = false;
    results_->analysis_result = result;
    std::move(finished_analysis_callback_).Run();
    return;
  }

  results_->success = true;
  results_->analysis_result = ArchiveAnalysisResult::kValid;

  if (ResumeExtraction()) {
    std::move(finished_analysis_callback_).Run();
  }
}

void ArchiveAnalyzer::NestedAnalysisFinished(base::File entry,
                                             base::FilePath path,
                                             int entry_size) {
  // `results_->success` will contain the latest analyzer's success
  // status and can be used to determine if the nester archive unpacked
  // successfully.
  if (!results_->success) {
    results_->has_archive = true;
    results_->file_count++;
    results_->archived_archive_filenames.push_back(path.BaseName());
    ClientDownloadRequest::ArchivedBinary* archived_archive =
        results_->archived_binary.Add();
    archived_archive->set_download_type(ClientDownloadRequest::ARCHIVE);
    archived_archive->set_is_encrypted(false);
    archived_archive->set_is_archive(true);
    SetNameForContainedFile(path, archived_archive);
    SetLengthAndDigestForContainedFile(&entry, entry_size, archived_archive);
  }

  if (ResumeExtraction()) {
    std::move(finished_analysis_callback_).Run();
  }
}

bool ArchiveAnalyzer::IsTopLevelArchive() const {
  return root_path_.empty();
}

}  // namespace safe_browsing