diff --git a/third_party/blink/renderer/core/feature_policy/feature_policy_parser.cc b/third_party/blink/renderer/core/feature_policy/feature_policy_parser.cc index c445e237a7bfe9..5712bb5dac846e 100644 --- a/third_party/blink/renderer/core/feature_policy/feature_policy_parser.cc +++ b/third_party/blink/renderer/core/feature_policy/feature_policy_parser.cc @@ -8,7 +8,6 @@ #include #include "base/metrics/histogram_macros.h" -#include "net/http/structured_headers.h" #include "third_party/blink/public/mojom/feature_policy/feature_policy.mojom-blink.h" #include "third_party/blink/renderer/core/execution_context/execution_context.h" #include "third_party/blink/renderer/core/frame/web_feature.h" @@ -51,11 +50,12 @@ class ParsingContext { ~ParsingContext() = default; - ParsedFeaturePolicy ParseIR(const internal::FeaturePolicyNode& root); - internal::FeaturePolicyNode ParseFeaturePolicyToIR(const String& policy); - internal::FeaturePolicyNode ParsePermissionsPolicyToIR(const String& policy); + ParsedFeaturePolicy Parse(const String& policy); private: + ParsedFeaturePolicy ParseIR(const internal::FeaturePolicyNode& root); + internal::FeaturePolicyNode ParseToIR(const String& policy); + // normally 1 char = 1 byte // max length to parse = 2^16 = 64 kB static constexpr wtf_size_t MAX_LENGTH_PARSE = 1 << 16; @@ -329,6 +329,10 @@ base::Optional ParsingContext::ParseFeature( return parsed_feature; } +ParsedFeaturePolicy ParsingContext::Parse(const String& policy) { + return ParseIR(ParseToIR(policy)); +} + ParsedFeaturePolicy ParsingContext::ParseIR( const internal::FeaturePolicyNode& root) { ParsedFeaturePolicy parsed_policy; @@ -344,8 +348,7 @@ ParsedFeaturePolicy ParsingContext::ParseIR( return parsed_policy; } -internal::FeaturePolicyNode ParsingContext::ParseFeaturePolicyToIR( - const String& policy) { +internal::FeaturePolicyNode ParsingContext::ParseToIR(const String& policy) { internal::FeaturePolicyNode root; if (policy.length() > MAX_LENGTH_PARSE) { @@ -401,122 +404,38 @@ internal::FeaturePolicyNode ParsingContext::ParseFeaturePolicyToIR( return root; } -internal::FeaturePolicyNode ParsingContext::ParsePermissionsPolicyToIR( - const String& policy) { - auto root = net::structured_headers::ParseDictionary(policy.Utf8()); - if (!root) { - logger_.Error( - "Parse of permission policy failed because of errors reported by " - "strctured header parser."); - return {}; - } - - internal::FeaturePolicyNode ir_root; - for (const auto& feature_entry : root.value()) { - const auto& key = feature_entry.first; - const char* feature_name = key.c_str(); - const auto& value = feature_entry.second; - - if (!value.params.empty()) { - logger_.Warn( - String::Format("Feature %s's parameters are ignored.", feature_name)); - } - - Vector allowlist; - for (const auto& parameterized_item : value.member) { - if (!parameterized_item.params.empty()) { - logger_.Warn(String::Format("Feature %s's parameters are ignored.", - feature_name)); - } - - String allowlist_item; - if (parameterized_item.item.is_token()) { - // All special keyword appears as token, i.e. self, src and *. - const std::string& token_value = parameterized_item.item.GetString(); - if (token_value != "*" && token_value != "self") { - logger_.Warn(String::Format( - "Invalid allowlist item(%s) for feature %s. Allowlist item " - "must be *, self or quoted url.", - token_value.c_str(), feature_name)); - continue; - } - - if (token_value == "*") { - allowlist_item = "*"; - } else { - allowlist_item = String::Format("'%s'", token_value.c_str()); - } - } else if (parameterized_item.item.is_string()) { - allowlist_item = parameterized_item.item.GetString().c_str(); - } else { - logger_.Warn( - String::Format("Invalid allowlist item for feature %s. Allowlist " - "item must be *, self, or quoted url.", - feature_name)); - continue; - } - allowlist.push_back(allowlist_item); - } - - if (allowlist.IsEmpty()) - allowlist.push_back("'none'"); - - ir_root.push_back( - internal::FeaturePolicyDeclarationNode{feature_name, allowlist}); - } - - return ir_root; -} - } // namespace ParsedFeaturePolicy FeaturePolicyParser::ParseHeader( - const String& feature_policy_header, - scoped_refptr origin, - PolicyParserMessageBuffer& logger, - FeaturePolicyParserDelegate* delegate, - const String& permissions_policy_header) { - ParsingContext context(logger, origin, nullptr, GetDefaultFeatureNameMap(), - delegate); - auto policy_ir = - context.ParsePermissionsPolicyToIR(permissions_policy_header); - policy_ir.AppendVector(context.ParseFeaturePolicyToIR(feature_policy_header)); - return context.ParseIR(policy_ir); -} - -ParsedFeaturePolicy FeaturePolicyParser::ParseAttribute( const String& policy, - scoped_refptr self_origin, - scoped_refptr src_origin, + scoped_refptr origin, PolicyParserMessageBuffer& logger, FeaturePolicyParserDelegate* delegate) { - ParsingContext context(logger, self_origin, src_origin, - GetDefaultFeatureNameMap(), delegate); - return context.ParseIR(context.ParseFeaturePolicyToIR(policy)); + return Parse(policy, origin, nullptr, logger, GetDefaultFeatureNameMap(), + delegate); } -ParsedFeaturePolicy FeaturePolicyParser::ParseFeaturePolicyForTest( +ParsedFeaturePolicy FeaturePolicyParser::ParseAttribute( const String& policy, scoped_refptr self_origin, scoped_refptr src_origin, PolicyParserMessageBuffer& logger, - const FeatureNameMap& feature_names, FeaturePolicyParserDelegate* delegate) { - ParsingContext context(logger, self_origin, src_origin, feature_names, - delegate); - return context.ParseIR(context.ParseFeaturePolicyToIR(policy)); + return Parse(policy, self_origin, src_origin, logger, + GetDefaultFeatureNameMap(), delegate); } -ParsedFeaturePolicy FeaturePolicyParser::ParsePermissionsPolicyForTest( +// static +ParsedFeaturePolicy FeaturePolicyParser::Parse( const String& policy, scoped_refptr self_origin, scoped_refptr src_origin, PolicyParserMessageBuffer& logger, const FeatureNameMap& feature_names, FeaturePolicyParserDelegate* delegate) { - ParsingContext context(logger, self_origin, src_origin, feature_names, - delegate); - return context.ParseIR(context.ParsePermissionsPolicyToIR(policy)); + return ParsingContext(logger, self_origin, src_origin, feature_names, + delegate) + .Parse(policy); } bool IsFeatureDeclared(mojom::blink::FeaturePolicyFeature feature, diff --git a/third_party/blink/renderer/core/feature_policy/feature_policy_parser.h b/third_party/blink/renderer/core/feature_policy/feature_policy_parser.h index 0d5016f3546ff3..5c7bcf73ec3153 100644 --- a/third_party/blink/renderer/core/feature_policy/feature_policy_parser.h +++ b/third_party/blink/renderer/core/feature_policy/feature_policy_parser.h @@ -58,11 +58,10 @@ class CORE_EXPORT FeaturePolicyParser { // parsing. Example of a feature policy string: // "vibrate a.com b.com; fullscreen 'none'; payment 'self', payment *". static ParsedFeaturePolicy ParseHeader( - const String& feature_policy_header, + const String& policy, scoped_refptr, PolicyParserMessageBuffer& logger, - FeaturePolicyParserDelegate* delegate = nullptr, - const String& permission_policy_header = g_empty_string); + FeaturePolicyParserDelegate* delegate = nullptr); // Converts a container policy string into a vector of allowlists, given self // and src origins provided, one for each feature specified. Unrecognized @@ -76,15 +75,13 @@ class CORE_EXPORT FeaturePolicyParser { PolicyParserMessageBuffer& logger, FeaturePolicyParserDelegate* delegate = nullptr); - static ParsedFeaturePolicy ParseFeaturePolicyForTest( - const String& policy, - scoped_refptr self_origin, - scoped_refptr src_origin, - PolicyParserMessageBuffer& logger, - const FeatureNameMap& feature_names, - FeaturePolicyParserDelegate* delegate = nullptr); - - static ParsedFeaturePolicy ParsePermissionsPolicyForTest( + // Converts a feature policy string into a vector of allowlists (see comments + // above), with an explicit FeatureNameMap. This algorithm is called by both + // header policy parsing and container policy parsing. |self_origin|, + // |src_origin|, and |execution_context| are nullable. The optional + // ExecutionContext is used to determine if any origin trials affect the + // parsing. + static ParsedFeaturePolicy Parse( const String& policy, scoped_refptr self_origin, scoped_refptr src_origin, @@ -92,7 +89,6 @@ class CORE_EXPORT FeaturePolicyParser { const FeatureNameMap& feature_names, FeaturePolicyParserDelegate* delegate = nullptr); }; - // Returns true iff any declaration in the policy is for the given feature. CORE_EXPORT bool IsFeatureDeclared(mojom::blink::FeaturePolicyFeature, const ParsedFeaturePolicy&); diff --git a/third_party/blink/renderer/core/feature_policy/feature_policy_test.cc b/third_party/blink/renderer/core/feature_policy/feature_policy_test.cc index 8f3ca728993248..7f8639a5c9b023 100644 --- a/third_party/blink/renderer/core/feature_policy/feature_policy_test.cc +++ b/third_party/blink/renderer/core/feature_policy/feature_policy_test.cc @@ -24,10 +24,6 @@ #define ORIGIN_C "https://example.org/" #define OPAQUE_ORIGIN "" -// identifier used for feature/permissions policy parsing test. -// when there is a testcase for one syntax but not the other. -#define NOT_APPLICABLE nullptr - class GURL; namespace blink { @@ -114,8 +110,7 @@ struct FeaturePolicyParserTestCase { const char* test_name; // Test inputs. - const char* feature_policy_string; - const char* permissions_policy_string; + const char* policy_string; const char* self_origin; const char* src_origin; @@ -126,63 +121,26 @@ struct FeaturePolicyParserTestCase { class FeaturePolicyParserParsingTest : public FeaturePolicyParserTest, public ::testing::WithParamInterface { - private: - scoped_refptr GetSrcOrigin(const char* origin_str) { + protected: + ParsedFeaturePolicy Parse(const char* policy_string, + const char* self_origin_string, + const char* src_origin_string, + PolicyParserMessageBuffer& logger, + const FeatureNameMap& feature_names) { + scoped_refptr self_origin = + SecurityOrigin::CreateFromString(self_origin_string); + scoped_refptr src_origin; - if (String(origin_str) == OPAQUE_ORIGIN) { + if (String(src_origin_string) == OPAQUE_ORIGIN) { src_origin = SecurityOrigin::CreateUniqueOpaque(); } else { - src_origin = - origin_str ? SecurityOrigin::CreateFromString(origin_str) : nullptr; + src_origin = src_origin_string + ? SecurityOrigin::CreateFromString(src_origin_string) + : nullptr; } - return src_origin; - } - - protected: - ParsedFeaturePolicy ParseFeaturePolicy( - const char* policy_string, - const char* self_origin_string, - const char* src_origin_string, - PolicyParserMessageBuffer& logger, - const FeatureNameMap& feature_names, - FeaturePolicyParserDelegate* delegate = nullptr) { - return FeaturePolicyParser::ParseFeaturePolicyForTest( - policy_string, SecurityOrigin::CreateFromString(self_origin_string), - GetSrcOrigin(src_origin_string), logger, feature_names, delegate); - } - - ParsedFeaturePolicy ParsePermissionsPolicy( - const char* policy_string, - const char* self_origin_string, - const char* src_origin_string, - PolicyParserMessageBuffer& logger, - const FeatureNameMap& feature_names, - FeaturePolicyParserDelegate* delegate = nullptr) { - return FeaturePolicyParser::ParsePermissionsPolicyForTest( - policy_string, SecurityOrigin::CreateFromString(self_origin_string), - GetSrcOrigin(src_origin_string), logger, feature_names, delegate); - } - void CheckParsedPolicy(const ParsedFeaturePolicy& actual, - const ParsedPolicyForTest& expected) { - ASSERT_EQ(actual.size(), expected.size()); - for (size_t i = 0; i < actual.size(); ++i) { - const auto& actual_declaration = actual[i]; - const auto& expected_declaration = expected[i]; - - EXPECT_EQ(actual_declaration.feature, expected_declaration.feature); - EXPECT_EQ(actual_declaration.fallback_value, - expected_declaration.fallback_value); - EXPECT_EQ(actual_declaration.opaque_value, - expected_declaration.opaque_value); - - ASSERT_EQ(actual_declaration.allowed_origins.size(), - expected_declaration.origins.size()); - for (size_t j = 0; j < actual_declaration.allowed_origins.size(); ++j) { - EXPECT_TRUE(actual_declaration.allowed_origins[j].IsSameOriginWith( - url::Origin::Create(GURL(expected_declaration.origins[j])))); - } - } + return FeaturePolicyParser::Parse(policy_string, self_origin, src_origin, + logger, feature_names); } public: @@ -192,32 +150,14 @@ class FeaturePolicyParserParsingTest const FeaturePolicyParserTestCase FeaturePolicyParserParsingTest::kCases[] = { { /* test_name */ "EmptyPolicy", - /* feature_policy_string */ "", - /* permissions_policy_string */ "", + /* policy_string */ "", /* self_origin */ ORIGIN_A, /* src_origin */ ORIGIN_B, /* expected_parse_result */ {}, }, { /* test_name */ "SimplePolicyWithSelf", - /* feature_policy_string */ "geolocation 'self'", - /* permissions_policy_string */ "geolocation=self", - /* self_origin */ ORIGIN_A, - /* src_origin */ ORIGIN_B, - /* expected_parse_result */ - { - { - mojom::blink::FeaturePolicyFeature::kGeolocation, - /* fallback_value */ false, - /* opaque_value */ false, - {ORIGIN_A}, - }, - }, - }, - { - /* test_name */ "SimplePolicyWithSelfExplicitListSyntax", - /* feature_policy_string */ NOT_APPLICABLE, - /* permissions_policy_string */ "geolocation=(self)", + /* policy_string */ "geolocation 'self'", /* self_origin */ ORIGIN_A, /* src_origin */ ORIGIN_B, /* expected_parse_result */ @@ -232,8 +172,7 @@ const FeaturePolicyParserTestCase FeaturePolicyParserParsingTest::kCases[] = { }, { /* test_name */ "SimplePolicyWithStar", - /* feature_policy_string */ "geolocation *", - /* permissions_policy_string */ "geolocation=*", + /* policy_string */ "geolocation *", /* self_origin */ ORIGIN_A, /* src_origin */ ORIGIN_B, /* expected_parse_result */ @@ -248,14 +187,10 @@ const FeaturePolicyParserTestCase FeaturePolicyParserParsingTest::kCases[] = { }, { /* test_name */ "ComplicatedPolicy", - /* feature_policy_string */ + /* policy_string */ "geolocation *; " "fullscreen " ORIGIN_B " " ORIGIN_C "; " "payment 'self'", - /* permissions_policy_string */ - "geolocation=*, " - "fullscreen=(\"" ORIGIN_B "\" \"" ORIGIN_C "\")," - "payment=self", /* self_origin */ ORIGIN_A, /* src_origin */ ORIGIN_B, /* expected_parse_result */ @@ -281,15 +216,11 @@ const FeaturePolicyParserTestCase FeaturePolicyParserParsingTest::kCases[] = { }, }, { - /* test_name */ "MultiplePoliciesIncludingBadFeatureName", - /* feature_policy_string */ + /* test_name */ "MultiplePolicies", + /* policy_string */ "geolocation * " ORIGIN_B "; " - "fullscreen " ORIGIN_B " bad_feature_name " ORIGIN_C "," + "fullscreen " ORIGIN_B " none " ORIGIN_C "," "payment 'self' badorigin", - /* permissions_policy_string */ - "geolocation=(* \"" ORIGIN_B "\")," - "fullscreen=(\"" ORIGIN_B "\" bad_feature_name \"" ORIGIN_C "\")," - "payment=(self \"badorigin\")", /* self_origin */ ORIGIN_A, /* src_origin */ ORIGIN_B, /* expected_parse_result */ @@ -316,12 +247,7 @@ const FeaturePolicyParserTestCase FeaturePolicyParserParsingTest::kCases[] = { }, { /* test_name */ "HeaderPoliciesWithNoOptionalOriginLists", - /* feature_policy_string */ "geolocation;fullscreen;payment", - // Note: In structured header, if no value is associated with a key - // in dictionary, default value would be boolean true, which is - // not allowed as allowlist value in permission policy syntax. - /* permissions_policy_string */ - "geolocation=self,fullscreen=self,payment=self", + /* policy_string */ "geolocation;fullscreen;payment", /* self_origin */ ORIGIN_A, /* src_origin */ nullptr, /* expected_parse_result */ @@ -348,16 +274,14 @@ const FeaturePolicyParserTestCase FeaturePolicyParserParsingTest::kCases[] = { }, { /* test_name */ "EmptyPolicyOpaqueSrcOrigin", - /* feature_policy_string */ "", - /* permissions_policy_string */ "", + /* policy_string */ "", /* self_origin */ ORIGIN_A, /* src_origin */ OPAQUE_ORIGIN, /* expected_parse_result */ {}, }, { /* test_name */ "SimplePolicyOpaqueSrcOrigin", - /* feature_policy_string */ "geolocation", - /* permissions_policy_string */ NOT_APPLICABLE, + /* policy_string */ "geolocation", /* self_origin */ ORIGIN_A, /* src_origin */ OPAQUE_ORIGIN, /* expected_parse_result */ @@ -372,8 +296,7 @@ const FeaturePolicyParserTestCase FeaturePolicyParserParsingTest::kCases[] = { }, { /* test_name */ "SimplePolicyWithSrcOpaqueSrcOrigin", - /* feature_policy_string */ "geolocation 'src'", - /* permissions_policy_string */ NOT_APPLICABLE, + /* policy_string */ "geolocation 'src'", /* self_origin */ ORIGIN_A, /* src_origin */ OPAQUE_ORIGIN, /* expected_parse_result */ @@ -388,8 +311,7 @@ const FeaturePolicyParserTestCase FeaturePolicyParserParsingTest::kCases[] = { }, { /* test_name */ "SimplePolicyWithStarOpaqueSrcOrigin", - /* feature_policy_string */ "geolocation *", - /* permissions_policy_string */ "geolocation=*", + /* policy_string */ "geolocation *", /* self_origin */ ORIGIN_A, /* src_origin */ OPAQUE_ORIGIN, /* expected_parse_result */ @@ -404,9 +326,7 @@ const FeaturePolicyParserTestCase FeaturePolicyParserParsingTest::kCases[] = { }, { /* test_name */ "PolicyWithExplicitOriginsOpaqueSrcOrigin", - /* feature_policy_string */ "geolocation " ORIGIN_B " " ORIGIN_C, - /* permissions_policy_string */ - "geolocation=(\"" ORIGIN_B "\" \"" ORIGIN_C "\")", + /* policy_string */ "geolocation " ORIGIN_B " " ORIGIN_C, /* self_origin */ ORIGIN_A, /* src_origin */ OPAQUE_ORIGIN, /* expected_parse_result */ @@ -422,8 +342,7 @@ const FeaturePolicyParserTestCase FeaturePolicyParserParsingTest::kCases[] = { { /* test_name */ "PolicyWithMultipleOriginsIncludingSrc" "OpaqueSrcOrigin", - /* feature_policy_string */ "geolocation " ORIGIN_B " 'src'", - /* permissions_policy_string */ NOT_APPLICABLE, + /* policy_string */ "geolocation " ORIGIN_B " 'src'", /* self_origin */ ORIGIN_A, /* src_origin */ OPAQUE_ORIGIN, /* expected_parse_result */ @@ -436,57 +355,6 @@ const FeaturePolicyParserTestCase FeaturePolicyParserParsingTest::kCases[] = { }, }, }, - { - /* test_name */ "PolicyWithInvalidDataTypeInt", - /* feature_policy_string */ NOT_APPLICABLE, - // int value should be rejected as allowlist items. - /* permissions_policy_string */ "geolocation=9", - /* self_origin */ ORIGIN_A, - /* src_origin */ nullptr, - /* expected_parse_result */ - { - { - mojom::blink::FeaturePolicyFeature::kGeolocation, - /* fallback_value */ false, - /* opaque_value */ false, - {}, - }, - }, - }, - { - /* test_name */ "PolicyWithInvalidDataTypeFloat", - /* feature_policy_string */ NOT_APPLICABLE, - // decimal value should be rejected as allowlist items. - /* permissions_policy_string */ "geolocation=1.1", - /* self_origin */ ORIGIN_A, - /* src_origin */ nullptr, - /* expected_parse_result */ - { - { - mojom::blink::FeaturePolicyFeature::kGeolocation, - /* fallback_value */ false, - /* opaque_value */ false, - {}, - }, - }, - }, - { - /* test_name */ "PolicyWithInvalidDataTypeBoolean", - /* feature_policy_string */ NOT_APPLICABLE, - // boolean value should be rejected as allowlist items. - /* permissions_policy_string */ "geolocation=?0", - /* self_origin */ ORIGIN_A, - /* src_origin */ nullptr, - /* expected_parse_result */ - { - { - mojom::blink::FeaturePolicyFeature::kGeolocation, - /* fallback_value */ false, - /* opaque_value */ false, - {}, - }, - }, - }, }; INSTANTIATE_TEST_SUITE_P( @@ -497,70 +365,39 @@ INSTANTIATE_TEST_SUITE_P( return param_info.param.test_name; }); -TEST_P(FeaturePolicyParserParsingTest, FeaturePolicyParsedCorrectly) { +TEST_P(FeaturePolicyParserParsingTest, PolicyParsedCorrectly) { PolicyParserMessageBuffer logger; const FeaturePolicyParserTestCase& test_case = GetParam(); - if (test_case.feature_policy_string == NOT_APPLICABLE) - return; - ASSERT_NE(test_case.self_origin, nullptr); - CheckParsedPolicy( - ParseFeaturePolicy(test_case.feature_policy_string, test_case.self_origin, - test_case.src_origin, logger, test_feature_name_map), - test_case.expected_parse_result); -} - -TEST_P(FeaturePolicyParserParsingTest, PermissionsPolicyParsedCorrectly) { - PolicyParserMessageBuffer logger; - const FeaturePolicyParserTestCase& test_case = GetParam(); - if (test_case.permissions_policy_string == NOT_APPLICABLE) - return; - - ASSERT_NE(test_case.self_origin, nullptr); - CheckParsedPolicy( - ParsePermissionsPolicy(test_case.permissions_policy_string, - test_case.self_origin, test_case.src_origin, - logger, test_feature_name_map), - test_case.expected_parse_result); -} - -TEST_F(FeaturePolicyParserParsingTest, - FeaturePolicyHeaderPermissionsPolicyHeaderCoExist) { - PolicyParserMessageBuffer logger; - // When there is conflict take the value from permission policy. - // Non-conflicting entries will be merged. - CheckParsedPolicy( - FeaturePolicyParser::ParseHeader( - "geolocation 'none', fullscreen 'self'", origin_a_.get(), logger, - nullptr /* delegate */, "geolocation=self, payment=*"), - { - { - mojom::blink::FeaturePolicyFeature::kGeolocation, - /* fallback_value */ false, - /* opaque_value */ false, - {ORIGIN_A}, - }, - { - mojom::blink::FeaturePolicyFeature::kPayment, - /* fallback_value */ true, - /* opaque_value */ true, - {}, - }, - { - mojom::blink::FeaturePolicyFeature::kFullscreen, - /* fallback_value */ false, - /* opaque_value */ false, - {ORIGIN_A}, - }, - }); + const ParsedFeaturePolicy actual = + Parse(test_case.policy_string, test_case.self_origin, + test_case.src_origin, logger, test_feature_name_map); + const ParsedPolicyForTest& expected = test_case.expected_parse_result; + ASSERT_EQ(actual.size(), expected.size()); + for (size_t i = 0; i < actual.size(); ++i) { + const auto& actual_declaration = actual[i]; + const auto& expected_declaration = expected[i]; + + EXPECT_EQ(actual_declaration.feature, expected_declaration.feature); + EXPECT_EQ(actual_declaration.fallback_value, + expected_declaration.fallback_value); + EXPECT_EQ(actual_declaration.opaque_value, + expected_declaration.opaque_value); + + ASSERT_EQ(actual_declaration.allowed_origins.size(), + expected_declaration.origins.size()); + for (size_t j = 0; j < actual_declaration.allowed_origins.size(); ++j) { + EXPECT_TRUE(actual_declaration.allowed_origins[j].IsSameOriginWith( + url::Origin::Create(GURL(expected_declaration.origins[j])))); + } + } } TEST_F(FeaturePolicyParserTest, ParseValidPolicy) { for (const char* policy_string : kValidPolicies) { PolicyParserMessageBuffer logger; - FeaturePolicyParser::ParseFeaturePolicyForTest( - policy_string, origin_a_.get(), origin_b_.get(), logger, - test_feature_name_map); + FeaturePolicyParser::Parse(policy_string, origin_a_.get(), origin_b_.get(), + logger, test_feature_name_map); EXPECT_EQ(0UL, logger.GetMessages().size()) << "Should parse " << policy_string; } @@ -569,9 +406,8 @@ TEST_F(FeaturePolicyParserTest, ParseValidPolicy) { TEST_F(FeaturePolicyParserTest, ParseInvalidPolicy) { for (const char* policy_string : kInvalidPolicies) { PolicyParserMessageBuffer logger; - FeaturePolicyParser::ParseFeaturePolicyForTest( - policy_string, origin_a_.get(), origin_b_.get(), logger, - test_feature_name_map); + FeaturePolicyParser::Parse(policy_string, origin_a_.get(), origin_b_.get(), + logger, test_feature_name_map); EXPECT_LT(0UL, logger.GetMessages().size()) << "Should fail to parse " << policy_string; } @@ -580,9 +416,8 @@ TEST_F(FeaturePolicyParserTest, ParseInvalidPolicy) { TEST_F(FeaturePolicyParserTest, ParseTooLongPolicy) { PolicyParserMessageBuffer logger; auto policy_string = "geolocation http://" + std::string(1 << 17, 'a'); - FeaturePolicyParser::ParseFeaturePolicyForTest( - policy_string.c_str(), origin_a_.get(), origin_b_.get(), logger, - test_feature_name_map); + FeaturePolicyParser::Parse(policy_string.c_str(), origin_a_.get(), + origin_b_.get(), logger, test_feature_name_map); EXPECT_EQ(1UL, logger.GetMessages().size()) << "Should fail to parse string with size " << policy_string.size(); } @@ -593,9 +428,8 @@ TEST_F(FeaturePolicyParserTest, HeaderHistogram) { HistogramTester tester; PolicyParserMessageBuffer logger; - FeaturePolicyParser::ParseFeaturePolicyForTest("payment; fullscreen", - origin_a_.get(), nullptr, - logger, test_feature_name_map); + FeaturePolicyParser::Parse("payment; fullscreen", origin_a_.get(), nullptr, + logger, test_feature_name_map); tester.ExpectTotalCount(histogram_name, 2); tester.ExpectBucketCount( histogram_name, @@ -614,12 +448,11 @@ TEST_F(FeaturePolicyParserTest, HistogramMultiple) { // If the same feature is listed multiple times, it should only be counted // once. - FeaturePolicyParser::ParseFeaturePolicyForTest( - "geolocation 'self'; payment; geolocation *", origin_a_.get(), nullptr, - logger, test_feature_name_map); - FeaturePolicyParser::ParseFeaturePolicyForTest( - "fullscreen 'self', fullscreen *", origin_a_.get(), nullptr, logger, - test_feature_name_map); + FeaturePolicyParser::Parse("geolocation 'self'; payment; geolocation *", + origin_a_.get(), nullptr, logger, + test_feature_name_map); + FeaturePolicyParser::Parse("fullscreen 'self', fullscreen *", origin_a_.get(), + nullptr, logger, test_feature_name_map); tester.ExpectTotalCount(histogram_name, 3); tester.ExpectBucketCount( histogram_name, @@ -639,13 +472,12 @@ TEST_F(FeaturePolicyParserTest, AllowHistogramSameDocument) { PolicyParserMessageBuffer logger; auto dummy = std::make_unique(); - FeaturePolicyParser::ParseFeaturePolicyForTest( - "payment; fullscreen", origin_a_.get(), origin_b_.get(), logger, - test_feature_name_map, dummy->GetFrame().DomWindow()); - FeaturePolicyParser::ParseFeaturePolicyForTest( - "fullscreen; geolocation", origin_a_.get(), origin_b_.get(), logger, - test_feature_name_map, dummy->GetFrame().DomWindow()); - + FeaturePolicyParser::Parse("payment; fullscreen", origin_a_.get(), + origin_b_.get(), logger, test_feature_name_map, + dummy->GetFrame().DomWindow()); + FeaturePolicyParser::Parse("fullscreen; geolocation", origin_a_.get(), + origin_b_.get(), logger, test_feature_name_map, + dummy->GetFrame().DomWindow()); tester.ExpectTotalCount(histogram_name, 3); tester.ExpectBucketCount( histogram_name, @@ -669,13 +501,12 @@ TEST_F(FeaturePolicyParserTest, AllowHistogramDifferentDocument) { auto dummy = std::make_unique(); auto dummy2 = std::make_unique(); - FeaturePolicyParser::ParseFeaturePolicyForTest( - "payment; fullscreen", origin_a_.get(), origin_b_.get(), logger, - test_feature_name_map, dummy->GetFrame().DomWindow()); - FeaturePolicyParser::ParseFeaturePolicyForTest( - "fullscreen; geolocation", origin_a_.get(), origin_b_.get(), logger, - test_feature_name_map, dummy2->GetFrame().DomWindow()); - + FeaturePolicyParser::Parse("payment; fullscreen", origin_a_.get(), + origin_b_.get(), logger, test_feature_name_map, + dummy->GetFrame().DomWindow()); + FeaturePolicyParser::Parse("fullscreen; geolocation", origin_a_.get(), + origin_b_.get(), logger, test_feature_name_map, + dummy2->GetFrame().DomWindow()); tester.ExpectTotalCount(histogram_name, 4); tester.ExpectBucketCount( histogram_name, diff --git a/third_party/blink/renderer/core/origin_trials/origin_trial_context_test.cc b/third_party/blink/renderer/core/origin_trials/origin_trial_context_test.cc index 66c6b68fa2c852..5536f33760861d 100644 --- a/third_party/blink/renderer/core/origin_trials/origin_trial_context_test.cc +++ b/third_party/blink/renderer/core/origin_trials/origin_trial_context_test.cc @@ -428,8 +428,8 @@ TEST_F(OriginTrialContextTest, FeaturePolicy) { PolicyParserMessageBuffer logger; ParsedFeaturePolicy result; - result = FeaturePolicyParser::ParseFeaturePolicyForTest( - "frobulate", security_origin, nullptr, logger, feature_map, window); + result = FeaturePolicyParser::Parse("frobulate", security_origin, nullptr, + logger, feature_map, window); EXPECT_TRUE(logger.GetMessages().IsEmpty()); ASSERT_EQ(1u, result.size()); EXPECT_EQ(mojom::blink::FeaturePolicyFeature::kFrobulate, result[0].feature);