File: file_transfer_analysis_delegate.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (218 lines) | stat: -rw-r--r-- 9,343 bytes parent folder | download | duplicates (4)
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
// 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.

#ifndef CHROME_BROWSER_ENTERPRISE_CONNECTORS_ANALYSIS_FILE_TRANSFER_ANALYSIS_DELEGATE_H_
#define CHROME_BROWSER_ENTERPRISE_CONNECTORS_ANALYSIS_FILE_TRANSFER_ANALYSIS_DELEGATE_H_

#include <vector>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/enterprise/connectors/analysis/content_analysis_delegate_base.h"
#include "chrome/browser/enterprise/connectors/analysis/content_analysis_info.h"
#include "chrome/browser/enterprise/connectors/common.h"
#include "chrome/browser/safe_browsing/cloud_content_scanning/deep_scanning_utils.h"
#include "components/enterprise/common/proto/connectors.pb.h"
#include "storage/browser/file_system/file_system_url.h"

class Profile;

namespace storage {
class FileSystemContext;
class RecursiveOperationDelegate;
}  // namespace storage

namespace enterprise_connectors {

class FilesRequestHandler;

// `FileTransferAnalysisDelegate` handles scanning and reporting of ChromeOS
// file system transfers.
// A user of `FileTransferAnalysisDelegate` should first check whether scanning
// is enabled for a pair of filesystem urls using `IsEnabledVec()`.
// Note: `IsEnabledVec()` allows checking for multiple source urls at once. In
// this case, a user has to create a FileTransferAnalysisDelegate for each
// source url.
//
// If scanning is enabled, a user proceeds with the creation of the class and
// then calls `UploadData()` to start the scan. Once the scans are complete,
// `callback_` is run.
// After a completed scan, a user of `FileTransferAnalysisDelegate` can access
// the scanning results for different filesystem urls using
// `GetAnalysisResult()`.
//
// If `source_url` is a directory, all files contained within the directory or
// any descended directory will be scanned. If `source_url` is a file only that
// file will be scanned.
class FileTransferAnalysisDelegate : public ContentAnalysisInfo {
 public:
  using FileTransferAnalysisDelegateFactory = base::RepeatingCallback<
      std::unique_ptr<enterprise_connectors::FileTransferAnalysisDelegate>(
          safe_browsing::DeepScanAccessPoint access_point,
          storage::FileSystemURL source_url,
          storage::FileSystemURL destination_url,
          Profile* profile,
          storage::FileSystemContext* file_system_context,
          enterprise_connectors::AnalysisSettings settings)>;

  // The verdict of an analysis.
  enum Verdict {
    // The file transfer is allowed.
    ALLOWED,
    // The file transfer is blocked.
    BLOCKED,
    // A file transfer result verdict is unknown if the file was copied/moved to
    // a scanned directory after the scan was started
    UNKNOWN,
  };

  // The result of an analysis.
  class FileTransferAnalysisResult {
   public:
    // Creates a result representing an allowed file transfer.
    static FileTransferAnalysisResult Allowed();
    // Creates a result for a file transfer blocked because of `tag`.
    static FileTransferAnalysisResult Blocked(
        FinalContentAnalysisResult final_result,
        const std::string& tag);
    // Represents a file transfer for which there is no known result.
    static FileTransferAnalysisResult Unknown();

    ~FileTransferAnalysisResult();
    FileTransferAnalysisResult(const FileTransferAnalysisResult& other);
    FileTransferAnalysisResult& operator=(FileTransferAnalysisResult&& other);

    bool IsAllowed() const;
    bool IsBlocked() const;
    bool IsUnknown() const;

    const std::string& tag() const;
    const std::optional<FinalContentAnalysisResult> final_result() const;

   private:
    FileTransferAnalysisResult(
        Verdict verdict,
        std::optional<FinalContentAnalysisResult> final_result,
        const std::string& tag);

    Verdict verdict_ = Verdict::UNKNOWN;
    // The tag ("dlp" or "malware") is only relevant when verdict is BLOCKED.
    // Note however that results associated with blocked files can have an empty
    // tag. This may happen when the file is blocked because it's encrypted or
    // it's too large to be uploaded.
    // For blocked files with an empty tag, final result contains the reason for
    // which they were blocked.
    std::optional<FinalContentAnalysisResult> final_result_;
    std::string tag_;
  };

  virtual ~FileTransferAnalysisDelegate();

  // Create the FileTransferAnalysisDelegate. This function uses the factory if
  // it is set via `SetFactoryForTesting()`.
  //
  // For `block_until_verdict == 0`, the `destination_url` has to point to the
  // copied file/directory and not its parent. If it points to the parent, all
  // files within the destination directory are scanned.
  static std::unique_ptr<FileTransferAnalysisDelegate> Create(
      safe_browsing::DeepScanAccessPoint access_point,
      storage::FileSystemURL source_url,
      storage::FileSystemURL destination_url,
      Profile* profile,
      storage::FileSystemContext* file_system_context,
      AnalysisSettings settings);

  // Set a factory for the FileTransferAnalysisDelegate.
  // Can be used in testing to create `MockFileTransferAnalysisDelegate`s.
  static void SetFactoryForTesting(FileTransferAnalysisDelegateFactory factory);

  // Returns a vector with the AnalysisSettings for file transfers from the
  // respective source url to the destination_url.
  // If the enterprise connectors are not enabled for any of the transfers an
  // empty vector is returned. Each entry in the returned vector corresponds to
  // the entry in the `source_urls` vector with the same index.
  static std::vector<std::optional<AnalysisSettings>> IsEnabledVec(
      Profile* profile,
      const std::vector<storage::FileSystemURL>& source_urls,
      storage::FileSystemURL destination_url);

  // Main entrypoint to start the file uploads.
  // Once scanning is complete `callback_` will be called.
  virtual void UploadData(base::OnceClosure completion_callback);

  // Calling this function is only allowed after the scan is complete!
  virtual FileTransferAnalysisResult GetAnalysisResultAfterScan(
      storage::FileSystemURL url);
  // Calling this function is only allowed after the scan is complete!
  virtual std::vector<storage::FileSystemURL> GetWarnedFiles() const;

  // Called when the user cancels a transfer.
  void Cancel(bool warning);

  // Called when the user byapass a warning.
  void BypassWarnings(std::optional<std::u16string> user_justification);

  // Returns the custom message specified by the admin for the given tag, or
  // std::nullopt if there isn't any.
  std::optional<std::u16string> GetCustomMessage(const std::string& tag) const;

  // Returns the custom "learn more" URL specified by the admin for the given
  // tag, or std::nullopt if there isn't any.
  std::optional<GURL> GetCustomLearnMoreUrl(const std::string& tag) const;

  // Returns whether a user justification is required for the given tag.
  bool BypassRequiresJustification(const std::string& tag) const;

  FilesRequestHandler* GetFilesRequestHandlerForTesting();

  // ContentAnalysisInfo:
  const AnalysisSettings& settings() const override;
  signin::IdentityManager* identity_manager() const override;
  int user_action_requests_count() const override;
  std::string tab_title() const override;
  std::string user_action_id() const override;
  std::string email() const override;
  std::string url() const override;
  const GURL& tab_url() const override;
  ContentAnalysisRequest::Reason reason() const override;
  google::protobuf::RepeatedPtrField<::safe_browsing::ReferrerChainEntry>
  referrer_chain() const override;
  google::protobuf::RepeatedPtrField<std::string> frame_url_chain()
      const override;

 protected:
  // For `block_until_verdict == 0`, the `destination_url` has to point to the
  // copied file/directory and not its parent. If it points to the parent, all
  // files within the destination directory are scanned.
  FileTransferAnalysisDelegate(safe_browsing::DeepScanAccessPoint access_point,
                               storage::FileSystemURL source_url,
                               storage::FileSystemURL destination_url,
                               Profile* profile,
                               storage::FileSystemContext* file_system_context,
                               AnalysisSettings settings);

 private:
  void OnGotFileURLs(std::vector<storage::FileSystemURL> source_urls);

  void ContentAnalysisCompleted(std::vector<RequestHandlerResult> results);

  AnalysisSettings settings_;
  raw_ptr<Profile> profile_;
  safe_browsing::DeepScanAccessPoint access_point_;
  std::vector<storage::FileSystemURL> scanning_urls_;
  storage::FileSystemURL source_url_;
  storage::FileSystemURL destination_url_;
  base::OnceClosure callback_;
  std::vector<RequestHandlerResult> results_;
  std::vector<size_t> warned_file_indices_;
  bool warning_is_bypassed_{false};

  std::unique_ptr<storage::RecursiveOperationDelegate> get_file_urls_delegate_;
  std::unique_ptr<FilesRequestHandler> request_handler_;

  base::WeakPtrFactory<FileTransferAnalysisDelegate> weak_ptr_factory_{this};
};
}  // namespace enterprise_connectors

#endif  // CHROME_BROWSER_ENTERPRISE_CONNECTORS_ANALYSIS_FILE_TRANSFER_ANALYSIS_DELEGATE_H_