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
|
// 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/webshare/share_service_impl.h"
#include <algorithm>
#include <memory>
#include <string_view>
#include "base/feature_list.h"
#include "base/files/safe_base_name.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "chrome/browser/bad_message.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "chrome/common/chrome_features.h"
#include "components/safe_browsing/content/common/file_type_policies.h"
#include "components/safe_browsing/core/browser/db/database_manager.h"
#include "content/public/browser/web_contents.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "services/network/public/mojom/permissions_policy/permissions_policy_feature.mojom.h"
#if BUILDFLAG(IS_MAC)
#include "chrome/browser/webshare/mac/sharing_service_operation.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "chrome/browser/webshare/win/share_operation.h"
#endif
// IsDangerousFilename() and IsDangerousMimeType() should be kept in sync with
// //third_party/blink/renderer/modules/webshare/FILE_TYPES.md
// //components/browser_ui/webshare/android/java/src/org/chromium/components/browser_ui/webshare/ShareServiceImpl.java
ShareServiceImpl::ShareServiceImpl(
content::RenderFrameHost& render_frame_host,
mojo::PendingReceiver<blink::mojom::ShareService> receiver)
: content::DocumentService<blink::mojom::ShareService>(render_frame_host,
std::move(receiver))
#if BUILDFLAG(IS_CHROMEOS)
,
sharesheet_client_(
content::WebContents::FromRenderFrameHost(&render_frame_host))
#endif
{
#if !BUILDFLAG(IS_WIN) && !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_MAC)
NOTREACHED();
#endif
}
ShareServiceImpl::~ShareServiceImpl() = default;
// static
void ShareServiceImpl::Create(
content::RenderFrameHost* render_frame_host,
mojo::PendingReceiver<blink::mojom::ShareService> receiver) {
CHECK(render_frame_host);
if (render_frame_host->IsNestedWithinFencedFrame()) {
// The renderer should have checked and disallowed the request for fenced
// frames in NavigatorShare and thrown a DOMException. Ignore the request
// and mark it as bad if it didn't happen for some reason.
bad_message::ReceivedBadMessage(render_frame_host->GetProcess(),
bad_message::SSI_CREATE_FENCED_FRAME);
return;
}
new ShareServiceImpl(*render_frame_host, std::move(receiver));
}
// static
bool ShareServiceImpl::IsDangerousFilename(const base::FilePath& path) {
constexpr const base::FilePath::CharType* kPermitted[] = {
FILE_PATH_LITERAL(".avif"), // image/avif
FILE_PATH_LITERAL(".bmp"), // image/bmp / image/x-ms-bmp
FILE_PATH_LITERAL(".css"), // text/css
FILE_PATH_LITERAL(".csv"), // text/csv / text/comma-separated-values
FILE_PATH_LITERAL(".ehtml"), // text/html
FILE_PATH_LITERAL(".flac"), // audio/flac
FILE_PATH_LITERAL(".gif"), // image/gif
FILE_PATH_LITERAL(".htm"), // text/html
FILE_PATH_LITERAL(".html"), // text/html
FILE_PATH_LITERAL(".ico"), // image/x-icon
FILE_PATH_LITERAL(".jfif"), // image/jpeg
FILE_PATH_LITERAL(".jpeg"), // image/jpeg
FILE_PATH_LITERAL(".jpg"), // image/jpeg
FILE_PATH_LITERAL(".m4a"), // audio/x-m4a
FILE_PATH_LITERAL(".m4v"), // video/mp4
FILE_PATH_LITERAL(".mp3"), // audio/mpeg audio/mp3
FILE_PATH_LITERAL(".mp4"), // video/mp4
FILE_PATH_LITERAL(".mpeg"), // video/mpeg
FILE_PATH_LITERAL(".mpg"), // video/mpeg
FILE_PATH_LITERAL(".oga"), // audio/ogg
FILE_PATH_LITERAL(".ogg"), // audio/ogg
FILE_PATH_LITERAL(".ogm"), // video/ogg
FILE_PATH_LITERAL(".ogv"), // video/ogg
FILE_PATH_LITERAL(".opus"), // audio/ogg
FILE_PATH_LITERAL(".pdf"), // application/pdf
FILE_PATH_LITERAL(".pjp"), // image/jpeg
FILE_PATH_LITERAL(".pjpeg"), // image/jpeg
FILE_PATH_LITERAL(".png"), // image/png
FILE_PATH_LITERAL(".shtm"), // text/html
FILE_PATH_LITERAL(".shtml"), // text/html
FILE_PATH_LITERAL(".svg"), // image/svg+xml
FILE_PATH_LITERAL(".svgz"), // image/svg+xml
FILE_PATH_LITERAL(".text"), // text/plain
FILE_PATH_LITERAL(".tif"), // image/tiff
FILE_PATH_LITERAL(".tiff"), // image/tiff
FILE_PATH_LITERAL(".txt"), // text/plain
FILE_PATH_LITERAL(".wav"), // audio/wav
FILE_PATH_LITERAL(".weba"), // audio/webm
FILE_PATH_LITERAL(".webm"), // video/webm
FILE_PATH_LITERAL(".webp"), // image/webp
FILE_PATH_LITERAL(".xbm"), // image/x-xbitmap
};
for (const base::FilePath::CharType* permitted : kPermitted) {
if (base::EndsWith(path.value(), permitted,
base::CompareCase::INSENSITIVE_ASCII))
return false;
}
return true;
}
// static
bool ShareServiceImpl::IsDangerousMimeType(std::string_view content_type) {
constexpr std::array<const char*, 28> kPermitted = {
"application/pdf",
"audio/flac",
"audio/mp3",
"audio/mpeg",
"audio/ogg",
"audio/wav",
"audio/webm",
"audio/x-m4a",
"image/avif",
"image/bmp",
"image/gif",
"image/jpeg",
"image/png",
"image/svg+xml",
"image/tiff",
"image/webp",
"image/x-icon",
"image/x-ms-bmp",
"image/x-xbitmap",
"text/comma-separated-values",
"text/css",
"text/csv",
"text/html",
"text/plain",
"video/mp4",
"video/mpeg",
"video/ogg",
"video/webm",
};
for (const char* permitted : kPermitted) {
if (content_type == permitted)
return false;
}
return true;
}
void ShareServiceImpl::Share(const std::string& title,
const std::string& text,
const GURL& share_url,
std::vector<blink::mojom::SharedFilePtr> files,
ShareCallback callback) {
UMA_HISTOGRAM_ENUMERATION(kWebShareApiCountMetric, WebShareMethod::kShare);
if (!render_frame_host().IsFeatureEnabled(
network::mojom::PermissionsPolicyFeature::kWebShare)) {
std::move(callback).Run(blink::mojom::ShareError::PERMISSION_DENIED);
ReportBadMessageAndDeleteThis("Feature policy blocks Web Share");
return;
}
content::WebContents* const web_contents =
content::WebContents::FromRenderFrameHost(&render_frame_host());
if (!web_contents) {
VLOG(1) << "Cannot share after navigating away";
std::move(callback).Run(blink::mojom::ShareError::PERMISSION_DENIED);
return;
}
if (files.size() > kMaxSharedFileCount) {
VLOG(1) << "Share too large: " << files.size() << " files";
std::move(callback).Run(blink::mojom::ShareError::PERMISSION_DENIED);
return;
}
bool should_check_url = false;
for (auto& file : files) {
if (!file || !file->blob || !file->blob->blob) {
mojo::ReportBadMessage("Invalid file to share()");
return;
}
const base::FilePath& path = file->name.path();
if (IsDangerousFilename(path) ||
IsDangerousMimeType(file->blob->content_type)) {
VLOG(1) << "File type is not supported: " << path << " has mime type "
<< file->blob->content_type;
std::move(callback).Run(blink::mojom::ShareError::PERMISSION_DENIED);
return;
}
// Check if at least one file is marked by the download protection service
// to send a ping to check this file type.
if (!should_check_url &&
safe_browsing::FileTypePolicies::GetInstance()->IsCheckedBinaryFile(
path)) {
should_check_url = true;
}
// In the case where the original blob handle was to a native file (of
// unknown size), the serialized data does not contain an accurate file
// size. To handle this, the comparison against kMaxSharedFileBytes should
// be done by the platform-specific implementations as part of processing
// the blobs.
}
DCHECK(!safe_browsing_request_);
if (should_check_url && g_browser_process->safe_browsing_service()) {
safe_browsing_request_.emplace(
g_browser_process->safe_browsing_service()->database_manager(),
web_contents->GetLastCommittedURL(),
base::BindOnce(&ShareServiceImpl::OnSafeBrowsingResultReceived,
weak_factory_.GetWeakPtr(), title, text, share_url,
std::move(files), std::move(callback)));
return;
}
OnSafeBrowsingResultReceived(title, text, share_url, std::move(files),
std::move(callback),
/*is_url_safe=*/true);
}
void ShareServiceImpl::OnSafeBrowsingResultReceived(
const std::string& title,
const std::string& text,
const GURL& share_url,
std::vector<blink::mojom::SharedFilePtr> files,
ShareCallback callback,
bool is_url_safe) {
safe_browsing_request_.reset();
content::WebContents* const web_contents =
content::WebContents::FromRenderFrameHost(&render_frame_host());
if (!web_contents) {
VLOG(1) << "Cannot share after navigating away";
std::move(callback).Run(blink::mojom::ShareError::PERMISSION_DENIED);
return;
}
if (!is_url_safe) {
VLOG(1) << "File not safe to share from this website";
std::move(callback).Run(blink::mojom::ShareError::PERMISSION_DENIED);
return;
}
#if BUILDFLAG(IS_CHROMEOS)
sharesheet_client_.Share(title, text, share_url, std::move(files),
std::move(callback));
#elif BUILDFLAG(IS_MAC)
auto sharing_service_operation =
std::make_unique<webshare::SharingServiceOperation>(
title, text, share_url, std::move(files), web_contents);
// grab a safe reference to |sharing_service_operation| before calling move on
// it.
webshare::SharingServiceOperation* sharing_service_operation_ptr =
sharing_service_operation.get();
// Wrap the |callback| in a binding that owns the |sharing_service_operation|
// so its lifetime can be preserved till its done.
sharing_service_operation_ptr->Share(base::BindOnce(
[](std::unique_ptr<webshare::SharingServiceOperation>
sharing_service_operation,
ShareCallback callback,
blink::mojom::ShareError result) { std::move(callback).Run(result); },
std::move(sharing_service_operation), std::move(callback)));
#elif BUILDFLAG(IS_WIN)
// Drop fullscreen mode so the Share UI can be easily clicked away from,
// without clicking back into the web contents
base::ScopedClosureRunner fullscreen_block =
web_contents->ForSecurityDropFullscreen(
/*display_id=*/display::kInvalidDisplayId);
auto share_operation = std::make_unique<webshare::ShareOperation>(
title, text, share_url, web_contents);
auto* const share_operation_ptr = share_operation.get();
share_operation_ptr->Run(
std::move(files),
base::BindOnce(
[](std::unique_ptr<webshare::ShareOperation> share_operation,
base::ScopedClosureRunner fullscreen_block, ShareCallback callback,
blink::mojom::ShareError result) {
fullscreen_block.RunAndReset();
std::move(callback).Run(result);
},
std::move(share_operation), std::move(fullscreen_block),
std::move(callback)));
#else
NOTREACHED();
#endif
}
|