#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "extensions/browser/api/declarative_net_request/indexed_rule.h"
#include <utility>
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/types/optional_util.h"
#include "components/url_pattern_index/url_pattern_index.h"
#include "extensions/browser/api/declarative_net_request/constants.h"
#include "extensions/browser/api/declarative_net_request/utils.h"
#include "extensions/common/api/declarative_net_request.h"
#include "extensions/common/extension_features.h"
#include "net/http/http_util.h"
#include "third_party/re2/src/re2/re2.h"
#include "url/gurl.h"
#include "url/url_constants.h"
namespace extensions::declarative_net_request {
namespace {
flat_rule;
dnr_api;
constexpr char kAnchorCharacter = …;
constexpr char kSeparatorCharacter = …;
constexpr char kWildcardCharacter = …;
constexpr int kLargeRegexUMALimit = …;
constexpr bool IsSubset(unsigned sub, unsigned super) { … }
class UrlFilterParser { … };
bool IsCaseSensitive(const dnr_api::Rule& parsed_rule) { … }
uint8_t GetOptionsMask(const dnr_api::Rule& parsed_rule) { … }
uint8_t GetActivationTypes(const dnr_api::Rule& parsed_rule) { … }
uint16_t GetRequestMethodsMask(
const std::optional<std::vector<dnr_api::RequestMethod>>& request_methods) { … }
ParseResult ComputeRequestMethods(const dnr_api::Rule& rule,
uint16_t* request_methods_mask) { … }
uint16_t GetResourceTypesMask(
const std::optional<std::vector<dnr_api::ResourceType>>& resource_types) { … }
ParseResult ComputeElementTypes(const dnr_api::Rule& rule,
uint16_t* element_types) { … }
bool CanonicalizeDomains(std::optional<std::vector<std::string>> domains,
std::vector<std::string>* output) { … }
bool IsRedirectUrlRelative(const std::string& redirect_url) { … }
bool IsValidTransformScheme(const std::optional<std::string>& scheme) { … }
bool IsValidPort(const std::optional<std::string>& port) { … }
bool IsEmptyOrStartsWith(const std::optional<std::string>& str,
char starts_with) { … }
ParseResult ValidateTransform(const dnr_api::URLTransform& transform) { … }
ParseResult ParseRedirect(dnr_api::Redirect redirect,
const GURL& base_url,
IndexedRule* indexed_rule) { … }
uint8_t GetActionTypePriority(dnr_api::RuleActionType action_type) { … }
void RecordLargeRegexUMA(bool is_large_regex) { … }
void RecordRegexRuleSizeUMA(int program_size) { … }
void RecordRuleSizeForLargeRegex(const std::string& regex_string,
bool is_case_sensitive,
bool require_capturing) { … }
ParseResult ValidateRegex(
bool is_case_sensitive,
const std::optional<std::string>& regex_filter,
const std::optional<std::string>& regex_substitution) { … }
ParseResult ValidateHeadersForModification(
const std::vector<dnr_api::ModifyHeaderInfo>& headers,
bool are_request_headers) { … }
void ParseTabIds(const std::vector<int>* input_tab_ids,
base::flat_set<int>& output_tab_ids) { … }
ParseResult ValidateMatchingResponseHeaderValues(
const dnr_api::HeaderInfo& header_info) { … }
ParseResult ValidateResponseHeadersForMatching(
const std::vector<dnr_api::HeaderInfo>& response_headers,
const std::vector<dnr_api::HeaderInfo>& excluded_response_headers) { … }
}
IndexedRule::IndexedRule() = default;
IndexedRule::~IndexedRule() = default;
IndexedRule::IndexedRule(IndexedRule&& other) = default;
IndexedRule& IndexedRule::operator=(IndexedRule&& other) = default;
ParseResult IndexedRule::CreateIndexedRule(dnr_api::Rule parsed_rule,
const GURL& base_url,
RulesetID ruleset_id,
IndexedRule* indexed_rule) { … }
uint64_t ComputeIndexedRulePriority(int parsed_rule_priority,
dnr_api::RuleActionType action_type) { … }
}