File: file_analysis_request.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 (332 lines) | stat: -rw-r--r-- 12,158 bytes parent folder | download | duplicates (3)
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
// Copyright 2020 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/cloud_content_scanning/file_analysis_request.h"

#include <algorithm>
#include <string_view>

#include "base/containers/span.h"
#include "base/feature_list.h"
#include "base/files/memory_mapped_file.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/enterprise/connectors/analysis/content_analysis_features.h"
#include "chrome/browser/file_util_service.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/common/safe_browsing/archive_analyzer_results.h"
#include "components/enterprise/obfuscation/core/download_obfuscator.h"
#include "components/file_access/scoped_file_access.h"
#include "components/file_access/scoped_file_access_delegate.h"
#include "components/safe_browsing/core/common/features.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "crypto/secure_hash.h"
#include "crypto/sha2.h"
#include "net/base/filename_util.h"
#include "net/base/mime_sniffer.h"
#include "net/base/mime_util.h"

namespace safe_browsing {

namespace {

constexpr size_t kReadFileChunkSize = 4096;
constexpr size_t kMaxUploadSizeMetricsKB = 500 * 1024;

std::string GetFileMimeType(const base::FilePath& path,
                            std::string_view first_bytes) {
  std::string sniffed_mime_type;
  bool sniff_found = net::SniffMimeType(
      std::string_view(first_bytes.data(),
                       std::min(first_bytes.size(),
                                static_cast<size_t>(net::kMaxBytesToSniff))),
      net::FilePathToFileURL(path),
      /*type_hint*/ std::string(), net::ForceSniffFileUrlsForHtml::kDisabled,
      &sniffed_mime_type);

  if (sniff_found && !sniffed_mime_type.empty() &&
      sniffed_mime_type != "text/*" &&
      sniffed_mime_type != "application/octet-stream") {
    return sniffed_mime_type;
  }

  // If the file got a trivial or empty mime type sniff, fall back to using its
  // extension if possible.
  base::FilePath::StringType ext = path.FinalExtension();
  if (ext.empty())
    return sniffed_mime_type;

  if (ext[0] == FILE_PATH_LITERAL('.'))
    ext = ext.substr(1);

  std::string ext_mime_type;
  bool ext_found = net::GetMimeTypeFromExtension(ext, &ext_mime_type);

  if (!ext_found || ext_mime_type.empty())
    return sniffed_mime_type;

  return ext_mime_type;
}

std::pair<BinaryUploadService::Result, BinaryUploadService::Request::Data>
GetFileDataBlocking(const base::FilePath& path,
                    bool detect_mime_type,
                    bool is_obfuscated) {
  DCHECK(!path.empty());

  // The returned `Data` must always have a valid `path` member, regardless
  // if this function succeeds or not.  The other members of `Data` may or
  // may not be filled in.
  BinaryUploadService::Request::Data file_data;
  file_data.path = path;

  // FLAG_WIN_SHARE_DELETE is necessary to allow the file to be renamed by the
  // user clicking "Open Now" without causing download errors.
  base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ |
                            base::File::FLAG_WIN_SHARE_DELETE);

  if (!file.IsValid()) {
    return std::make_pair(BinaryUploadService::Result::UNKNOWN, file_data);
  }

  file_data.size = file.GetLength();
  if (file_data.size == 0) {
    return std::make_pair(BinaryUploadService::Result::SUCCESS, file_data);
  }

  std::unique_ptr<crypto::SecureHash> secure_hash =
      crypto::SecureHash::Create(crypto::SecureHash::SHA256);
  size_t bytes_read = 0;
  std::vector<char> buf(kReadFileChunkSize);

  while (bytes_read < file_data.size) {
    std::optional<size_t> bytes_currently_read =
        file.ReadAtCurrentPos(base::as_writable_byte_span(buf));
    if (!bytes_currently_read.has_value()) {
      // Reset the size to zero since some code assumes an UNKNOWN result is
      // matched with a zero size.
      file_data.size = 0;
      return {BinaryUploadService::Result::UNKNOWN, file_data};
    }

    // Use the first read chunk to get the mimetype as necessary.
    if (detect_mime_type && bytes_read == 0) {
      file_data.mime_type = GetFileMimeType(
          path, std::string_view(buf.data(), bytes_currently_read.value()));
    }

    secure_hash->Update(
        base::as_byte_span(buf).first(bytes_currently_read.value()));
    bytes_read += bytes_currently_read.value();
  }

  std::array<uint8_t, crypto::kSHA256Length> hash;
  secure_hash->Finish(hash);

  // TODO(b/367257039): Pass along hash of unobfuscated file for enterprise
  // scans
  file_data.hash = base::HexEncode(hash);

  // Since we will be sending the deobfuscated file data in the request, set the
  // size to match.
  if (is_obfuscated) {
    enterprise_obfuscation::DownloadObfuscator obfuscator;
    auto overhead = obfuscator.CalculateDeobfuscationOverhead(file);
    if (overhead.has_value()) {
      file_data.size -= overhead.value();
      file_data.is_obfuscated = true;
    }
  }

  // Create a histogram to track the size of files being scanned up to 500MB.
  base::UmaHistogramCustomCounts(
      "Enterprise.FileAnalysisRequest.FileSize", file_data.size / 1024, 1,
      kMaxUploadSizeMetricsKB, 50);

  size_t max_file_size_bytes = BinaryUploadService::kMaxUploadSizeBytes;
  if (base::FeatureList::IsEnabled(
          enterprise_connectors::kEnableNewUploadSizeLimit)) {
    max_file_size_bytes =
        1024 * 1024 * enterprise_connectors::kMaxContentAnalysisFileSizeMB.Get();
  }
  return {file_data.size <= max_file_size_bytes
              ? BinaryUploadService::Result::SUCCESS
              : BinaryUploadService::Result::FILE_TOO_LARGE,
          std::move(file_data)};
}

bool IsZipFile(const base::FilePath::StringType& extension,
               const std::string& mime_type) {
  return extension == FILE_PATH_LITERAL(".zip") ||
         mime_type == "application/x-zip-compressed" ||
         mime_type == "application/zip";
}

bool IsRarFile(const base::FilePath::StringType& extension,
               const std::string& mime_type) {
  return extension == FILE_PATH_LITERAL(".rar") ||
         mime_type == "application/vnd.rar" ||
         mime_type == "application/x-rar-compressed";
}

}  // namespace

FileAnalysisRequest::FileAnalysisRequest(
    const enterprise_connectors::AnalysisSettings& analysis_settings,
    base::FilePath path,
    base::FilePath file_name,
    std::string mime_type,
    bool delay_opening_file,
    BinaryUploadService::ContentAnalysisCallback callback,
    BinaryUploadService::Request::RequestStartCallback start_callback,
    bool is_obfuscated)
    : Request(std::move(callback),
              analysis_settings.cloud_or_local_settings,
              std::move(start_callback)),
      has_cached_result_(false),
      tag_settings_(analysis_settings.tags),
      path_(std::move(path)),
      file_name_(std::move(file_name)),
      delay_opening_file_(delay_opening_file),
      is_obfuscated_(is_obfuscated) {
  DCHECK(!path_.empty());
  set_filename(path_.AsUTF8Unsafe());
  cached_data_.mime_type = std::move(mime_type);
}

FileAnalysisRequest::~FileAnalysisRequest() = default;

void FileAnalysisRequest::GetRequestData(DataCallback callback) {
  data_callback_ = std::move(callback);

  if (has_cached_result_) {
    RunCallback();
    return;
  }

  if (!delay_opening_file_) {
    file_access::RequestFilesAccessForSystem(
        {path_}, base::BindOnce(&FileAnalysisRequest::GetData,
                                weakptr_factory_.GetWeakPtr()));
  }
}

void FileAnalysisRequest::OpenFile() {
  DCHECK(!data_callback_.is_null());

  // Opening the file synchronously here is OK since OpenFile should be called
  // on a base::MayBlock() thread.
  std::pair<BinaryUploadService::Result, Data> file_data = GetFileDataBlocking(
      path_, cached_data_.mime_type.empty(), is_obfuscated_);

  // The result of opening the file is passed back to the UI thread since
  // |data_callback_| calls functions that must run there.
  content::GetUIThreadTaskRunner({})->PostTask(
      FROM_HERE,
      base::BindOnce(&FileAnalysisRequest::OnGotFileData,
                     weakptr_factory_.GetWeakPtr(), std::move(file_data)));
}

bool FileAnalysisRequest::HasMalwareRequest() const {
  for (const std::string& tag : content_analysis_request().tags()) {
    if (tag == "malware")
      return true;
  }
  return false;
}

void FileAnalysisRequest::OnGotFileData(
    std::pair<BinaryUploadService::Result, Data> result_and_data) {
  DCHECK(!result_and_data.second.path.empty());
  DCHECK_EQ(result_and_data.second.path, path_);

  scoped_file_access_.reset();
  if (result_and_data.first != BinaryUploadService::Result::SUCCESS) {
    CacheResultAndData(result_and_data.first,
                       std::move(result_and_data.second));
    RunCallback();
    return;
  }

  const std::string& mime_type = cached_data_.mime_type.empty()
                                     ? result_and_data.second.mime_type
                                     : cached_data_.mime_type;
  base::FilePath::StringType ext(file_name_.FinalExtension());
  std::ranges::transform(ext, ext.begin(), tolower);
  if (IsZipFile(ext, mime_type)) {
    zip_analyzer_ = SandboxedZipAnalyzer::CreateAnalyzer(
        path_,
        /*password=*/password(),
        base::BindOnce(&FileAnalysisRequest::OnCheckedForEncryption,
                       weakptr_factory_.GetWeakPtr(),
                       std::move(result_and_data.second)),
        LaunchFileUtilService());
    zip_analyzer_->Start();
  } else if (IsRarFile(ext, mime_type)) {
    rar_analyzer_ = SandboxedRarAnalyzer::CreateAnalyzer(
        path_,
        /*password=*/password(),
        base::BindOnce(&FileAnalysisRequest::OnCheckedForEncryption,
                       weakptr_factory_.GetWeakPtr(),
                       std::move(result_and_data.second)),
        LaunchFileUtilService());
    rar_analyzer_->Start();
  } else {
    CacheResultAndData(BinaryUploadService::Result::SUCCESS,
                       std::move(result_and_data.second));
    RunCallback();
  }
}

void FileAnalysisRequest::OnCheckedForEncryption(
    Data data,
    const ArchiveAnalyzerResults& analyzer_result) {
  bool encrypted = analyzer_result.encryption_info.is_encrypted &&
                   analyzer_result.encryption_info.password_status ==
                       EncryptionInfo::kKnownIncorrect;

  BinaryUploadService::Result result =
      encrypted ? BinaryUploadService::Result::FILE_ENCRYPTED
                : BinaryUploadService::Result::SUCCESS;
  CacheResultAndData(result, std::move(data));
  RunCallback();
}

void FileAnalysisRequest::CacheResultAndData(BinaryUploadService::Result result,
                                             Data data) {
  has_cached_result_ = true;
  cached_result_ = result;

  // If the mime type is already set, it shouldn't be overwritten.
  if (!cached_data_.mime_type.empty())
    data.mime_type = std::move(cached_data_.mime_type);

  DCHECK(!data.path.empty());
  cached_data_ = std::move(data);

  set_digest(cached_data_.hash);
  set_content_type(cached_data_.mime_type);
}

void FileAnalysisRequest::RunCallback() {
  if (!data_callback_.is_null()) {
    std::move(data_callback_).Run(cached_result_, cached_data_);
  }
}

void FileAnalysisRequest::GetData(file_access::ScopedFileAccess file_access) {
  scoped_file_access_ =
      std::make_unique<file_access::ScopedFileAccess>(std::move(file_access));
  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::TaskPriority::USER_VISIBLE, base::MayBlock()},
      base::BindOnce(&GetFileDataBlocking, path_,
                     cached_data_.mime_type.empty(), is_obfuscated_),
      base::BindOnce(&FileAnalysisRequest::OnGotFileData,
                     weakptr_factory_.GetWeakPtr()));
}

}  // namespace safe_browsing