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
|
// Copyright 2019 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/ui/tab_contents/chrome_web_contents_view_handle_drop.h"
#include <optional>
#include "base/containers/contains.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/profiles/profile.h"
#include "components/enterprise/buildflags/buildflags.h"
#include "components/enterprise/common/files_scan_data.h"
#include "components/safe_browsing/buildflags.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_view_delegate.h"
#include "content/public/common/drop_data.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
#include "ui/base/clipboard/file_info.h"
#if BUILDFLAG(ENTERPRISE_CLOUD_CONTENT_ANALYSIS) && \
BUILDFLAG(SAFE_BROWSING_AVAILABLE)
#include "chrome/browser/enterprise/connectors/analysis/content_analysis_delegate.h"
#include "chrome/browser/safe_browsing/cloud_content_scanning/deep_scanning_utils.h"
namespace {
void CompletionCallback(
content::DropData drop_data,
std::unique_ptr<enterprise_connectors::FilesScanData> files_scan_data,
content::WebContentsViewDelegate::DropCompletionCallback callback,
const enterprise_connectors::ContentAnalysisDelegate::Data& data,
enterprise_connectors::ContentAnalysisDelegate::Result& result) {
// If there are no negative results, proceed with just `drop_data`.
bool all_text_results_allowed = !base::Contains(result.text_results, false);
bool all_file_results_allowed = !base::Contains(result.paths_results, false);
if (all_text_results_allowed && all_file_results_allowed) {
std::move(callback).Run(std::move(drop_data));
return;
}
// For text drag-drops, block the drop if any result is negative.
if (!all_text_results_allowed) {
std::move(callback).Run(std::nullopt);
return;
}
// For file drag-drops, block file paths depending on the verdict obtained for
// child paths.
DCHECK(files_scan_data);
std::set<size_t> file_indexes_to_block =
files_scan_data->IndexesToBlock(result.paths_results);
// If every file path should be blocked, the drop is aborted, otherwise it
// continues by blocking sub-elements of the list. When everything is blocked,
// it implies that no `result.paths_results` is allowed.
if (file_indexes_to_block.size() == drop_data.filenames.size()) {
for (size_t i = 0; i < data.paths.size(); ++i) {
result.paths_results[i] = false;
}
std::move(callback).Run(std::nullopt);
return;
}
// A specific index could be blocked due to its parent folder being
// blocked and not because it got a bad verdict itself, so `result` needs
// to be updated to reflect that.
DCHECK_EQ(data.paths.size(),
files_scan_data->expanded_paths_indexes().size());
for (size_t i = 0; i < data.paths.size(); ++i) {
int parent_index =
files_scan_data->expanded_paths_indexes().at(data.paths[i]);
if (file_indexes_to_block.count(parent_index)) {
result.paths_results[i] = false;
}
}
std::vector<ui::FileInfo> final_filenames;
for (size_t i = 0; i < drop_data.filenames.size(); ++i) {
if (file_indexes_to_block.count(i)) {
continue;
}
final_filenames.push_back(std::move(drop_data.filenames[i]));
}
drop_data.filenames = std::move(final_filenames);
std::move(callback).Run(std::move(drop_data));
}
// Helper class to handle WebContents being destroyed while files are opened in
// the threadpool. This class deletes itself either when it's no longer needed
// when ScanData is called, or when its corresponding web contents is destroyed
// so its weak ptrs are invalidated.
class HandleDropScanData : public content::WebContentsObserver {
public:
HandleDropScanData(
content::WebContents* web_contents,
content::DropData drop_data,
enterprise_connectors::ContentAnalysisDelegate::Data analysis_data,
content::WebContentsViewDelegate::DropCompletionCallback callback)
: content::WebContentsObserver(web_contents),
drop_data_(std::move(drop_data)),
analysis_data_(std::move(analysis_data)),
callback_(std::move(callback)) {}
void ScanData(
std::unique_ptr<enterprise_connectors::FilesScanData> files_scan_data) {
DCHECK(web_contents());
if (files_scan_data) {
for (const auto& path : files_scan_data->expanded_paths()) {
analysis_data_.paths.push_back(path);
}
}
enterprise_connectors::ContentAnalysisDelegate::CreateForWebContents(
web_contents(), std::move(analysis_data_),
base::BindOnce(&CompletionCallback, std::move(drop_data_),
std::move(files_scan_data), std::move(callback_)),
safe_browsing::DeepScanAccessPoint::DRAG_AND_DROP);
delete this;
}
void WebContentsDestroyed() override { delete this; }
base::WeakPtr<HandleDropScanData> GetWeakPtr() {
return weakptr_factory_.GetWeakPtr();
}
private:
content::DropData drop_data_;
enterprise_connectors::ContentAnalysisDelegate::Data analysis_data_;
content::WebContentsViewDelegate::DropCompletionCallback callback_;
base::WeakPtrFactory<HandleDropScanData> weakptr_factory_{this};
};
} // namespace
#endif // BUILDFLAG(ENTERPRISE_CLOUD_CONTENT_ANALYSIS) &&
// BUILDFLAG(SAFE_BROWSING_AVAILABLE)
void HandleOnPerformingDrop(
content::WebContents* web_contents,
content::DropData drop_data,
content::WebContentsViewDelegate::DropCompletionCallback callback) {
CHECK(callback);
absl::Cleanup cleanup = [&] {
std::move(callback).Run(std::move(drop_data));
};
#if BUILDFLAG(ENTERPRISE_CLOUD_CONTENT_ANALYSIS) && \
BUILDFLAG(SAFE_BROWSING_AVAILABLE)
enterprise_connectors::ContentAnalysisDelegate::Data data;
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
auto connector =
drop_data.filenames.empty()
? enterprise_connectors::AnalysisConnector::BULK_DATA_ENTRY
: enterprise_connectors::AnalysisConnector::FILE_ATTACHED;
if (!enterprise_connectors::ContentAnalysisDelegate::IsEnabled(
profile, web_contents->GetLastCommittedURL(), &data, connector)) {
// If the enterprise policy is not enabled, make sure that the renderer
// never forces a default action.
drop_data.document_is_handling_drag = true;
return;
}
// If the page will not handle the drop, no need to perform content analysis.
if (!drop_data.document_is_handling_drag) {
return;
}
data.reason = enterprise_connectors::ContentAnalysisRequest::DRAG_AND_DROP;
// Collect the data that needs to be scanned.
if (!drop_data.url_title.empty()) {
data.text.push_back(base::UTF16ToUTF8(drop_data.url_title));
}
if (drop_data.text) {
data.text.push_back(base::UTF16ToUTF8(*drop_data.text));
}
if (drop_data.html) {
data.text.push_back(base::UTF16ToUTF8(*drop_data.html));
}
// `callback` should only run asynchronously when scanning is blocking.
content::WebContentsViewDelegate::DropCompletionCallback scan_callback =
base::DoNothing();
if (data.settings.block_until_verdict ==
enterprise_connectors::BlockUntilVerdict::kBlock) {
std::move(cleanup).Cancel();
scan_callback = std::move(callback);
}
// `handle_drop_scan_data` is created on the heap to stay alive regardless of
// how long the threadpool work takes or in case `web_contents` is destroyed.
// It deletes itself when `HandleDropScanData::ScanData` is called or when
// `web_contents` gets destroyed.
auto* handle_drop_scan_data = new HandleDropScanData(
web_contents, drop_data, std::move(data), std::move(scan_callback));
if (drop_data.filenames.empty()) {
handle_drop_scan_data->ScanData(/*files_scan_data=*/nullptr);
} else {
auto files_scan_data =
std::make_unique<enterprise_connectors::FilesScanData>(
drop_data.filenames);
auto* files_scan_data_raw = files_scan_data.get();
files_scan_data_raw->ExpandPaths(base::BindOnce(
&HandleDropScanData::ScanData, handle_drop_scan_data->GetWeakPtr(),
std::move(files_scan_data)));
}
#else
// If content analysis is not available, make sure that the renderer never
// forces a default action.
drop_data.document_is_handling_drag = true;
return;
#endif // !BUILDFLAG(ENTERPRISE_CLOUD_CONTENT_ANALYSIS) ||
// !BUILDFLAG(SAFE_BROWSING_AVAILABLE)
}
|