Skip to content

Commit

Permalink
Merge pull request #15134 from brave/maxk-fix-sec-ch-ua
Browse files Browse the repository at this point in the history
Fixes client hints UA to have invariable brand name.
  • Loading branch information
mkarolin authored Sep 21, 2022
2 parents 2c0b02c + 91e31f3 commit a888a7f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
15 changes: 14 additions & 1 deletion chromium_src/components/embedder_support/user_agent_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@
* 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/. */

#define BRAVE_GET_USER_AGENT_BRAND_LIST brand = version_info::GetProductName();
namespace {

constexpr char kBraveBrandNameForCHUA[] = "Brave";

} // namespace

// Chromium uses `version_info::GetProductName()` to get the browser's "brand"
// name, but on MacOS we use different names for different channels (adding Beta
// or Nightly, for example). In the UA client hint, though, we want a consistent
// name regardless of the channel, so we just hard-code it. Note, that we use
// IDS_PRODUCT_NAME from app/chromium_strings.grd (brave_strings.grd) in
// constructing the UA in brave/browser/brave_content_browser_client.cc, but we
// can't use it here in the //components.
#define BRAVE_GET_USER_AGENT_BRAND_LIST brand = kBraveBrandNameForCHUA;

#include "src/components/embedder_support/user_agent_utils.cc"
#undef BRAVE_GET_USER_AGENT_BRAND_LIST
21 changes: 21 additions & 0 deletions components/embedder_support/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) 2022 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 http://mozilla.org/MPL/2.0/. */

import("//testing/test.gni")

source_set("unit_tests") {
testonly = true

sources = [ "user_agent_utils_unittest.cc" ]

deps = [
"//components/embedder_support:browser_util",
"//components/version_info",
"//testing/gtest",
"//third_party/blink/public/common",
]

defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]
}
36 changes: 36 additions & 0 deletions components/embedder_support/user_agent_utils_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright (c) 2022 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 http://mozilla.org/MPL/2.0/. */

#include "components/embedder_support/user_agent_utils.h"
#include "components/version_info/version_info.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/user_agent/user_agent_metadata.h"

namespace embedder_support {

namespace {

bool ContainsBrandVersion(const blink::UserAgentBrandList& brand_list,
const blink::UserAgentBrandVersion brand_version) {
for (const auto& brand_list_entry : brand_list) {
if (brand_list_entry == brand_version)
return true;
}
return false;
}

} // namespace

TEST(UserAgentUtilsTest, UserAgentMetadata) {
auto metadata = GetUserAgentMetadata();

const std::string major_version = version_info::GetMajorVersionNumber();
const blink::UserAgentBrandVersion product_brand_version = {"Brave",
major_version};
EXPECT_TRUE(
ContainsBrandVersion(metadata.brand_version_list, product_brand_version));
}

} // namespace embedder_support
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ test("brave_unit_tests") {
"//brave/components/constants",
"//brave/components/de_amp/browser/test:unit_tests",
"//brave/components/debounce/browser/test:unit_tests",
"//brave/components/embedder_support:unit_tests",
"//brave/components/ipfs/buildflags",
"//brave/components/ipfs/test:brave_ipfs_unit_tests",
"//brave/components/json:brave_json_unit_tests",
Expand Down

0 comments on commit a888a7f

Please sign in to comment.