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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/enterprise/connectors/core/common.h"
#include <algorithm>
#include "base/containers/map_util.h"
#include "base/notreached.h"
#include "base/strings/escape.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "components/enterprise/connectors/core/connectors_prefs.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#if BUILDFLAG(USE_BLINK)
#include "components/download/public/common/download_item.h" // nogncheck
#endif // BUILDFLAG(USE_BLINK)
namespace enterprise_connectors {
namespace {
inline constexpr auto kUmaEnumToStringMap =
base::MakeFixedFlatMap<EnterpriseReportingEventType, std::string_view>({
{EnterpriseReportingEventType::kPasswordReuseEvent,
kPasswordReuseUmaMetricName},
{EnterpriseReportingEventType::kPasswordChangedEvent,
kPasswordChangedUmaMetricName},
{EnterpriseReportingEventType::kDangerousDownloadEvent,
kDangerousDownloadUmaMetricName},
{EnterpriseReportingEventType::kInterstitialEvent,
kInterstitialUmaMetricName},
{EnterpriseReportingEventType::kSensitiveDataEvent,
kSensitiveDataUmaMetricName},
{EnterpriseReportingEventType::kUnscannedFileEvent,
kUnscannedFileUmaMetricName},
{EnterpriseReportingEventType::kLoginEvent, kLoginUmaMetricName},
{EnterpriseReportingEventType::kPasswordBreachEvent,
kPasswordBreachUmaMetricName},
{EnterpriseReportingEventType::kUrlFilteringInterstitialEvent,
kUrlFilteringInterstitialUmaMetricName},
{EnterpriseReportingEventType::kExtensionInstallEvent,
kExtensionInstallUmaMetricName},
{EnterpriseReportingEventType::kBrowserCrashEvent,
kBrowserCrashUmaMetricName},
{EnterpriseReportingEventType::kExtensionTelemetryEvent,
kExtensionTelemetryUmaMetricName},
});
inline constexpr auto kEventCaseToUmaEnumMap =
base::MakeFixedFlatMap<EventCase, EnterpriseReportingEventType>({
{EventCase::kPasswordReuseEvent,
EnterpriseReportingEventType::kPasswordReuseEvent},
{EventCase::kPasswordChangedEvent,
EnterpriseReportingEventType::kPasswordChangedEvent},
{EventCase::kDangerousDownloadEvent,
EnterpriseReportingEventType::kDangerousDownloadEvent},
{EventCase::kInterstitialEvent,
EnterpriseReportingEventType::kInterstitialEvent},
{EventCase::kSensitiveDataEvent,
EnterpriseReportingEventType::kSensitiveDataEvent},
{EventCase::kUnscannedFileEvent,
EnterpriseReportingEventType::kUnscannedFileEvent},
{EventCase::kLoginEvent, EnterpriseReportingEventType::kLoginEvent},
{EventCase::kPasswordBreachEvent,
EnterpriseReportingEventType::kPasswordBreachEvent},
{EventCase::kUrlFilteringInterstitialEvent,
EnterpriseReportingEventType::kUrlFilteringInterstitialEvent},
{EventCase::kBrowserExtensionInstallEvent,
EnterpriseReportingEventType::kExtensionInstallEvent},
{EventCase::kBrowserCrashEvent,
EnterpriseReportingEventType::kBrowserCrashEvent},
{EventCase::kExtensionTelemetryEvent,
EnterpriseReportingEventType::kExtensionTelemetryEvent},
});
ContentAnalysisAcknowledgement::FinalAction RuleActionToAckAction(
TriggeredRule::Action action) {
switch (action) {
case TriggeredRule::ACTION_UNSPECIFIED:
return ContentAnalysisAcknowledgement::ACTION_UNSPECIFIED;
case TriggeredRule::REPORT_ONLY:
return ContentAnalysisAcknowledgement::REPORT_ONLY;
case TriggeredRule::WARN:
return ContentAnalysisAcknowledgement::WARN;
case TriggeredRule::BLOCK:
return ContentAnalysisAcknowledgement::BLOCK;
}
}
} // namespace
ReportingSettings::ReportingSettings() = default;
ReportingSettings::ReportingSettings(const std::string& dm_token,
bool per_profile)
: dm_token(dm_token), per_profile(per_profile) {}
ReportingSettings::ReportingSettings(ReportingSettings&&) = default;
ReportingSettings::ReportingSettings(const ReportingSettings&) = default;
ReportingSettings& ReportingSettings::operator=(ReportingSettings&&) = default;
ReportingSettings::~ReportingSettings() = default;
const char* AnalysisConnectorPref(AnalysisConnector connector) {
switch (connector) {
case AnalysisConnector::BULK_DATA_ENTRY:
return kOnBulkDataEntryPref;
case AnalysisConnector::FILE_DOWNLOADED:
return kOnFileDownloadedPref;
case AnalysisConnector::FILE_ATTACHED:
return kOnFileAttachedPref;
case AnalysisConnector::PRINT:
return kOnPrintPref;
case AnalysisConnector::FILE_TRANSFER:
#if BUILDFLAG(IS_CHROMEOS)
return kOnFileTransferPref;
#endif
case AnalysisConnector::ANALYSIS_CONNECTOR_UNSPECIFIED:
NOTREACHED() << "Using unspecified analysis connector";
}
}
const char* AnalysisConnectorScopePref(AnalysisConnector connector) {
switch (connector) {
case AnalysisConnector::BULK_DATA_ENTRY:
return kOnBulkDataEntryScopePref;
case AnalysisConnector::FILE_DOWNLOADED:
return kOnFileDownloadedScopePref;
case AnalysisConnector::FILE_ATTACHED:
return kOnFileAttachedScopePref;
case AnalysisConnector::PRINT:
return kOnPrintScopePref;
case AnalysisConnector::FILE_TRANSFER:
#if BUILDFLAG(IS_CHROMEOS)
return kOnFileTransferScopePref;
#endif
case AnalysisConnector::ANALYSIS_CONNECTOR_UNSPECIFIED:
NOTREACHED() << "Using unspecified analysis connector";
}
}
TriggeredRule::Action GetHighestPrecedenceAction(
const ContentAnalysisResponse& response,
std::string* tag) {
auto action = TriggeredRule::ACTION_UNSPECIFIED;
for (const auto& result : response.results()) {
if (!result.has_status() ||
result.status() != ContentAnalysisResponse::Result::SUCCESS) {
continue;
}
for (const auto& rule : result.triggered_rules()) {
auto higher_precedence_action =
GetHighestPrecedenceAction(action, rule.action());
if (higher_precedence_action != action && tag != nullptr) {
*tag = result.tag();
}
action = higher_precedence_action;
}
}
return action;
}
TriggeredRule::Action GetHighestPrecedenceAction(
const TriggeredRule::Action& action_1,
const TriggeredRule::Action& action_2) {
// Don't use the enum's int values to determine precedence since that
// may introduce bugs for new actions later.
//
// The current precedence is BLOCK > WARN > REPORT_ONLY > UNSPECIFIED
if (action_1 == TriggeredRule::BLOCK || action_2 == TriggeredRule::BLOCK) {
return TriggeredRule::BLOCK;
}
if (action_1 == TriggeredRule::WARN || action_2 == TriggeredRule::WARN) {
return TriggeredRule::WARN;
}
if (action_1 == TriggeredRule::REPORT_ONLY ||
action_2 == TriggeredRule::REPORT_ONLY) {
return TriggeredRule::REPORT_ONLY;
}
if (action_1 == TriggeredRule::ACTION_UNSPECIFIED ||
action_2 == TriggeredRule::ACTION_UNSPECIFIED) {
return TriggeredRule::ACTION_UNSPECIFIED;
}
NOTREACHED();
}
ContentAnalysisAcknowledgement::FinalAction GetHighestPrecedenceAction(
const ContentAnalysisAcknowledgement::FinalAction& action_1,
const ContentAnalysisAcknowledgement::FinalAction& action_2) {
// Don't use the enum's int values to determine precedence since that
// may introduce bugs for new actions later.
//
// The current precedence is BLOCK > WARN > REPORT_ONLY > ALLOW > UNSPECIFIED
if (action_1 == ContentAnalysisAcknowledgement::BLOCK ||
action_2 == ContentAnalysisAcknowledgement::BLOCK) {
return ContentAnalysisAcknowledgement::BLOCK;
}
if (action_1 == ContentAnalysisAcknowledgement::WARN ||
action_2 == ContentAnalysisAcknowledgement::WARN) {
return ContentAnalysisAcknowledgement::WARN;
}
if (action_1 == ContentAnalysisAcknowledgement::REPORT_ONLY ||
action_2 == ContentAnalysisAcknowledgement::REPORT_ONLY) {
return ContentAnalysisAcknowledgement::REPORT_ONLY;
}
if (action_1 == ContentAnalysisAcknowledgement::ALLOW ||
action_2 == ContentAnalysisAcknowledgement::ALLOW) {
return ContentAnalysisAcknowledgement::ALLOW;
}
if (action_1 == ContentAnalysisAcknowledgement::ACTION_UNSPECIFIED ||
action_2 == ContentAnalysisAcknowledgement::ACTION_UNSPECIFIED) {
return ContentAnalysisAcknowledgement::ACTION_UNSPECIFIED;
}
NOTREACHED();
}
FileMetadata::FileMetadata(const std::string& filename,
const std::string& sha256,
const std::string& mime_type,
int64_t size,
const ContentAnalysisResponse& scan_response)
: filename(filename),
sha256(sha256),
mime_type(mime_type),
size(size),
scan_response(scan_response) {}
FileMetadata::FileMetadata(FileMetadata&&) = default;
FileMetadata::FileMetadata(const FileMetadata&) = default;
FileMetadata& FileMetadata::operator=(const FileMetadata&) = default;
FileMetadata::~FileMetadata() = default;
const char ScanResult::kKey[] = "enterprise_connectors.scan_result_key";
ScanResult::ScanResult() = default;
ScanResult::ScanResult(FileMetadata metadata) {
file_metadata.push_back(std::move(metadata));
}
ScanResult::~ScanResult() = default;
RequestHandlerResult::RequestHandlerResult() = default;
RequestHandlerResult::~RequestHandlerResult() = default;
RequestHandlerResult::RequestHandlerResult(RequestHandlerResult&&) = default;
RequestHandlerResult& RequestHandlerResult::operator=(RequestHandlerResult&&) =
default;
RequestHandlerResult::RequestHandlerResult(const RequestHandlerResult&) =
default;
RequestHandlerResult& RequestHandlerResult::operator=(
const RequestHandlerResult&) = default;
ContentAnalysisAcknowledgement::FinalAction GetAckFinalAction(
const ContentAnalysisResponse& response) {
auto final_action = ContentAnalysisAcknowledgement::ALLOW;
for (const auto& result : response.results()) {
if (!result.has_status() ||
result.status() != ContentAnalysisResponse::Result::SUCCESS) {
continue;
}
for (const auto& rule : result.triggered_rules()) {
final_action = GetHighestPrecedenceAction(
final_action, RuleActionToAckAction(rule.action()));
}
}
return final_action;
}
std::u16string GetCustomRuleString(
const ContentAnalysisResponse::Result::TriggeredRule::CustomRuleMessage&
custom_rule_message) {
std::u16string custom_message;
for (const auto& custom_segment : custom_rule_message.message_segments()) {
base::StrAppend(
&custom_message,
{base::UnescapeForHTML(base::UTF8ToUTF16(custom_segment.text()))});
}
return custom_message;
}
std::vector<std::pair<gfx::Range, GURL>> GetCustomRuleStyles(
const ContentAnalysisResponse::Result::TriggeredRule::CustomRuleMessage&
custom_rule_message,
size_t offset) {
std::vector<std::pair<gfx::Range, GURL>> linked_ranges;
for (const auto& custom_segment : custom_rule_message.message_segments()) {
std::u16string unescaped_segment =
base::UnescapeForHTML(base::UTF8ToUTF16(custom_segment.text()));
if (custom_segment.has_link()) {
GURL url(custom_segment.link());
if (url.is_valid()) {
linked_ranges.emplace_back(
gfx::Range(offset, offset + unescaped_segment.length()), url);
}
}
offset += unescaped_segment.length();
}
return linked_ranges;
}
ContentAnalysisResponse::Result::TriggeredRule::CustomRuleMessage
CreateSampleCustomRuleMessage(const std::u16string& msg,
const std::string& url) {
ContentAnalysisResponse::Result::TriggeredRule::CustomRuleMessage
custom_message;
auto* custom_segment = custom_message.add_message_segments();
custom_segment->set_text(base::UTF16ToUTF8(msg));
custom_segment->set_link(url);
return custom_message;
}
#if BUILDFLAG(USE_BLINK)
std::optional<ContentAnalysisResponse::Result::TriggeredRule::CustomRuleMessage>
GetDownloadsCustomRuleMessage(const download::DownloadItem* download_item,
download::DownloadDangerType danger_type) {
if (!download_item) {
return std::nullopt;
}
// Custom rule message is currently only present for either warning or block
// danger types.
TriggeredRule::Action current_action;
if (danger_type == download::DOWNLOAD_DANGER_TYPE_SENSITIVE_CONTENT_WARNING) {
current_action = TriggeredRule::WARN;
} else if (danger_type ==
download::DOWNLOAD_DANGER_TYPE_SENSITIVE_CONTENT_BLOCK) {
current_action = TriggeredRule::BLOCK;
} else {
return std::nullopt;
}
enterprise_connectors::ScanResult* scan_result =
static_cast<enterprise_connectors::ScanResult*>(
download_item->GetUserData(enterprise_connectors::ScanResult::kKey));
if (!scan_result) {
return std::nullopt;
}
for (const auto& metadata : scan_result->file_metadata) {
for (const auto& result : metadata.scan_response.results()) {
for (const auto& rule : result.triggered_rules()) {
if (rule.action() == current_action && rule.has_custom_rule_message()) {
return rule.custom_rule_message();
}
}
}
}
return std::nullopt;
}
#endif // BUILDFLAG(USE_BLINK)
bool ContainsMalwareVerdict(const ContentAnalysisResponse& response) {
return std::ranges::any_of(response.results(), [](const auto& result) {
return result.tag() == kMalwareTag && !result.triggered_rules().empty();
});
}
GURL GetRegionalizedEndpoint(base::span<const char* const> region_urls,
DataRegion data_region) {
switch (data_region) {
case DataRegion::NO_PREFERENCE:
return GURL(region_urls[0]);
case DataRegion::UNITED_STATES:
return GURL(region_urls[1]);
case DataRegion::EUROPE:
return GURL(region_urls[2]);
}
}
DataRegion ChromeDataRegionSettingToEnum(int chrome_data_region_setting) {
switch (chrome_data_region_setting) {
case 0:
return DataRegion::NO_PREFERENCE;
case 1:
return DataRegion::UNITED_STATES;
case 2:
return DataRegion::EUROPE;
}
NOTREACHED();
}
EnterpriseReportingEventType GetUmaEnumFromEventName(
std::string_view eventName) {
auto it = kEventNameToUmaEnumMap.find(eventName);
return it != kEventNameToUmaEnumMap.end()
? it->second
: EnterpriseReportingEventType::kUnknownEvent;
}
EnterpriseReportingEventType GetUmaEnumFromEventCase(EventCase eventCase) {
auto it = kEventCaseToUmaEnumMap.find(eventCase);
return it != kEventCaseToUmaEnumMap.end()
? it->second
: EnterpriseReportingEventType::kUnknownEvent;
}
std::string EventResultToString(EventResult result) {
switch (result) {
case EventResult::UNKNOWN:
return "EVENT_RESULT_UNKNOWN";
case EventResult::ALLOWED:
return "EVENT_RESULT_ALLOWED";
case EventResult::WARNED:
return "EVENT_RESULT_WARNED";
case EventResult::BLOCKED:
return "EVENT_RESULT_BLOCKED";
case EventResult::BYPASSED:
return "EVENT_RESULT_BYPASSED";
}
NOTREACHED();
}
std::string GetProfileEmail(signin::IdentityManager* identity_manager) {
// If the profile is not signed in, GetPrimaryAccountInfo() returns an
// empty account info.
return identity_manager
? identity_manager
->GetPrimaryAccountInfo(signin::ConsentLevel::kSignin)
.email
: std::string();
}
std::string GetSuccessfulUploadDurationUmaMetricName(
EnterpriseReportingEventType event_type) {
auto* metric_name = base::FindOrNull(kUmaEnumToStringMap, event_type);
return metric_name
? base::StrCat({*metric_name, "UploadSuccess.Duration"})
: base::StrCat({kUnknownUmaMetricName, "UploadSuccess.Duration"});
}
std::string GetFailedUploadDurationUmaMetricName(
EnterpriseReportingEventType event_type) {
auto* metric_name = base::FindOrNull(kUmaEnumToStringMap, event_type);
return metric_name
? base::StrCat({*metric_name, "UploadFailure.Duration"})
: base::StrCat({kUnknownUmaMetricName, "UploadFailure.Duration"});
}
} // namespace enterprise_connectors
|