1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/worker_host/shared_worker_host.h"
#include <memory>
#include <string>
#include <utility>
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/unguessable_token.h"
#include "content/browser/navigation_subresource_loader_params.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/service_worker/embedded_worker_test_helper.h"
#include "content/browser/service_worker/service_worker_main_resource_handle.h"
#include "content/browser/site_instance_impl.h"
#include "content/browser/worker_host/mock_shared_worker.h"
#include "content/browser/worker_host/shared_worker_connector_impl.h"
#include "content/browser/worker_host/shared_worker_service_impl.h"
#include "content/browser/worker_host/worker_script_fetcher.h"
#include "content/public/browser/shared_worker_instance.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_utils.h"
#include "content/test/test_content_browser_client.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "mojo/public/cpp/test_support/test_utils.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "services/network/public/cpp/cross_origin_embedder_policy.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/not_implemented_url_loader_factory.h"
#include "services/network/public/mojom/parsed_headers.mojom.h"
#include "services/network/test/client_security_state_builder.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/messaging/message_port_channel.h"
#include "third_party/blink/public/common/messaging/message_port_descriptor.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "url/origin.h"
using blink::MessagePortChannel;
namespace content {
namespace {
const ukm::SourceId kClientUkmSourceId = 12345;
} // namespace
class SharedWorkerHostTest : public testing::Test {
public:
const GURL kSiteUrl{"http://www.example.com/"};
const GURL kWorkerUrl{"http://www.example.com/w.js"};
void SetUp() override {
helper_ = std::make_unique<EmbeddedWorkerTestHelper>(base::FilePath());
RenderProcessHostImpl::set_render_process_host_factory_for_testing(
&mock_render_process_host_factory_);
site_instance_ =
SiteInstanceImpl::CreateForTesting(&browser_context_, kWorkerUrl);
RenderProcessHost* rph = site_instance_->GetOrCreateProcess();
std::vector<std::unique_ptr<MockRenderProcessHost>>* processes =
mock_render_process_host_factory_.GetProcesses();
ASSERT_EQ(processes->size(), 1u);
mock_render_process_host_ = processes->at(0).get();
ASSERT_EQ(rph, mock_render_process_host_);
ASSERT_TRUE(mock_render_process_host_->Init());
}
SharedWorkerHostTest()
: service_(nullptr /* storage_partition */,
nullptr /* service_worker_context */) {}
SharedWorkerHostTest(const SharedWorkerHostTest&) = delete;
SharedWorkerHostTest& operator=(const SharedWorkerHostTest&) = delete;
base::WeakPtr<SharedWorkerHost> CreateHost() {
return CreateHostWithExtendedLifetime(/*extended_lifetime=*/false);
}
base::WeakPtr<SharedWorkerHost> CreateHostWithExtendedLifetime(
bool extended_lifetime) {
SharedWorkerInstance instance(
kWorkerUrl, blink::mojom::ScriptType::kClassic,
network::mojom::CredentialsMode::kSameOrigin, "name",
blink::StorageKey::CreateFirstParty(url::Origin::Create(kWorkerUrl)),
blink::mojom::SharedWorkerCreationContextType::kSecure,
blink::mojom::SharedWorkerSameSiteCookies::kAll, extended_lifetime);
auto host = std::make_unique<SharedWorkerHost>(
&service_, instance, site_instance_,
std::vector<network::mojom::ContentSecurityPolicyPtr>(),
base::MakeRefCounted<PolicyContainerHost>());
auto weak_host = host->AsWeakPtr();
service_.worker_hosts_.insert(std::move(host));
return weak_host;
}
void StartWorker(
SharedWorkerHost* host,
mojo::PendingRemote<blink::mojom::SharedWorkerFactory> factory,
const GURL& final_response_url = GURL(),
network::mojom::URLResponseHeadPtr response_head =
network::mojom::URLResponseHead::New()) {
auto main_script_load_params =
blink::mojom::WorkerMainScriptLoadParams::New();
if (!response_head->parsed_headers) {
response_head->parsed_headers = network::mojom::ParsedHeaders::New();
}
main_script_load_params->response_head = std::move(response_head);
mojo::ScopedDataPipeProducerHandle producer_handle;
mojo::ScopedDataPipeConsumerHandle consumer_handle;
MojoResult rv =
mojo::CreateDataPipe(nullptr, producer_handle, consumer_handle);
ASSERT_EQ(MOJO_RESULT_OK, rv);
main_script_load_params->response_body = std::move(consumer_handle);
auto subresource_loader_factories =
std::make_unique<blink::PendingURLLoaderFactoryBundle>();
SubresourceLoaderParams subresource_loader_params =
SubresourceLoaderParams();
mojo::PendingRemote<network::mojom::URLLoaderFactory>
loader_factory_remote =
network::NotImplementedURLLoaderFactory::Create();
// Set up for service worker.
auto service_worker_handle =
std::make_unique<ServiceWorkerMainResourceHandle>(
helper_->context_wrapper(), base::DoNothing(),
/*fetch_event_client_id=*/"");
service_worker_handle->set_service_worker_client(
helper_->context()
->service_worker_client_owner()
.CreateServiceWorkerClientForWorker(
mock_render_process_host_->GetDeprecatedID(),
ServiceWorkerClientInfo(host->token())),
net::IsolationInfo());
host->SetServiceWorkerHandle(std::move(service_worker_handle));
TestContentBrowserClient client;
host->Start(std::move(factory),
blink::mojom::FetchClientSettingsObject::New(
network::mojom::ReferrerPolicy::kDefault,
/*outgoing_referrer=*/GURL(),
blink::mojom::InsecureRequestsPolicy::kDoNotUpgrade),
&client,
WorkerScriptFetcherResult(
std::move(subresource_loader_factories),
std::move(main_script_load_params),
PolicyContainerPolicies(), final_response_url));
}
MessagePortChannel AddClient(
SharedWorkerHost* host,
mojo::PendingRemote<blink::mojom::SharedWorkerClient> client) {
GlobalRenderFrameHostId dummy_render_frame_host_id(
mock_render_process_host_->GetDeprecatedID(), 22);
blink::MessagePortDescriptorPair port_pair;
MessagePortChannel local_port(port_pair.TakePort0());
MessagePortChannel remote_port(port_pair.TakePort1());
host->AddClient(std::move(client), dummy_render_frame_host_id,
std::move(remote_port), kClientUkmSourceId);
return local_port;
}
void RunNormalTest(bool extended_lifetime) {
base::WeakPtr<SharedWorkerHost> host = CreateHostWithExtendedLifetime(
/*extended_lifetime=*/extended_lifetime);
// Start the worker.
mojo::PendingRemote<blink::mojom::SharedWorkerFactory> factory;
MockSharedWorkerFactory factory_impl(
factory.InitWithNewPipeAndPassReceiver());
StartWorker(host.get(), std::move(factory));
// Add the initiating client.
MockSharedWorkerClient client;
mojo::PendingRemote<blink::mojom::SharedWorkerClient> remote_client;
client.Bind(remote_client.InitWithNewPipeAndPassReceiver());
MessagePortChannel local_port =
AddClient(host.get(), std::move(remote_client));
base::RunLoop().RunUntilIdle();
// The factory should have gotten the CreateSharedWorker message.
mojo::Remote<blink::mojom::SharedWorkerHost> worker_host;
mojo::PendingReceiver<blink::mojom::SharedWorker> worker_receiver;
EXPECT_TRUE(factory_impl.CheckReceivedCreateSharedWorker(
host->instance().url(), host->instance().name(),
host->content_security_policies(), &worker_host, &worker_receiver));
{
MockSharedWorker worker(std::move(worker_receiver));
base::RunLoop().RunUntilIdle();
// The worker and client should have gotten initial messages.
int connection_request_id;
MessagePortChannel port;
EXPECT_TRUE(worker.CheckReceivedConnect(&connection_request_id, &port));
EXPECT_TRUE(client.CheckReceivedOnCreated());
// Simulate events the shared worker would send.
// Create message pipes. We may need to keep |devtools_agent_receiver| and
// |devtools_agent_host_remote| if we want not to invoke connection error
// handlers.
mojo::PendingRemote<blink::mojom::DevToolsAgent> devtools_agent_remote;
mojo::PendingReceiver<blink::mojom::DevToolsAgent>
devtools_agent_receiver =
devtools_agent_remote.InitWithNewPipeAndPassReceiver();
mojo::PendingRemote<blink::mojom::DevToolsAgentHost>
devtools_agent_host_remote;
worker_host->OnReadyForInspection(
std::move(devtools_agent_remote),
devtools_agent_host_remote.InitWithNewPipeAndPassReceiver());
worker_host->OnConnected(connection_request_id);
base::RunLoop().RunUntilIdle();
// The client should be connected.
EXPECT_TRUE(client.CheckReceivedOnConnected({
blink::mojom::WebFeature::kCoepNoneSharedWorker,
}));
client.Close();
base::RunLoop().RunUntilIdle();
if (extended_lifetime) {
// Close the client. The host should detect that there are no clients
// left but the worker keeps on running due to the extended_lifetime`
// option.
EXPECT_FALSE(worker.CheckReceivedTerminate());
} else {
// Close the client. The host should detect that there are no clients
// left and ask the worker to terminate.
EXPECT_TRUE(worker.CheckReceivedTerminate());
}
// Simulate the worker terminating by breaking the Mojo connection when
// |worker| goes out of scope. `OnWorkerConnectionLost()` should be
// kicked.
}
base::RunLoop().RunUntilIdle();
// The host should have self-destructed.
EXPECT_FALSE(host);
}
protected:
BrowserTaskEnvironment task_environment_;
TestBrowserContext browser_context_;
MockRenderProcessHostFactory mock_render_process_host_factory_;
raw_ptr<MockRenderProcessHost> mock_render_process_host_;
std::unique_ptr<EmbeddedWorkerTestHelper> helper_;
scoped_refptr<SiteInstanceImpl> site_instance_;
SharedWorkerServiceImpl service_;
};
TEST_F(SharedWorkerHostTest, Normal) {
RunNormalTest(/*extended_lifetime=*/false);
}
TEST_F(SharedWorkerHostTest, NormalWithExtendedLifetime) {
RunNormalTest(/*extended_lifetime=*/true);
}
TEST_F(SharedWorkerHostTest, TerminateBeforeStarting) {
base::WeakPtr<SharedWorkerHost> host = CreateHost();
// Add a client.
MockSharedWorkerClient client;
mojo::PendingRemote<blink::mojom::SharedWorkerClient> remote_client;
client.Bind(remote_client.InitWithNewPipeAndPassReceiver());
MessagePortChannel local_port =
AddClient(host.get(), std::move(remote_client));
base::RunLoop().RunUntilIdle();
// Terminate the host before starting.
service_.DestroyHost(host.get());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(host);
// The client should be told startup failed.
EXPECT_TRUE(client.CheckReceivedOnScriptLoadFailed());
}
TEST_F(SharedWorkerHostTest, TerminateAfterStarting) {
base::WeakPtr<SharedWorkerHost> host = CreateHost();
// Create the factory.
mojo::PendingRemote<blink::mojom::SharedWorkerFactory> factory;
MockSharedWorkerFactory factory_impl(
factory.InitWithNewPipeAndPassReceiver());
// Start the worker.
StartWorker(host.get(), std::move(factory));
// Add a client.
MockSharedWorkerClient client;
mojo::PendingRemote<blink::mojom::SharedWorkerClient> remote_client;
client.Bind(remote_client.InitWithNewPipeAndPassReceiver());
MessagePortChannel local_port =
AddClient(host.get(), std::move(remote_client));
base::RunLoop().RunUntilIdle();
{
mojo::Remote<blink::mojom::SharedWorkerHost> worker_host;
mojo::PendingReceiver<blink::mojom::SharedWorker> worker_receiver;
EXPECT_TRUE(factory_impl.CheckReceivedCreateSharedWorker(
host->instance().url(), host->instance().name(),
host->content_security_policies(), &worker_host, &worker_receiver));
MockSharedWorker worker(std::move(worker_receiver));
// Terminate after starting.
service_.DestroyHost(host.get());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(worker.CheckReceivedTerminate());
// We simulate the worker terminating by breaking the Mojo connection when
// it goes out of scope.
}
// Simulate the worker in the renderer terminating.
base::RunLoop().RunUntilIdle();
// The client should not have been told startup failed.
EXPECT_FALSE(client.CheckReceivedOnScriptLoadFailed());
// The host should no longer exist.
EXPECT_FALSE(host);
}
TEST_F(SharedWorkerHostTest, OnContextClosed) {
base::WeakPtr<SharedWorkerHost> host = CreateHost();
// Start the worker.
mojo::PendingRemote<blink::mojom::SharedWorkerFactory> factory;
MockSharedWorkerFactory factory_impl(
factory.InitWithNewPipeAndPassReceiver());
StartWorker(host.get(), std::move(factory));
// Add a client.
MockSharedWorkerClient client;
mojo::PendingRemote<blink::mojom::SharedWorkerClient> remote_client;
client.Bind(remote_client.InitWithNewPipeAndPassReceiver());
MessagePortChannel local_port =
AddClient(host.get(), std::move(remote_client));
base::RunLoop().RunUntilIdle();
{
mojo::Remote<blink::mojom::SharedWorkerHost> worker_host;
mojo::PendingReceiver<blink::mojom::SharedWorker> worker_receiver;
EXPECT_TRUE(factory_impl.CheckReceivedCreateSharedWorker(
host->instance().url(), host->instance().name(),
host->content_security_policies(), &worker_host, &worker_receiver));
MockSharedWorker worker(std::move(worker_receiver));
// Simulate the worker calling OnContextClosed().
worker_host->OnContextClosed();
base::RunLoop().RunUntilIdle();
// Close the client. The host should detect that there are no clients left
// and terminate the worker.
client.Close();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(worker.CheckReceivedTerminate());
// Simulate the worker terminating by breaking the Mojo connection when
// |worker| goes out of scope.
}
base::RunLoop().RunUntilIdle();
// The host should no longer exist.
EXPECT_FALSE(host);
}
TEST_F(SharedWorkerHostTest, CreateNetworkFactoryParamsForSubresources) {
base::WeakPtr<SharedWorkerHost> host = CreateHost();
// Start the worker.
mojo::PendingRemote<blink::mojom::SharedWorkerFactory> factory;
MockSharedWorkerFactory factory_impl(
factory.InitWithNewPipeAndPassReceiver());
StartWorker(host.get(), std::move(factory));
network::mojom::URLLoaderFactoryParamsPtr params =
host->CreateNetworkFactoryParamsForSubresources();
EXPECT_EQ(host->GetStorageKey().origin(),
params->isolation_info.frame_origin());
EXPECT_FALSE(params->isolation_info.nonce().has_value());
}
TEST_F(SharedWorkerHostTest,
CreateNetworkFactoryParamsForSubresourcesWithNonce) {
base::UnguessableToken nonce = base::UnguessableToken::Create();
SharedWorkerInstance instance(
kWorkerUrl, blink::mojom::ScriptType::kClassic,
network::mojom::CredentialsMode::kSameOrigin, "name",
blink::StorageKey::CreateWithNonce(url::Origin::Create(kWorkerUrl),
nonce),
blink::mojom::SharedWorkerCreationContextType::kSecure,
blink::mojom::SharedWorkerSameSiteCookies::kNone,
/*extended_lifetime=*/false);
auto host = std::make_unique<SharedWorkerHost>(
&service_, instance, site_instance_,
std::vector<network::mojom::ContentSecurityPolicyPtr>(),
base::MakeRefCounted<PolicyContainerHost>());
// Start the worker.
mojo::PendingRemote<blink::mojom::SharedWorkerFactory> factory;
MockSharedWorkerFactory factory_impl(
factory.InitWithNewPipeAndPassReceiver());
StartWorker(host.get(), std::move(factory));
network::mojom::URLLoaderFactoryParamsPtr params =
host->CreateNetworkFactoryParamsForSubresources();
EXPECT_EQ(url::Origin::Create(kWorkerUrl),
params->isolation_info.frame_origin());
EXPECT_THAT(params->isolation_info.nonce(), testing::Optional(nonce));
}
// Enable PrivateNetworkAccessForWorkers.
class SharedWorkerHostTestWithPNAEnabled : public SharedWorkerHostTest {
public:
SharedWorkerHostTestWithPNAEnabled() {
feature_list_.InitWithFeatures(
{
features::kPrivateNetworkAccessSendPreflights,
features::kPrivateNetworkAccessForWorkers,
},
{});
}
~SharedWorkerHostTestWithPNAEnabled() override = default;
private:
base::test::ScopedFeatureList feature_list_;
};
TEST_F(SharedWorkerHostTestWithPNAEnabled,
CreateNetworkFactoryParamsForSubresources) {
SharedWorkerInstance instance(
kWorkerUrl, blink::mojom::ScriptType::kClassic,
network::mojom::CredentialsMode::kSameOrigin, "name",
blink::StorageKey::CreateFirstParty(url::Origin::Create(kWorkerUrl)),
blink::mojom::SharedWorkerCreationContextType::kSecure,
blink::mojom::SharedWorkerSameSiteCookies::kAll,
/*extended_lifetime=*/false);
PolicyContainerPolicies policies;
policies.cross_origin_embedder_policy.value =
network::mojom::CrossOriginEmbedderPolicyValue::kRequireCorp;
policies.ip_address_space = network::mojom::IPAddressSpace::kPublic;
policies.is_web_secure_context = true;
network::CrossOriginEmbedderPolicy worker_cross_origin_embedder_policy;
worker_cross_origin_embedder_policy.value =
network::mojom::CrossOriginEmbedderPolicyValue::kCredentialless;
network::mojom::URLResponseHeadPtr response_head =
network::mojom::URLResponseHead::New();
response_head->parsed_headers = network::mojom::ParsedHeaders::New();
response_head->parsed_headers->cross_origin_embedder_policy =
worker_cross_origin_embedder_policy;
auto host = std::make_unique<SharedWorkerHost>(
&service_, instance, site_instance_,
std::vector<network::mojom::ContentSecurityPolicyPtr>(),
base::MakeRefCounted<PolicyContainerHost>(std::move(policies)));
// Start the worker.
mojo::PendingRemote<blink::mojom::SharedWorkerFactory> factory;
MockSharedWorkerFactory factory_impl(
factory.InitWithNewPipeAndPassReceiver());
StartWorker(host.get(), std::move(factory), GURL("devtools://test.url"),
std::move(response_head));
network::mojom::URLLoaderFactoryParamsPtr params =
host->CreateNetworkFactoryParamsForSubresources();
ASSERT_TRUE(params->client_security_state);
EXPECT_TRUE(params->client_security_state->is_web_secure_context);
EXPECT_EQ(params->client_security_state->ip_address_space,
network::mojom::IPAddressSpace::kLocal);
EXPECT_EQ(params->client_security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kPreflightWarn);
EXPECT_EQ(params->client_security_state->cross_origin_embedder_policy.value,
network::mojom::CrossOriginEmbedderPolicyValue::kCredentialless);
}
} // namespace content
|