File: file_system_access_metadata.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 (164 lines) | stat: -rw-r--r-- 5,499 bytes parent folder | download | duplicates (5)
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
// Copyright 2025 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_system_access_metadata.h"

#include "base/strings/string_util.h"
#include "chrome/browser/safe_browsing/download_protection/download_protection_util.h"
#include "components/download/public/common/download_danger_type.h"
#include "components/enterprise/connectors/core/reporting_utils.h"
#include "net/base/mime_util.h"

namespace safe_browsing {

FileSystemAccessMetadata::FileSystemAccessMetadata(
    std::unique_ptr<content::FileSystemAccessWriteItem> item)
    : item_(std::move(item)),
      tab_url_(item_->web_contents ? item_->web_contents->GetLastCommittedURL()
                                   : GURL()) {
  CHECK(item_);
}

FileSystemAccessMetadata::~FileSystemAccessMetadata() = default;

content::BrowserContext* FileSystemAccessMetadata::GetBrowserContext() const {
  return item_->browser_context;
}

safe_browsing::ReferrerChain FileSystemAccessMetadata::GetReferrerChain()
    const {
  std::unique_ptr<safe_browsing::ReferrerChainData> referrer_chain_data =
      safe_browsing::IdentifyReferrerChain(
          *item_, enterprise_connectors::kReferrerUserGestureLimit);
  if (referrer_chain_data && referrer_chain_data->GetReferrerChain()) {
    return *referrer_chain_data->GetReferrerChain();
  }
  return safe_browsing::ReferrerChain();
}

const base::FilePath& FileSystemAccessMetadata::GetFullPath() const {
  return item_->full_path;
}

const base::FilePath& FileSystemAccessMetadata::GetTargetFilePath() const {
  return item_->target_file_path;
}

const std::string& FileSystemAccessMetadata::GetHash() const {
  return item_->sha256_hash;
}

int64_t FileSystemAccessMetadata::GetTotalBytes() const {
  return item_->size;
}

std::string FileSystemAccessMetadata::GetMimeType() const {
  if (!mime_type_computed_) {
    // Get MIME type based on file extension.
    std::string mime_type;
    base::FilePath::StringType ext =
        base::ToLowerASCII(item_->target_file_path.FinalExtension());

    // Remove leading dot from extension.
    if (!ext.empty() && ext[0] == FILE_PATH_LITERAL('.')) {
      ext = ext.substr(1);
    }

    // Searches within chrome's built-in list of filetype/extension
    // associations, as using `GetMimeTypeFromExtension` includes
    // platform-defined mime type mappings that can potentially block.
    // TODO(crbug.com/407598185): Add platform MIME types lookup for better
    // protection.
    if (net::GetWellKnownMimeTypeFromExtension(ext, &mime_type)) {
      mime_type_ = mime_type;
    } else {
      // Default to octet-stream for unknown MIME type.
      mime_type_ = "application/octet-stream";
    }
    mime_type_computed_ = true;
  }
  return mime_type_;
}

const GURL& FileSystemAccessMetadata::GetURL() const {
  return item_->frame_url;
}

const GURL& FileSystemAccessMetadata::GetTabUrl() const {
  return tab_url_;
}

bool FileSystemAccessMetadata::HasUserGesture() const {
  return item_->has_user_gesture;
}

bool FileSystemAccessMetadata::IsObfuscated() const {
  // No support for enterprise obfuscation for filesystem access API.
  return false;
}

bool FileSystemAccessMetadata::IsTopLevelEncryptedArchive() const {
  // No support for password-protected files for filesystem access API.
  return false;
}

bool FileSystemAccessMetadata::IsForDownloadItem(
    download::DownloadItem* download) const {
  return false;
}

download::DownloadDangerType FileSystemAccessMetadata::GetDangerType() const {
  // Used as the default pre-scan and fallback danger type since FSA doesn't
  // have preliminary danger type checks.
  return download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS;
}

enterprise_connectors::EventResult
FileSystemAccessMetadata::GetPreScanEventResult(
    download::DownloadDangerType danger_type) const {
  // Currently for file system access deep scans, pre-scan danger type
  // should always be DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS and event result
  // ALLOWED.
  if (danger_type != download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) {
    DVLOG(1) << "Unexpected danger type for fsa scan: " << danger_type;
  }
  return enterprise_connectors::EventResult::ALLOWED;
}

std::unique_ptr<DownloadRequestMaker>
FileSystemAccessMetadata::CreateDownloadRequestFromMetadata(
    scoped_refptr<BinaryFeatureExtractor> binary_feature_extractor) const {
  return DownloadRequestMaker::CreateFromFileSystemAccess(
      binary_feature_extractor, *item_);
}

std::unique_ptr<DeepScanningMetadata::DownloadScopedObservation>
FileSystemAccessMetadata::GetDownloadObservation(
    download::DownloadItem::Observer* observer) {
  return nullptr;
}

void FileSystemAccessMetadata::SetCallback(CheckDownloadCallback callback) {
  callback_ = std::move(callback);
}

void FileSystemAccessMetadata::ProcessScanResult(
    DownloadCheckResultReason reason,
    DownloadCheckResult deep_scan_result) {
  if (deep_scan_result == DownloadCheckResult::ASYNC_SCANNING ||
      deep_scan_result == DownloadCheckResult::ASYNC_LOCAL_PASSWORD_SCANNING) {
    return;
  }

  // Callback should only be run once, ignore any subsequent scan results.
  if (!callback_.is_null()) {
    std::move(callback_).Run(MaybeOverrideScanResult(reason, deep_scan_result));
  }
}

base::WeakPtr<FileSystemAccessMetadata> FileSystemAccessMetadata::GetWeakPtr() {
  return weakptr_factory_.GetWeakPtr();
}

}  // namespace safe_browsing