Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 3e807c4 | 2012-08-28 23:25:58 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tom Sepez | 8726d30e | 2025-01-29 02:11:08 | [diff] [blame] | 5 | #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] | 3e807c4 | 2012-08-28 23:25:58 | [diff] [blame] | 10 | #include "chrome/common/chrome_content_client.h" |
| 11 | |
thestig | c62f109 | 2016-07-15 21:02:37 | [diff] [blame] | 12 | #include <string> |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 13 | |
Lei Zhang | 435755f | 2021-05-11 20:58:55 | [diff] [blame] | 14 | #include "base/containers/contains.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 15 | #include "base/memory/raw_ptr.h" |
[email protected] | 1988e1c | 2013-02-28 20:27:42 | [diff] [blame] | 16 | #include "base/strings/string_split.h" |
thestig | c62f109 | 2016-07-15 21:02:37 | [diff] [blame] | 17 | #include "base/strings/string_util.h" |
| 18 | #include "base/test/scoped_command_line.h" |
Jason Chase | b8287fc | 2017-05-17 15:02:25 | [diff] [blame] | 19 | #include "base/threading/platform_thread.h" |
avi | 2729e44 | 2015-12-26 05:27:45 | [diff] [blame] | 20 | #include "build/build_config.h" |
[email protected] | 3e807c4 | 2012-08-28 23:25:58 | [diff] [blame] | 21 | #include "content/public/common/content_switches.h" |
Eric Lawrence | 46acd92 | 2017-07-14 00:55:20 | [diff] [blame] | 22 | #include "content/public/common/origin_util.h" |
Matt Siembor | 8db9de0 | 2019-05-23 04:26:58 | [diff] [blame] | 23 | #include "content/public/test/test_utils.h" |
Lukasz Anforowicz | 6a6a05d | 2021-01-12 01:40:07 | [diff] [blame] | 24 | #include "extensions/buildflags/buildflags.h" |
Scott Violet | 02e38b9 | 2018-03-27 23:42:14 | [diff] [blame] | 25 | #include "ppapi/buildflags/buildflags.h" |
Frédéric Wang | 86b11be | 2020-12-04 16:27:53 | [diff] [blame] | 26 | #include "services/network/public/cpp/is_potentially_trustworthy.h" |
[email protected] | 3e807c4 | 2012-08-28 23:25:58 | [diff] [blame] | 27 | #include "testing/gtest/include/gtest/gtest.h" |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 28 | #include "url/gurl.h" |
| 29 | #include "url/origin.h" |
| 30 | #include "url/url_util.h" |
[email protected] | 3e807c4 | 2012-08-28 23:25:58 | [diff] [blame] | 31 | |
Devlin Cronin | 3eb0e96 | 2022-02-03 22:01:32 | [diff] [blame] | 32 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 33 | #include "extensions/common/constants.h" |
| 34 | #endif |
| 35 | |
[email protected] | 3e807c4 | 2012-08-28 23:25:58 | [diff] [blame] | 36 | namespace chrome_common { |
| 37 | |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 38 | TEST(ChromeContentClientTest, AdditionalSchemes) { |
Devlin Cronin | 3eb0e96 | 2022-02-03 22:01:32 | [diff] [blame] | 39 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 40 | 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 Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 46 | url::Origin origin = url::Origin::Create(extension_url); |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 47 | EXPECT_EQ("chrome-extension://abcdefghijklmnopqrstuvwxyzabcdef", |
| 48 | origin.Serialize()); |
Devlin Cronin | 3eb0e96 | 2022-02-03 22:01:32 | [diff] [blame] | 49 | #endif |
Eric Lawrence | 46acd92 | 2017-07-14 00:55:20 | [diff] [blame] | 50 | |
Lukasz Anforowicz | 6a6a05d | 2021-01-12 01:40:07 | [diff] [blame] | 51 | // 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 Flach | 247a7d84 | 2022-07-20 10:46:51 | [diff] [blame] | 62 | "isolated-app://foo/", |
Lukasz Anforowicz | 6a6a05d | 2021-01-12 01:40:07 | [diff] [blame] | 63 | #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 Falkenhagen | ea30604 | 2018-11-29 00:45:25 | [diff] [blame] | 74 | |
Matt Siembor | 8db9de0 | 2019-05-23 04:26:58 | [diff] [blame] | 75 | GURL chrome_url(content::GetWebUIURL("dummyurl")); |
Frédéric Wang | 073e74a | 2020-12-16 17:43:32 | [diff] [blame] | 76 | EXPECT_TRUE(network::IsUrlPotentiallyTrustworthy(chrome_url)); |
Dominic Schulz | de441ff | 2022-11-18 03:28:06 | [diff] [blame] | 77 | EXPECT_TRUE(content::OriginCanAccessServiceWorkers(chrome_url)); |
Matt Falkenhagen | ea30604 | 2018-11-29 00:45:25 | [diff] [blame] | 78 | EXPECT_TRUE( |
Frédéric Wang | 86b11be | 2020-12-04 16:27:53 | [diff] [blame] | 79 | network::IsOriginPotentiallyTrustworthy(url::Origin::Create(chrome_url))); |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 80 | } |
| 81 | |
Jason Chase | b8287fc | 2017-05-17 15:02:25 | [diff] [blame] | 82 | class OriginTrialInitializationTestThread |
| 83 | : public base::PlatformThread::Delegate { |
| 84 | public: |
| 85 | explicit OriginTrialInitializationTestThread( |
| 86 | ChromeContentClient* chrome_client) |
| 87 | : chrome_client_(chrome_client) {} |
| 88 | |
Peter Boström | fadb175 | 2021-09-30 19:17:01 | [diff] [blame] | 89 | OriginTrialInitializationTestThread( |
| 90 | const OriginTrialInitializationTestThread&) = delete; |
| 91 | OriginTrialInitializationTestThread& operator=( |
| 92 | const OriginTrialInitializationTestThread&) = delete; |
| 93 | |
Jason Chase | b8287fc | 2017-05-17 15:02:25 | [diff] [blame] | 94 | 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 Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 99 | std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>* |
| 100 | policy_objects) { |
Jason Chase | b8287fc | 2017-05-17 15:02:25 | [diff] [blame] | 101 | // Repeatedly access the lazily-created origin trial policy |
| 102 | for (int i = 0; i < 20; i++) { |
Lucas Furukawa Gadani | 6c24cdae | 2018-04-27 00:28:40 | [diff] [blame] | 103 | blink::OriginTrialPolicy* policy = content_client->GetOriginTrialPolicy(); |
Jason Chase | b8287fc | 2017-05-17 15:02:25 | [diff] [blame] | 104 | policy_objects->push_back(policy); |
| 105 | base::PlatformThread::YieldCurrentThread(); |
| 106 | } |
| 107 | } |
| 108 | |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 109 | const std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>* |
| 110 | policy_objects() const { |
Jason Chase | b8287fc | 2017-05-17 15:02:25 | [diff] [blame] | 111 | return &policy_objects_; |
| 112 | } |
| 113 | |
| 114 | private: |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 115 | raw_ptr<ChromeContentClient> chrome_client_; |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 116 | std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>> |
| 117 | policy_objects_; |
Jason Chase | b8287fc | 2017-05-17 15:02:25 | [diff] [blame] | 118 | }; |
| 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. |
| 123 | TEST(ChromeContentClientTest, OriginTrialPolicyConcurrentInitialization) { |
| 124 | ChromeContentClient content_client; |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 125 | std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>> |
| 126 | policy_objects; |
Jason Chase | b8287fc | 2017-05-17 15:02:25 | [diff] [blame] | 127 | 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 Gadani | 6c24cdae | 2018-04-27 00:28:40 | [diff] [blame] | 140 | blink::OriginTrialPolicy* first_policy = policy_objects[0]; |
Jason Chase | b8287fc | 2017-05-17 15:02:25 | [diff] [blame] | 141 | |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 142 | const std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>* |
| 143 | all_policy_objects[] = { |
| 144 | &policy_objects, |
| 145 | thread.policy_objects(), |
| 146 | }; |
Jason Chase | b8287fc | 2017-05-17 15:02:25 | [diff] [blame] | 147 | |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 148 | for (const std::vector<raw_ptr<blink::OriginTrialPolicy, VectorExperimental>>* |
| 149 | thread_policy_objects : all_policy_objects) { |
Jason Chase | b8287fc | 2017-05-17 15:02:25 | [diff] [blame] | 150 | EXPECT_GE(20UL, thread_policy_objects->size()); |
Lucas Furukawa Gadani | 6c24cdae | 2018-04-27 00:28:40 | [diff] [blame] | 151 | for (blink::OriginTrialPolicy* policy : *(thread_policy_objects)) { |
Jason Chase | b8287fc | 2017-05-17 15:02:25 | [diff] [blame] | 152 | EXPECT_EQ(first_policy, policy); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
[email protected] | 3e807c4 | 2012-08-28 23:25:58 | [diff] [blame] | 157 | } // namespace chrome_common |