Commit abf8d983 authored by Kelvin Jiang's avatar Kelvin Jiang Committed by Commit Bot

[DNR] Add flatbuffer schemas and indexing for modifyHeader rules

Bug: 947591
Change-Id: I06b2ff6505ed0f1efd326df2a73cac8ee3ec35bf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2081363
Commit-Queue: Kelvin Jiang <kelvinjiang@chromium.org>
Reviewed-by: default avatarKaran Bhatia <karandeepb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#751592}
parent 05edc003
...@@ -59,7 +59,9 @@ bool IsExtraHeadersMatcherInternal( ...@@ -59,7 +59,9 @@ bool IsExtraHeadersMatcherInternal(
const ExtensionUrlPatternIndexMatcher::UrlPatternIndexList* index_list) { const ExtensionUrlPatternIndexMatcher::UrlPatternIndexList* index_list) {
// We only support removing a subset of extra headers currently. If that // We only support removing a subset of extra headers currently. If that
// changes, the implementation here should change as well. // changes, the implementation here should change as well.
static_assert(flat::IndexType_count == 5, // TODO(crbug.com/947591): Modify this method for
// flat::IndexType_modify_headers.
static_assert(flat::IndexType_count == 6,
"Modify this method to ensure IsExtraHeadersMatcherInternal is " "Modify this method to ensure IsExtraHeadersMatcherInternal is "
"updated as new actions are added."); "updated as new actions are added.");
static const flat::IndexType extra_header_indices[] = { static const flat::IndexType extra_header_indices[] = {
...@@ -189,6 +191,7 @@ ExtensionUrlPatternIndexMatcher::GetBeforeRequestActionHelper( ...@@ -189,6 +191,7 @@ ExtensionUrlPatternIndexMatcher::GetBeforeRequestActionHelper(
return CreateUpgradeAction(params, *rule); return CreateUpgradeAction(params, *rule);
case flat::ActionType_allow_all_requests: case flat::ActionType_allow_all_requests:
case flat::ActionType_remove_headers: case flat::ActionType_remove_headers:
case flat::ActionType_modify_headers:
case flat::ActionType_count: case flat::ActionType_count:
NOTREACHED(); NOTREACHED();
} }
......
...@@ -18,6 +18,7 @@ enum ActionType : ubyte { ...@@ -18,6 +18,7 @@ enum ActionType : ubyte {
redirect, redirect,
upgrade_scheme, upgrade_scheme,
remove_headers, remove_headers,
modify_headers,
allow_all_requests, allow_all_requests,
/// Number of actions. Must be the last entry. /// Number of actions. Must be the last entry.
count count
...@@ -74,6 +75,11 @@ table UrlRuleMetadata { ...@@ -74,6 +75,11 @@ table UrlRuleMetadata {
/// UrlTransform for this rule. /// UrlTransform for this rule.
transform : UrlTransform; transform : UrlTransform;
/// A list of ModifyHeaderInfo, for both request and response headers. Valid
/// for "modifyHeaders" rules.
request_headers: [ModifyHeaderInfo];
response_headers: [ModifyHeaderInfo];
} }
/// This provides a mapping from an index type to its index within /// This provides a mapping from an index type to its index within
...@@ -91,6 +97,8 @@ enum IndexType : ubyte { ...@@ -91,6 +97,8 @@ enum IndexType : ubyte {
remove_referer_header, remove_referer_header,
remove_set_cookie_header, remove_set_cookie_header,
modify_headers,
/// Number of indices. Must be the last entry. /// Number of indices. Must be the last entry.
count count
} }
...@@ -103,6 +111,19 @@ enum RemoveHeaderType : ubyte (bit_flags) { ...@@ -103,6 +111,19 @@ enum RemoveHeaderType : ubyte (bit_flags) {
set_cookie set_cookie
} }
/// The type of header operation for modifyHeaders rules. Corresponds to
/// extensions::api::declarative_net_request::HeaderOperation.
enum HeaderOperation : ubyte {
remove
}
/// Describes the header to be modified and operation to be performed on it.
/// Corresponds to extensions::api::declarative_net_request::ModifyHeaderInfo.
table ModifyHeaderInfo {
operation: HeaderOperation;
header: string;
}
/// Completely represents a rule with a regex filter. /// Completely represents a rule with a regex filter.
table RegexRule { table RegexRule {
/// The underlying UrlRule. /// The underlying UrlRule.
......
...@@ -152,6 +152,33 @@ FlatOffset<flat::UrlTransform> BuildTransformOffset( ...@@ -152,6 +152,33 @@ FlatOffset<flat::UrlTransform> BuildTransformOffset(
clear_fragment, fragment, username, password); clear_fragment, fragment, username, password);
} }
FlatVectorOffset<flat::ModifyHeaderInfo> BuildModifyHeaderInfoOffset(
flatbuffers::FlatBufferBuilder* builder,
const std::vector<dnr_api::ModifyHeaderInfo>& modify_header_list) {
std::vector<FlatOffset<flat::ModifyHeaderInfo>> flat_modify_header_list;
flat_modify_header_list.reserve(modify_header_list.size());
for (const dnr_api::ModifyHeaderInfo& header_info : modify_header_list) {
flat::HeaderOperation operation = flat::HeaderOperation_remove;
switch (header_info.operation) {
case dnr_api::HeaderOperation::HEADER_OPERATION_NONE:
NOTREACHED();
break;
case dnr_api::HEADER_OPERATION_REMOVE:
operation = flat::HeaderOperation_remove;
break;
}
FlatStringOffset header_name =
builder->CreateSharedString(header_info.header);
flat_modify_header_list.push_back(
flat::CreateModifyHeaderInfo(*builder, operation, header_name));
}
return builder->CreateVector(flat_modify_header_list);
}
} // namespace } // namespace
FlatRulesetIndexer::FlatRulesetIndexer() FlatRulesetIndexer::FlatRulesetIndexer()
...@@ -206,10 +233,17 @@ void FlatRulesetIndexer::AddUrlRule(const IndexedRule& indexed_rule) { ...@@ -206,10 +233,17 @@ void FlatRulesetIndexer::AddUrlRule(const IndexedRule& indexed_rule) {
transform_offset = transform_offset =
BuildTransformOffset(&builder_, *indexed_rule.url_transform); BuildTransformOffset(&builder_, *indexed_rule.url_transform);
} }
FlatVectorOffset<flat::ModifyHeaderInfo> request_headers_offset =
BuildModifyHeaderInfoOffset(&builder_, indexed_rule.request_headers);
FlatVectorOffset<flat::ModifyHeaderInfo> response_headers_offset =
BuildModifyHeaderInfoOffset(&builder_, indexed_rule.response_headers);
metadata_.push_back(flat::CreateUrlRuleMetadata( metadata_.push_back(flat::CreateUrlRuleMetadata(
builder_, indexed_rule.id, builder_, indexed_rule.id,
ConvertToFlatActionType(indexed_rule.action_type), redirect_url_offset, ConvertToFlatActionType(indexed_rule.action_type), redirect_url_offset,
transform_offset)); transform_offset, request_headers_offset, response_headers_offset));
} }
void FlatRulesetIndexer::Finish() { void FlatRulesetIndexer::Finish() {
...@@ -281,6 +315,7 @@ FlatRulesetIndexer::GetBuilders(const IndexedRule& indexed_rule) { ...@@ -281,6 +315,7 @@ FlatRulesetIndexer::GetBuilders(const IndexedRule& indexed_rule) {
case dnr_api::RULE_ACTION_TYPE_REMOVEHEADERS: case dnr_api::RULE_ACTION_TYPE_REMOVEHEADERS:
return GetRemoveHeaderBuilders(indexed_rule.remove_headers_set); return GetRemoveHeaderBuilders(indexed_rule.remove_headers_set);
case dnr_api::RULE_ACTION_TYPE_MODIFYHEADERS: case dnr_api::RULE_ACTION_TYPE_MODIFYHEADERS:
return {index_builders_[flat::IndexType_modify_headers].get()};
case dnr_api::RULE_ACTION_TYPE_NONE: case dnr_api::RULE_ACTION_TYPE_NONE:
break; break;
} }
......
...@@ -26,6 +26,7 @@ enum ActionType : ubyte { ...@@ -26,6 +26,7 @@ enum ActionType : ubyte {
redirect, redirect,
upgrade_scheme, upgrade_scheme,
remove_headers, remove_headers,
modify_headers,
allow_all_requests, allow_all_requests,
count count
} }
...@@ -54,6 +55,8 @@ table UrlRuleMetadata { ...@@ -54,6 +55,8 @@ table UrlRuleMetadata {
action : ActionType; action : ActionType;
redirect_url : string; redirect_url : string;
transform : UrlTransform; transform : UrlTransform;
request_headers: [ModifyHeaderInfo];
response_headers: [ModifyHeaderInfo];
} }
enum IndexType : ubyte { enum IndexType : ubyte {
before_request_except_allow_all_requests = 0, before_request_except_allow_all_requests = 0,
...@@ -61,6 +64,7 @@ enum IndexType : ubyte { ...@@ -61,6 +64,7 @@ enum IndexType : ubyte {
remove_cookie_header, remove_cookie_header,
remove_referer_header, remove_referer_header,
remove_set_cookie_header, remove_set_cookie_header,
modify_headers,
count count
} }
enum RemoveHeaderType : ubyte (bit_flags) { enum RemoveHeaderType : ubyte (bit_flags) {
...@@ -68,6 +72,13 @@ enum RemoveHeaderType : ubyte (bit_flags) { ...@@ -68,6 +72,13 @@ enum RemoveHeaderType : ubyte (bit_flags) {
referer, referer,
set_cookie set_cookie
} }
enum HeaderOperation : ubyte {
remove
}
table ModifyHeaderInfo {
operation: HeaderOperation;
header: string;
}
table RegexRule { table RegexRule {
url_rule: url_pattern_index.flat.UrlRule; url_rule: url_pattern_index.flat.UrlRule;
action_type: ActionType; action_type: ActionType;
...@@ -145,7 +156,7 @@ TEST_F(IndexedRulesetFormatVersionTest, CheckVersionUpdated) { ...@@ -145,7 +156,7 @@ TEST_F(IndexedRulesetFormatVersionTest, CheckVersionUpdated) {
EXPECT_EQ(StripCommentsAndWhitespace(kFlatbufferSchemaExpected), EXPECT_EQ(StripCommentsAndWhitespace(kFlatbufferSchemaExpected),
StripCommentsAndWhitespace(flatbuffer_schema)) StripCommentsAndWhitespace(flatbuffer_schema))
<< "Schema change detected; update this test and the schema version."; << "Schema change detected; update this test and the schema version.";
EXPECT_EQ(15, GetIndexedRulesetFormatVersionForTesting()) EXPECT_EQ(16, GetIndexedRulesetFormatVersionForTesting())
<< "Update this test if you update the schema version."; << "Update this test if you update the schema version.";
} }
......
...@@ -25,7 +25,9 @@ bool IsExtraHeadersMatcherInternal( ...@@ -25,7 +25,9 @@ bool IsExtraHeadersMatcherInternal(
// We only support removing a subset of extra headers currently. If that // We only support removing a subset of extra headers currently. If that
// changes, the implementation here should change as well. // changes, the implementation here should change as well.
static_assert(flat::ActionType_count == 6, // TODO(crbug.com/947591): Modify this method for
// flat::ActionType_modify_headers.
static_assert(flat::ActionType_count == 7,
"Modify this method to ensure IsExtraHeadersMatcherInternal is " "Modify this method to ensure IsExtraHeadersMatcherInternal is "
"updated as new actions are added."); "updated as new actions are added.");
...@@ -64,6 +66,7 @@ bool IsBeforeRequestAction(flat::ActionType action_type) { ...@@ -64,6 +66,7 @@ bool IsBeforeRequestAction(flat::ActionType action_type) {
case flat::ActionType_allow_all_requests: case flat::ActionType_allow_all_requests:
return true; return true;
case flat::ActionType_remove_headers: case flat::ActionType_remove_headers:
case flat::ActionType_modify_headers:
return false; return false;
case flat::ActionType_count: case flat::ActionType_count:
NOTREACHED(); NOTREACHED();
...@@ -185,6 +188,7 @@ RegexRulesMatcher::GetBeforeRequestActionIgnoringAncestors( ...@@ -185,6 +188,7 @@ RegexRulesMatcher::GetBeforeRequestActionIgnoringAncestors(
case flat::ActionType_allow_all_requests: case flat::ActionType_allow_all_requests:
return CreateAllowAllRequestsAction(params, rule); return CreateAllowAllRequestsAction(params, rule);
case flat::ActionType_remove_headers: case flat::ActionType_remove_headers:
case flat::ActionType_modify_headers:
case flat::ActionType_count: case flat::ActionType_count:
NOTREACHED(); NOTREACHED();
break; break;
......
...@@ -180,7 +180,9 @@ bool RulesetManager::HasExtraHeadersMatcherForRequest( ...@@ -180,7 +180,9 @@ bool RulesetManager::HasExtraHeadersMatcherForRequest(
// We only support removing a subset of extra headers currently. If that // We only support removing a subset of extra headers currently. If that
// changes, the implementation here should change as well. // changes, the implementation here should change as well.
static_assert(flat::ActionType_count == 6, // TODO(crbug.com/947591): Modify this method for
// flat::ActionType_modify_headers.
static_assert(flat::ActionType_count == 7,
"Modify this method to ensure HasExtraHeadersMatcherForRequest " "Modify this method to ensure HasExtraHeadersMatcherForRequest "
"is updated as new actions are added."); "is updated as new actions are added.");
......
...@@ -57,7 +57,9 @@ RequestAction CreateRequestActionForTesting(RequestAction::Type type, ...@@ -57,7 +57,9 @@ RequestAction CreateRequestActionForTesting(RequestAction::Type type,
// with gtest. This reuses the logic used to test action equality in // with gtest. This reuses the logic used to test action equality in
// TestRequestACtion in test_utils.h. // TestRequestACtion in test_utils.h.
bool operator==(const RequestAction& lhs, const RequestAction& rhs) { bool operator==(const RequestAction& lhs, const RequestAction& rhs) {
static_assert(flat::IndexType_count == 5, // TODO(crbug.com/947591): Modify this method for
// flat::IndexType_modify_headers.
static_assert(flat::IndexType_count == 6,
"Modify this method to ensure it stays updated as new actions " "Modify this method to ensure it stays updated as new actions "
"are added."); "are added.");
......
...@@ -37,7 +37,7 @@ namespace dnr_api = api::declarative_net_request; ...@@ -37,7 +37,7 @@ namespace dnr_api = api::declarative_net_request;
// url_pattern_index.fbs. Whenever an extension with an indexed ruleset format // url_pattern_index.fbs. Whenever an extension with an indexed ruleset format
// version different from the one currently used by Chrome is loaded, the // version different from the one currently used by Chrome is loaded, the
// extension ruleset will be reindexed. // extension ruleset will be reindexed.
constexpr int kIndexedRulesetFormatVersion = 15; constexpr int kIndexedRulesetFormatVersion = 16;
// This static assert is meant to catch cases where // This static assert is meant to catch cases where
// url_pattern_index::kUrlPatternIndexFormatVersion is incremented without // url_pattern_index::kUrlPatternIndexFormatVersion is incremented without
...@@ -256,11 +256,12 @@ flat::ActionType ConvertToFlatActionType(dnr_api::RuleActionType action_type) { ...@@ -256,11 +256,12 @@ flat::ActionType ConvertToFlatActionType(dnr_api::RuleActionType action_type) {
return flat::ActionType_redirect; return flat::ActionType_redirect;
case dnr_api::RULE_ACTION_TYPE_REMOVEHEADERS: case dnr_api::RULE_ACTION_TYPE_REMOVEHEADERS:
return flat::ActionType_remove_headers; return flat::ActionType_remove_headers;
case dnr_api::RULE_ACTION_TYPE_MODIFYHEADERS:
return flat::ActionType_modify_headers;
case dnr_api::RULE_ACTION_TYPE_UPGRADESCHEME: case dnr_api::RULE_ACTION_TYPE_UPGRADESCHEME:
return flat::ActionType_upgrade_scheme; return flat::ActionType_upgrade_scheme;
case dnr_api::RULE_ACTION_TYPE_ALLOWALLREQUESTS: case dnr_api::RULE_ACTION_TYPE_ALLOWALLREQUESTS:
return flat::ActionType_allow_all_requests; return flat::ActionType_allow_all_requests;
case dnr_api::RULE_ACTION_TYPE_MODIFYHEADERS:
case dnr_api::RULE_ACTION_TYPE_NONE: case dnr_api::RULE_ACTION_TYPE_NONE:
break; break;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment