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
content / browser / renderer_host / embedding_token_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/test/scoped_feature_list.h"
#include "base/unguessable_token.h"
#include "content/browser/renderer_host/frame_tree.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/content_navigation_policy.h"
#include "content/public/test/back_forward_cache_util.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_utils.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
class EmbeddingTokenBrowserTest : public ContentBrowserTest {
public:
EmbeddingTokenBrowserTest() = default;
void SetUpCommandLine(base::CommandLine* command_line) override {
scoped_feature_list_.InitWithFeaturesAndParameters(
GetDefaultEnabledBackForwardCacheFeaturesForTesting(
/*ignore_outstanding_network_request=*/false),
GetDefaultDisabledBackForwardCacheFeaturesForTesting());
IsolateAllSitesForTesting(command_line);
}
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
SetupCrossSiteRedirector(embedded_test_server());
ASSERT_TRUE(embedded_test_server()->Start());
}
WebContents* web_contents() { return shell()->web_contents(); }
RenderFrameHostImpl* top_frame_host() {
return static_cast<RenderFrameHostImpl*>(
web_contents()->GetPrimaryMainFrame());
}
EmbeddingTokenBrowserTest(const EmbeddingTokenBrowserTest&) = delete;
EmbeddingTokenBrowserTest& operator=(const EmbeddingTokenBrowserTest&) =
delete;
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_F(EmbeddingTokenBrowserTest, EmbeddingTokenOnMainFrame) {
GURL a_url = embedded_test_server()->GetURL("a.com", "/site_isolation/");
GURL b_url = embedded_test_server()->GetURL("b.com", "/site_isolation/");
// Starts without an embedding token.
EXPECT_FALSE(top_frame_host()->GetEmbeddingToken().has_value());
// Embedding tokens should get added to the main frame.
EXPECT_TRUE(NavigateToURL(shell(), a_url.Resolve("blank.html")));
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
auto first_token = top_frame_host()->GetEmbeddingToken().value();
EXPECT_TRUE(NavigateToURL(shell(), b_url.Resolve("blank.html")));
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
EXPECT_NE(top_frame_host()->GetEmbeddingToken().value(), first_token);
}
IN_PROC_BROWSER_TEST_F(EmbeddingTokenBrowserTest,
EmbeddingTokensAddedToCrossDocumentIFrames) {
EXPECT_TRUE(NavigateToURL(
shell(), embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(b(a),c,a)")));
ASSERT_EQ(3U, top_frame_host()->child_count());
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
auto top_token = top_frame_host()->GetEmbeddingToken().value();
// Child 0 (b) should have an embedding token.
auto child_0_token =
top_frame_host()->child_at(0)->current_frame_host()->GetEmbeddingToken();
ASSERT_TRUE(child_0_token.has_value());
EXPECT_NE(base::UnguessableToken::Null(), child_0_token);
EXPECT_NE(top_token, child_0_token);
// Child 0 (a) of Child 0 (b) should have an embedding token.
ASSERT_EQ(1U, top_frame_host()->child_at(0)->child_count());
auto child_0_0_token = top_frame_host()
->child_at(0)
->child_at(0)
->current_frame_host()
->GetEmbeddingToken();
ASSERT_TRUE(child_0_0_token.has_value());
EXPECT_NE(base::UnguessableToken::Null(), child_0_0_token);
EXPECT_NE(top_token, child_0_0_token);
EXPECT_NE(child_0_token, child_0_0_token);
// Child 1 (c) should have an embedding token.
auto child_1_token =
top_frame_host()->child_at(1)->current_frame_host()->GetEmbeddingToken();
ASSERT_TRUE(child_1_token.has_value());
EXPECT_NE(base::UnguessableToken::Null(), child_1_token);
EXPECT_NE(top_token, child_1_token);
EXPECT_NE(child_0_token, child_1_token);
EXPECT_NE(child_0_0_token, child_1_token);
// Child 2 (a) should have an embedding token.
auto child_2_token =
top_frame_host()->child_at(2)->current_frame_host()->GetEmbeddingToken();
ASSERT_TRUE(child_2_token.has_value());
EXPECT_NE(base::UnguessableToken::Null(), child_2_token);
EXPECT_NE(top_token, child_2_token);
EXPECT_NE(child_0_token, child_2_token);
EXPECT_NE(child_0_0_token, child_2_token);
// TODO(ckitagawa): Somehow assert that the parent and child have matching
// embedding tokens in parent HTMLOwnerElement and child LocalFrame.
}
IN_PROC_BROWSER_TEST_F(EmbeddingTokenBrowserTest,
EmbeddingTokenSwapsOnCrossDocumentNavigation) {
EXPECT_TRUE(NavigateToURL(
shell(), embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(b)")));
ASSERT_EQ(1U, top_frame_host()->child_count());
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
auto top_token = top_frame_host()->GetEmbeddingToken().value();
// Child 0 (b) should have an embedding token.
RenderFrameHost* target = top_frame_host()->child_at(0)->current_frame_host();
auto child_0_token = target->GetEmbeddingToken();
ASSERT_TRUE(child_0_token.has_value());
EXPECT_NE(base::UnguessableToken::Null(), child_0_token);
EXPECT_NE(top_token, child_0_token);
// Navigate child 0 (b) to same-site the token should swap.
NavigateIframeToURL(shell()->web_contents(), "child-0",
embedded_test_server()
->GetURL("b.com", "/site_isolation/")
.Resolve("blank.html"));
auto same_site_new_child_0_token =
top_frame_host()->child_at(0)->current_frame_host()->GetEmbeddingToken();
ASSERT_TRUE(same_site_new_child_0_token.has_value());
EXPECT_NE(base::UnguessableToken::Null(), same_site_new_child_0_token);
EXPECT_NE(top_token, same_site_new_child_0_token);
EXPECT_NE(child_0_token, same_site_new_child_0_token);
// Navigate child 0 (b) to another site (cross-process) the token should swap.
{
// The RenderFrameHost might have been replaced when the frame navigated.
target = top_frame_host()->child_at(0)->current_frame_host();
RenderFrameDeletedObserver deleted_observer(target);
NavigateIframeToURL(shell()->web_contents(), "child-0",
embedded_test_server()
->GetURL("c.com", "/site_isolation/")
.Resolve("blank.html"));
deleted_observer.WaitUntilDeleted();
}
auto new_site_child_0_token =
top_frame_host()->child_at(0)->current_frame_host()->GetEmbeddingToken();
ASSERT_TRUE(same_site_new_child_0_token.has_value());
EXPECT_NE(base::UnguessableToken::Null(), new_site_child_0_token);
EXPECT_NE(top_token, new_site_child_0_token);
EXPECT_NE(child_0_token, new_site_child_0_token);
EXPECT_NE(same_site_new_child_0_token, new_site_child_0_token);
// TODO(ckitagawa): Somehow assert that the parent and child have matching
// embedding tokens in parent HTMLOwnerElement and child LocalFrame.
}
IN_PROC_BROWSER_TEST_F(
EmbeddingTokenBrowserTest,
EmbeddingTokenNotChangedOnSubframeSameDocumentNavigation) {
EXPECT_TRUE(NavigateToURL(
shell(), embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(a)")));
ASSERT_EQ(1U, top_frame_host()->child_count());
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
auto top_token = top_frame_host()->GetEmbeddingToken().value();
// Child 0 (a) should have an embedding token.
RenderFrameHost* target = top_frame_host()->child_at(0)->current_frame_host();
auto child_0_token = target->GetEmbeddingToken();
ASSERT_TRUE(child_0_token.has_value());
EXPECT_NE(base::UnguessableToken::Null(), child_0_token);
EXPECT_NE(top_token, child_0_token);
auto b_url = embedded_test_server()->GetURL("b.com", "/site_isolation/");
// Navigate child 0 to another site (cross-process) a token should be created.
{
RenderFrameDeletedObserver deleted_observer(
top_frame_host()->child_at(0)->current_frame_host());
NavigateIframeToURL(web_contents(), "child-0", b_url.Resolve("blank.html"));
deleted_observer.WaitUntilDeleted();
}
// Child 0 (b) should have a new embedding token.
auto new_child_0_token =
top_frame_host()->child_at(0)->current_frame_host()->GetEmbeddingToken();
ASSERT_TRUE(child_0_token.has_value());
EXPECT_NE(base::UnguessableToken::Null(), new_child_0_token);
EXPECT_NE(top_token, new_child_0_token);
EXPECT_NE(child_0_token, new_child_0_token);
// Navigate child 0 (b) to same document the token should not swap.
NavigateIframeToURL(web_contents(), "child-0",
b_url.Resolve("blank.html#foo"));
auto same_document_new_child_0_token =
top_frame_host()->child_at(0)->current_frame_host()->GetEmbeddingToken();
ASSERT_TRUE(same_document_new_child_0_token.has_value());
EXPECT_EQ(new_child_0_token, same_document_new_child_0_token);
// TODO(ckitagawa): Somehow assert that the parent and child have matching
// embedding tokens in parent HTMLOwnerElement and child LocalFrame.
}
IN_PROC_BROWSER_TEST_F(EmbeddingTokenBrowserTest,
EmbeddingTokenChangedOnSubframeNavigationToNewDocument) {
auto a_url = embedded_test_server()->GetURL("a.com", "/");
EXPECT_TRUE(NavigateToURL(
shell(), a_url.Resolve("cross_site_iframe_factory.html?a(b)")));
ASSERT_EQ(1U, top_frame_host()->child_count());
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
auto top_token = top_frame_host()->GetEmbeddingToken().value();
// Child 0 (b) should have an embedding token.
RenderFrameHost* target = top_frame_host()->child_at(0)->current_frame_host();
auto child_0_token = target->GetEmbeddingToken();
ASSERT_TRUE(child_0_token.has_value());
EXPECT_NE(base::UnguessableToken::Null(), child_0_token);
EXPECT_NE(top_token, child_0_token);
// Navigate child 0 (b) to the same site as the main frame. This should create
// an embedding token.
{
RenderFrameDeletedObserver deleted_observer(target);
NavigateIframeToURL(web_contents(), "child-0",
a_url.Resolve("site_isolation/").Resolve("blank.html"));
deleted_observer.WaitUntilDeleted();
}
auto new_child_0_token =
top_frame_host()->child_at(0)->current_frame_host()->GetEmbeddingToken();
ASSERT_TRUE(new_child_0_token.has_value());
EXPECT_NE(base::UnguessableToken::Null(), new_child_0_token);
EXPECT_NE(top_token, new_child_0_token);
EXPECT_NE(child_0_token, new_child_0_token);
// TODO(ckitagawa): Somehow assert that the parent and child have matching
// embedding tokens in parent HTMLOwnerElement and child LocalFrame.
}
IN_PROC_BROWSER_TEST_F(EmbeddingTokenBrowserTest,
BackForwardCacheCrossDocument) {
auto a_url = embedded_test_server()->GetURL("a.com", "/site_isolation/");
auto b_url = embedded_test_server()->GetURL("b.com", "/site_isolation/");
EXPECT_TRUE(NavigateToURL(shell(), a_url.Resolve("blank.html")));
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
auto top_token_a = top_frame_host()->GetEmbeddingToken().value();
EXPECT_TRUE(NavigateToURL(shell(), b_url.Resolve("blank.html")));
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
auto top_token_b = top_frame_host()->GetEmbeddingToken().value();
EXPECT_NE(top_token_a, top_token_b);
// Navigate back to the first origin. The back forward cache should keep
// the embedding token.
web_contents()->GetController().GoBack();
EXPECT_TRUE(content::WaitForLoadStop(web_contents()));
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
auto top_token_a_prime = top_frame_host()->GetEmbeddingToken().value();
EXPECT_EQ(top_token_a, top_token_a_prime);
}
IN_PROC_BROWSER_TEST_F(EmbeddingTokenBrowserTest,
BackForwardCacheCrossDocumentAfterSameDocument) {
auto a_url = embedded_test_server()->GetURL("a.com", "/site_isolation/");
auto b_url = embedded_test_server()->GetURL("b.com", "/site_isolation/");
EXPECT_TRUE(NavigateToURL(shell(), a_url.Resolve("blank.html")));
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
auto top_token_a = top_frame_host()->GetEmbeddingToken().value();
EXPECT_TRUE(NavigateToURL(shell(), a_url.Resolve("blank.html#foo")));
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
EXPECT_EQ(top_frame_host()->GetEmbeddingToken().value(), top_token_a);
EXPECT_TRUE(NavigateToURL(shell(), b_url.Resolve("blank.html")));
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
auto top_token_b = top_frame_host()->GetEmbeddingToken().value();
EXPECT_NE(top_token_a, top_token_b);
// Navigate back to the first origin. The back forward cache should keep
// the embedding token even when the embedding token is not present in the
// most recent navigation.
web_contents()->GetController().GoBack();
EXPECT_TRUE(content::WaitForLoadStop(web_contents()));
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
auto top_token_a_prime = top_frame_host()->GetEmbeddingToken().value();
EXPECT_EQ(top_token_a, top_token_a_prime);
}
IN_PROC_BROWSER_TEST_F(EmbeddingTokenBrowserTest,
SameDocumentHistoryPreservesTokens) {
auto a_url = embedded_test_server()->GetURL("a.com", "/site_isolation/");
EXPECT_TRUE(NavigateToURL(shell(), a_url.Resolve("blank.html")));
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
auto top_token_a = top_frame_host()->GetEmbeddingToken().value();
EXPECT_TRUE(NavigateToURL(shell(), a_url.Resolve("blank.html#foo")));
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
auto top_token_a_prime = top_frame_host()->GetEmbeddingToken().value();
EXPECT_EQ(top_token_a, top_token_a_prime);
// Navigate back to before the fragment was added. This should preserve the
// embedding token.
web_contents()->GetController().GoBack();
EXPECT_TRUE(content::WaitForLoadStop(web_contents()));
EXPECT_TRUE(top_frame_host()->GetEmbeddingToken().has_value());
auto top_token_a_prime_prime = top_frame_host()->GetEmbeddingToken().value();
EXPECT_EQ(top_token_a, top_token_a_prime_prime);
}
} // namespace content