From 7bfc821a28ad8fa2170ff3697eea71b386ff9a70 Mon Sep 17 00:00:00 2001 From: brave-builds Date: Wed, 26 Apr 2023 22:59:24 +0000 Subject: [PATCH] Uplift of #18154 (squashed) to beta --- browser/about_flags.cc | 8 + .../disable_client_hints_browsertest.cc | 207 +++++++++++++++--- .../embedder_support/user_agent_utils.cc | 36 +++ .../embedder_support/user_agent_utils.h | 21 ++ .../client_hints/enabled_client_hints.cc | 48 ++-- .../third_party/blink/common/features.cc | 11 + .../blink/public/common/features.h | 8 + components/embedder_support/BUILD.gn | 1 + .../user_agent_utils_unittest.cc | 45 ++++ test/data/ch-meta-http-equiv-accept-ch.html | 2 +- test/data/ch-meta-name-accept-ch.html | 2 +- test/data/ch.html.mock-http-headers | 2 +- 12 files changed, 341 insertions(+), 50 deletions(-) create mode 100644 chromium_src/components/embedder_support/user_agent_utils.h diff --git a/browser/about_flags.cc b/browser/about_flags.cc index 5e212e21a345..43db08e353a5 100644 --- a/browser/about_flags.cc +++ b/browser/about_flags.cc @@ -389,6 +389,14 @@ kOsAll, \ FEATURE_VALUE_TYPE(blink::features::kAllowCertainClientHints), \ }, \ + { \ + "clamp-platform-version-client-hint", \ + "Clamp platform version client hint", \ + "Clamps the patch field of the platform version client hint", \ + kOsAll, \ + FEATURE_VALUE_TYPE( \ + blink::features::kClampPlatformVersionClientHint), \ + }, \ { \ "brave-ntp-branded-wallpaper-demo", \ "New Tab Page Demo Branded Wallpaper", \ diff --git a/browser/test/disabled_features/disable_client_hints_browsertest.cc b/browser/test/disabled_features/disable_client_hints_browsertest.cc index ac63c89325c2..0d60920fa421 100644 --- a/browser/test/disabled_features/disable_client_hints_browsertest.cc +++ b/browser/test/disabled_features/disable_client_hints_browsertest.cc @@ -4,6 +4,7 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include +#include #include #include "base/containers/contains.h" @@ -11,6 +12,8 @@ #include "base/functional/bind.h" #include "base/path_service.h" #include "base/run_loop.h" +#include "base/strings/strcat.h" +#include "base/strings/string_util.h" #include "base/test/scoped_feature_list.h" #include "brave/components/constants/brave_paths.h" #include "chrome/test/base/in_process_browser_test.h" @@ -27,12 +30,15 @@ #include "third_party/blink/public/common/features.h" namespace { +const char kNoClientHintsHeaders[] = "/simple.html"; const char kClientHints[] = "/ch.html"; const char kClientHintsDelegationMerge[] = "/ch_delegation_merge.html"; const char KClientHintsMetaHTTPEquivAcceptCH[] = "/ch-meta-http-equiv-accept-ch.html"; const char KClientHintsMetaNameAcceptCH[] = "/ch-meta-name-accept-ch.html"; +const char kPlatformVersionClientHintPatchValue[] = "x"; + const std::reference_wrapper kTestFeatures[] = { // Individual hints features blink::features::kClientHintsDeviceMemory, @@ -69,6 +75,7 @@ class ClientHintsBrowserTest EXPECT_TRUE(https_server_.Start()); + no_client_hints_headers_url_ = https_server_.GetURL(kNoClientHintsHeaders); client_hints_url_ = https_server_.GetURL(kClientHints); client_hints_delegation_merge_url_ = https_server_.GetURL(kClientHintsDelegationMerge); @@ -89,25 +96,33 @@ class ClientHintsBrowserTest void SetUp() override { // Test that even with CH features enabled, there is no header. - std::vector enabled_features; + std::vector enabled_features; std::vector disabled_features; for (const auto& feature : kTestFeatures) { if (IsClientHintHeaderEnabled()) { - enabled_features.push_back(feature.get()); + enabled_features.emplace_back(feature.get(), base::FieldTrialParams()); } else { disabled_features.push_back(feature.get()); } } if (IsBraveClientHintFeatureEnabled()) { - enabled_features.push_back(blink::features::kAllowCertainClientHints); + enabled_features.emplace_back(blink::features::kAllowCertainClientHints, + base::FieldTrialParams()); + base::FieldTrialParams parameters; + parameters[blink::features::kClampPlatformVersionClientHintPatchValue + .name] = kPlatformVersionClientHintPatchValue; + enabled_features.emplace_back( + blink::features::kClampPlatformVersionClientHint, parameters); } else { disabled_features.push_back(blink::features::kAllowCertainClientHints); } - scoped_feature_list_.InitWithFeatures(enabled_features, disabled_features); + scoped_feature_list_.InitWithFeaturesAndParameters(enabled_features, + disabled_features); if (IsBraveClientHintFeatureEnabled()) { + PopulateDefaultClientHints(); PopulateAllowedClientHints(); } InProcessBrowserTest::SetUp(); @@ -120,6 +135,10 @@ class ClientHintsBrowserTest void TearDownOnMainThread() override {} + const GURL& no_client_hints_headers_url() const { + return no_client_hints_headers_url_; + } + const GURL& client_hints_url() const { return client_hints_url_; } const GURL& client_hints_delegation_merge_url() const { @@ -133,49 +152,129 @@ class ClientHintsBrowserTest return client_hints_meta_name_accept_ch_url_; } - size_t count_client_hints_headers_seen() const { - return count_client_hints_headers_seen_; + size_t default_client_hints_headers_seen_count() const { + return default_client_hints_headers_seen_.size(); + } + + size_t allowed_client_hints_headers_seen_count() const { + return allowed_client_hints_headers_seen_.size(); + } + + size_t client_hints_headers_seen_count() const { + return unexpected_client_hints_headers_seen_.size(); + } + + std::string default_client_hints_headers_seen() const { + return base::JoinString( + std::vector(default_client_hints_headers_seen_.begin(), + default_client_hints_headers_seen_.end()), + ", "); + } + + std::string allowed_client_hints_headers_seen() const { + return base::JoinString( + std::vector(allowed_client_hints_headers_seen_.begin(), + allowed_client_hints_headers_seen_.end()), + ", "); } - void reset_client_hints_headers_seen_count() { - count_client_hints_headers_seen_ = 0; + std::string unexpected_client_hints_headers_seen() const { + return base::JoinString(unexpected_client_hints_headers_seen_, ", "); + } + + std::string platform_version_client_hint_value() const { + return platform_version_client_hint_value_; + } + + void reset_client_hints_headers_seen() { + default_client_hints_headers_seen_.clear(); + allowed_client_hints_headers_seen_.clear(); + unexpected_client_hints_headers_seen_.clear(); + platform_version_client_hint_value_ = ""; + } + + bool VerifyPlatformVersionClientHintPatchValue() const { + return base::EndsWith( + platform_version_client_hint_value_, + base::StrCat({".", kPlatformVersionClientHintPatchValue, "\""})); } private: - void PopulateAllowedClientHints() { + void PopulateDefaultClientHints() { const auto& hints_map = network::GetClientHintToNameMap(); - allowed_hints_.push_back( + default_hints_.push_back( hints_map.at(network::mojom::WebClientHintsType::kUA)); - allowed_hints_.push_back( + default_hints_.push_back( hints_map.at(network::mojom::WebClientHintsType::kUAMobile)); - allowed_hints_.push_back( + default_hints_.push_back( hints_map.at(network::mojom::WebClientHintsType::kUAPlatform)); } + void PopulateAllowedClientHints() { + const auto& hints_map = network::GetClientHintToNameMap(); + allowed_hints_.push_back( + hints_map.at(network::mojom::WebClientHintsType::kUAModel)); + allowed_hints_.push_back( + hints_map.at(network::mojom::WebClientHintsType::kUAPlatformVersion)); + } + + bool IsPlatformVersionClientHintHeader(const std::string& header) const { + return header == + network::GetClientHintToNameMap().at( + network::mojom::WebClientHintsType::kUAPlatformVersion); + } + + void StorePlatformVersionClientHintHeaderValue( + const net::test_server::HttpRequest::HeaderMap& headers) { + const auto& hints_map = network::GetClientHintToNameMap(); + const std::string& platform_version_header_name = + hints_map.at(network::mojom::WebClientHintsType::kUAPlatformVersion); + platform_version_client_hint_value_ = + headers.at(platform_version_header_name); + } + void MonitorResourceRequest(const net::test_server::HttpRequest& request) { for (const auto& elem : network::GetClientHintToNameMap()) { const auto& header = elem.second; if (base::Contains(request.headers, header)) { - if (IsBraveClientHintFeatureEnabled() && - base::Contains(allowed_hints_, header)) { - continue; + if (IsBraveClientHintFeatureEnabled()) { + if (base::Contains(default_hints_, header)) { + default_client_hints_headers_seen_.insert(header); + continue; + } else if (base::Contains(allowed_hints_, header)) { + allowed_client_hints_headers_seen_.insert(header); + if (IsPlatformVersionClientHintHeader(header)) { + StorePlatformVersionClientHintHeaderValue(request.headers); + } + continue; + } } - count_client_hints_headers_seen_++; + unexpected_client_hints_headers_seen_.push_back(header); } } } net::EmbeddedTestServer https_server_; + + GURL no_client_hints_headers_url_; GURL client_hints_delegation_merge_url_; GURL client_hints_meta_http_equiv_accept_ch_url_; GURL client_hints_meta_name_accept_ch_url_; GURL client_hints_url_; - size_t count_client_hints_headers_seen_ = 0; + + std::set default_client_hints_headers_seen_; + std::set allowed_client_hints_headers_seen_; + std::vector unexpected_client_hints_headers_seen_; + + std::vector default_hints_; std::vector allowed_hints_; + + std::string platform_version_client_hint_value_; + base::test::ScopedFeatureList scoped_feature_list_; }; -IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, ClientHintsDisabled) { +IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, CheckClientHints) { for (const auto& feature : kTestFeatures) { EXPECT_EQ(IsClientHintHeaderEnabled(), base::FeatureList::IsEnabled(feature)); @@ -183,23 +282,77 @@ IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, ClientHintsDisabled) { EXPECT_EQ( IsBraveClientHintFeatureEnabled(), base::FeatureList::IsEnabled(blink::features::kAllowCertainClientHints)); + + const size_t expected_default_client_hints_count = + IsClientHintHeaderEnabled() && IsBraveClientHintFeatureEnabled() ? 3u + : 0u; + const size_t expected_allowed_client_hints_count = + IsClientHintHeaderEnabled() && IsBraveClientHintFeatureEnabled() ? 2u + : 0u; + + ASSERT_TRUE( + ui_test_utils::NavigateToURL(browser(), no_client_hints_headers_url())); + EXPECT_EQ(expected_default_client_hints_count, + default_client_hints_headers_seen_count()) + << "Default headers seen: " << default_client_hints_headers_seen(); + EXPECT_EQ(0u, allowed_client_hints_headers_seen_count()) + << "Allowed headers seen: " << allowed_client_hints_headers_seen(); + EXPECT_EQ(0u, client_hints_headers_seen_count()) + << "Unexpected headers: " << unexpected_client_hints_headers_seen(); + + reset_client_hints_headers_seen(); ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), client_hints_url())); - EXPECT_EQ(0u, count_client_hints_headers_seen()); + EXPECT_EQ(expected_default_client_hints_count, + default_client_hints_headers_seen_count()) + << "Default headers seen: " << default_client_hints_headers_seen(); + EXPECT_EQ(expected_allowed_client_hints_count, + allowed_client_hints_headers_seen_count()) + << "Allowed headers seen: " << allowed_client_hints_headers_seen(); + EXPECT_EQ(0u, client_hints_headers_seen_count()) + << "Unexpected headers: " << unexpected_client_hints_headers_seen(); + if (IsClientHintHeaderEnabled() && IsBraveClientHintFeatureEnabled()) { + EXPECT_TRUE(VerifyPlatformVersionClientHintPatchValue()) + << "Expected the patch field value to be: '" + << kPlatformVersionClientHintPatchValue << "'. " + << "Actual platform version value: " + << platform_version_client_hint_value(); + } - reset_client_hints_headers_seen_count(); + reset_client_hints_headers_seen(); ASSERT_TRUE(ui_test_utils::NavigateToURL( browser(), client_hints_meta_http_equiv_accept_ch_url())); - EXPECT_EQ(0u, count_client_hints_headers_seen()); - - reset_client_hints_headers_seen_count(); + EXPECT_EQ(expected_default_client_hints_count, + default_client_hints_headers_seen_count()) + << "Default headers seen: " << default_client_hints_headers_seen(); + EXPECT_EQ(expected_allowed_client_hints_count, + allowed_client_hints_headers_seen_count()) + << "Allowed headers seen: " << allowed_client_hints_headers_seen(); + EXPECT_EQ(0u, client_hints_headers_seen_count()) + << "Unexpected headers: " << unexpected_client_hints_headers_seen(); + + reset_client_hints_headers_seen(); ASSERT_TRUE(ui_test_utils::NavigateToURL( browser(), client_hints_meta_name_accept_ch_url())); - EXPECT_EQ(0u, count_client_hints_headers_seen()); - - reset_client_hints_headers_seen_count(); + EXPECT_EQ(expected_default_client_hints_count, + default_client_hints_headers_seen_count()) + << "Default headers seen: " << default_client_hints_headers_seen(); + EXPECT_EQ(expected_allowed_client_hints_count, + allowed_client_hints_headers_seen_count()) + << "Allowed headers seen: " << allowed_client_hints_headers_seen(); + EXPECT_EQ(0u, client_hints_headers_seen_count()) + << "Unexpected headers: " << unexpected_client_hints_headers_seen(); + + reset_client_hints_headers_seen(); ASSERT_TRUE(ui_test_utils::NavigateToURL( browser(), client_hints_delegation_merge_url())); - EXPECT_EQ(0u, count_client_hints_headers_seen()); + EXPECT_EQ(expected_default_client_hints_count, + default_client_hints_headers_seen_count()) + << "Default headers seen: " << default_client_hints_headers_seen(); + EXPECT_EQ(expected_allowed_client_hints_count, + allowed_client_hints_headers_seen_count()) + << "Allowed headers seen: " << allowed_client_hints_headers_seen(); + EXPECT_EQ(0u, client_hints_headers_seen_count()) + << "Unexpected headers: " << unexpected_client_hints_headers_seen(); } INSTANTIATE_TEST_SUITE_P( diff --git a/chromium_src/components/embedder_support/user_agent_utils.cc b/chromium_src/components/embedder_support/user_agent_utils.cc index 05ea29fc0cdf..36290888da76 100644 --- a/chromium_src/components/embedder_support/user_agent_utils.cc +++ b/chromium_src/components/embedder_support/user_agent_utils.cc @@ -3,6 +3,13 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "components/embedder_support/user_agent_utils.h" + +#include "base/check_op.h" +#include "base/strings/stringprintf.h" +#include "base/version.h" +#include "third_party/blink/public/common/features.h" + namespace { constexpr char kBraveBrandNameForCHUA[] = "Brave"; @@ -18,5 +25,34 @@ constexpr char kBraveBrandNameForCHUA[] = "Brave"; // can't use it here in the //components. #define BRAVE_GET_USER_AGENT_BRAND_LIST brand = kBraveBrandNameForCHUA; +#define GetUserAgentMetadata GetUserAgentMetadata_ChromiumImpl #include "src/components/embedder_support/user_agent_utils.cc" +#undef GetUserAgentMetadata #undef BRAVE_GET_USER_AGENT_BRAND_LIST + +namespace embedder_support { + +blink::UserAgentMetadata GetUserAgentMetadata() { + return GetUserAgentMetadata(nullptr); +} + +blink::UserAgentMetadata GetUserAgentMetadata(const PrefService* pref_service) { + blink::UserAgentMetadata metadata = + GetUserAgentMetadata_ChromiumImpl(pref_service); + if (base::FeatureList::IsEnabled( + blink::features::kClampPlatformVersionClientHint)) { + // Clamp platform version + base::Version platform_version(metadata.platform_version); + // Expect Chromium code to return `major.minor.patch` version. If that + // changes we need to re-evaluate what we want to send. + CHECK_EQ(3u, platform_version.components().size()); + metadata.platform_version = base::StringPrintf( + "%d.%d.%s", platform_version.components()[0], + platform_version.components()[1], + blink::features::kClampPlatformVersionClientHintPatchValue.Get() + .c_str()); + } + return metadata; +} + +} // namespace embedder_support diff --git a/chromium_src/components/embedder_support/user_agent_utils.h b/chromium_src/components/embedder_support/user_agent_utils.h new file mode 100644 index 000000000000..b20553d082c8 --- /dev/null +++ b/chromium_src/components/embedder_support/user_agent_utils.h @@ -0,0 +1,21 @@ +/* Copyright (c) 2023 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_CHROMIUM_SRC_COMPONENTS_EMBEDDER_SUPPORT_USER_AGENT_UTILS_H_ +#define BRAVE_CHROMIUM_SRC_COMPONENTS_EMBEDDER_SUPPORT_USER_AGENT_UTILS_H_ + +#define GetUserAgentMetadata GetUserAgentMetadata_ChromiumImpl +#include "src/components/embedder_support/user_agent_utils.h" // IWYU pragma: export +#undef GetUserAgentMetadata + +namespace embedder_support { + +blink::UserAgentMetadata GetUserAgentMetadata(); + +blink::UserAgentMetadata GetUserAgentMetadata(const PrefService* pref_service); + +} // namespace embedder_support + +#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_EMBEDDER_SUPPORT_USER_AGENT_UTILS_H_ diff --git a/chromium_src/third_party/blink/common/client_hints/enabled_client_hints.cc b/chromium_src/third_party/blink/common/client_hints/enabled_client_hints.cc index 0e66729f32f4..126f0244336a 100644 --- a/chromium_src/third_party/blink/common/client_hints/enabled_client_hints.cc +++ b/chromium_src/third_party/blink/common/client_hints/enabled_client_hints.cc @@ -12,22 +12,43 @@ #undef SetIsEnabled +// If the kAllowCertainClientHints feature is turned on then, by default, we +// will send three (3) non-privacy-risking CHs: kUA, kUAMobile, and kUAPlatform. +// Additionally, if we receive CH requests for +// kUAPlatformVersion and/or kUAModel, we will send these, but: +// - kUAModel will be always set to an empty string; +// - kUAPlatformVersion will have a clamped patch field value. + namespace blink { +namespace { +bool AreCertainClientHintsAllowed() { + return base::FeatureList::IsEnabled( + blink::features::kAllowCertainClientHints); +} +} // namespace + void EnabledClientHints::SetIsEnabled(const WebClientHintsType type, const bool should_send) { + bool type_is_enabled = false; switch (type) { case WebClientHintsType::kUA: case WebClientHintsType::kUAMobile: + case WebClientHintsType::kUAModel: case WebClientHintsType::kUAPlatform: - if (base::FeatureList::IsEnabled( - blink::features::kAllowCertainClientHints)) { - SetIsEnabled_ChromiumImpl(type, should_send); - break; + case WebClientHintsType::kUAPlatformVersion: + if (AreCertainClientHintsAllowed()) { + type_is_enabled = true; } - ABSL_FALLTHROUGH_INTENDED; + break; default: - enabled_types_[static_cast(type)] = false; + break; + } + + if (type_is_enabled) { + SetIsEnabled_ChromiumImpl(type, should_send); + } else { + enabled_types_[static_cast(type)] = false; } } @@ -37,20 +58,7 @@ void EnabledClientHints::SetIsEnabled( const net::HttpResponseHeaders* response_headers, const network::mojom::WebClientHintsType type, const bool should_send) { - switch (type) { - case WebClientHintsType::kUA: - case WebClientHintsType::kUAMobile: - case WebClientHintsType::kUAPlatform: - if (base::FeatureList::IsEnabled( - blink::features::kAllowCertainClientHints)) { - SetIsEnabled_ChromiumImpl(url, third_party_url, response_headers, type, - should_send); - break; - } - ABSL_FALLTHROUGH_INTENDED; - default: - SetIsEnabled(type, should_send); - } + SetIsEnabled(type, should_send); } } // namespace blink diff --git a/chromium_src/third_party/blink/common/features.cc b/chromium_src/third_party/blink/common/features.cc index 2f473a32a035..d9a7c1d51a01 100644 --- a/chromium_src/third_party/blink/common/features.cc +++ b/chromium_src/third_party/blink/common/features.cc @@ -51,6 +51,17 @@ BASE_FEATURE(kAllowCertainClientHints, "AllowCertainClientHints", base::FEATURE_DISABLED_BY_DEFAULT); +// Clamp platform version client hint's patch field. +// Platform version client hint is only sent when requested and when the +// AllowCertainClientHints feature is turned on. +BASE_FEATURE(kClampPlatformVersionClientHint, + "ClampPlatformVersionClientHint", + base::FEATURE_ENABLED_BY_DEFAULT); + +constexpr base::FeatureParam + kClampPlatformVersionClientHintPatchValue{&kClampPlatformVersionClientHint, + "patch_value", "0"}; + BASE_FEATURE(kFileSystemAccessAPI, "FileSystemAccessAPI", base::FEATURE_DISABLED_BY_DEFAULT); diff --git a/chromium_src/third_party/blink/public/common/features.h b/chromium_src/third_party/blink/public/common/features.h index ccb0174135fd..fd209348c75d 100644 --- a/chromium_src/third_party/blink/public/common/features.h +++ b/chromium_src/third_party/blink/public/common/features.h @@ -8,10 +8,13 @@ #include "src/third_party/blink/public/common/features.h" // IWYU pragma: export +#include + namespace blink { namespace features { BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kAllowCertainClientHints); +BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kClampPlatformVersionClientHint); BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kFileSystemAccessAPI); BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kNavigatorConnectionAttribute); BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kPartitionBlinkMemoryCache); @@ -20,6 +23,11 @@ BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kBraveBlockScreenFingerprinting); BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kBraveRoundTimeStamps); BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kRestrictEventSourcePool); +// Specifies the value of the third component of the dotted version of the +// platform version client hint. +BLINK_COMMON_EXPORT extern const base::FeatureParam + kClampPlatformVersionClientHintPatchValue; + // Chromium used this function to control Prerender2 feature, but then the // feature was permanently enabled and the function was removed. We still want // to keep the Prerender2 functionality disabled, so putting back the function diff --git a/components/embedder_support/BUILD.gn b/components/embedder_support/BUILD.gn index e24f145d010d..8826e1b388d0 100644 --- a/components/embedder_support/BUILD.gn +++ b/components/embedder_support/BUILD.gn @@ -11,6 +11,7 @@ source_set("unit_tests") { sources = [ "user_agent_utils_unittest.cc" ] deps = [ + "//base/test:test_support", "//components/embedder_support:browser_util", "//components/version_info", "//testing/gtest", diff --git a/components/embedder_support/user_agent_utils_unittest.cc b/components/embedder_support/user_agent_utils_unittest.cc index 9e73e458dadd..5a777f246083 100644 --- a/components/embedder_support/user_agent_utils_unittest.cc +++ b/components/embedder_support/user_agent_utils_unittest.cc @@ -4,8 +4,12 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "components/embedder_support/user_agent_utils.h" +#include "base/strings/string_number_conversions.h" +#include "base/test/scoped_feature_list.h" +#include "base/version.h" #include "components/version_info/version_info.h" #include "testing/gtest/include/gtest/gtest.h" +#include "third_party/blink/public/common/features.h" #include "third_party/blink/public/common/user_agent/user_agent_metadata.h" namespace embedder_support { @@ -33,4 +37,45 @@ TEST(UserAgentUtilsTest, UserAgentMetadata) { ContainsBrandVersion(metadata.brand_version_list, product_brand_version)); } +TEST(UserAgentUtilsTest, DoNotClampPlatformVersion) { + base::test::ScopedFeatureList feature_list; + feature_list.InitWithFeatures( + {blink::features::kAllowCertainClientHints}, + {blink::features::kClampPlatformVersionClientHint}); + auto metadata = GetUserAgentMetadata_ChromiumImpl(); + auto brave_metadata = GetUserAgentMetadata(nullptr); + EXPECT_EQ(metadata, brave_metadata); +} + +TEST(UserAgentUtilsTest, ClampPlatformVersion) { + static const char kClampedValue[] = "7775777"; + base::test::ScopedFeatureList feature_list; + std::vector enabled_features; + enabled_features.emplace_back(blink::features::kAllowCertainClientHints, + base::FieldTrialParams()); + base::FieldTrialParams parameters; + parameters[blink::features::kClampPlatformVersionClientHintPatchValue.name] = + kClampedValue; + enabled_features.emplace_back( + blink::features::kClampPlatformVersionClientHint, parameters); + feature_list.InitWithFeaturesAndParameters(enabled_features, {}); + + auto metadata = GetUserAgentMetadata_ChromiumImpl(); + auto brave_metadata = GetUserAgentMetadata(nullptr); + + base::Version platform_version(metadata.platform_version); + base::Version brave_platform_version(brave_metadata.platform_version); + + EXPECT_EQ(3u, platform_version.components().size()); + EXPECT_EQ(3u, brave_platform_version.components().size()); + EXPECT_EQ(platform_version.components()[0], + brave_platform_version.components()[0]); + EXPECT_EQ(platform_version.components()[1], + brave_platform_version.components()[1]); + EXPECT_NE(platform_version.components()[2], + brave_platform_version.components()[2]); + EXPECT_EQ(kClampedValue, + base::NumberToString(brave_platform_version.components()[2])); +} + } // namespace embedder_support diff --git a/test/data/ch-meta-http-equiv-accept-ch.html b/test/data/ch-meta-http-equiv-accept-ch.html index 19f3f7282e00..afecd847171d 100644 --- a/test/data/ch-meta-http-equiv-accept-ch.html +++ b/test/data/ch-meta-http-equiv-accept-ch.html @@ -1,6 +1,6 @@ - + Hello World! diff --git a/test/data/ch-meta-name-accept-ch.html b/test/data/ch-meta-name-accept-ch.html index f79fccf8ac8f..d644cea8f255 100644 --- a/test/data/ch-meta-name-accept-ch.html +++ b/test/data/ch-meta-name-accept-ch.html @@ -1,6 +1,6 @@ - + Hello World! diff --git a/test/data/ch.html.mock-http-headers b/test/data/ch.html.mock-http-headers index 061daa2ac0af..353077c711ee 100644 --- a/test/data/ch.html.mock-http-headers +++ b/test/data/ch.html.mock-http-headers @@ -1,2 +1,2 @@ HTTP/1.1 200 OK -Accept-CH: arch,device-memory,downlink,dpr,ect,lang,model,platform,rtt,save-data,sec-ch-ua-full-version-list,ua,viewport-height,viewport-width,width +Accept-CH: arch,device-memory,downlink,dpr,ect,lang,model,platform,rtt,save-data,sec-ch-device-memory,sec-ch-dpr,sec-ch-prefers-color-scheme,sec-ch-prefers-reduced-motion,sec-ch-ua,sec-ch-ua-arch,sec-ch-ua-bitness,sec-ch-ua-full,sec-ch-ua-full-version,sec-ch-ua-full-version-list,sec-ch-ua-mobile,sec-ch-ua-model,sec-ch-ua-platform,sec-ch-ua-platform-version,sec-ch-ua-reduced,sec-ch-ua-wow64,sec-ch-viewport-height,sec-ch-viewport-width,sec-ch-width,ua,viewport-height,viewport-width,width