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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
|
// Copyright 2023 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/data_controls/core/browser/rule.h"
#include <string_view>
#include <vector>
#include "base/containers/fixed_flat_map.h"
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/strings/string_util.h"
#include "components/enterprise/buildflags/buildflags.h"
#include "components/enterprise/data_controls/core/browser/conditions/and_condition.h"
#include "components/enterprise/data_controls/core/browser/conditions/attributes_condition.h"
#include "components/enterprise/data_controls/core/browser/conditions/not_condition.h"
#include "components/enterprise/data_controls/core/browser/conditions/or_condition.h"
#include "components/enterprise/data_controls/core/browser/features.h"
#include "components/policy/core/browser/policy_error_map.h"
#include "components/strings/grit/components_strings.h"
namespace data_controls {
namespace {
// Constants used to parse sub-dictionaries of DLP policies that should map to
// an AttributesCondition.
constexpr char kKeyName[] = "name";
constexpr char kKeyRuleId[] = "rule_id";
constexpr char kKeyDescription[] = "description";
constexpr char kKeySources[] = "sources";
constexpr char kKeyDestinations[] = "destinations";
constexpr char kKeyRestrictions[] = "restrictions";
constexpr char kKeyAnd[] = "and";
constexpr char kKeyOr[] = "or";
constexpr char kKeyNot[] = "not";
constexpr char kKeyClass[] = "class";
constexpr char kKeyLevel[] = "level";
// Helper to make dictionary parsing code more readable.
std::string GetStringOrEmpty(const base::Value::Dict& dict, const char* key) {
const std::string* value = dict.FindString(key);
return value ? *value : std::string();
}
// A oneof attribute is an attribute that needs to be the only condition in
// their dictionary. If other attributes are present alongside them, it creates
// ambiguity as to how the rule is evaluated, and as such this is considered an
// error in the set policy.
std::vector<std::string_view> OneOfConditions(const base::Value::Dict& value) {
std::vector<std::string_view> oneof_conditions;
for (const char* oneof_value :
{// "and", "or" and "not" need to be the only value at their level as it
// is otherwise ambiguous which of them has precedence or how they are
// combined together into one condition.
kKeyAnd, kKeyOr, kKeyNot,
// "os_clipboard" needs to be the only value in its dictionary as it
// represents a unique source/destination. For example, a clipboard
// interaction cannot both be the OS clipboard and match URL patterns
// at the same time.
AttributesCondition::kKeyOsClipboard}) {
if (value.contains(oneof_value)) {
oneof_conditions.push_back(oneof_value);
}
}
return oneof_conditions;
}
// Returns any condition present in `value` that wouldn't match
// `OneOfConditions`.
std::vector<std::string_view> AnyOfConditions(const base::Value::Dict& value) {
std::vector<std::string_view> anyof_conditions;
for (const char* anyof_condition :
{kKeySources, kKeyDestinations, AttributesCondition::kKeyUrls,
AttributesCondition::kKeyIncognito,
AttributesCondition::kKeyOtherProfile,
#if BUILDFLAG(IS_CHROMEOS)
AttributesCondition::kKeyComponents
#endif // BUILDFLAG(IS_CHROMEOS)
}) {
if (value.contains(anyof_condition)) {
anyof_conditions.push_back(anyof_condition);
}
}
return anyof_conditions;
}
// Clones `error_path` and update the copy with a new value.
policy::PolicyErrorPath CreateErrorPath(
const policy::PolicyErrorPath& error_path,
std::string new_value,
std::optional<int> new_list_index = std::nullopt) {
policy::PolicyErrorPath new_error_path(error_path);
new_error_path.push_back(std::move(new_value));
if (new_list_index) {
new_error_path.push_back(*new_list_index);
}
return new_error_path;
}
// Helper to check if a restriction is allowed to be applied to a rule given
// the currently enabled features. If you have a Finch flag controlling whether
// a type of restriction should be applied or not, check it here.
bool IgnoreRestriction(Rule::Restriction restriction) {
return false;
}
} // namespace
Rule::Rule(Rule&& other) = default;
Rule::~Rule() = default;
Rule::Rule(std::string name,
std::string rule_id,
std::string description,
std::unique_ptr<const Condition> condition,
base::flat_map<Restriction, Level> restrictions)
: name_(std::move(name)),
rule_id_(std::move(rule_id)),
description_(std::move(description)),
condition_(std::move(condition)),
restrictions_(std::move(restrictions)) {}
// static
std::optional<Rule> Rule::Create(const base::Value& value) {
if (!value.is_dict()) {
return std::nullopt;
}
return Create(value.GetDict());
}
// static
std::optional<Rule> Rule::Create(const base::Value::Dict& value) {
auto condition = GetCondition(value);
if (!condition) {
return std::nullopt;
}
auto restrictions = GetRestrictions(value);
// To avoid create a `Rule` object with restrictions for disabled features,
// `restrictions` is first filtered. This is done here instead of in
// `GetRestrictions()` so that it can still be used by policy handler code to
// parse the policy correctly.
base::EraseIf(restrictions, [](const auto& entry) {
return IgnoreRestriction(entry.first);
});
if (restrictions.empty()) {
return std::nullopt;
}
return std::make_optional(Rule(
GetStringOrEmpty(value, kKeyName), GetStringOrEmpty(value, kKeyRuleId),
GetStringOrEmpty(value, kKeyDescription), std::move(condition),
std::move(restrictions)));
}
Rule::Level Rule::GetLevel(Restriction restriction,
const ActionContext& context) const {
// Evaluating the condition of a rule could be expensive, so check
// preemptively if there are any restrictions first.
if (!restrictions_.contains(restriction)) {
return Level::kNotSet;
}
if (condition_->CanBeEvaluated(context) && condition_->IsTriggered(context)) {
return restrictions_.at(restriction);
}
return Level::kNotSet;
}
const std::string& Rule::name() const {
return name_;
}
const std::string& Rule::rule_id() const {
return rule_id_;
}
const std::string& Rule::description() const {
return description_;
}
// static
std::unique_ptr<const Condition> Rule::GetCondition(
const base::Value::Dict& value) {
// `value` can hold different condition-related keys, namely:
// - The "not" key with a sub-dict that is parsed recursively.
// - The "and" or "not" keys with sub-arrays of conditions parsed recursively.
// - The "sources" and/or "destinations" keys with corresponding sub-dicts.
// These 3 cases are mutually exclusive and should preemptively be filtered
// by policy validations of the DataControlsRules policy, and as such the
// precedence in which these keys are parsed here should not matter.
if (const base::Value::Dict* condition = value.FindDict(kKeyNot)) {
return NotCondition::Create(GetCondition(*condition));
}
if (const base::Value::List* condition = value.FindList(kKeyAnd)) {
return AndCondition::Create(GetListConditions(*condition));
}
if (const base::Value::List* condition = value.FindList(kKeyOr)) {
return OrCondition::Create(GetListConditions(*condition));
}
// Reaching this statement implies `value` contains no boolean-logic keys
// ("not", "and", "or") and that it should simply be evaluated based on
// attributes keys ("sources", "destinations").
return GetSourcesAndDestinationsCondition(value);
}
// static
std::unique_ptr<const Condition> Rule::GetSourcesAndDestinationsCondition(
const base::Value::Dict& value) {
// This function will add a `Condition` for each of the following keys found
// in `value`:
// - "sources"
// - "destinations"
// Then combine them into an `AndCondition` to make an overall condition for
// the rule being constructed.
std::vector<std::unique_ptr<const Condition>> conditions;
const base::Value* sources_value = value.Find(kKeySources);
if (sources_value) {
std::unique_ptr<Condition> sources_condition =
SourceAttributesCondition::Create(*sources_value);
if (sources_condition) {
conditions.push_back(std::move(sources_condition));
}
}
const base::Value* destinations_value = value.Find(kKeyDestinations);
if (destinations_value) {
std::unique_ptr<Condition> destinations_condition =
DestinationAttributesCondition::Create(*destinations_value);
if (destinations_condition) {
conditions.push_back(std::move(destinations_condition));
}
}
return AndCondition::Create(std::move(conditions));
}
// static
std::vector<std::unique_ptr<const Condition>> Rule::GetListConditions(
const base::Value::List& value) {
std::vector<std::unique_ptr<const Condition>> sub_conditions;
for (const base::Value& sub_value : value) {
if (!sub_value.is_dict()) {
continue;
}
auto sub_condition = GetCondition(sub_value.GetDict());
if (sub_condition) {
sub_conditions.push_back(std::move(sub_condition));
}
}
return sub_conditions;
}
// static
base::flat_map<Rule::Restriction, Rule::Level> Rule::GetRestrictions(
const base::Value::Dict& value) {
const base::Value::List* restrictions_list = value.FindList(kKeyRestrictions);
if (!restrictions_list) {
return {};
}
base::flat_map<Restriction, Level> restrictions;
// `restrictions_list` should contain dictionaries of the following schema:
// {
// class: CLIPBOARD|SCREENSHOT|PRINTING|PRIVACY_SCREEN|etc,
// level: ALLOW|BLOCK|REPORT|WARN
// }
// For compatibility, just ignore unrecognized values and keep iterating and
// populating `restrictions`.
for (const base::Value& entry : *restrictions_list) {
if (!entry.is_dict()) {
continue;
}
const base::Value::Dict& entry_dict = entry.GetDict();
const std::string* class_string = entry_dict.FindString(kKeyClass);
const std::string* level_string = entry_dict.FindString(kKeyLevel);
if (!class_string || !level_string) {
continue;
}
Restriction restriction = StringToRestriction(*class_string);
Level level = StringToLevel(*level_string);
if (restriction == Restriction::kUnknownRestriction ||
level == Level::kNotSet) {
continue;
}
// If there is already an entry for `restriction`, only override it if our
// current `level` has precedence.
if (!restrictions.contains(restriction) ||
restrictions.at(restriction) < level) {
restrictions[restriction] = level;
}
}
return restrictions;
}
// static
Rule::Restriction Rule::StringToRestriction(const std::string& restriction) {
static constexpr auto kMap =
base::MakeFixedFlatMap<std::string_view, Restriction>({
{kRestrictionClipboard, Restriction::kClipboard},
{kRestrictionScreenshot, Restriction::kScreenshot},
{kRestrictionPrinting, Restriction::kPrinting},
{kRestrictionPrivacyScreen, Restriction::kPrivacyScreen},
{kRestrictionScreenShare, Restriction::kScreenShare},
{kRestrictionFiles, Restriction::kFiles},
});
static_assert(
static_cast<int>(Restriction::kMaxValue) == kMap.size(),
"The Restriction enum needs to have an equivalent string for each value");
if (!kMap.contains(restriction)) {
return Restriction::kUnknownRestriction;
}
return kMap.at(restriction);
}
// static
Rule::Level Rule::StringToLevel(const std::string& level) {
static constexpr auto kMap = base::MakeFixedFlatMap<std::string_view, Level>({
{kLevelAllow, Level::kAllow},
{kLevelBlock, Level::kBlock},
{kLevelWarn, Level::kWarn},
{kLevelReport, Level::kReport},
});
static_assert(
static_cast<int>(Level::kMaxValue) == kMap.size(),
"The Level enum needs to have an equivalent string for each value");
if (!kMap.contains(level)) {
return Level::kNotSet;
}
return kMap.at(level);
}
// static
const char* Rule::RestrictionToString(Restriction restriction) {
// A switch statement is used here instead of a map so that new values being
// added to the `Restriction` enum break compilation and force updating this
// code.
switch (restriction) {
case Restriction::kUnknownRestriction:
return nullptr;
case Restriction::kClipboard:
return kRestrictionClipboard;
case Restriction::kScreenshot:
return kRestrictionScreenshot;
case Restriction::kPrinting:
return kRestrictionPrinting;
case Restriction::kPrivacyScreen:
return kRestrictionPrivacyScreen;
case Restriction::kScreenShare:
return kRestrictionScreenShare;
case Restriction::kFiles:
return kRestrictionFiles;
}
}
// static
const char* Rule::LevelToString(Level level) {
// A switch statement is used here instead of a map so that new values being
// added to the `Level` enum break compilation and force updating this code.
switch (level) {
case Level::kNotSet:
return nullptr;
case Level::kAllow:
return kLevelAllow;
case Level::kBlock:
return kLevelBlock;
case Level::kWarn:
return kLevelWarn;
case Level::kReport:
return kLevelReport;
}
}
// static
bool Rule::ValidateRuleValue(const char* policy_name,
const base::Value::Dict& root_value,
policy::PolicyErrorPath error_path,
policy::PolicyErrorMap* errors) {
auto restrictions = GetRestrictions(root_value);
if (!AddUnsupportedRestrictionErrors(policy_name, restrictions, error_path,
errors) ||
restrictions.empty()) {
return false;
}
return ValidateRuleSubValues(policy_name, root_value, restrictions,
error_path, errors);
}
// static
bool Rule::ValidateRuleSubValues(
const char* policy_name,
const base::Value::Dict& value,
const base::flat_map<Rule::Restriction, Rule::Level>& restrictions,
policy::PolicyErrorPath error_path,
policy::PolicyErrorMap* errors) {
std::vector<std::string_view> oneof_conditions = OneOfConditions(value);
std::vector<std::string_view> anyof_conditions = AnyOfConditions(value);
if (oneof_conditions.size() > 1 ||
(oneof_conditions.size() == 1 && anyof_conditions.size() != 0)) {
AddMutuallyExclusiveErrors(oneof_conditions, anyof_conditions, policy_name,
std::move(error_path), errors);
return false;
}
if (!AddUnsupportedAttributeErrors(oneof_conditions, anyof_conditions,
restrictions, policy_name, error_path,
errors)) {
return false;
}
// Even if the values in `oneof_conditions` and `anyof_conditions` are
// acceptable for `value`, it's possible there are errors in nested values, so
// additional checks must be performed recursively.
bool valid = true;
for (const char* sub_key : {kKeySources, kKeyDestinations, kKeyNot}) {
if (value.contains(sub_key)) {
valid &= ValidateRuleSubValues(
policy_name, *value.FindDict(sub_key), restrictions,
CreateErrorPath(error_path, sub_key), errors);
}
}
for (const char* sub_key : {kKeyAnd, kKeyOr}) {
if (value.contains(sub_key)) {
int index = 0;
for (const base::Value& sub_condition : *value.FindList(sub_key)) {
valid &= ValidateRuleSubValues(
policy_name, sub_condition.GetDict(), restrictions,
CreateErrorPath(error_path, sub_key, index), errors);
++index;
}
}
}
return valid;
}
// static
void Rule::AddMutuallyExclusiveErrors(
const std::vector<std::string_view>& oneof_conditions,
const std::vector<std::string_view>& anyof_conditions,
const char* policy_name,
policy::PolicyErrorPath error_path,
policy::PolicyErrorMap* errors) {
if (!errors || oneof_conditions.size() == 0) {
return;
}
if (oneof_conditions.size() > 1) {
errors->AddError(policy_name,
IDS_POLICY_DATA_CONTROLS_MUTUALLY_EXCLUSIVE_KEYS,
base::JoinString(oneof_conditions, ", "), error_path);
}
if (anyof_conditions.size() > 0) {
errors->AddError(policy_name,
IDS_POLICY_DATA_CONTROLS_MUTUALLY_EXCLUSIVE_KEY_SETS,
base::JoinString(anyof_conditions, ", "),
base::JoinString(oneof_conditions, ", "), error_path);
}
}
// static
bool Rule::AddUnsupportedAttributeErrors(
const std::vector<std::string_view>& oneof_conditions,
const std::vector<std::string_view>& anyof_conditions,
base::flat_map<Rule::Restriction, Rule::Level> restrictions,
const char* policy_name,
policy::PolicyErrorPath error_path,
policy::PolicyErrorMap* errors) {
static const base::NoDestructor<
base::flat_map<Rule::Restriction, std::set<std::string_view>>>
kSupportedAttributes({
{Restriction::kClipboard,
{AttributesCondition::kKeyOsClipboard, AttributesCondition::kKeyUrls,
AttributesCondition::kKeyIncognito,
AttributesCondition::kKeyOtherProfile,
#if BUILDFLAG(IS_CHROMEOS)
AttributesCondition::kKeyComponents,
#endif // BUILDFLAG(IS_CHROMEOS)
kKeyAnd, kKeyOr, kKeyNot, kKeySources, kKeyDestinations}},
{Restriction::kScreenshot,
{AttributesCondition::kKeyUrls, AttributesCondition::kKeyIncognito,
#if BUILDFLAG(IS_CHROMEOS)
AttributesCondition::kKeyComponents,
#endif // BUILDFLAG(IS_CHROMEOS)
kKeyAnd, kKeyOr, kKeyNot, kKeySources}},
});
bool valid = true;
for (const auto& restriction : restrictions) {
if (!kSupportedAttributes->contains(restriction.first)) {
// This shouldn't be reached as `AddUnsupportedRestrictionErrors` should
// catch these unsupported restrictions.
NOTREACHED();
}
for (const auto& attribute : anyof_conditions) {
if (!kSupportedAttributes->at(restriction.first).contains(attribute)) {
if (errors) {
errors->AddError(policy_name,
IDS_POLICY_DATA_CONTROLS_UNSUPPORTED_CONDITION,
std::string(attribute),
RestrictionToString(restriction.first), error_path);
}
valid = false;
}
}
for (const auto& attribute : oneof_conditions) {
if (!kSupportedAttributes->at(restriction.first).contains(attribute)) {
if (errors) {
errors->AddError(policy_name,
IDS_POLICY_DATA_CONTROLS_UNSUPPORTED_CONDITION,
std::string(attribute),
RestrictionToString(restriction.first), error_path);
}
valid = false;
}
}
}
return valid;
}
// static
bool Rule::AddUnsupportedRestrictionErrors(
const char* policy_name,
const base::flat_map<Rule::Restriction, Rule::Level>& restrictions,
policy::PolicyErrorPath error_path,
policy::PolicyErrorMap* errors) {
static const base::NoDestructor<
base::flat_map<Rule::Restriction, std::set<Rule::Level>>>
kSupportedRestrictions({
{Restriction::kClipboard,
{Level::kNotSet, Level::kReport, Level::kWarn, Level::kBlock}},
#if BUILDFLAG(ENTERPRISE_SCREENSHOT_PROTECTION)
{Restriction::kScreenshot, {Level::kNotSet, Level::kBlock}},
#endif // BUILDFLAG(ENTERPRISE_SCREENSHOT_PROTECTION)
});
bool valid = true;
for (const auto& restriction : restrictions) {
if (!kSupportedRestrictions->contains(restriction.first)) {
if (errors) {
errors->AddError(policy_name,
IDS_POLICY_DATA_CONTROLS_UNSUPPORTED_RESTRICTION,
RestrictionToString(restriction.first), error_path);
}
valid = false;
continue;
}
if (!kSupportedRestrictions->at(restriction.first)
.contains(restriction.second)) {
if (errors) {
errors->AddError(policy_name,
IDS_POLICY_DATA_CONTROLS_UNSUPPORTED_LEVEL,
RestrictionToString(restriction.first),
LevelToString(restriction.second), error_path);
}
valid = false;
}
}
return valid;
}
} // namespace data_controls
|