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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
content / browser / security / dip / document_isolation_policy_browsertest.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/network/public/cpp/document_isolation_policy.h"
#include "base/command_line.h"
#include "base/strings/escape.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "content/browser/process_lock.h"
#include "content/browser/renderer_host/navigation_request.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/content_navigation_policy.h"
#include "content/public/browser/site_isolation_policy.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.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_content_browser_client.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/content_mock_cert_verifier.h"
#include "content/public/test/prerender_test_util.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/url_loader_interceptor.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "content/test/render_document_feature.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/default_handlers.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "net/test/embedded_test_server/request_handler_util.h"
#include "services/network/public/cpp/cross_origin_embedder_policy.h"
#include "services/network/public/cpp/features.h"
#include "testing/gmock/include/gmock/gmock.h"
using ::testing::HasSubstr;
namespace content {
namespace {
network::DocumentIsolationPolicy DipIsolateAndRequireCorp() {
network::DocumentIsolationPolicy dip;
dip.value =
network::mojom::DocumentIsolationPolicyValue::kIsolateAndRequireCorp;
return dip;
}
network::DocumentIsolationPolicy DipIsolateAndCredentialless() {
network::DocumentIsolationPolicy dip;
dip.value =
network::mojom::DocumentIsolationPolicyValue::kIsolateAndCredentialless;
return dip;
}
network::DocumentIsolationPolicy DipNone() {
return network::DocumentIsolationPolicy();
}
std::unique_ptr<net::test_server::HttpResponse> ServeDipOnSecondNavigation(
unsigned int& navigation_counter,
const net::test_server::HttpRequest& request) {
++navigation_counter;
auto http_response = std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_code(net::HttpStatusCode::HTTP_OK);
http_response->AddCustomHeader("Cache-Control", "no-store, must-revalidate");
if (navigation_counter > 1) {
http_response->AddCustomHeader("Document-Isolation-Policy",
"isolate-and-require-corp");
}
return http_response;
}
class DocumentIsolationPolicyBrowserTest
: public ContentBrowserTest,
public ::testing::WithParamInterface<
std::tuple<std::string, bool, bool>> {
public:
DocumentIsolationPolicyBrowserTest()
: prerender_helper_(base::BindRepeating(
&DocumentIsolationPolicyBrowserTest::prerender_web_contents,
base::Unretained(this))),
https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {
// Enable DIP and disable speculative RFH creation deferral. Currently,
// speculative RFH creation deferral makes it impossible to check that a
// speculative RFH is not created at the start of the navigation. This is
// needed to check that we do not create an extra process when navigating
// between two same-origin documents with DIP. Once RenderDocument ships, a
// speculative RFH will always be created, we'll then check that it is in
// the same SiteInstance as the current one. Then, we'll be able to
// re-enable the deferred speculative RFH creation, and just wait for the
// deferred creation of the speculative RenderDocument.
feature_list_.InitWithFeatures(
{network::features::kDocumentIsolationPolicy},
{features::kDeferSpeculativeRFHCreation, features::kSharedArrayBuffer});
// Enable RenderDocument:
InitAndEnableRenderDocumentFeature(&feature_list_for_render_document_,
std::get<0>(GetParam()));
// Enable BackForwardCache:
if (IsBackForwardCacheEnabled()) {
feature_list_for_back_forward_cache_.InitWithFeaturesAndParameters(
GetDefaultEnabledBackForwardCacheFeaturesForTesting(
/*ignore_outstanding_network_request=*/false),
GetDefaultDisabledBackForwardCacheFeaturesForTesting());
} else {
feature_list_for_back_forward_cache_.InitWithFeatures(
{}, {features::kBackForwardCache});
}
}
// Provides meaningful param names instead of /0, /1, ...
static std::string DescribeParams(
const testing::TestParamInfo<ParamType>& info) {
auto [render_document_level, enable_back_forward_cache, require_corp] =
info.param;
return base::StringPrintf(
"%s_%s_%s",
GetRenderDocumentLevelNameForTestParams(render_document_level).c_str(),
enable_back_forward_cache ? "BFCacheEnabled" : "BFCacheDisabled",
require_corp ? "RequireCorp" : "Credentialless");
}
bool IsBackForwardCacheEnabled() { return std::get<1>(GetParam()); }
net::EmbeddedTestServer* https_server() { return &https_server_; }
test::PrerenderTestHelper& prerender_helper() { return prerender_helper_; }
protected:
WebContentsImpl* web_contents() const {
return static_cast<WebContentsImpl*>(shell()->web_contents());
}
RenderFrameHostImpl* current_frame_host() {
return web_contents()->GetPrimaryMainFrame();
}
void SetUpOnMainThread() override {
ContentBrowserTest::SetUpOnMainThread();
mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(embedded_test_server()->Start());
https_server()->ServeFilesFromSourceDirectory(GetTestDataFilePath());
SetupCrossSiteRedirector(https_server());
net::test_server::RegisterDefaultHandlers(&https_server_);
AddRedirectOnSecondNavigationHandler(&https_server_);
unsigned int navigation_counter = 0;
https_server_.RegisterDefaultHandler(base::BindRepeating(
&net::test_server::HandlePrefixedRequest,
"/serve-dip-on-second-navigation",
base::BindRepeating(&ServeDipOnSecondNavigation,
base::OwnedRef(navigation_counter))));
prerender_helper().RegisterServerRequestMonitor(&https_server_);
ASSERT_TRUE(https_server()->Start());
}
GURL GetDocumentIsolationPolicyURL(
const std::string& host,
const std::optional<std::string>& additional_header = std::nullopt) {
std::string headers = "/set-header?";
if (std::get<2>(GetParam())) {
// Isolate-and-require-corp version of the test.
headers += "document-isolation-policy: isolate-and-require-corp";
} else {
// Isolate-and-credentialless version of the test.
headers += "document-isolation-policy: isolate-and-credentialless";
}
if (additional_header.has_value()) {
headers += "&" + additional_header.value();
}
return https_server()->GetURL(host, headers);
}
network::DocumentIsolationPolicy GetDocumentIsolationPolicy() {
// Isolate-and-require-corp version of the test.
if (std::get<2>(GetParam())) {
return DipIsolateAndRequireCorp();
}
// Isolate-and-credentialless version of the test.
return DipIsolateAndCredentialless();
}
private:
void SetUpCommandLine(base::CommandLine* command_line) override {
mock_cert_verifier_.SetUpCommandLine(command_line);
// Enable strict SiteIsolation. Currently DIP only supports strict
// SiteIsolation so force it in tests.
IsolateAllSitesForTesting(command_line);
}
void SetUpInProcessBrowserTestFixture() override {
ContentBrowserTest::SetUpInProcessBrowserTestFixture();
mock_cert_verifier_.SetUpInProcessBrowserTestFixture();
}
void TearDownInProcessBrowserTestFixture() override {
ContentBrowserTest::TearDownInProcessBrowserTestFixture();
mock_cert_verifier_.TearDownInProcessBrowserTestFixture();
}
// Variation of web_contents(), that returns a WebContents* instead of a
// WebContentsImpl*, required to bind the prerender_helper_ in the
// constructor.
WebContents* prerender_web_contents() { return shell()->web_contents(); }
content::ContentMockCertVerifier mock_cert_verifier_;
// This needs to be before ScopedFeatureLists, because it contains one
// internally and the destruction order matters.
test::PrerenderTestHelper prerender_helper_;
base::test::ScopedFeatureList feature_list_;
base::test::ScopedFeatureList feature_list_for_render_document_;
base::test::ScopedFeatureList feature_list_for_back_forward_cache_;
net::EmbeddedTestServer https_server_;
};
class DocumentIsolationPolicyWithoutFeatureBrowserTest
: public ContentBrowserTest,
public ::testing::WithParamInterface<
std::tuple<std::string, bool, bool>> {
public:
DocumentIsolationPolicyWithoutFeatureBrowserTest()
: https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {
// Disable the DocumentIsolationPolicy feature.
feature_list_.InitWithFeatures(
{}, {network::features::kDocumentIsolationPolicy});
// Enable RenderDocument:
InitAndEnableRenderDocumentFeature(&feature_list_for_render_document_,
std::get<0>(GetParam()));
// Enable BackForwardCache:
if (IsBackForwardCacheEnabled()) {
feature_list_for_back_forward_cache_.InitWithFeaturesAndParameters(
GetDefaultEnabledBackForwardCacheFeaturesForTesting(
/*ignore_outstanding_network_request=*/false),
GetDefaultDisabledBackForwardCacheFeaturesForTesting());
} else {
feature_list_for_back_forward_cache_.InitWithFeatures(
{}, {features::kBackForwardCache});
}
}
// Provides meaningful param names instead of /0, /1, ...
static std::string DescribeParams(
const testing::TestParamInfo<ParamType>& info) {
auto [render_document_level, enable_back_forward_cache, require_corp] =
info.param;
return base::StringPrintf(
"%s_%s_%s",
GetRenderDocumentLevelNameForTestParams(render_document_level).c_str(),
enable_back_forward_cache ? "BFCacheEnabled" : "BFCacheDisabled",
require_corp ? "RequireCorp" : "Credentialless");
}
net::EmbeddedTestServer* https_server() { return &https_server_; }
protected:
RenderFrameHostImpl* current_frame_host() {
return static_cast<WebContentsImpl*>(shell()->web_contents())
->GetPrimaryMainFrame();
}
GURL GetDocumentIsolationPolicyURL(const std::string& host) {
// Isolate-and-require-corp version of the test.
if (std::get<2>(GetParam())) {
return https_server()->GetURL(
host,
"/set-header?document-isolation-policy: isolate-and-require-corp");
}
// Isolate-and-credentialless version of the test.
return https_server()->GetURL(
host,
"/set-header?document-isolation-policy: isolate-and-credentialless");
}
void SetUpOnMainThread() override {
ContentBrowserTest::SetUpOnMainThread();
mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(embedded_test_server()->Start());
https_server()->ServeFilesFromSourceDirectory(GetTestDataFilePath());
SetupCrossSiteRedirector(https_server());
net::test_server::RegisterDefaultHandlers(&https_server_);
ASSERT_TRUE(https_server()->Start());
}
private:
void SetUpCommandLine(base::CommandLine* command_line) override {
ContentBrowserTest::SetUpCommandLine(command_line);
mock_cert_verifier_.SetUpCommandLine(command_line);
}
void SetUpInProcessBrowserTestFixture() override {
ContentBrowserTest::SetUpInProcessBrowserTestFixture();
mock_cert_verifier_.SetUpInProcessBrowserTestFixture();
}
void TearDownInProcessBrowserTestFixture() override {
ContentBrowserTest::TearDownInProcessBrowserTestFixture();
mock_cert_verifier_.TearDownInProcessBrowserTestFixture();
}
content::ContentMockCertVerifier mock_cert_verifier_;
base::test::ScopedFeatureList feature_list_;
base::test::ScopedFeatureList feature_list_for_render_document_;
base::test::ScopedFeatureList feature_list_for_back_forward_cache_;
net::EmbeddedTestServer https_server_;
};
class DocumentIsolationPolicyWithoutSiteIsolationBrowserTest
: public DocumentIsolationPolicyBrowserTest {
public:
DocumentIsolationPolicyWithoutSiteIsolationBrowserTest() = default;
private:
void SetUpCommandLine(base::CommandLine* command_line) override {
ContentBrowserTest::SetUpCommandLine(command_line);
mock_cert_verifier_.SetUpCommandLine(command_line);
// Disable SiteIsolation.
command_line->AppendSwitch(switches::kDisableSiteIsolation);
}
content::ContentMockCertVerifier mock_cert_verifier_;
};
} // namespace
// Checks that a Document-Isolation-Policy header is ignored if the
// DocumentIsolationPolicy feature flag is not enabled.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyWithoutFeatureBrowserTest,
DIP_Disabled) {
GURL starting_page = GetDocumentIsolationPolicyURL("a.test");
EXPECT_TRUE(NavigateToURL(shell(), starting_page));
EXPECT_EQ(current_frame_host()
->policy_container_host()
->policies()
.document_isolation_policy,
DipNone());
}
// Checks that a Document-Isolation-Policy header is ignored if SiteIsolation is
// not enabled.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyWithoutSiteIsolationBrowserTest,
DIP_Disabled) {
GURL starting_page = GetDocumentIsolationPolicyURL("a.test");
EXPECT_TRUE(NavigateToURL(shell(), starting_page));
EXPECT_EQ(current_frame_host()
->policy_container_host()
->policies()
.document_isolation_policy,
DipNone());
}
// Checks that DocumentIsolationPolicy is properly inherited from its creator by
// the about:blank document in a new popup.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
NewPopup_InheritsDIP) {
GURL starting_page = GetDocumentIsolationPolicyURL("a.test");
GURL no_dip(https_server()->GetURL("a.test", "/empty.html"));
EXPECT_TRUE(NavigateToURL(shell(), starting_page));
RenderFrameHostImpl* main_rfh = current_frame_host();
// Open a popup.
ShellAddedObserver shell_observer;
EXPECT_TRUE(ExecJs(main_rfh, "window.open('about:blank')"));
auto* popup_webcontents =
static_cast<WebContentsImpl*>(shell_observer.GetShell()->web_contents());
RenderFrameHostImpl* popup_rfh = popup_webcontents->GetPrimaryMainFrame();
EXPECT_EQ(
main_rfh->policy_container_host()->policies().document_isolation_policy,
GetDocumentIsolationPolicy());
EXPECT_EQ(
popup_rfh->policy_container_host()->policies().document_isolation_policy,
GetDocumentIsolationPolicy());
// Navigate the popup to a page without DIP. It should not longer have a
// DocumentIsolationPolicy.
ASSERT_TRUE(NavigateToURL(popup_webcontents, no_dip));
popup_rfh = popup_webcontents->GetPrimaryMainFrame();
EXPECT_EQ(
popup_rfh->policy_container_host()->policies().document_isolation_policy,
DipNone());
// Now add an iframe without DIP which will open a popup.
ASSERT_TRUE(ExecJs(main_rfh, R"(
const frame = document.createElement('iframe');
frame.src = '/empty.html';
document.body.appendChild(frame);
)"));
EXPECT_TRUE(WaitForLoadStop(web_contents()));
ShellAddedObserver shell_observer_2;
RenderFrameHostImpl* iframe_rfh = main_rfh->child_at(0)->current_frame_host();
EXPECT_TRUE(ExecJs(iframe_rfh, "window.open('about:blank')"));
RenderFrameHostImpl* popup_rfh_2 =
static_cast<WebContentsImpl*>(shell_observer_2.GetShell()->web_contents())
->GetPrimaryMainFrame();
EXPECT_EQ(
iframe_rfh->policy_container_host()->policies().document_isolation_policy,
DipNone());
EXPECT_EQ(popup_rfh_2->policy_container_host()
->policies()
.document_isolation_policy,
DipNone());
}
// Checks that a navigation to a Blob URL inherits the DocumentIsolationPolicy
// of its creator.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest, BlobInheritsDIP) {
GURL starting_page = GetDocumentIsolationPolicyURL("a.test");
EXPECT_TRUE(NavigateToURL(shell(), starting_page));
// Create and open blob.
ShellAddedObserver shell_observer;
ASSERT_TRUE(ExecJs(current_frame_host(), R"(
const blob = new Blob(['foo'], {type : 'text/html'});
const url = URL.createObjectURL(blob);
window.open(url);
)"));
EXPECT_TRUE(WaitForLoadStop(shell_observer.GetShell()->web_contents()));
RenderFrameHostImpl* popup_rfh =
static_cast<WebContentsImpl*>(shell_observer.GetShell()->web_contents())
->GetPrimaryMainFrame();
// DIP inherited from Blob creator
EXPECT_EQ(
popup_rfh->policy_container_host()->policies().document_isolation_policy,
GetDocumentIsolationPolicy());
}
// Checks that an about:blank iframe inherits its DocumentIsolationPolicy from
// its creator.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
AboutBlankInheritsDip) {
GURL starting_page = GetDocumentIsolationPolicyURL("a.test");
EXPECT_TRUE(NavigateToURL(shell(), starting_page));
EXPECT_EQ(current_frame_host()
->policy_container_host()
->policies()
.document_isolation_policy,
GetDocumentIsolationPolicy());
// Add an about:blank iframe.
EXPECT_TRUE(ExecJs(current_frame_host(),
"g_iframe = document.createElement('iframe');"
"g_iframe.src = 'about:blank';"
"document.body.appendChild(g_iframe);"));
WaitForLoadStop(web_contents());
RenderFrameHostImpl* iframe_rfh =
current_frame_host()->child_at(0)->current_frame_host();
// Document-Isolation-Policy should have been inherited.
EXPECT_EQ(
iframe_rfh->policy_container_host()->policies().document_isolation_policy,
GetDocumentIsolationPolicy());
}
// Checks that an iframe can enable DocumentIsolationPolicy even if its parent
// does not, and that it will be placed in an appropriate SiteInstance.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest, IframeCanSetDip) {
GURL starting_page(
https_server()->GetURL("a.com", "/cross_site_iframe_factory.html?a(b)"));
GURL iframe_navigation_url = GetDocumentIsolationPolicyURL("b.com");
EXPECT_TRUE(NavigateToURL(shell(), starting_page));
RenderFrameHostImpl* main_rfh = current_frame_host();
FrameTreeNode* iframe_ftn = main_rfh->child_at(0);
RenderFrameHostImpl* iframe_rfh = iframe_ftn->current_frame_host();
scoped_refptr<SiteInstanceImpl> non_dip_iframe_site_instance =
iframe_rfh->GetSiteInstance();
// The iframe should not have a DocumentIsolationPolicy.
EXPECT_EQ(
iframe_rfh->policy_container_host()->policies().document_isolation_policy,
DipNone());
// Navigate the iframe same-origin to a document with DIP header. The
// header should be taken into account.
EXPECT_TRUE(NavigateToURLFromRenderer(iframe_ftn, iframe_navigation_url));
iframe_rfh = iframe_ftn->current_frame_host();
// The navigation should have used a different SiteInstance from the one
// previously used as the DocumentIsolationPolicy do not match, even if the
// navigation is same-origin.
EXPECT_EQ(iframe_rfh->GetLastCommittedURL(), iframe_navigation_url);
EXPECT_NE(iframe_rfh->GetSiteInstance(), non_dip_iframe_site_instance);
EXPECT_EQ(
iframe_rfh->policy_container_host()->policies().document_isolation_policy,
GetDocumentIsolationPolicy());
}
// Checks that navigations are placed in the appropriate renderer process
// depending on their DocumentIsolationPolicy, even if the current renderer
// process is crashed or crashes during the navigation. In particular, this
// tests the navigation from a page without DIP to a page with DIP.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
NonDipPageCrashIntoDip) {
GURL non_dip_page(https_server()->GetURL("a.test", "/title1.html"));
GURL dip_page = GetDocumentIsolationPolicyURL("a.test");
// Test a crash before the navigation.
{
// Navigate to a non dip page.
EXPECT_TRUE(NavigateToURL(shell(), non_dip_page));
scoped_refptr<SiteInstance> initial_site_instance(
current_frame_host()->GetSiteInstance());
// Simulate the renderer process crashing.
RenderProcessHost* process = initial_site_instance->GetProcess();
ASSERT_TRUE(process);
std::unique_ptr<RenderProcessHostWatcher> crash_observer(
new RenderProcessHostWatcher(
process, RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT));
process->Shutdown(0);
crash_observer->Wait();
crash_observer.reset();
// Navigate to a DIP page.
EXPECT_TRUE(NavigateToURL(shell(), dip_page));
EXPECT_EQ(current_frame_host()
->policy_container_host()
->policies()
.document_isolation_policy,
GetDocumentIsolationPolicy());
}
// Test a crash during the navigation.
{
// Navigate to a non dip page.
EXPECT_TRUE(NavigateToURL(shell(), non_dip_page));
scoped_refptr<SiteInstance> initial_site_instance(
current_frame_host()->GetSiteInstance());
// Start navigating to a DIP page.
TestNavigationManager dip_navigation(web_contents(), dip_page);
shell()->LoadURL(dip_page);
EXPECT_TRUE(dip_navigation.WaitForRequestStart());
// Simulate the renderer process crashing.
RenderProcessHost* process = initial_site_instance->GetProcess();
ASSERT_TRUE(process);
std::unique_ptr<RenderProcessHostWatcher> crash_observer(
new RenderProcessHostWatcher(
process, RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT));
process->Shutdown(0);
crash_observer->Wait();
crash_observer.reset();
// Finish the navigation to the DIP page.
ASSERT_TRUE(dip_navigation.WaitForNavigationFinished());
// The navigation will fail if we create speculative RFH when the navigation
// started (instead of only when the response started), because the renderer
// process will crash and trigger deletion of the speculative RFH and the
// navigation using that speculative RFH. BFCache forces a BrowsingInstance
// swap (even in this same-site case), hence it also necessitates a
// speculative RFH.
// TODO(crbug.com/40261276): If the final RenderFrameHost picked for
// the navigation doesn't use the same process as the crashed process, we
// can crash the process after the final RenderFrameHost has been picked
// instead, and the navigation will commit normally.
if (ShouldCreateNewHostForAllFrames() || IsBackForwardCacheEnabled()) {
EXPECT_FALSE(dip_navigation.was_committed());
return;
}
EXPECT_TRUE(dip_navigation.was_successful());
EXPECT_EQ(current_frame_host()
->policy_container_host()
->policies()
.document_isolation_policy,
GetDocumentIsolationPolicy());
}
}
// Checks that navigations are placed in the appropriate renderer process
// depending on their DocumentIsolationPolicy, even if the current renderer
// process is crashed or crashes during the navigation. In particular, this
// tests the navigation from a page with DIP to a page without DIP.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
DipPageCrashIntoNonDip) {
GURL dip_page = GetDocumentIsolationPolicyURL("a.test");
GURL non_dip_page(https_server()->GetURL("a.test", "/empty.html"));
// Test a crash before the navigation.
{
// Navigate to a DIP page.
EXPECT_TRUE(NavigateToURL(shell(), dip_page));
scoped_refptr<SiteInstance> initial_site_instance(
current_frame_host()->GetSiteInstance());
// Simulate the renderer process crashing.
RenderProcessHost* process = initial_site_instance->GetProcess();
ASSERT_TRUE(process);
std::unique_ptr<RenderProcessHostWatcher> crash_observer(
new RenderProcessHostWatcher(
process, RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT));
process->Shutdown(0);
crash_observer->Wait();
crash_observer.reset();
// Navigate to a non DIP page.
EXPECT_TRUE(NavigateToURL(shell(), non_dip_page));
EXPECT_EQ(current_frame_host()
->policy_container_host()
->policies()
.document_isolation_policy,
DipNone());
}
// Test a crash during the navigation.
{
// Navigate to a DIP page.
EXPECT_TRUE(NavigateToURL(shell(), dip_page));
scoped_refptr<SiteInstance> initial_site_instance(
current_frame_host()->GetSiteInstance());
// Start navigating to a non DIP page.
TestNavigationManager non_dip_navigation(web_contents(), non_dip_page);
shell()->LoadURL(non_dip_page);
EXPECT_TRUE(non_dip_navigation.WaitForRequestStart());
// Simulate the renderer process crashing.
RenderProcessHost* process = initial_site_instance->GetProcess();
ASSERT_TRUE(process);
std::unique_ptr<RenderProcessHostWatcher> crash_observer(
new RenderProcessHostWatcher(
process, RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT));
process->Shutdown(0);
crash_observer->Wait();
crash_observer.reset();
// Finish the navigation to the non DIP page.
// TODO(crbug.com/343914483): This might need to change and match the test
// above if we implement an optimization to assume DIP value hasn't changed
// until response time.
ASSERT_TRUE(non_dip_navigation.WaitForNavigationFinished());
EXPECT_TRUE(non_dip_navigation.was_successful());
EXPECT_EQ(current_frame_host()
->policy_container_host()
->policies()
.document_isolation_policy,
DipNone());
}
}
// Checks that navigations are placed in the appropriate renderer process
// depending on their DocumentIsolationPolicy, even if the current renderer
// process is crashed or crashes during the navigation. In particular, this
// tests the navigation between two pages with DIP, including a reload of a
// crashed page.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
DipPageCrashIntoDip) {
GURL dip_page = GetDocumentIsolationPolicyURL("a.test");
// Test a crash before the navigation.
{
// Navigate to a DIP page.
EXPECT_TRUE(NavigateToURL(shell(), dip_page));
scoped_refptr<SiteInstance> initial_site_instance(
current_frame_host()->GetSiteInstance());
EXPECT_EQ(current_frame_host()
->policy_container_host()
->policies()
.document_isolation_policy,
GetDocumentIsolationPolicy());
// Simulate the renderer process crashing.
RenderProcessHost* process = initial_site_instance->GetProcess();
ASSERT_TRUE(process);
std::unique_ptr<RenderProcessHostWatcher> crash_observer(
new RenderProcessHostWatcher(
process, RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT));
process->Shutdown(0);
crash_observer->Wait();
crash_observer.reset();
// Reload the DIP page.
ReloadBlockUntilNavigationsComplete(shell(), 1);
EXPECT_TRUE(current_frame_host()->GetSiteInstance()->IsRelatedSiteInstance(
initial_site_instance.get()));
EXPECT_EQ(current_frame_host()
->policy_container_host()
->policies()
.document_isolation_policy,
GetDocumentIsolationPolicy());
}
// Test a crash during the navigation.
{
// Navigate to a DIP page.
EXPECT_TRUE(NavigateToURL(shell(), dip_page));
scoped_refptr<SiteInstance> initial_site_instance(
current_frame_host()->GetSiteInstance());
// Start navigating to a DIP page.
TestNavigationManager dip_navigation(web_contents(), dip_page);
shell()->LoadURL(dip_page);
EXPECT_TRUE(dip_navigation.WaitForRequestStart());
// Simulate the renderer process crashing.
RenderProcessHost* process = initial_site_instance->GetProcess();
ASSERT_TRUE(process);
std::unique_ptr<RenderProcessHostWatcher> crash_observer(
new RenderProcessHostWatcher(
process, RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT));
process->Shutdown(0);
crash_observer->Wait();
crash_observer.reset();
// Finish the navigation to the DIP page.
ASSERT_TRUE(dip_navigation.WaitForNavigationFinished());
EXPECT_EQ(current_frame_host()
->policy_container_host()
->policies()
.document_isolation_policy,
GetDocumentIsolationPolicy());
}
}
// Checks that a navigation to a document with DocumentIsolationPolicy will be
// placed in a separate process even if the process limit has been reached.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
IsolateInNewProcessDespiteLimitReached) {
// Set a process limit of 1 for testing.
RenderProcessHostImpl::SetMaxRendererProcessCount(1);
// Navigate to a starting page.
GURL starting_page(https_server()->GetURL("a.test", "/title1.html"));
EXPECT_TRUE(NavigateToURL(shell(), starting_page));
scoped_refptr<SiteInstance> initial_site_instance(
current_frame_host()->GetSiteInstance());
// Open a popup with DocumentIsolationPolicy set.
GURL url_openee = GetDocumentIsolationPolicyURL("a.test");
auto* popup_webcontents =
OpenPopup(current_frame_host(), url_openee, "popup")->web_contents();
EXPECT_TRUE(WaitForLoadStop(popup_webcontents));
// The page and its popup should be in different processes even though the
// process limit was reached.
EXPECT_NE(initial_site_instance,
popup_webcontents->GetPrimaryMainFrame()->GetSiteInstance());
EXPECT_NE(current_frame_host()->GetProcess(),
popup_webcontents->GetPrimaryMainFrame()->GetProcess());
}
// Checks that a process hosting a document with DocumentIsolationPolicy is not
// reused for documents without DocumentIsolationPolicy, even if the process
// limit has been reached.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
NoProcessReuseForDIPProcesses) {
// Set a process limit of 1 for testing.
RenderProcessHostImpl::SetMaxRendererProcessCount(1);
// Navigate to a starting page with DocumentIsolationPolicy set.
GURL starting_page = GetDocumentIsolationPolicyURL("a.test");
EXPECT_TRUE(NavigateToURL(shell(), starting_page));
scoped_refptr<SiteInstance> initial_site_instance(
current_frame_host()->GetSiteInstance());
// Create a new shell.
Shell* new_shell = CreateBrowser();
// Navigate it to a same-origin page without DIP.
GURL non_dip_url = https_server()->GetURL("a.test", "/title1.html");
EXPECT_TRUE(NavigateToURL(new_shell, non_dip_url));
// The original page and the page in the new shell should be in different
// processes even though the process limit was reached.
EXPECT_NE(
initial_site_instance,
new_shell->web_contents()->GetPrimaryMainFrame()->GetSiteInstance());
EXPECT_NE(current_frame_host()->GetProcess(),
new_shell->web_contents()->GetPrimaryMainFrame()->GetProcess());
}
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
SpeculativeRfhsAndDip) {
GURL non_dip_page(https_server()->GetURL("a.test", "/title1.html"));
GURL dip_page = GetDocumentIsolationPolicyURL("a.test");
// Non-DIP into DIP.
{
SCOPED_TRACE("Non-DIP to DIP");
// Start on a non DIP page.
EXPECT_TRUE(NavigateToURL(shell(), non_dip_page));
scoped_refptr<SiteInstance> initial_site_instance(
current_frame_host()->GetSiteInstance());
// Navigate to a DIP page.
TestNavigationManager dip_navigation(web_contents(), dip_page);
shell()->LoadURL(dip_page);
EXPECT_TRUE(dip_navigation.WaitForRequestStart());
auto* speculative_rfh = web_contents()
->GetPrimaryFrameTree()
.root()
->render_manager()
->speculative_frame_host();
if (CanSameSiteMainFrameNavigationsChangeRenderFrameHosts()) {
// When ProactivelySwapBrowsingInstance or RenderDocument is enabled on
// same-site main-frame navigations, the navigation will result in a new
// RFH, so it will create a pending RFH.
EXPECT_TRUE(speculative_rfh);
} else {
EXPECT_FALSE(speculative_rfh);
}
ASSERT_TRUE(dip_navigation.WaitForNavigationFinished());
// Even if the origin of the documents is the same, because their
// DocumentIsolationPolicies do not match, the navigation is classified as a
// cross-site browser-initiated request and the browser triggers a
// speculative BrowsingInstance swap.
EXPECT_FALSE(current_frame_host()->GetSiteInstance()->IsRelatedSiteInstance(
initial_site_instance.get()));
EXPECT_EQ(current_frame_host()
->policy_container_host()
->policies()
.document_isolation_policy,
GetDocumentIsolationPolicy());
}
// DIP into non-DIP.
{
SCOPED_TRACE("DIP to non-DIP");
// Start on a DIP page.
EXPECT_TRUE(NavigateToURL(shell(), dip_page));
scoped_refptr<SiteInstance> initial_site_instance(
current_frame_host()->GetSiteInstance());
// Navigate to a non DIP page.
TestNavigationManager non_dip_navigation(web_contents(), non_dip_page);
shell()->LoadURL(non_dip_page);
EXPECT_TRUE(non_dip_navigation.WaitForRequestStart());
auto* speculative_rfh = web_contents()
->GetPrimaryFrameTree()
.root()
->render_manager()
->speculative_frame_host();
// The navigation is considered cross-site, because the AgentClusterKey of
// the current page has an IsolationKey, and the request does not have one.
// TODO(https://issues.chromium.org/343914483): Avoid creating a speculative
// RFH in this case.
EXPECT_TRUE(speculative_rfh);
ASSERT_TRUE(non_dip_navigation.WaitForNavigationFinished());
// Even if the origin of the documents is the same, because their
// DocumentIsolationPolicies do not match, the navigation is classified as a
// cross-site browser-initiated request and the browser triggers a
// speculative BrowsingInstance swap.
EXPECT_FALSE(current_frame_host()->GetSiteInstance()->IsRelatedSiteInstance(
initial_site_instance.get()));
EXPECT_EQ(current_frame_host()
->policy_container_host()
->policies()
.document_isolation_policy,
DipNone());
}
// DIP into DIP.
{
SCOPED_TRACE("DIP to DIP");
// Start on a DIP page.
EXPECT_TRUE(NavigateToURL(shell(), dip_page));
scoped_refptr<SiteInstance> initial_site_instance(
current_frame_host()->GetSiteInstance());
// Navigate to a DIP page.
TestNavigationManager dip_navigation(web_contents(), dip_page);
shell()->LoadURL(dip_page);
EXPECT_TRUE(dip_navigation.WaitForRequestStart());
auto* speculative_rfh = web_contents()
->GetPrimaryFrameTree()
.root()
->render_manager()
->speculative_frame_host();
// The navigation is considered cross-site, because the AgentClusterKey of
// the current page has an IsolationKey, and the request does not have one.
// TODO(https://issues.chromium.org/343914483): Avoid creating a speculative
// RFH in this case.
EXPECT_TRUE(speculative_rfh);
ASSERT_TRUE(dip_navigation.WaitForNavigationFinished());
EXPECT_TRUE(current_frame_host()->GetSiteInstance()->IsRelatedSiteInstance(
initial_site_instance.get()));
EXPECT_EQ(current_frame_host()
->policy_container_host()
->policies()
.document_isolation_policy,
GetDocumentIsolationPolicy());
}
}
// A test to make sure that loading a page with DIP sets
// requires_origin_keyed_process() on the SiteInstance's SiteInfo.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest, DipOriginKeyed) {
GURL isolated_page = GetDocumentIsolationPolicyURL("a.test");
EXPECT_TRUE(NavigateToURL(shell(), isolated_page));
SiteInstanceImpl* current_si = current_frame_host()->GetSiteInstance();
EXPECT_TRUE(current_si->IsCrossOriginIsolated());
// Currently, use of the DIP header does not cause
// SiteInfo::requires_origin_keyed_process() to return true. In practice, the
// process will be origin-keyed because the AgentClusterKey is. Once we
// refactor Origin-Agent-Cluster to use the AgentClusterKey, using DIP should
// also cause SiteInfo::requires_origin_keyed_process() to return true.
// Note: if kOriginKeyedProcessesByDefault is enabled, then
// requires_origin_keyed_process() will return true.
EXPECT_EQ(SiteIsolationPolicy::AreOriginKeyedProcessesEnabledByDefault(),
current_si->GetSiteInfo().requires_origin_keyed_process());
EXPECT_TRUE(current_si->GetSiteInfo().agent_cluster_key()->IsOriginKeyed());
}
// Tests that main frame navigations are correctly assigned cross-origin
// isolated SiteInstances based on their DocumentIsolationPolicy.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
CrossOriginIsolatedSiteInstance_MainFrame) {
GURL isolated_page = GetDocumentIsolationPolicyURL("a.test");
GURL isolated_page_b = GetDocumentIsolationPolicyURL("cdn.a.test");
GURL non_isolated_page(https_server()->GetURL("a.test", "/title1.html"));
// Navigation from/to cross-origin isolated pages.
// Initial non cross-origin isolated page.
{
EXPECT_TRUE(NavigateToURL(shell(), non_isolated_page));
SiteInstanceImpl* current_si = current_frame_host()->GetSiteInstance();
EXPECT_FALSE(current_si->IsCrossOriginIsolated());
}
// Navigation to a cross-origin isolated page.
{
scoped_refptr<SiteInstanceImpl> previous_si =
current_frame_host()->GetSiteInstance();
EXPECT_TRUE(NavigateToURL(shell(), isolated_page));
SiteInstanceImpl* current_si = current_frame_host()->GetSiteInstance();
EXPECT_TRUE(current_si->IsCrossOriginIsolated());
EXPECT_NE(current_si->GetProcess(), previous_si->GetProcess());
// The navigation triggers a speculative BrowsingInstance swap because it is
// browser-initiated and end up being cross-site due to the DIP mismatch.
EXPECT_FALSE(current_si->IsRelatedSiteInstance(previous_si.get()));
}
// Navigation to a non cross-origin isolated page.
{
scoped_refptr<SiteInstanceImpl> previous_si =
current_frame_host()->GetSiteInstance();
EXPECT_TRUE(NavigateToURL(shell(), non_isolated_page));
SiteInstanceImpl* current_si = current_frame_host()->GetSiteInstance();
EXPECT_FALSE(current_si->IsCrossOriginIsolated());
EXPECT_NE(current_si->GetProcess(), previous_si->GetProcess());
// The navigation triggers a speculative BrowsingInstance swap because it is
// browser-initiated and end up being cross-site due to the DIP mismatch.
EXPECT_FALSE(current_si->IsRelatedSiteInstance(previous_si.get()));
}
// Back navigation from a a non cross-origin isolated page to a cross-origin
// isolated page.
{
scoped_refptr<SiteInstanceImpl> previous_si =
current_frame_host()->GetSiteInstance();
web_contents()->GetController().GoBack();
EXPECT_TRUE(WaitForLoadStop(web_contents()));
SiteInstanceImpl* current_si = current_frame_host()->GetSiteInstance();
EXPECT_TRUE(current_si->IsCrossOriginIsolated());
EXPECT_NE(current_si->GetProcess(), previous_si->GetProcess());
// The navigation triggers a speculative BrowsingInstance swap because it is
// browser-initiated and end up being cross-site due to the DIP mismatch.
EXPECT_FALSE(current_si->IsRelatedSiteInstance(previous_si.get()));
}
// Back navigation to the non cross-origin isolated initial page.
{
scoped_refptr<SiteInstanceImpl> previous_si =
current_frame_host()->GetSiteInstance();
web_contents()->GetController().GoBack();
EXPECT_TRUE(WaitForLoadStop(web_contents()));
SiteInstanceImpl* current_si = current_frame_host()->GetSiteInstance();
EXPECT_FALSE(current_si->IsCrossOriginIsolated());
EXPECT_NE(current_si->GetProcess(), previous_si->GetProcess());
// The navigation triggers a speculative BrowsingInstance swap because it is
// browser-initiated and end up being cross-site due to the DIP mismatch.
EXPECT_FALSE(current_si->IsRelatedSiteInstance(previous_si.get()));
}
// Cross origin navigation in between two cross-origin isolated pages.
{
EXPECT_TRUE(NavigateToURL(shell(), isolated_page));
scoped_refptr<SiteInstanceImpl> site_instance_1 =
current_frame_host()->GetSiteInstance();
EXPECT_TRUE(NavigateToURL(shell(), isolated_page_b));
SiteInstanceImpl* site_instance_2 = current_frame_host()->GetSiteInstance();
EXPECT_TRUE(site_instance_1->IsCrossOriginIsolated());
EXPECT_TRUE(site_instance_2->IsCrossOriginIsolated());
EXPECT_NE(site_instance_1->GetProcess(), site_instance_2->GetProcess());
// The navigation triggers a speculative BrowsingInstance swap because it is
// browser-initiated and cross-site.
EXPECT_FALSE(site_instance_1->IsRelatedSiteInstance(site_instance_2));
}
}
// Tests that renderer-initiated main frames navigations are assigned a
// cross-origin isolated SiteInstance based on their DocumentIsolationPolicy.
IN_PROC_BROWSER_TEST_P(
DocumentIsolationPolicyBrowserTest,
CrossOriginIsolatedSiteInstance_MainFrameRendererInitiated) {
GURL isolated_page = GetDocumentIsolationPolicyURL("a.test");
GURL isolated_page_b = GetDocumentIsolationPolicyURL("cdn.a.test");
GURL non_isolated_page(https_server()->GetURL("a.test", "/title1.html"));
// Navigation from/to cross-origin isolated pages.
// Initial non cross-origin isolated page.
{
EXPECT_TRUE(NavigateToURL(shell(), non_isolated_page));
SiteInstanceImpl* current_si = current_frame_host()->GetSiteInstance();
EXPECT_FALSE(current_si->IsCrossOriginIsolated());
}
// Navigation to a cross-origin isolated page.
{
scoped_refptr<SiteInstanceImpl> previous_si =
current_frame_host()->GetSiteInstance();
EXPECT_TRUE(NavigateToURLFromRenderer(shell(), isolated_page));
SiteInstanceImpl* current_si = current_frame_host()->GetSiteInstance();
EXPECT_TRUE(current_si->IsCrossOriginIsolated());
EXPECT_NE(current_si->GetProcess(), previous_si->GetProcess());
// When BfCache is enabled, a pro-active BrowsingInstance swap happens.
if (IsBackForwardCacheEnabled()) {
EXPECT_FALSE(current_si->IsRelatedSiteInstance(previous_si.get()));
} else {
EXPECT_TRUE(current_si->IsRelatedSiteInstance(previous_si.get()));
}
}
// Navigation to the same cross-origin isolated page.
{
scoped_refptr<SiteInstanceImpl> previous_si =
current_frame_host()->GetSiteInstance();
EXPECT_TRUE(NavigateToURLFromRenderer(shell(), isolated_page));
SiteInstanceImpl* current_si = current_frame_host()->GetSiteInstance();
EXPECT_TRUE(current_si->IsCrossOriginIsolated());
EXPECT_EQ(current_si, previous_si);
}
// Navigation to a non cross-origin isolated page.
{
scoped_refptr<SiteInstanceImpl> previous_si =
current_frame_host()->GetSiteInstance();
EXPECT_TRUE(NavigateToURLFromRenderer(shell(), non_isolated_page));
SiteInstanceImpl* current_si = current_frame_host()->GetSiteInstance();
EXPECT_FALSE(current_si->IsCrossOriginIsolated());
EXPECT_NE(current_si->GetProcess(), previous_si->GetProcess());
// When BfCache is enabled, a pro-active BrowsingInstance swap happens.
if (IsBackForwardCacheEnabled()) {
EXPECT_FALSE(current_si->IsRelatedSiteInstance(previous_si.get()));
} else {
EXPECT_TRUE(current_si->IsRelatedSiteInstance(previous_si.get()));
}
}
// Navigate back to a cross-origin isolated page.
{
scoped_refptr<SiteInstanceImpl> previous_si =
current_frame_host()->GetSiteInstance();
web_contents()->GetController().GoBack();
ASSERT_TRUE(WaitForLoadStop(web_contents()));
SiteInstanceImpl* current_si = current_frame_host()->GetSiteInstance();
EXPECT_TRUE(current_si->IsCrossOriginIsolated());
EXPECT_NE(current_si->GetProcess(), previous_si->GetProcess());
// When BfCache is enabled, a pro-active BrowsingInstance swap happens.
if (IsBackForwardCacheEnabled()) {
EXPECT_FALSE(current_si->IsRelatedSiteInstance(previous_si.get()));
} else {
EXPECT_TRUE(current_si->IsRelatedSiteInstance(previous_si.get()));
}
}
// Cross origin navigation in between two cross-origin isolated pages.
{
EXPECT_TRUE(NavigateToURLFromRenderer(shell(), isolated_page));
scoped_refptr<SiteInstanceImpl> site_instance_1 =
current_frame_host()->GetSiteInstance();
EXPECT_TRUE(NavigateToURLFromRenderer(shell(), isolated_page_b));
SiteInstanceImpl* site_instance_2 = current_frame_host()->GetSiteInstance();
EXPECT_TRUE(site_instance_1->IsCrossOriginIsolated());
EXPECT_TRUE(site_instance_2->IsCrossOriginIsolated());
EXPECT_NE(site_instance_1->GetProcess(), site_instance_2->GetProcess());
// When BfCache is enabled, a pro-active BrowsingInstance swap happens.
if (IsBackForwardCacheEnabled()) {
EXPECT_FALSE(
site_instance_2->IsRelatedSiteInstance(site_instance_1.get()));
} else {
EXPECT_TRUE(
site_instance_2->IsRelatedSiteInstance(site_instance_1.get()));
}
}
}
// Tests that iframe navigations are assigned a cross-origin isolated
// SiteInstance based on their DocumentIsolationPolicy.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
CrossOriginIsolatedSiteInstance_IFrame) {
GURL isolated_page = GetDocumentIsolationPolicyURL("a.test");
GURL isolated_page_b = GetDocumentIsolationPolicyURL("cdn.a.test");
GURL non_isolated_page(https_server()->GetURL("a.test", "/title1.html"));
// Initial cross-origin isolated page.
EXPECT_TRUE(NavigateToURL(shell(), isolated_page));
SiteInstanceImpl* main_si = current_frame_host()->GetSiteInstance();
EXPECT_TRUE(main_si->IsCrossOriginIsolated());
// Same origin cross-origin isolated iframe.
TestNavigationManager coi_iframe_navigation(web_contents(), isolated_page);
EXPECT_TRUE(
ExecJs(web_contents(),
JsReplace("const iframe = document.createElement('iframe'); "
"iframe.src = $1; "
"document.body.appendChild(iframe);",
isolated_page)));
ASSERT_TRUE(coi_iframe_navigation.WaitForNavigationFinished());
EXPECT_TRUE(coi_iframe_navigation.was_successful());
RenderFrameHostImpl* coi_iframe_rfh =
current_frame_host()->child_at(0)->current_frame_host();
SiteInstanceImpl* coi_iframe_si = coi_iframe_rfh->GetSiteInstance();
EXPECT_EQ(coi_iframe_si, main_si);
// Same origin non cross-origin isolated iframe.
TestNavigationManager non_coi_iframe_navigation(web_contents(),
non_isolated_page);
EXPECT_TRUE(
ExecJs(web_contents(),
JsReplace("const iframe = document.createElement('iframe'); "
"iframe.src = $1; "
"document.body.appendChild(iframe);",
non_isolated_page)));
ASSERT_TRUE(non_coi_iframe_navigation.WaitForNavigationFinished());
EXPECT_TRUE(non_coi_iframe_navigation.was_successful());
RenderFrameHostImpl* non_coi_iframe_rfh =
current_frame_host()->child_at(1)->current_frame_host();
SiteInstanceImpl* non_coi_iframe_si = non_coi_iframe_rfh->GetSiteInstance();
EXPECT_FALSE(non_coi_iframe_si->IsCrossOriginIsolated());
EXPECT_TRUE(non_coi_iframe_si->IsRelatedSiteInstance(main_si));
EXPECT_NE(non_coi_iframe_si, main_si);
EXPECT_NE(non_coi_iframe_si->GetProcess(), main_si->GetProcess());
EXPECT_NE(non_coi_iframe_si, coi_iframe_si);
EXPECT_NE(non_coi_iframe_si->GetProcess(), coi_iframe_si->GetProcess());
// Cross origin iframe.
TestNavigationManager cross_origin_iframe_navigation(web_contents(),
isolated_page_b);
EXPECT_TRUE(
ExecJs(web_contents(),
JsReplace("const iframe = document.createElement('iframe'); "
"iframe.src = $1; "
"document.body.appendChild(iframe);",
isolated_page_b)));
ASSERT_TRUE(cross_origin_iframe_navigation.WaitForNavigationFinished());
EXPECT_TRUE(cross_origin_iframe_navigation.was_successful());
RenderFrameHostImpl* iframe_rfh =
current_frame_host()->child_at(2)->current_frame_host();
SiteInstanceImpl* cross_origin_iframe_si = iframe_rfh->GetSiteInstance();
EXPECT_TRUE(cross_origin_iframe_si->IsCrossOriginIsolated());
EXPECT_TRUE(cross_origin_iframe_si->IsRelatedSiteInstance(main_si));
EXPECT_NE(cross_origin_iframe_si, main_si);
EXPECT_NE(cross_origin_iframe_si->GetProcess(), main_si->GetProcess());
EXPECT_NE(cross_origin_iframe_si, coi_iframe_si);
EXPECT_NE(cross_origin_iframe_si->GetProcess(), coi_iframe_si->GetProcess());
EXPECT_NE(cross_origin_iframe_si, non_coi_iframe_si);
EXPECT_NE(cross_origin_iframe_si->GetProcess(),
non_coi_iframe_si->GetProcess());
// Navigate to a non cross-origin isolated page with a cross-origin isolated
// iframe.
{
scoped_refptr<SiteInstanceImpl> previous_si =
current_frame_host()->GetSiteInstance();
EXPECT_TRUE(NavigateToURLFromRenderer(shell(), non_isolated_page));
main_si = current_frame_host()->GetSiteInstance();
EXPECT_FALSE(main_si->IsCrossOriginIsolated());
EXPECT_NE(main_si->GetProcess(), previous_si->GetProcess());
// When BfCache is enabled, a pro-active BrowsingInstance swap happens.
if (IsBackForwardCacheEnabled()) {
EXPECT_FALSE(main_si->IsRelatedSiteInstance(previous_si.get()));
} else {
EXPECT_TRUE(main_si->IsRelatedSiteInstance(previous_si.get()));
}
TestNavigationManager same_origin_iframe_navigation(web_contents(),
isolated_page);
EXPECT_TRUE(
ExecJs(web_contents(),
JsReplace("const iframe = document.createElement('iframe'); "
"iframe.src = $1; "
"document.body.appendChild(iframe);",
isolated_page)));
ASSERT_TRUE(same_origin_iframe_navigation.WaitForNavigationFinished());
EXPECT_TRUE(same_origin_iframe_navigation.was_successful());
iframe_rfh = current_frame_host()->child_at(0)->current_frame_host();
SiteInstanceImpl* iframe_si = iframe_rfh->GetSiteInstance();
EXPECT_TRUE(iframe_si->IsCrossOriginIsolated());
EXPECT_TRUE(iframe_si->IsRelatedSiteInstance(main_si));
EXPECT_NE(iframe_si, main_si);
EXPECT_NE(iframe_si->GetProcess(), main_si->GetProcess());
}
}
// Tests that navigations in popups are correctly assigned a cross-origin
// isolated SiteInstance based on their DocumentIsolationPolicy.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
CrossOriginIsolatedSiteInstance_Popup) {
GURL isolated_page = GetDocumentIsolationPolicyURL("a.test");
GURL isolated_page_b = GetDocumentIsolationPolicyURL("cdn.a.test");
GURL non_isolated_page(
embedded_test_server()->GetURL("a.test", "/title1.html"));
// Initial cross-origin isolated page.
EXPECT_TRUE(NavigateToURL(shell(), isolated_page));
SiteInstanceImpl* main_si = current_frame_host()->GetSiteInstance();
EXPECT_TRUE(main_si->IsCrossOriginIsolated());
// Open a non isolated popup.
{
RenderFrameHostImpl* popup_rfh =
static_cast<WebContentsImpl*>(
OpenPopup(current_frame_host(), non_isolated_page, "")
->web_contents())
->GetPrimaryMainFrame();
EXPECT_FALSE(popup_rfh->GetSiteInstance()->IsCrossOriginIsolated());
EXPECT_TRUE(popup_rfh->GetSiteInstance()->IsRelatedSiteInstance(
current_frame_host()->GetSiteInstance()));
}
// Open an isolated popup.
{
RenderFrameHostImpl* popup_rfh =
static_cast<WebContentsImpl*>(
OpenPopup(current_frame_host(), isolated_page, "")->web_contents())
->GetPrimaryMainFrame();
EXPECT_TRUE(popup_rfh->GetSiteInstance()->IsCrossOriginIsolated());
EXPECT_EQ(popup_rfh->GetSiteInstance(),
current_frame_host()->GetSiteInstance());
}
// Open an isolated popup, but cross-origin.
{
RenderFrameHostImpl* popup_rfh =
static_cast<WebContentsImpl*>(
OpenPopup(current_frame_host(), isolated_page_b, "")
->web_contents())
->GetPrimaryMainFrame();
EXPECT_TRUE(popup_rfh->GetSiteInstance()->IsCrossOriginIsolated());
EXPECT_TRUE(popup_rfh->GetSiteInstance()->IsRelatedSiteInstance(
current_frame_host()->GetSiteInstance()));
EXPECT_NE(popup_rfh->GetSiteInstance()->GetProcess(),
current_frame_host()->GetSiteInstance()->GetProcess());
}
}
// Tests that navigations involving error pages are correctly assigned a
// cross-origin isolated SiteInstance based on their DocumentIsolationPolicy
// status.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
CrossOriginIsolatedSiteInstance_ErrorPage) {
GURL isolated_page = GetDocumentIsolationPolicyURL(
"a.test", "Cross-Origin-Embedder-Policy: require-corp");
GURL non_coep_page(https_server()->GetURL("b.test",
"/set-header?"
"Access-Control-Allow-Origin: *"));
GURL invalid_url(
https_server()->GetURL("a.test", "/this_page_does_not_exist.html"));
GURL error_url(https_server()->GetURL("a.test", "/page404.html"));
GURL non_isolated_page(
https_server()->GetURL("a.test",
"/set-header?"
"Cross-Origin-Embedder-Policy: require-corp"));
// Initial cross-origin isolated page.
EXPECT_TRUE(NavigateToURL(shell(), isolated_page));
SiteInstanceImpl* main_si = current_frame_host()->GetSiteInstance();
EXPECT_TRUE(main_si->IsCrossOriginIsolated());
// Iframe.
{
TestNavigationManager iframe_navigation(web_contents(), invalid_url);
EXPECT_TRUE(
ExecJs(web_contents(),
JsReplace("const iframe = document.createElement('iframe'); "
"iframe.src = $1; "
"document.body.appendChild(iframe);",
invalid_url)));
ASSERT_TRUE(iframe_navigation.WaitForNavigationFinished());
EXPECT_FALSE(iframe_navigation.was_successful());
RenderFrameHostImpl* iframe_rfh =
current_frame_host()->child_at(0)->current_frame_host();
SiteInstanceImpl* iframe_si = iframe_rfh->GetSiteInstance();
// The load of the document with 404 status code is blocked by COEP.
// An error page is expected in lieu of that document.
EXPECT_EQ(GURL(kUnreachableWebDataURL),
EvalJs(iframe_rfh, "document.location.href;"));
EXPECT_TRUE(IsExpectedSubframeErrorTransition(main_si, iframe_si));
EXPECT_FALSE(iframe_si->IsCrossOriginIsolated());
EXPECT_NE(iframe_si, main_si);
}
// Iframe with a body added to the HTTP 404.
{
TestNavigationManager iframe_navigation(web_contents(), error_url);
EXPECT_TRUE(
ExecJs(web_contents(),
JsReplace("const iframe = document.createElement('iframe'); "
"iframe.src = $1; "
"document.body.appendChild(iframe);",
error_url)));
ASSERT_TRUE(iframe_navigation.WaitForNavigationFinished());
EXPECT_FALSE(iframe_navigation.was_successful());
RenderFrameHostImpl* iframe_rfh =
current_frame_host()->child_at(0)->current_frame_host();
SiteInstanceImpl* iframe_si = iframe_rfh->GetSiteInstance();
EXPECT_TRUE(IsExpectedSubframeErrorTransition(main_si, iframe_si));
// The load of the document with 404 status code and custom body is blocked
// by COEP. An error page is expected in lieu of that document.
EXPECT_EQ(GURL(kUnreachableWebDataURL),
EvalJs(iframe_rfh, "document.location.href;"));
EXPECT_FALSE(iframe_si->IsCrossOriginIsolated());
EXPECT_NE(iframe_si, main_si);
}
// Iframe blocked by coep.
{
TestNavigationManager iframe_navigation(web_contents(), non_coep_page);
EXPECT_TRUE(
ExecJs(web_contents(),
JsReplace("const iframe = document.createElement('iframe'); "
"iframe.src = $1; "
"document.body.appendChild(iframe);",
non_coep_page)));
ASSERT_TRUE(iframe_navigation.WaitForNavigationFinished());
EXPECT_FALSE(iframe_navigation.was_successful());
RenderFrameHostImpl* iframe_rfh =
current_frame_host()->child_at(0)->current_frame_host();
SiteInstanceImpl* iframe_si = iframe_rfh->GetSiteInstance();
EXPECT_TRUE(IsExpectedSubframeErrorTransition(main_si, iframe_si));
EXPECT_FALSE(iframe_si->IsCrossOriginIsolated());
}
// Top frame.
{
scoped_refptr<SiteInstanceImpl> previous_si =
current_frame_host()->GetSiteInstance();
EXPECT_FALSE(NavigateToURL(shell(), invalid_url));
SiteInstanceImpl* current_si = current_frame_host()->GetSiteInstance();
if (IsBackForwardCacheEnabled()) {
EXPECT_FALSE(current_si->IsRelatedSiteInstance(previous_si.get()));
} else {
EXPECT_TRUE(current_si->IsRelatedSiteInstance(previous_si.get()));
}
EXPECT_NE(current_si->GetProcess(), previous_si->GetProcess());
EXPECT_FALSE(current_si->IsCrossOriginIsolated());
}
// DIP iframe inside non-DIP page.
{
EXPECT_TRUE(NavigateToURL(shell(), non_isolated_page));
SiteInstanceImpl* current_si = current_frame_host()->GetSiteInstance();
EXPECT_FALSE(current_si->IsCrossOriginIsolated());
// First, add an iframe with DocumentIsolationPolicy.
TestNavigationManager iframe_navigation(web_contents(), isolated_page);
EXPECT_TRUE(
ExecJs(web_contents(),
JsReplace("const iframe = document.createElement('iframe'); "
"iframe.id = 'iframe';"
"iframe.src = $1; "
"document.body.appendChild(iframe);",
isolated_page)));
ASSERT_TRUE(iframe_navigation.WaitForNavigationFinished());
EXPECT_TRUE(iframe_navigation.was_successful());
RenderFrameHostImpl* iframe_rfh =
current_frame_host()->child_at(0)->current_frame_host();
scoped_refptr<SiteInstanceImpl> iframe_si = iframe_rfh->GetSiteInstance();
EXPECT_TRUE(iframe_si->IsCrossOriginIsolated());
EXPECT_NE(current_si, iframe_si);
EXPECT_NE(current_si->GetProcess(), iframe_si->GetProcess());
// Now navigate the iframe to an error page.
TestNavigationManager error_navigation(web_contents(), invalid_url);
EXPECT_TRUE(ExecJs(
web_contents(),
JsReplace("document.getElementById('iframe').src = $1;", invalid_url)));
ASSERT_TRUE(error_navigation.WaitForNavigationFinished());
EXPECT_FALSE(error_navigation.was_successful());
iframe_rfh = current_frame_host()->child_at(0)->current_frame_host();
scoped_refptr<SiteInstanceImpl> error_si = iframe_rfh->GetSiteInstance();
EXPECT_FALSE(error_si->IsCrossOriginIsolated());
EXPECT_NE(error_si, iframe_si);
EXPECT_NE(error_si->GetProcess(), iframe_si->GetProcess());
EXPECT_EQ(error_si, current_si);
EXPECT_EQ(error_si->GetProcess(), current_si->GetProcess());
}
}
// Tests that a reload navigation that redirects to a page with a
// Document-Isolation-Policy header is placed in a cross-origin isolated
// SiteInstance, even if the original page did not have DocumentIsolationPolicy.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
ReloadRedirectsToDipPage) {
GURL dip_page = GetDocumentIsolationPolicyURL("a.test");
GURL redirect_page(https_server()->GetURL(
"a.test", "/redirect-on-second-navigation?" + dip_page.spec()));
// Navigate to the redirect page. On the first navigation, this is a simple
// empty page with no headers.
EXPECT_TRUE(NavigateToURL(shell(), redirect_page));
scoped_refptr<SiteInstanceImpl> main_si =
current_frame_host()->GetSiteInstance();
EXPECT_EQ(current_frame_host()->GetLastCommittedURL(), redirect_page);
// Reload. This time we should be redirected to a DIP:
// isolate-and-require-corp page.
ReloadBlockUntilNavigationsComplete(shell(), 1);
EXPECT_EQ(current_frame_host()->GetLastCommittedURL(), dip_page);
// We should have swapped SiteInstance.
EXPECT_NE(main_si, current_frame_host()->GetSiteInstance());
EXPECT_TRUE(
main_si->IsRelatedSiteInstance(current_frame_host()->GetSiteInstance()));
EXPECT_NE(main_si->GetProcess(),
current_frame_host()->GetSiteInstance()->GetProcess());
}
// Tests that a reload navigation where the page starts sending
// DocumentIsolationPolicy header on the reload (while the initial load did not
// have them). Tha navigation should end up in a cross-origin isolated
// SiteInstance.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
ReloadPageWithUpdatedDipHeader) {
GURL changing_dip_page(
https_server()->GetURL("a.test", "/serve-dip-on-second-navigation"));
// Navigate to the page. On the first navigation, this is a simple empty page
// with no headers.
EXPECT_TRUE(NavigateToURL(shell(), changing_dip_page));
scoped_refptr<SiteInstanceImpl> main_si =
current_frame_host()->GetSiteInstance();
// Reload. This time the page should be served with DIP:
// isolate-and-require-corp.
ReloadBlockUntilNavigationsComplete(shell(), 1);
// We should have swapped SiteInstance.
EXPECT_NE(main_si, current_frame_host()->GetSiteInstance());
EXPECT_TRUE(
main_si->IsRelatedSiteInstance(current_frame_host()->GetSiteInstance()));
EXPECT_NE(main_si->GetProcess(),
current_frame_host()->GetSiteInstance()->GetProcess());
}
// Checks that a cross-origin but same site iframe is placed in a different
// SiteInstance from its parent when both have DocumentIsolationPolicy.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
CrossOriginSameSiteIframe) {
GURL isolated_page = GetDocumentIsolationPolicyURL("a.test");
GURL isolated_page_b = GetDocumentIsolationPolicyURL("cdn.a.test");
// Initial cross-origin isolated page.
EXPECT_TRUE(NavigateToURL(shell(), isolated_page));
SiteInstanceImpl* main_si = current_frame_host()->GetSiteInstance();
EXPECT_TRUE(main_si->IsCrossOriginIsolated());
TestNavigationManager cross_origin_iframe_navigation(web_contents(),
isolated_page_b);
// Add a cross-origin but same-site iframe.
EXPECT_TRUE(
ExecJs(web_contents(),
JsReplace("const iframe = document.createElement('iframe'); "
"iframe.src = $1; "
"document.body.appendChild(iframe);",
isolated_page_b)));
ASSERT_TRUE(cross_origin_iframe_navigation.WaitForNavigationFinished());
EXPECT_TRUE(cross_origin_iframe_navigation.was_successful());
RenderFrameHostImpl* iframe_rfh =
current_frame_host()->child_at(0)->current_frame_host();
SiteInstanceImpl* iframe_si = iframe_rfh->GetSiteInstance();
EXPECT_NE(iframe_si, main_si);
EXPECT_TRUE(iframe_si->IsCrossOriginIsolated());
EXPECT_TRUE(iframe_si->IsRelatedSiteInstance(main_si));
EXPECT_NE(iframe_si->GetProcess(), main_si->GetProcess());
// Open an isolated popup from the cross-origin but same-site iframe. It
// should end up in the same SiteInstance as the main frame, since they are
// same-origin with the same Document-Isolation-Policy.
{
RenderFrameHostImpl* popup_rfh =
static_cast<WebContentsImpl*>(
OpenPopup(iframe_rfh, isolated_page, "", "", true)->web_contents())
->GetPrimaryMainFrame();
EXPECT_EQ(main_si, popup_rfh->GetSiteInstance());
}
}
// Checks that the WebExposedIsolationLevel of a RenderFrameHost is properly
// computed when cross-origin isolation is enabled through
// DocumentIsolationPolicy.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
WebExposedIsolationLevel) {
GURL isolated_page = GetDocumentIsolationPolicyURL("a.test");
GURL isolated_page_b = GetDocumentIsolationPolicyURL("b.test");
// Not isolated:
EXPECT_TRUE(NavigateToURL(shell(), https_server()->GetURL("/empty.html")));
EXPECT_EQ(WebExposedIsolationLevel::kNotIsolated,
current_frame_host()->GetWebExposedIsolationLevel());
// Cross-Origin Isolated:
EXPECT_TRUE(NavigateToURL(shell(), isolated_page));
EXPECT_EQ(WebExposedIsolationLevel::kIsolated,
current_frame_host()->GetWebExposedIsolationLevel());
// Cross-origin isolated iframe without permission delegation. The iframe
// should be cross-origin isolated, as the permission only applies to
// cross-origin isolation inherited from the parent (and enabled by COOP &
// COEP).
std::string create_iframe = R"(
new Promise(resolve => {
const iframe = document.createElement('iframe');
iframe.src = $1;
iframe.allow = "cross-origin-isolated 'none'";
iframe.addEventListener('load', () => resolve(true));
document.body.appendChild(iframe);
});
)";
EXPECT_TRUE(ExecJs(shell(), JsReplace(create_iframe, isolated_page_b)));
RenderFrameHostImpl* iframe_rfh =
current_frame_host()->child_at(0)->current_frame_host();
EXPECT_EQ(WebExposedIsolationLevel::kIsolated,
iframe_rfh->GetWebExposedIsolationLevel());
}
// Checks that a document with document isolation policy has its
// crossOriginIsolated property set to true and can instantiate
// SharedArrayBuffers.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest, SAB) {
GURL url = GetDocumentIsolationPolicyURL("a.test");
EXPECT_TRUE(NavigateToURL(shell(), url));
EXPECT_EQ(true, EvalJs(current_frame_host(), "self.crossOriginIsolated"));
EXPECT_EQ(true,
EvalJs(current_frame_host(), "'SharedArrayBuffer' in globalThis"));
}
// Checks that a document with document isolation policy can transfer a
// SharedArrayBuffer to a crossOriginIsolated same-origin iframe.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
SAB_TransferToIframe) {
CHECK(!base::FeatureList::IsEnabled(features::kSharedArrayBuffer));
GURL url = GetDocumentIsolationPolicyURL("a.test");
EXPECT_TRUE(NavigateToURL(shell(), url));
EXPECT_TRUE(ExecJs(current_frame_host(),
"g_iframe = document.createElement('iframe');"
"g_iframe.src = location.href;"
"document.body.appendChild(g_iframe);"));
WaitForLoadStop(web_contents());
RenderFrameHostImpl* main_document = current_frame_host();
RenderFrameHostImpl* sub_document =
current_frame_host()->child_at(0)->current_frame_host();
EXPECT_EQ(true, EvalJs(main_document, "self.crossOriginIsolated"));
EXPECT_EQ(true, EvalJs(sub_document, "self.crossOriginIsolated"));
EXPECT_TRUE(ExecJs(sub_document, R"(
g_sab_size = new Promise(resolve => {
addEventListener("message", event => resolve(event.data.byteLength));
});
)",
EXECUTE_SCRIPT_NO_RESOLVE_PROMISES));
EXPECT_TRUE(ExecJs(main_document, R"(
const sab = new SharedArrayBuffer(1234);
g_iframe.contentWindow.postMessage(sab, "*");
)"));
EXPECT_EQ(1234, EvalJs(sub_document, "g_sab_size"));
}
// Checks that a document with document isolation policy can transfer a
// SharedArrayBuffer to a crossOriginIsolated same-origin about:blank iframe.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
SAB_TransferToAboutBlankIframe) {
CHECK(!base::FeatureList::IsEnabled(features::kSharedArrayBuffer));
GURL url = GetDocumentIsolationPolicyURL("a.test");
EXPECT_TRUE(NavigateToURL(shell(), url));
EXPECT_TRUE(ExecJs(current_frame_host(),
"g_iframe = document.createElement('iframe');"
"g_iframe.src = 'about:blank';"
"document.body.appendChild(g_iframe);"));
WaitForLoadStop(web_contents());
RenderFrameHostImpl* main_document = current_frame_host();
RenderFrameHostImpl* sub_document =
current_frame_host()->child_at(0)->current_frame_host();
EXPECT_EQ(true, EvalJs(main_document, "self.crossOriginIsolated"));
EXPECT_EQ(true, EvalJs(sub_document, "self.crossOriginIsolated"));
EXPECT_EQ(true, EvalJs(sub_document, "'SharedArrayBuffer' in globalThis"));
}
// Checks that an about:blank iframe created by a cross-origin isolated document
// (through Document-Isolation-Policy) is set as crossOriginIsolated
// synchronously.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
AboutBlankIsSetCOISynchronously) {
CHECK(!base::FeatureList::IsEnabled(features::kSharedArrayBuffer));
GURL url = GetDocumentIsolationPolicyURL("a.test");
EXPECT_TRUE(NavigateToURL(shell(), url));
EXPECT_EQ(true, EvalJs(current_frame_host(),
"const iframe = document.createElement('iframe');"
"document.body.appendChild(iframe);"
"iframe.contentWindow.crossOriginIsolated;"));
}
// Transfer a SharedArrayBuffer in between two documents with a parent/child
// relationship. The child has not set Document-Isolation-Policy, and is not
// cross-origin isolated. It cannot receive the object.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
SAB_TransferToNoCrossOriginIsolatedIframe) {
CHECK(!base::FeatureList::IsEnabled(features::kSharedArrayBuffer));
GURL main_url = GetDocumentIsolationPolicyURL("a.test");
GURL iframe_url = https_server()->GetURL("a.test", "/title1.html");
EXPECT_TRUE(NavigateToURL(shell(), main_url));
EXPECT_TRUE(ExecJs(current_frame_host(),
JsReplace("g_iframe = document.createElement('iframe');"
"g_iframe.src = $1;"
"document.body.appendChild(g_iframe);",
iframe_url)));
WaitForLoadStop(web_contents());
RenderFrameHostImpl* main_document = current_frame_host();
RenderFrameHostImpl* sub_document =
current_frame_host()->child_at(0)->current_frame_host();
EXPECT_EQ(true, EvalJs(main_document, "self.crossOriginIsolated"));
EXPECT_EQ(false, EvalJs(sub_document, "self.crossOriginIsolated"));
// The parent and its child frame are in different processes, so it's not
// possible to transfer a SharedArrayBuffer between the two of them.
EXPECT_NE(main_document->GetSiteInstance()->GetProcess(),
sub_document->GetSiteInstance()->GetProcess());
}
// Transfer a SharedArrayBuffer in between two documents with a
// parent/child relationship. The child does not have Document-Isolation-Policy.
// This non-cross-origin-isolated document cannot transfer a SharedArrayBuffer
// toward the cross-origin-isolated one.
IN_PROC_BROWSER_TEST_P(DocumentIsolationPolicyBrowserTest,
SAB_TransferFromNoCrossOriginIsolatedIframe) {
CHECK(!base::FeatureList::IsEnabled(features::kSharedArrayBuffer));
GURL main_url = GetDocumentIsolationPolicyURL("a.test");
GURL iframe_url = https_server()->GetURL("a.test", "/title1.html");
EXPECT_TRUE(NavigateToURL(shell(), main_url));
EXPECT_TRUE(ExecJs(current_frame_host(),
JsReplace("g_iframe = document.createElement('iframe');"
"g_iframe.src = $1;"
"document.body.appendChild(g_iframe);",
iframe_url)));
WaitForLoadStop(web_contents());
RenderFrameHostImpl* main_document = current_frame_host();
RenderFrameHostImpl* sub_document =
current_frame_host()->child_at(0)->current_frame_host();
EXPECT_EQ(true, EvalJs(main_document, "self.crossOriginIsolated"));
EXPECT_EQ(false, EvalJs(sub_document, "self.crossOriginIsolated"));
EXPECT_TRUE(ExecJs(main_document, R"(
g_sab_size = new Promise(resolve => {
addEventListener("message", event => resolve(event.data.byteLength));
});
)",
EXECUTE_SCRIPT_NO_RESOLVE_PROMISES));
EXPECT_EQ(false, EvalJs(sub_document, "'SharedArrayBuffer' in globalThis"));
// Unlike crossOriginIsolation enabled by COOP and COEP, the SAB cannot be
// created in the non-crossOriginIsolated iframe.
// See https://crbug.com/1144838 for discussions about this behavior in COOP
// and COEP.
EXPECT_FALSE(ExecJs(sub_document, R"(
// Create a WebAssembly Memory to try to bypass the SAB constructor
// restriction.
const sab = new (new WebAssembly.Memory(
{ shared:true, initial:1, maximum:1 }).buffer.constructor)(1234);
parent.postMessage(sab, "*");
)"));
}
// TODO(crbug.com/349104385): Add a test checking that the
// Document-Isolation-Policy header is ignored on redirect responses.
static auto kTestParams =
testing::Combine(testing::ValuesIn(RenderDocumentFeatureLevelValues()),
testing::Bool(),
testing::Bool());
INSTANTIATE_TEST_SUITE_P(All,
DocumentIsolationPolicyBrowserTest,
kTestParams,
DocumentIsolationPolicyBrowserTest::DescribeParams);
INSTANTIATE_TEST_SUITE_P(All,
DocumentIsolationPolicyWithoutFeatureBrowserTest,
kTestParams,
DocumentIsolationPolicyBrowserTest::DescribeParams);
INSTANTIATE_TEST_SUITE_P(All,
DocumentIsolationPolicyWithoutSiteIsolationBrowserTest,
kTestParams,
DocumentIsolationPolicyBrowserTest::DescribeParams);
} // namespace content