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
content / browser / worker_network_isolation_key_browsertest.cc [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "content/public/common/content_paths.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/url_loader_interceptor.h"
#include "content/shell/browser/shell.h"
#include "net/base/features.h"
#include "net/dns/mock_host_resolver.h"
#include "services/network/public/cpp/features.h"
namespace content {
namespace {
bool SupportsSharedWorker() {
#if BUILDFLAG(IS_ANDROID)
// SharedWorkers are not enabled on Android. https://crbug.com/154571
return false;
#else
return true;
#endif
}
} // namespace
enum class WorkerType {
kServiceWorker,
kSharedWorker,
};
class WorkerNetworkIsolationKeyBrowserTest : public ContentBrowserTest {
public:
WorkerNetworkIsolationKeyBrowserTest() {
feature_list_.InitAndEnableFeature(
net::features::kSplitCacheByNetworkIsolationKey);
}
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
https_server_ = std::make_unique<net::EmbeddedTestServer>(
net::EmbeddedTestServer::TYPE_HTTPS);
https_server_->SetSSLConfig(net::EmbeddedTestServer::CERT_TEST_NAMES);
https_server_->AddDefaultHandlers(GetTestDataFilePath());
content::SetupCrossSiteRedirector(https_server_.get());
ASSERT_TRUE(https_server_->Start());
}
net::EmbeddedTestServer* https_server() { return https_server_.get(); }
// Register a service/shared worker |main_script_file| in the scope of
// |subframe_rfh|'s origin.
void RegisterWorker(RenderFrameHost* subframe_rfh,
WorkerType worker_type,
const std::string& main_script_file) {
RegisterWorkerWithUrlParameters(subframe_rfh, worker_type, main_script_file,
{});
}
// Register a service/shared worker |main_script_file| in the scope of
// |subframe_rfh|'s origin, that does
// importScripts(|import_script_url|) and fetch(|fetch_url|).
void RegisterWorkerThatDoesImportScriptsAndFetch(
RenderFrameHost* subframe_rfh,
WorkerType worker_type,
const std::string& main_script_file,
const GURL& import_script_url,
const GURL& fetch_url) {
RegisterWorkerWithUrlParameters(
subframe_rfh, worker_type, main_script_file,
{{"import_script_url", import_script_url.spec()},
{"fetch_url", fetch_url.spec()}});
}
RenderFrameHost* CreateSubframe(const GURL& subframe_url) {
DCHECK_EQ(shell()->web_contents()->GetLastCommittedURL().path(),
"/workers/frame_factory.html");
content::TestNavigationObserver navigation_observer(
shell()->web_contents(), /*number_of_navigations*/ 1,
content::MessageLoopRunner::QuitMode::DEFERRED);
std::string subframe_name = GetUniqueSubframeName();
EvalJsResult result = EvalJs(
shell()->web_contents()->GetPrimaryMainFrame(),
JsReplace("createFrame($1, $2)", subframe_url.spec(), subframe_name));
DCHECK(result.error.empty());
navigation_observer.Wait();
RenderFrameHost* subframe_rfh = FrameMatchingPredicate(
shell()->web_contents()->GetPrimaryPage(),
base::BindRepeating(&FrameMatchesName, subframe_name));
DCHECK(subframe_rfh);
return subframe_rfh;
}
private:
void RegisterWorkerWithUrlParameters(
RenderFrameHost* subframe_rfh,
WorkerType worker_type,
const std::string& main_script_file,
const std::map<std::string, std::string>& params) {
std::string main_script_file_with_param(main_script_file);
for (auto it = params.begin(); it != params.end(); ++it) {
main_script_file_with_param += base::StrCat(
{(it == params.begin()) ? "?" : "&", it->first, "=", it->second});
}
switch (worker_type) {
case WorkerType::kServiceWorker:
DCHECK(subframe_rfh->GetLastCommittedURL().path() ==
"/workers/service_worker_setup.html");
EXPECT_EQ("ok",
EvalJs(subframe_rfh,
JsReplace("setup($1,$2)", main_script_file_with_param,
"{\"updateViaCache\": \"all\"}")));
break;
case WorkerType::kSharedWorker:
EXPECT_EQ(nullptr, EvalJs(subframe_rfh,
JsReplace("let worker = new SharedWorker($1)",
main_script_file_with_param)));
break;
}
}
std::string GetUniqueSubframeName() {
subframe_id_ += 1;
return "subframe_name_" + base::NumberToString(subframe_id_);
}
size_t subframe_id_ = 0;
base::test::ScopedFeatureList feature_list_;
std::unique_ptr<net::EmbeddedTestServer> https_server_;
};
class WorkerImportScriptsAndFetchRequestNetworkIsolationKeyBrowserTest
: public WorkerNetworkIsolationKeyBrowserTest,
public ::testing::WithParamInterface<
std::tuple<bool /* test_same_network_isolation_key */, WorkerType>> {
};
// Test that network isolation key is filled in correctly for service/shared
// workers. The test navigates to "a.test" and creates two cross-origin iframes
// that each start a worker. The frames/workers may have the same origin, so
// worker1 is on "b.test" and worker2 is on either "b.test" or "c.test". The
// test checks the cache status of importScripts() and a fetch() request from
// the workers to another origin "d.test". When the workers had the same origin
// (the same network isolation key), we expect the second importScripts() and
// fetch() request to exist in the cache. When the origins are different, we
// expect the second requests to not exist in the cache.
IN_PROC_BROWSER_TEST_P(
WorkerImportScriptsAndFetchRequestNetworkIsolationKeyBrowserTest,
ImportScriptsAndFetchRequest) {
bool test_same_network_isolation_key;
WorkerType worker_type;
std::tie(test_same_network_isolation_key, worker_type) = GetParam();
if (worker_type == WorkerType::kSharedWorker && !SupportsSharedWorker())
return;
GURL import_script_url =
https_server()->GetURL("d.test", "/workers/empty.js");
GURL fetch_url = https_server()->GetURL("d.test", "/workers/empty.html");
std::map<GURL, size_t> request_completed_count;
base::RunLoop cache_status_waiter;
URLLoaderInterceptor interceptor(
base::BindLambdaForTesting(
[&](URLLoaderInterceptor::RequestParams* params) { return false; }),
base::BindLambdaForTesting(
[&](const GURL& request_url,
const network::URLLoaderCompletionStatus& status) {
if (request_url == import_script_url || request_url == fetch_url) {
size_t& num_completed = request_completed_count[request_url];
num_completed += 1;
if (num_completed == 1) {
EXPECT_FALSE(status.exists_in_cache);
} else if (num_completed == 2) {
EXPECT_EQ(status.exists_in_cache,
test_same_network_isolation_key);
} else {
NOTREACHED_IN_MIGRATION();
}
}
if (request_completed_count[import_script_url] == 2 &&
request_completed_count[fetch_url] == 2) {
cache_status_waiter.Quit();
}
}),
{});
NavigateToURLBlockUntilNavigationsComplete(
shell(), https_server()->GetURL("a.test", "/workers/frame_factory.html"),
1);
RenderFrameHost* subframe_rfh_1 = CreateSubframe(
https_server()->GetURL("b.test", "/workers/service_worker_setup.html"));
RegisterWorkerThatDoesImportScriptsAndFetch(subframe_rfh_1, worker_type,
"worker_with_import_and_fetch.js",
import_script_url, fetch_url);
RenderFrameHost* subframe_rfh_2 = CreateSubframe(https_server()->GetURL(
test_same_network_isolation_key ? "b.test" : "c.test",
"/workers/service_worker_setup.html"));
RegisterWorkerThatDoesImportScriptsAndFetch(
subframe_rfh_2, worker_type, "worker_with_import_and_fetch_2.js",
import_script_url, fetch_url);
cache_status_waiter.Run();
}
INSTANTIATE_TEST_SUITE_P(
All,
WorkerImportScriptsAndFetchRequestNetworkIsolationKeyBrowserTest,
::testing::Combine(testing::Bool(),
::testing::Values(WorkerType::kServiceWorker,
WorkerType::kSharedWorker)));
class ServiceWorkerMainScriptRequestNetworkIsolationKeyBrowserTest
: public WorkerNetworkIsolationKeyBrowserTest {
public:
ServiceWorkerMainScriptRequestNetworkIsolationKeyBrowserTest() {
// TODO(crbug.com/40053828): Tests under this class fail when
// kThirdPartyStoragePartitioning is enabled.
feature_list_.InitAndDisableFeature(
net::features::kThirdPartyStoragePartitioning);
}
private:
base::test::ScopedFeatureList feature_list_;
};
// Test that network isolation key is filled in correctly for service worker's
// main script request. The test navigates to "a.test" and creates an iframe
// having origin "c.test" that registers |worker1|. The test then navigates to
// "b.test" and creates an iframe also having origin "c.test". We now want to
// test a second register request for |worker1| but just calling register()
// would be a no-op since |worker1| is already the current worker. So we
// register a new |worker2| and then |worker1| again.
//
// Note that the second navigation to "c.test" also triggers an update check for
// |worker1|. We expect both the second register request for |worker1| and this
// update request to exist in the cache.
//
// Note that it's sufficient not to test the cache miss when subframe origins
// are different as in that case the two script urls must be different and it
// also won't trigger an update.
//
// TODO(crbug.com/40053828): Update test to not depend on
// kThirdPartyStoragePartitioning being disabled.
IN_PROC_BROWSER_TEST_F(
ServiceWorkerMainScriptRequestNetworkIsolationKeyBrowserTest,
ServiceWorkerMainScriptRequest) {
size_t num_completed = 0;
std::string main_script_file = "empty.js";
GURL main_script_request_url =
https_server()->GetURL("c.test", "/workers/" + main_script_file);
base::RunLoop cache_status_waiter;
URLLoaderInterceptor interceptor(
base::BindLambdaForTesting(
[&](URLLoaderInterceptor::RequestParams* params) { return false; }),
base::BindLambdaForTesting(
[&](const GURL& request_url,
const network::URLLoaderCompletionStatus& status) {
if (request_url == main_script_request_url) {
num_completed += 1;
if (num_completed == 1) {
EXPECT_FALSE(status.exists_in_cache);
} else if (num_completed == 2) {
EXPECT_TRUE(status.exists_in_cache);
} else if (num_completed == 3) {
EXPECT_TRUE(status.exists_in_cache);
cache_status_waiter.Quit();
} else {
NOTREACHED_IN_MIGRATION();
}
}
}),
{});
// Navigate to "a.test" and create the iframe "c.test", which registers
// |worker1|.
NavigateToURLBlockUntilNavigationsComplete(
shell(), https_server()->GetURL("a.test", "/workers/frame_factory.html"),
1);
RenderFrameHost* subframe_rfh_1 = CreateSubframe(
https_server()->GetURL("c.test", "/workers/service_worker_setup.html"));
RegisterWorker(subframe_rfh_1, WorkerType::kServiceWorker, "empty.js");
// Navigate to "b.test" and create the another iframe on "c.test", which
// registers |worker2| and then |worker1| again.
NavigateToURLBlockUntilNavigationsComplete(
shell(), https_server()->GetURL("b.test", "/workers/frame_factory.html"),
1);
RenderFrameHost* subframe_rfh_2 = CreateSubframe(
https_server()->GetURL("c.test", "/workers/service_worker_setup.html"));
RegisterWorker(subframe_rfh_2, WorkerType::kServiceWorker, "empty2.js");
RegisterWorker(subframe_rfh_2, WorkerType::kServiceWorker, "empty.js");
cache_status_waiter.Run();
}
using SharedWorkerMainScriptRequestNetworkIsolationKeyBrowserTest =
WorkerNetworkIsolationKeyBrowserTest;
// Test that network isolation key is filled in correctly for shared worker's
// main script request. The test navigates to "a.test" and creates an iframe
// having origin "c.test" that creates |worker1|. The test then navigates to
// "b.test" and creates an iframe also having origin "c.test" that creates
// |worker1| again.
//
// We expect the second creation request for |worker1| to not exist in the
// cache since the workers should be partitioned by top-level site.
//
// Note that it's sufficient not to test the cache miss when subframe origins
// are different as in that case the two script urls must be different.
IN_PROC_BROWSER_TEST_F(
SharedWorkerMainScriptRequestNetworkIsolationKeyBrowserTest,
SharedWorkerMainScriptRequest) {
if (!SupportsSharedWorker())
return;
size_t num_completed = 0;
std::string main_script_file = "empty.js";
GURL main_script_request_url =
https_server()->GetURL("c.test", "/workers/" + main_script_file);
base::RunLoop cache_status_waiter;
URLLoaderInterceptor interceptor(
base::BindLambdaForTesting(
[&](URLLoaderInterceptor::RequestParams* params) { return false; }),
base::BindLambdaForTesting(
[&](const GURL& request_url,
const network::URLLoaderCompletionStatus& status) {
if (request_url == main_script_request_url) {
num_completed += 1;
if (num_completed == 1) {
EXPECT_FALSE(status.exists_in_cache);
} else if (num_completed == 2) {
EXPECT_FALSE(status.exists_in_cache);
cache_status_waiter.Quit();
} else {
NOTREACHED_IN_MIGRATION();
}
}
}),
{});
// Navigate to "a.test" and create the iframe "c.test", which creates
// |worker1|.
NavigateToURLBlockUntilNavigationsComplete(
shell(), https_server()->GetURL("a.test", "/workers/frame_factory.html"),
1);
RenderFrameHost* subframe_rfh_1 = CreateSubframe(
https_server()->GetURL("c.test", "/workers/service_worker_setup.html"));
RegisterWorker(subframe_rfh_1, WorkerType::kSharedWorker, "empty.js");
// Navigate to "b.test" and create the another iframe on "c.test", which
// creates |worker1| again.
NavigateToURLBlockUntilNavigationsComplete(
shell(), https_server()->GetURL("b.test", "/workers/frame_factory.html"),
1);
RenderFrameHost* subframe_rfh_2 = CreateSubframe(
https_server()->GetURL("c.test", "/workers/service_worker_setup.html"));
RegisterWorker(subframe_rfh_2, WorkerType::kSharedWorker, "empty.js");
cache_status_waiter.Run();
}
} // namespace content