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 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/api/declarative_net_request/declarative_net_request_api.h"
#include <functional>
#include <iterator>
#include <memory>
#include <optional>
#include <set>
#include <utility>
#include <vector>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/time/time.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/api/declarative_net_request/action_tracker.h"
#include "extensions/browser/api/declarative_net_request/composite_matcher.h"
#include "extensions/browser/api/declarative_net_request/constants.h"
#include "extensions/browser/api/declarative_net_request/file_backed_ruleset_source.h"
#include "extensions/browser/api/declarative_net_request/prefs_helper.h"
#include "extensions/browser/api/declarative_net_request/request_action.h"
#include "extensions/browser/api/declarative_net_request/request_params.h"
#include "extensions/browser/api/declarative_net_request/rules_monitor_service.h"
#include "extensions/browser/api/declarative_net_request/ruleset_manager.h"
#include "extensions/browser/api/declarative_net_request/ruleset_matcher.h"
#include "extensions/browser/api/declarative_net_request/utils.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/api/web_request/permission_helper.h"
#include "extensions/browser/api/web_request/web_request_permissions.h"
#include "extensions/browser/extension_file_task_runner.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/quota_service.h"
#include "extensions/common/api/declarative_net_request.h"
#include "extensions/common/api/declarative_net_request/constants.h"
#include "extensions/common/api/declarative_net_request/dnr_manifest_data.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/extension_id.h"
namespace extensions {
namespace {
namespace dnr_api = api::declarative_net_request;
// Returns whether |extension| can call getMatchedRules for the specified
// |tab_id| and populates |error| if it can't. If no tab ID is specified, then
// the API call is for all tabs.
bool CanCallGetMatchedRules(content::BrowserContext* browser_context,
const Extension* extension,
std::optional<int> tab_id,
std::string* error) {
bool can_call =
declarative_net_request::HasDNRFeedbackPermission(extension, tab_id);
if (!can_call) {
*error = declarative_net_request::kErrorGetMatchedRulesMissingPermissions;
}
return can_call;
}
// Filter the fetched dynamic/session rules by the user provided rule filter.
void FilterRules(std::vector<dnr_api::Rule>& rules,
const dnr_api::GetRulesFilter& rule_filter) {
// Filter the rules by the rule IDs, if provided.
if (rule_filter.rule_ids) {
const base::flat_set<int>& rule_ids = *rule_filter.rule_ids;
std::erase_if(rules, [rule_ids](const auto& rule) {
return !rule_ids.contains(rule.id);
});
}
}
// Returns if the first action in `actions` will intercept the request (i.e.
// block or redirect it).
// Note: If `actions` contains more than one action, then it's guaranteed to be
// modifyHeaders actions which do not intercept the request. See
// DeclarativeNetRequestTestMatchOutcomeFunction::GetActions, which mirrors
// RulesetManager::EvaluateRequestInternal for more details.
bool IsRequestIntercepted(
const std::vector<declarative_net_request::RequestAction>& actions) {
return !actions.empty() &&
(actions[0].IsBlockOrCollapse() || actions[0].IsRedirectOrUpgrade());
}
// Returns the priority of the matching allow action in `actions` or 0 if none
// exists. Note: DeclarativeNetRequestTestMatchOutcomeFunction::GetActions.
// which is based off of RulesetManager::EvaluateRequestInternal, will return
// either no action, a list of modifyheaders actions or a single action of any
// other type. Based on this, only the first action of `actions` need to be
// examined.
uint64_t GetAllowActionPriority(
const std::vector<declarative_net_request::RequestAction>& actions) {
uint64_t max_priority = 0;
if (!actions.empty() && actions[0].IsAllowOrAllowAllRequests()) {
max_priority = actions[0].index_priority;
}
return max_priority;
}
} // namespace
DeclarativeNetRequestUpdateDynamicRulesFunction::
DeclarativeNetRequestUpdateDynamicRulesFunction() = default;
DeclarativeNetRequestUpdateDynamicRulesFunction::
~DeclarativeNetRequestUpdateDynamicRulesFunction() = default;
ExtensionFunction::ResponseAction
DeclarativeNetRequestUpdateDynamicRulesFunction::Run() {
using Params = dnr_api::UpdateDynamicRules::Params;
auto params = Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params.has_value());
std::vector<int> rule_ids_to_remove;
if (params->options.remove_rule_ids) {
rule_ids_to_remove = std::move(*params->options.remove_rule_ids);
}
std::vector<dnr_api::Rule> rules_to_add;
if (params->options.add_rules) {
rules_to_add = std::move(*params->options.add_rules);
}
// Early return if there is nothing to do.
if (rule_ids_to_remove.empty() && rules_to_add.empty()) {
return RespondNow(NoArguments());
}
// Collect rules to add in the Extension Telemetry Service.
if (!rules_to_add.empty()) {
ExtensionsBrowserClient::Get()->NotifyExtensionApiDeclarativeNetRequest(
browser_context(), extension_id(), rules_to_add);
}
auto* rules_monitor_service =
declarative_net_request::RulesMonitorService::Get(browser_context());
DCHECK(rules_monitor_service);
DCHECK(extension());
auto callback = base::BindOnce(
&DeclarativeNetRequestUpdateDynamicRulesFunction::OnDynamicRulesUpdated,
this);
rules_monitor_service->UpdateDynamicRules(
*extension(), std::move(rule_ids_to_remove), std::move(rules_to_add),
std::move(callback));
return RespondLater();
}
void DeclarativeNetRequestUpdateDynamicRulesFunction::OnDynamicRulesUpdated(
std::optional<std::string> error) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (error) {
Respond(Error(std::move(*error)));
} else {
Respond(NoArguments());
}
}
DeclarativeNetRequestGetDynamicRulesFunction::
DeclarativeNetRequestGetDynamicRulesFunction() = default;
DeclarativeNetRequestGetDynamicRulesFunction::
~DeclarativeNetRequestGetDynamicRulesFunction() = default;
ExtensionFunction::ResponseAction
DeclarativeNetRequestGetDynamicRulesFunction::Run() {
using Params = dnr_api::GetDynamicRules::Params;
auto params = Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params.has_value());
auto source = declarative_net_request::FileBackedRulesetSource::CreateDynamic(
browser_context(), extension()->id());
auto read_dynamic_rules = base::BindOnce(
[](const declarative_net_request::FileBackedRulesetSource& source) {
return source.ReadJSONRulesUnsafe();
},
std::move(source));
GetExtensionFileTaskRunner()->PostTaskAndReplyWithResult(
FROM_HERE, std::move(read_dynamic_rules),
base::BindOnce(
&DeclarativeNetRequestGetDynamicRulesFunction::OnDynamicRulesFetched,
this, std::move(params).value()));
return RespondLater();
}
void DeclarativeNetRequestGetDynamicRulesFunction::OnDynamicRulesFetched(
dnr_api::GetDynamicRules::Params params,
declarative_net_request::ReadJSONRulesResult read_json_result) {
using Status = declarative_net_request::ReadJSONRulesResult::Status;
LogReadDynamicRulesStatus(read_json_result.status);
DCHECK(read_json_result.status == Status::kSuccess ||
read_json_result.rules.empty());
// Unlike errors such as kJSONParseError, which normally denote corruption, a
// read error is probably a transient error. Hence raise an error instead of
// returning an empty list.
if (read_json_result.status == Status::kFileReadError) {
Respond(Error(declarative_net_request::kInternalErrorGettingDynamicRules));
return;
}
if (params.filter) {
FilterRules(read_json_result.rules, *params.filter);
}
Respond(ArgumentList(
dnr_api::GetDynamicRules::Results::Create(read_json_result.rules)));
}
DeclarativeNetRequestUpdateSessionRulesFunction::
DeclarativeNetRequestUpdateSessionRulesFunction() = default;
DeclarativeNetRequestUpdateSessionRulesFunction::
~DeclarativeNetRequestUpdateSessionRulesFunction() = default;
ExtensionFunction::ResponseAction
DeclarativeNetRequestUpdateSessionRulesFunction::Run() {
using Params = dnr_api::UpdateSessionRules::Params;
auto params = Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params.has_value());
std::vector<int> rule_ids_to_remove;
if (params->options.remove_rule_ids) {
rule_ids_to_remove = std::move(*params->options.remove_rule_ids);
}
std::vector<dnr_api::Rule> rules_to_add;
if (params->options.add_rules) {
rules_to_add = std::move(*params->options.add_rules);
}
// Early return if there is nothing to do.
if (rule_ids_to_remove.empty() && rules_to_add.empty()) {
return RespondNow(NoArguments());
}
// Collect rules to add in the Extension Telemetry Service.
if (!rules_to_add.empty()) {
ExtensionsBrowserClient::Get()->NotifyExtensionApiDeclarativeNetRequest(
browser_context(), extension_id(), rules_to_add);
}
auto* rules_monitor_service =
declarative_net_request::RulesMonitorService::Get(browser_context());
DCHECK(rules_monitor_service);
rules_monitor_service->UpdateSessionRules(
*extension(), std::move(rule_ids_to_remove), std::move(rules_to_add),
base::BindOnce(&DeclarativeNetRequestUpdateSessionRulesFunction::
OnSessionRulesUpdated,
this));
return RespondLater();
}
void DeclarativeNetRequestUpdateSessionRulesFunction::OnSessionRulesUpdated(
std::optional<std::string> error) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (error) {
Respond(Error(std::move(*error)));
} else {
Respond(NoArguments());
}
}
DeclarativeNetRequestGetSessionRulesFunction::
DeclarativeNetRequestGetSessionRulesFunction() = default;
DeclarativeNetRequestGetSessionRulesFunction::
~DeclarativeNetRequestGetSessionRulesFunction() = default;
ExtensionFunction::ResponseAction
DeclarativeNetRequestGetSessionRulesFunction::Run() {
using Params = dnr_api::GetSessionRules::Params;
auto params = Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params.has_value());
auto* rules_monitor_service =
declarative_net_request::RulesMonitorService::Get(browser_context());
DCHECK(rules_monitor_service);
auto rules = rules_monitor_service->GetSessionRules(extension_id());
if (params->filter) {
FilterRules(rules, *params->filter);
}
return RespondNow(
ArgumentList(dnr_api::GetSessionRules::Results::Create(rules)));
}
DeclarativeNetRequestUpdateEnabledRulesetsFunction::
DeclarativeNetRequestUpdateEnabledRulesetsFunction() = default;
DeclarativeNetRequestUpdateEnabledRulesetsFunction::
~DeclarativeNetRequestUpdateEnabledRulesetsFunction() = default;
ExtensionFunction::ResponseAction
DeclarativeNetRequestUpdateEnabledRulesetsFunction::Run() {
using Params = dnr_api::UpdateEnabledRulesets::Params;
using RulesetID = declarative_net_request::RulesetID;
using DNRManifestData = declarative_net_request::DNRManifestData;
auto params = Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params.has_value());
std::set<RulesetID> ids_to_disable;
std::set<RulesetID> ids_to_enable;
const DNRManifestData::ManifestIDToRulesetMap& public_id_map =
DNRManifestData::GetManifestIDToRulesetMap(*extension());
if (params->options.enable_ruleset_ids) {
for (const std::string& public_id_to_enable :
*params->options.enable_ruleset_ids) {
auto it = public_id_map.find(public_id_to_enable);
if (it == public_id_map.end()) {
return RespondNow(Error(ErrorUtils::FormatErrorMessage(
declarative_net_request::kInvalidRulesetIDError,
public_id_to_enable)));
}
ids_to_enable.insert(it->second->id);
}
}
if (params->options.disable_ruleset_ids) {
for (const std::string& public_id_to_disable :
*params->options.disable_ruleset_ids) {
auto it = public_id_map.find(public_id_to_disable);
if (it == public_id_map.end()) {
return RespondNow(Error(ErrorUtils::FormatErrorMessage(
declarative_net_request::kInvalidRulesetIDError,
public_id_to_disable)));
}
// |ruleset_ids_to_enable| takes priority over |ruleset_ids_to_disable|.
RulesetID id = it->second->id;
if (base::Contains(ids_to_enable, id)) {
continue;
}
ids_to_disable.insert(id);
}
}
if (ids_to_enable.empty() && ids_to_disable.empty()) {
return RespondNow(NoArguments());
}
auto* rules_monitor_service =
declarative_net_request::RulesMonitorService::Get(browser_context());
DCHECK(rules_monitor_service);
DCHECK(extension());
rules_monitor_service->UpdateEnabledStaticRulesets(
*extension(), std::move(ids_to_disable), std::move(ids_to_enable),
base::BindOnce(&DeclarativeNetRequestUpdateEnabledRulesetsFunction::
OnEnabledStaticRulesetsUpdated,
this));
return did_respond() ? AlreadyResponded() : RespondLater();
}
void DeclarativeNetRequestUpdateEnabledRulesetsFunction::
OnEnabledStaticRulesetsUpdated(std::optional<std::string> error) {
if (error) {
Respond(Error(std::move(*error)));
} else {
Respond(NoArguments());
}
}
DeclarativeNetRequestGetEnabledRulesetsFunction::
DeclarativeNetRequestGetEnabledRulesetsFunction() = default;
DeclarativeNetRequestGetEnabledRulesetsFunction::
~DeclarativeNetRequestGetEnabledRulesetsFunction() = default;
ExtensionFunction::ResponseAction
DeclarativeNetRequestGetEnabledRulesetsFunction::Run() {
auto* rules_monitor_service =
declarative_net_request::RulesMonitorService::Get(browser_context());
DCHECK(rules_monitor_service);
std::vector<std::string> public_ids;
declarative_net_request::CompositeMatcher* matcher =
rules_monitor_service->ruleset_manager()->GetMatcherForExtension(
extension_id());
if (matcher) {
DCHECK(extension());
public_ids = GetPublicRulesetIDs(*extension(), *matcher);
// Exclude any reserved ruleset IDs since they would correspond to
// non-static rulesets.
std::erase_if(public_ids, [](const std::string& id) {
DCHECK(!id.empty());
return id[0] == declarative_net_request::kReservedRulesetIDPrefix;
});
}
return RespondNow(
ArgumentList(dnr_api::GetEnabledRulesets::Results::Create(public_ids)));
}
DeclarativeNetRequestUpdateStaticRulesFunction::
DeclarativeNetRequestUpdateStaticRulesFunction() = default;
DeclarativeNetRequestUpdateStaticRulesFunction::
~DeclarativeNetRequestUpdateStaticRulesFunction() = default;
ExtensionFunction::ResponseAction
DeclarativeNetRequestUpdateStaticRulesFunction::Run() {
using Params = dnr_api::UpdateStaticRules::Params;
using DNRManifestData = declarative_net_request::DNRManifestData;
using RulesMonitorService = declarative_net_request::RulesMonitorService;
using RuleIdsToUpdate = declarative_net_request::PrefsHelper::RuleIdsToUpdate;
auto params = Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params.has_value());
const DNRManifestData::ManifestIDToRulesetMap& public_id_map =
DNRManifestData::GetManifestIDToRulesetMap(*extension());
auto it = public_id_map.find(params->options.ruleset_id);
if (it == public_id_map.end()) {
return RespondNow(Error(ErrorUtils::FormatErrorMessage(
declarative_net_request::kInvalidRulesetIDError,
params->options.ruleset_id)));
}
RuleIdsToUpdate rule_ids_to_update(params->options.disable_rule_ids,
params->options.enable_rule_ids);
if (rule_ids_to_update.Empty()) {
return RespondNow(NoArguments());
}
auto* rules_monitor_service = RulesMonitorService::Get(browser_context());
DCHECK(rules_monitor_service);
DCHECK(extension());
rules_monitor_service->UpdateStaticRules(
*extension(), it->second->id, std::move(rule_ids_to_update),
base::BindOnce(
&DeclarativeNetRequestUpdateStaticRulesFunction::OnStaticRulesUpdated,
this));
return did_respond() ? AlreadyResponded() : RespondLater();
}
void DeclarativeNetRequestUpdateStaticRulesFunction::OnStaticRulesUpdated(
std::optional<std::string> error) {
if (error) {
Respond(Error(std::move(*error)));
} else {
Respond(NoArguments());
}
}
DeclarativeNetRequestGetDisabledRuleIdsFunction::
DeclarativeNetRequestGetDisabledRuleIdsFunction() = default;
DeclarativeNetRequestGetDisabledRuleIdsFunction::
~DeclarativeNetRequestGetDisabledRuleIdsFunction() = default;
ExtensionFunction::ResponseAction
DeclarativeNetRequestGetDisabledRuleIdsFunction::Run() {
using Params = dnr_api::GetDisabledRuleIds::Params;
using RulesetID = declarative_net_request::RulesetID;
using DNRManifestData = declarative_net_request::DNRManifestData;
auto params = Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params.has_value());
const DNRManifestData::ManifestIDToRulesetMap& public_id_map =
DNRManifestData::GetManifestIDToRulesetMap(*extension());
auto it = public_id_map.find(params->options.ruleset_id);
if (it == public_id_map.end()) {
return RespondNow(Error(ErrorUtils::FormatErrorMessage(
declarative_net_request::kInvalidRulesetIDError,
params->options.ruleset_id)));
}
RulesetID ruleset_id = it->second->id;
auto* rules_monitor_service =
declarative_net_request::RulesMonitorService::Get(browser_context());
DCHECK(rules_monitor_service);
DCHECK(extension());
rules_monitor_service->GetDisabledRuleIds(
*extension(), std::move(ruleset_id),
base::BindOnce(&DeclarativeNetRequestGetDisabledRuleIdsFunction::
OnDisabledRuleIdsRead,
this));
return did_respond() ? AlreadyResponded() : RespondLater();
}
void DeclarativeNetRequestGetDisabledRuleIdsFunction::OnDisabledRuleIdsRead(
std::vector<int> disabled_rule_ids) {
Respond(ArgumentList(
dnr_api::GetDisabledRuleIds::Results::Create(disabled_rule_ids)));
}
// static
bool
DeclarativeNetRequestGetMatchedRulesFunction::disable_throttling_for_test_ =
false;
DeclarativeNetRequestGetMatchedRulesFunction::
DeclarativeNetRequestGetMatchedRulesFunction() = default;
DeclarativeNetRequestGetMatchedRulesFunction::
~DeclarativeNetRequestGetMatchedRulesFunction() = default;
ExtensionFunction::ResponseAction
DeclarativeNetRequestGetMatchedRulesFunction::Run() {
using Params = dnr_api::GetMatchedRules::Params;
auto params = Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params.has_value());
std::optional<int> tab_id;
base::Time min_time_stamp = base::Time::Min();
if (params->filter) {
if (params->filter->tab_id) {
tab_id = *params->filter->tab_id;
}
if (params->filter->min_time_stamp) {
min_time_stamp = base::Time::FromMillisecondsSinceUnixEpoch(
*params->filter->min_time_stamp);
}
}
// Return an error if an invalid tab ID is specified. The unknown tab ID is
// valid as it would cause the API call to return all rules matched that were
// not associated with any currently open tabs.
if (tab_id && *tab_id != extension_misc::kUnknownTabId &&
!ExtensionsBrowserClient::Get()->IsValidTabId(browser_context(), *tab_id,
/*include_incognito=*/true,
/*web_contents=*/nullptr)) {
return RespondNow(Error(ErrorUtils::FormatErrorMessage(
declarative_net_request::kTabNotFoundError,
base::NumberToString(*tab_id))));
}
std::string permission_error;
if (!CanCallGetMatchedRules(browser_context(), extension(), tab_id,
&permission_error)) {
return RespondNow(Error(permission_error));
}
declarative_net_request::RulesMonitorService* rules_monitor_service =
declarative_net_request::RulesMonitorService::Get(browser_context());
DCHECK(rules_monitor_service);
declarative_net_request::ActionTracker& action_tracker =
rules_monitor_service->action_tracker();
dnr_api::RulesMatchedDetails details;
details.rules_matched_info =
action_tracker.GetMatchedRules(*extension(), tab_id, min_time_stamp);
return RespondNow(
ArgumentList(dnr_api::GetMatchedRules::Results::Create(details)));
}
void DeclarativeNetRequestGetMatchedRulesFunction::GetQuotaLimitHeuristics(
QuotaLimitHeuristics* heuristics) const {
QuotaLimitHeuristic::Config limit = {
dnr_api::MAX_GETMATCHEDRULES_CALLS_PER_INTERVAL,
base::Minutes(dnr_api::GETMATCHEDRULES_QUOTA_INTERVAL)};
heuristics->push_back(std::make_unique<QuotaService::TimedLimit>(
limit, std::make_unique<QuotaLimitHeuristic::SingletonBucketMapper>(),
"MAX_GETMATCHEDRULES_CALLS_PER_INTERVAL"));
}
bool DeclarativeNetRequestGetMatchedRulesFunction::ShouldSkipQuotaLimiting()
const {
return user_gesture() || disable_throttling_for_test_;
}
DeclarativeNetRequestSetExtensionActionOptionsFunction::
DeclarativeNetRequestSetExtensionActionOptionsFunction() = default;
DeclarativeNetRequestSetExtensionActionOptionsFunction::
~DeclarativeNetRequestSetExtensionActionOptionsFunction() = default;
ExtensionFunction::ResponseAction
DeclarativeNetRequestSetExtensionActionOptionsFunction::Run() {
using Params = dnr_api::SetExtensionActionOptions::Params;
auto params = Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params.has_value());
declarative_net_request::RulesMonitorService* rules_monitor_service =
declarative_net_request::RulesMonitorService::Get(browser_context());
DCHECK(rules_monitor_service);
declarative_net_request::PrefsHelper helper(
*ExtensionPrefs::Get(browser_context()));
declarative_net_request::ActionTracker& action_tracker =
rules_monitor_service->action_tracker();
bool use_action_count_as_badge_text =
helper.GetUseActionCountAsBadgeText(extension_id());
if (params->options.display_action_count_as_badge_text &&
*params->options.display_action_count_as_badge_text !=
use_action_count_as_badge_text) {
use_action_count_as_badge_text =
*params->options.display_action_count_as_badge_text;
helper.SetUseActionCountAsBadgeText(extension_id(),
use_action_count_as_badge_text);
// If the preference is switched on, update the extension's badge text
// with the number of actions matched for this extension. Otherwise, clear
// the action count for the extension's icon and show the default badge
// text if set.
if (use_action_count_as_badge_text) {
action_tracker.OnActionCountAsBadgeTextPreferenceEnabled(extension_id());
} else {
DCHECK(ExtensionsAPIClient::Get());
ExtensionsAPIClient::Get()->ClearActionCount(browser_context(),
*extension());
}
}
if (params->options.tab_update) {
if (!use_action_count_as_badge_text) {
return RespondNow(
Error(declarative_net_request::
kIncrementActionCountWithoutUseAsBadgeTextError));
}
const auto& update_options = *params->options.tab_update;
int tab_id = update_options.tab_id;
if (!ExtensionsBrowserClient::Get()->IsValidTabId(
browser_context(), tab_id, /*include_incognito=*/true,
/*web_contents=*/nullptr)) {
return RespondNow(Error(ErrorUtils::FormatErrorMessage(
declarative_net_request::kTabNotFoundError,
base::NumberToString(tab_id))));
}
action_tracker.IncrementActionCountForTab(extension_id(), tab_id,
update_options.increment);
}
return RespondNow(NoArguments());
}
DeclarativeNetRequestIsRegexSupportedFunction::
DeclarativeNetRequestIsRegexSupportedFunction() = default;
DeclarativeNetRequestIsRegexSupportedFunction::
~DeclarativeNetRequestIsRegexSupportedFunction() = default;
ExtensionFunction::ResponseAction
DeclarativeNetRequestIsRegexSupportedFunction::Run() {
using Params = dnr_api::IsRegexSupported::Params;
auto params = Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params.has_value());
bool is_case_sensitive = params->regex_options.is_case_sensitive
? *params->regex_options.is_case_sensitive
: true;
bool require_capturing = params->regex_options.require_capturing
? *params->regex_options.require_capturing
: false;
re2::RE2 regex(params->regex_options.regex,
declarative_net_request::CreateRE2Options(is_case_sensitive,
require_capturing));
dnr_api::IsRegexSupportedResult result;
if (regex.ok()) {
result.is_supported = true;
} else {
result.is_supported = false;
result.reason = regex.error_code() == re2::RE2::ErrorPatternTooLarge
? dnr_api::UnsupportedRegexReason::kMemoryLimitExceeded
: dnr_api::UnsupportedRegexReason::kSyntaxError;
}
return RespondNow(
ArgumentList(dnr_api::IsRegexSupported::Results::Create(result)));
}
DeclarativeNetRequestGetAvailableStaticRuleCountFunction::
DeclarativeNetRequestGetAvailableStaticRuleCountFunction() = default;
DeclarativeNetRequestGetAvailableStaticRuleCountFunction::
~DeclarativeNetRequestGetAvailableStaticRuleCountFunction() = default;
ExtensionFunction::ResponseAction
DeclarativeNetRequestGetAvailableStaticRuleCountFunction::Run() {
auto* rules_monitor_service =
declarative_net_request::RulesMonitorService::Get(browser_context());
DCHECK(rules_monitor_service);
declarative_net_request::CompositeMatcher* composite_matcher =
rules_monitor_service->ruleset_manager()->GetMatcherForExtension(
extension_id());
// First get the total enabled static rule count for the extension.
size_t enabled_static_rule_count =
GetEnabledStaticRuleCount(composite_matcher);
size_t static_rule_limit =
static_cast<size_t>(declarative_net_request::GetMaximumRulesPerRuleset());
DCHECK_LE(enabled_static_rule_count, static_rule_limit);
const declarative_net_request::GlobalRulesTracker& global_rules_tracker =
rules_monitor_service->global_rules_tracker();
size_t available_allocation =
global_rules_tracker.GetAvailableAllocation(extension_id());
size_t guaranteed_static_minimum =
declarative_net_request::GetStaticGuaranteedMinimumRuleCount();
// If an extension's rule count is below the guaranteed minimum, include the
// difference.
size_t available_static_rule_count = 0;
if (enabled_static_rule_count < guaranteed_static_minimum) {
available_static_rule_count =
(guaranteed_static_minimum - enabled_static_rule_count) +
available_allocation;
} else {
size_t used_global_allocation =
enabled_static_rule_count - guaranteed_static_minimum;
DCHECK_GE(available_allocation, used_global_allocation);
available_static_rule_count = available_allocation - used_global_allocation;
}
// Ensure conversion to int below doesn't underflow.
DCHECK_LE(available_static_rule_count,
static_cast<size_t>(std::numeric_limits<int>::max()));
return RespondNow(
WithArguments(static_cast<int>(available_static_rule_count)));
}
DeclarativeNetRequestTestMatchOutcomeFunction::
DeclarativeNetRequestTestMatchOutcomeFunction() = default;
DeclarativeNetRequestTestMatchOutcomeFunction::
~DeclarativeNetRequestTestMatchOutcomeFunction() = default;
ExtensionFunction::ResponseAction
DeclarativeNetRequestTestMatchOutcomeFunction::Run() {
using Params = dnr_api::TestMatchOutcome::Params;
auto params = Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params.has_value());
// Create a RequestParams for the pretend request.
GURL url = GURL(params->request.url);
if (!url.is_valid()) {
return RespondNow(Error(declarative_net_request::kInvalidTestURLError));
}
url::Origin initiator;
if (params->request.initiator) {
GURL initiator_url = GURL(*params->request.initiator);
if (!initiator_url.is_valid()) {
return RespondNow(
Error(declarative_net_request::kInvalidTestInitiatorError));
}
initiator = url::Origin::Create(std::move(initiator_url));
}
int tab_id = params->request.tab_id ? *params->request.tab_id
: extension_misc::kUnknownTabId;
if (tab_id < extension_misc::kUnknownTabId) {
return RespondNow(Error(declarative_net_request::kInvalidTestTabIdError));
}
auto method = params->request.method == dnr_api::RequestMethod::kNone
? dnr_api::RequestMethod::kGet
: params->request.method;
// If enabled, parse response headers from function args into
// `response_headers`.
// Note: If headers are not specified in function args, then
// `response_headers` is set to an empty header object.
scoped_refptr<const net::HttpResponseHeaders> response_headers = nullptr;
if (base::FeatureList::IsEnabled(
extensions_features::kDeclarativeNetRequestResponseHeaderMatching)) {
std::string parse_header_error;
response_headers =
ParseHeaders(params->request.response_headers, parse_header_error);
if (!parse_header_error.empty()) {
return RespondNow(Error(parse_header_error));
}
DCHECK(response_headers);
}
declarative_net_request::RequestParams request_params(
url, initiator, params->request.type, method, tab_id, response_headers);
// Set up the rule matcher.
auto* rules_monitor_service =
declarative_net_request::RulesMonitorService::Get(browser_context());
DCHECK(rules_monitor_service);
DCHECK(extension());
declarative_net_request::CompositeMatcher* matcher =
rules_monitor_service->ruleset_manager()->GetMatcherForExtension(
extension_id());
if (!matcher) {
return RespondNow(ArgumentList(dnr_api::TestMatchOutcome::Results::Create(
dnr_api::TestMatchOutcomeResult())));
}
// Determine if the extension has permission to redirect the request.
auto web_request_resource_type =
declarative_net_request::GetWebRequestResourceType(params->request.type);
PermissionsData::PageAccess page_access =
WebRequestPermissions::CanExtensionAccessURL(
PermissionHelper::Get(browser_context()), extension_id(), url, tab_id,
/*crosses_incognito=*/false,
WebRequestPermissions::HostPermissionsCheck::
REQUIRE_HOST_PERMISSION_FOR_URL_AND_INITIATOR,
initiator, web_request_resource_type);
// First, match against rules which operate on the request before it would be
// sent.
std::vector<declarative_net_request::RequestAction> before_request_actions =
GetActions(
*matcher, request_params,
declarative_net_request::RulesetMatchingStage::kOnBeforeRequest,
page_access);
// Stop here if response headers should not be matched or the request is
// intercepted before it is sent.
if (!response_headers || IsRequestIntercepted(before_request_actions)) {
return RespondNow(
ArgumentList(CreateMatchedRulesFromActions(before_request_actions)));
}
DCHECK(response_headers);
// Match against rules which match with response headers.
std::vector<declarative_net_request::RequestAction> headers_received_actions =
GetActions(
*matcher, request_params,
declarative_net_request::RulesetMatchingStage::kOnHeadersReceived,
page_access);
// Same as in production, erase any actions from `before_request_actions` with
// lower priority than the max allow priority from `headers_received_actions`.
// Note that `request_params.max_priority_allow_action` means that reusing
// `request_params` when calling `GetActions` for kOnHeadersReceived will only
// return actions with a higher priority than the max allow priority from
// kOnBeforeRequest.
uint64_t headers_received_allow_priority =
GetAllowActionPriority(headers_received_actions);
std::erase_if(before_request_actions,
[headers_received_allow_priority](
const declarative_net_request::RequestAction& action) {
return action.index_priority <
headers_received_allow_priority;
});
// At this point, the set of matching request actions for the test request
// would be {before_request_actions, headers_received_actions}. If one list of
// actions is empty (which means no actions were applied onto the request from
// that request phase), return the other.
if (before_request_actions.empty() ||
IsRequestIntercepted(headers_received_actions)) {
return RespondNow(
ArgumentList(CreateMatchedRulesFromActions(headers_received_actions)));
}
if (headers_received_actions.empty()) {
return RespondNow(
ArgumentList(CreateMatchedRulesFromActions(before_request_actions)));
}
// At this point, if both lists are non-empty, then either:
// - both lists contain the same max priority allow action matched in
// OnBeforeRequest. Return either list.
// - one list contains an allow action, and the other contains modify header
// actions. In this case, return the list with modify header actions. This
// mimics the logic in CompositeMatcher::GetModifyHeadersActions where only
// modify header actions above the max allow rule's priority are returned.
// - both lists contain modify header actions. In this case, merge them and
// sort in descending order of priority. The merging is done since each
// action is relevant to the request and shouldn't be ignored, and the
// sorting reflects CompositeMatcher::GetModifyHeadersActions which returns
// a list of actions in sorted order.
DCHECK(!before_request_actions.empty());
DCHECK(!headers_received_actions.empty());
if (before_request_actions[0].IsAllowOrAllowAllRequests()) {
return RespondNow(
ArgumentList(CreateMatchedRulesFromActions(headers_received_actions)));
}
if (headers_received_actions[0].IsAllowOrAllowAllRequests()) {
return RespondNow(
ArgumentList(CreateMatchedRulesFromActions(before_request_actions)));
}
std::vector<declarative_net_request::RequestAction> merged_actions;
merged_actions.reserve(before_request_actions.size() +
headers_received_actions.size());
merged_actions.insert(merged_actions.end(),
std::make_move_iterator(before_request_actions.begin()),
std::make_move_iterator(before_request_actions.end()));
merged_actions.insert(
merged_actions.end(),
std::make_move_iterator(headers_received_actions.begin()),
std::make_move_iterator(headers_received_actions.end()));
std::sort(merged_actions.begin(), merged_actions.end(), std::greater<>());
return RespondNow(
ArgumentList(CreateMatchedRulesFromActions(merged_actions)));
}
scoped_refptr<const net::HttpResponseHeaders>
DeclarativeNetRequestTestMatchOutcomeFunction::ParseHeaders(
std::optional<TestResponseHeaders>& test_headers,
std::string& error) const {
net::HttpResponseHeaders::Builder builder(net::HttpVersion(1, 1), "200");
if (test_headers.has_value()) {
for (const auto [header, values] : test_headers->additional_properties) {
if (!net::HttpUtil::IsValidHeaderName(header)) {
error = ErrorUtils::FormatErrorMessage(
declarative_net_request::kInvalidResponseHeaderNameError, header);
return nullptr;
}
// Header values must be specified as a list.
if (!values.is_list()) {
error = ErrorUtils::FormatErrorMessage(
declarative_net_request::kInvalidResponseHeaderObjectError, header);
return nullptr;
}
// Assume an empty string as a header value if an empty list is specified.
if (values.GetList().empty()) {
builder.AddHeader(header, "");
continue;
}
for (const auto& value : values.GetList()) {
// Check that header values are valid strings.
if (!value.is_string() ||
!net::HttpUtil::IsValidHeaderName(value.GetString())) {
error = ErrorUtils::FormatErrorMessage(
declarative_net_request::kInvalidResponseHeaderValueError,
header);
return nullptr;
}
builder.AddHeader(header, value.GetString());
}
}
}
return builder.Build();
}
base::Value::List
DeclarativeNetRequestTestMatchOutcomeFunction::CreateMatchedRulesFromActions(
const std::vector<declarative_net_request::RequestAction>& actions) const {
dnr_api::TestMatchOutcomeResult result;
std::vector<dnr_api::MatchedRule> matched_rules;
for (auto& action : actions) {
dnr_api::MatchedRule match;
match.rule_id = action.rule_id;
match.ruleset_id = GetPublicRulesetID(*extension(), action.ruleset_id);
matched_rules.push_back(std::move(match));
}
result.matched_rules = std::move(matched_rules);
return dnr_api::TestMatchOutcome::Results::Create(result);
}
std::vector<declarative_net_request::RequestAction>
DeclarativeNetRequestTestMatchOutcomeFunction::GetActions(
const declarative_net_request::CompositeMatcher& matcher,
const declarative_net_request::RequestParams& params,
declarative_net_request::RulesetMatchingStage stage,
PermissionsData::PageAccess page_access) const {
// TODO(crbug.com/343503170): The logic here is very similar to that of
// `RulesetManager::EvaluateRequestInternal` except this is for a single
// extension. One way to DRY this up is to put this logic in utils, and pass
// in function objects for matching a singular action and matching modify
// headers actions.
using RequestAction = declarative_net_request::RequestAction;
std::vector<RequestAction> actions;
std::optional<RequestAction> action =
matcher.GetAction(params, stage, page_access).action;
if (action) {
bool is_request_modifying_action = !action->IsAllowOrAllowAllRequests();
actions.push_back(std::move(*action));
if (is_request_modifying_action) {
return actions;
}
}
std::vector<RequestAction> modify_headers_actions =
matcher.GetModifyHeadersActions(params, stage);
if (!modify_headers_actions.empty()) {
return modify_headers_actions;
}
return actions;
}
} // namespace extensions
|