blob: 616a3edcb4123c25d0220c9564b9d8a08242e71a [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2012 The Chromium Authors
[email protected]3e807c42012-08-28 23:25:582// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Tom Sepez8726d30e2025-01-29 02:11:085#ifdef UNSAFE_BUFFERS_BUILD
6// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
7#pragma allow_unsafe_libc_calls
8#endif
9
[email protected]3e807c42012-08-28 23:25:5810#include "chrome/common/chrome_content_client.h"
11
thestigc62f1092016-07-15 21:02:3712#include <string>
tyoshino11a7c9fe2015-08-19 08:51:4613
Lei Zhang435755f2021-05-11 20:58:5514#include "base/containers/contains.h"
Keishi Hattori0e45c022021-11-27 09:25:5215#include "base/memory/raw_ptr.h"
[email protected]1988e1c2013-02-28 20:27:4216#include "base/strings/string_split.h"
thestigc62f1092016-07-15 21:02:3717#include "base/strings/string_util.h"
18#include "base/test/scoped_command_line.h"
Jason Chaseb8287fc2017-05-17 15:02:2519#include "base/threading/platform_thread.h"
avi2729e442015-12-26 05:27:4520#include "build/build_config.h"
[email protected]3e807c42012-08-28 23:25:5821#include "content/public/common/content_switches.h"
Eric Lawrence46acd922017-07-14 00:55:2022#include "content/public/common/origin_util.h"
Matt Siembor8db9de02019-05-23 04:26:5823#include "content/public/test/test_utils.h"
Lukasz Anforowicz6a6a05d2021-01-12 01:40:0724#include "extensions/buildflags/buildflags.h"
Scott Violet02e38b92018-03-27 23:42:1425#include "ppapi/buildflags/buildflags.h"
Frédéric Wang86b11be2020-12-04 16:27:5326#include "services/network/public/cpp/is_potentially_trustworthy.h"
[email protected]3e807c42012-08-28 23:25:5827#include "testing/gtest/include/gtest/gtest.h"
tyoshino11a7c9fe2015-08-19 08:51:4628#include "url/gurl.h"
29#include "url/origin.h"
30#include "url/url_util.h"
[email protected]3e807c42012-08-28 23:25:5831
Devlin Cronin3eb0e962022-02-03 22:01:3232#if BUILDFLAG(ENABLE_EXTENSIONS)
33#include "extensions/common/constants.h"
34#endif
35
[email protected]3e807c42012-08-28 23:25:5836namespace chrome_common {
37
tyoshino11a7c9fe2015-08-19 08:51:4638TEST(ChromeContentClientTest, AdditionalSchemes) {
Devlin Cronin3eb0e962022-02-03 22:01:3239#if BUILDFLAG(ENABLE_EXTENSIONS)
tyoshino11a7c9fe2015-08-19 08:51:4640 EXPECT_TRUE(url::IsStandard(
41 extensions::kExtensionScheme,
42 url::Component(0, strlen(extensions::kExtensionScheme))));
43
44 GURL extension_url(
45 "chrome-extension://abcdefghijklmnopqrstuvwxyzabcdef/foo.html");
Daniel Cheng88186bd52017-10-20 08:14:4646 url::Origin origin = url::Origin::Create(extension_url);
tyoshino11a7c9fe2015-08-19 08:51:4647 EXPECT_EQ("chrome-extension://abcdefghijklmnopqrstuvwxyzabcdef",
48 origin.Serialize());
Devlin Cronin3eb0e962022-02-03 22:01:3249#endif
Eric Lawrence46acd922017-07-14 00:55:2050
Lukasz Anforowicz6a6a05d2021-01-12 01:40:0751 // IsUrlPotentiallyTrustworthy assertions test for https://crbug.com/734581.
52 constexpr const char* kChromeLayerUrlsRegisteredAsSecure[] = {
53 // The schemes below are registered both as secure and no-access. Product
54 // code needs to treat such URLs as trustworthy, even though no-access
55 // schemes translate into an opaque origin (which is untrustworthy).
56 "chrome-native://newtab/",
57 "chrome-error://foo/",
58 // The schemes below are registered as secure (but not as no-access).
59 "chrome://foo/",
60 "chrome-untrusted://foo/",
61 "chrome-search://foo/",
Christian Flach247a7d842022-07-20 10:46:5162 "isolated-app://foo/",
Lukasz Anforowicz6a6a05d2021-01-12 01:40:0763#if BUILDFLAG(ENABLE_EXTENSIONS)
64 "chrome-extension://foo/",
65#endif
66 "devtools://foo/",
67 };
68 for (const std::string& str : kChromeLayerUrlsRegisteredAsSecure) {
69 SCOPED_TRACE(str);
70 GURL url(str);
71 EXPECT_TRUE(base::Contains(url::GetSecureSchemes(), url.scheme()));
72 EXPECT_TRUE(network::IsUrlPotentiallyTrustworthy(url));
73 }
Matt Falkenhagenea306042018-11-29 00:45:2574
Matt Siembor8db9de02019-05-23 04:26:5875 GURL chrome_url(content::GetWebUIURL("dummyurl"));
Frédéric Wang073e74a2020-12-16 17:43:3276 EXPECT_TRUE(network::IsUrlPotentiallyTrustworthy(chrome_url));
Dominic Schulzde441ff2022-11-18 03:28:0677 EXPECT_TRUE(content::OriginCanAccessServiceWorkers(chrome_url));
Matt Falkenhagenea306042018-11-29 00:45:2578 EXPECT_TRUE(
Frédéric Wang86b11be2020-12-04 16:27:5379 network::IsOriginPotentiallyTrustworthy(url::Origin::Create(chrome_url)));
tyoshino11a7c9fe2015-08-19 08:51:4680}
81
Jason Chaseb8287fc2017-05-17 15:02:2582class OriginTrialInitializationTestThread
83 : public base::PlatformThread::Delegate {
84 public:
85 explicit OriginTrialInitializationTestThread(
86 ChromeContentClient* chrome_client)
87 : chrome_client_(chrome_client) {}
88
Peter Boströmfadb1752021-09-30 19:17:0189 OriginTrialInitializationTestThread(
90 const OriginTrialInitializationTestThread&) = delete;
91 OriginTrialInitializationTestThread& operator=(
92 const OriginTrialInitializationTestThread&) = delete;
93
Jason Chaseb8287fc2017-05-17 15:02:2594 void ThreadMain() override { AccessPolicy(chrome_client_, &policy_objects_); }
95
96 // Static helper which can also be called from the main thread.
97 static void AccessPolicy(
98 ChromeContentClient* content_client,
Ali Hijazie63cbaf62023-12-20 19:29:3599 std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>*
100 policy_objects) {
Jason Chaseb8287fc2017-05-17 15:02:25101 // Repeatedly access the lazily-created origin trial policy
102 for (int i = 0; i < 20; i++) {
Lucas Furukawa Gadani6c24cdae2018-04-27 00:28:40103 blink::OriginTrialPolicy* policy = content_client->GetOriginTrialPolicy();
Jason Chaseb8287fc2017-05-17 15:02:25104 policy_objects->push_back(policy);
105 base::PlatformThread::YieldCurrentThread();
106 }
107 }
108
Ali Hijazie63cbaf62023-12-20 19:29:35109 const std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>*
110 policy_objects() const {
Jason Chaseb8287fc2017-05-17 15:02:25111 return &policy_objects_;
112 }
113
114 private:
Keishi Hattori0e45c022021-11-27 09:25:52115 raw_ptr<ChromeContentClient> chrome_client_;
Ali Hijazie63cbaf62023-12-20 19:29:35116 std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>
117 policy_objects_;
Jason Chaseb8287fc2017-05-17 15:02:25118};
119
120// Test that the lazy initialization of Origin Trial policy is resistant to
121// races with concurrent access. Failures (especially flaky) indicate that the
122// race prevention is no longer sufficient.
123TEST(ChromeContentClientTest, OriginTrialPolicyConcurrentInitialization) {
124 ChromeContentClient content_client;
Ali Hijazie63cbaf62023-12-20 19:29:35125 std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>
126 policy_objects;
Jason Chaseb8287fc2017-05-17 15:02:25127 OriginTrialInitializationTestThread thread(&content_client);
128 base::PlatformThreadHandle handle;
129
130 ASSERT_TRUE(base::PlatformThread::Create(0, &thread, &handle));
131
132 // Repeatedly access the lazily-created origin trial policy
133 OriginTrialInitializationTestThread::AccessPolicy(&content_client,
134 &policy_objects);
135
136 base::PlatformThread::Join(handle);
137
138 ASSERT_EQ(20UL, policy_objects.size());
139
Lucas Furukawa Gadani6c24cdae2018-04-27 00:28:40140 blink::OriginTrialPolicy* first_policy = policy_objects[0];
Jason Chaseb8287fc2017-05-17 15:02:25141
Ali Hijazie63cbaf62023-12-20 19:29:35142 const std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>*
143 all_policy_objects[] = {
144 &policy_objects,
145 thread.policy_objects(),
146 };
Jason Chaseb8287fc2017-05-17 15:02:25147
Ali Hijazie63cbaf62023-12-20 19:29:35148 for (const std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>*
149 thread_policy_objects : all_policy_objects) {
Jason Chaseb8287fc2017-05-17 15:02:25150 EXPECT_GE(20UL, thread_policy_objects->size());
Lucas Furukawa Gadani6c24cdae2018-04-27 00:28:40151 for (blink::OriginTrialPolicy* policy : *(thread_policy_objects)) {
Jason Chaseb8287fc2017-05-17 15:02:25152 EXPECT_EQ(first_policy, policy);
153 }
154 }
155}
156
[email protected]3e807c42012-08-28 23:25:58157} // namespace chrome_common