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 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <set>
#include <string>
#include <string_view>
#include <vector>
#include "base/command_line.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/synchronization/lock.h"
#include "base/test/scoped_feature_list.h"
#include "base/thread_annotations.h"
#include "build/build_config.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/public/common/content_client.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/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/private_network_access_util.h"
#include "content/public/test/resource_load_observer.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/http/http_byte_range.h"
#include "net/http/http_util.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/embedded_test_server_connection_listener.h"
#include "net/test/embedded_test_server/http_request.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/network_switches.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace content {
namespace {
using ::net::test_server::METHOD_GET;
using ::net::test_server::METHOD_OPTIONS;
using ::testing::ElementsAre;
using ::testing::IsEmpty;
// These domains are mapped to the IP addresses above using the
// `--host-resolver-rules` command-line switch. The exact values come from the
// embedded HTTPS server, which has certificates for these domains
constexpr char kLocalHost[] = "a.test";
constexpr char kOtherLocalHost[] = "d.test";
constexpr char kPrivateHost[] = "b.test";
constexpr char kPublicHost[] = "c.test";
// Path to a default response served by all servers in this test.
constexpr char kDefaultPath[] = "/defaultresponse";
// Path to a response with the `treat-as-public-address` CSP directive.
constexpr char kTreatAsPublicAddressPath[] =
"/set-header?Content-Security-Policy: treat-as-public-address";
// Path to a response with a wide-open CORS header. This can be fetched
// cross-origin without triggering CORS violations.
constexpr char kCorsPath[] = "/set-header?Access-Control-Allow-Origin: *";
// Path to a response that passes Private Network Access checks.
constexpr char kPnaPath[] =
"/set-header"
"?Access-Control-Allow-Origin: *"
"&Access-Control-Allow-Private-Network: true";
// Path to a cacheable response.
constexpr char kCacheablePath[] = "/cachetime";
// Path to a cacheable variant of `kCorsPath`.
constexpr char kCacheableCorsPath[] =
"/set-header"
"?Cache-Control: max-age%3D60"
"&Access-Control-Allow-Origin: *";
// Path to a cacheable variant of `kPnaPath`.
constexpr char kCacheablePnaPath[] =
"/set-header"
"?Cache-Control: max-age%3D60"
"&Access-Control-Allow-Origin: *"
"&Access-Control-Allow-Private-Network: true";
// Returns a path to a response that passes Private Network Access checks.
//
// This can be used to construct the `src` URL for an iframe.
std::string MakePnaPathForIframe(const url::Origin& initiator_origin) {
return base::StrCat({
"/set-header"
// Apparently a wildcard `*` is not sufficient in this case, so we need
// to explicitly allow the initiator origin instead.
"?Access-Control-Allow-Origin: ",
initiator_origin.Serialize(),
"&Access-Control-Allow-Private-Network: true"
// It seems navigation requests carry credentials...
"&Access-Control-Allow-Credentials: true"
// And the following couple headers.
"&Access-Control-Allow-Headers: upgrade-insecure-requests,accept",
});
}
// Returns a snippet of Javascript that fetch()es the given URL.
//
// The snippet evaluates to a boolean promise which resolves to true iff the
// fetch was successful. The promise never rejects, as doing so makes it hard
// to assert failure.
std::string FetchSubresourceScript(const GURL& url) {
return JsReplace(
R"(fetch($1).then(
response => response.ok,
error => {
console.log('Error fetching ' + $1, error);
return false;
});
)",
url);
}
// A |ContentBrowserClient| implementation that allows modifying the return
// value of |ShouldAllowInsecurePrivateNetworkRequests()| at will.
class PolicyTestContentBrowserClient
: public ContentBrowserTestContentBrowserClient {
public:
PolicyTestContentBrowserClient() = default;
PolicyTestContentBrowserClient(const PolicyTestContentBrowserClient&) =
delete;
PolicyTestContentBrowserClient& operator=(
const PolicyTestContentBrowserClient&) = delete;
~PolicyTestContentBrowserClient() override = default;
// Adds an origin to the allowlist.
void SetAllowInsecurePrivateNetworkRequestsFrom(const url::Origin& origin) {
allowlisted_origins_.insert(origin);
}
void SetBlockInsteadOfWarn() { block_instead_of_warn_ = true; }
ContentBrowserClient::PrivateNetworkRequestPolicyOverride
ShouldOverridePrivateNetworkRequestPolicy(
content::BrowserContext* browser_context,
const url::Origin& origin) override {
if (block_instead_of_warn_) {
return ContentBrowserClient::PrivateNetworkRequestPolicyOverride::
kBlockInsteadOfWarn;
}
return allowlisted_origins_.find(origin) != allowlisted_origins_.end()
? ContentBrowserClient::PrivateNetworkRequestPolicyOverride::
kForceAllow
: ContentBrowserClient::PrivateNetworkRequestPolicyOverride::
kDefault;
}
private:
bool block_instead_of_warn_ = false;
std::set<url::Origin> allowlisted_origins_;
};
// An embedded test server connection listener that simply counts connections.
// Thread-safe.
class ConnectionCounter
: public net::test_server::EmbeddedTestServerConnectionListener {
public:
ConnectionCounter() = default;
// Instances of this class are neither copyable nor movable.
ConnectionCounter(const ConnectionCounter&) = delete;
ConnectionCounter& operator=(const ConnectionCounter&) = delete;
ConnectionCounter(ConnectionCounter&&) = delete;
ConnectionCounter& operator=(ConnectionCounter&&) = delete;
// Returns the number of sockets accepted by the servers we are listening to.
int count() const {
base::AutoLock guard(lock_);
return count_;
}
private:
// EmbeddedTestServerConnectionListener implementation.
std::unique_ptr<net::StreamSocket> AcceptedSocket(
std::unique_ptr<net::StreamSocket> socket) override {
{
base::AutoLock guard(lock_);
count_++;
}
return socket;
}
void ReadFromSocket(const net::StreamSocket& socket, int rv) override {}
// `count_` is incremented on the embedded test server thread and read on the
// test thread, so we synchronize accesses with a lock.
mutable base::Lock lock_;
int count_ GUARDED_BY(lock_) = 0;
};
class RequestObserver {
public:
RequestObserver() = default;
// The returned callback must not outlive this instance.
net::test_server::EmbeddedTestServer::MonitorRequestCallback BindCallback() {
return base::BindRepeating(&RequestObserver::Observe,
base::Unretained(this));
}
// The origin of the URL is not checked for equality.
std::vector<net::test_server::HttpMethod> RequestMethodsForUrl(
const GURL& url) const {
std::string path = url.PathForRequest();
std::vector<net::test_server::HttpMethod> methods;
{
base::AutoLock guard(lock_);
for (const auto& request : requests_) {
if (request.GetURL().PathForRequest() == path) {
methods.push_back(request.method);
}
}
}
return methods;
}
private:
void Observe(const net::test_server::HttpRequest& request) {
base::AutoLock guard(lock_);
requests_.push_back(request);
}
// `requests_` is mutated on the embedded test server thread and read on the
// test thread, so we synchronize accesses with a lock.
mutable base::Lock lock_;
std::vector<net::test_server::HttpRequest> requests_ GUARDED_BY(lock_);
};
// Removes `prefix` from the start of `str`, if present.
// Returns nullopt otherwise.
std::optional<std::string_view> StripPrefix(std::string_view str,
std::string_view prefix) {
if (!base::StartsWith(str, prefix)) {
return std::nullopt;
}
return str.substr(prefix.size());
}
// Returns a pointer to the value of the `header` header in `request`, if any.
// Returns nullptr otherwise.
const std::string* FindRequestHeader(
const net::test_server::HttpRequest& request,
std::string_view header) {
const auto it = request.headers.find(header);
if (it == request.headers.end()) {
return nullptr;
}
return &it->second;
}
// Returns the `Content-Range` header value for a given `range` of bytes out of
// the given `total_size` number of bytes.
std::string GetContentRangeHeader(const net::HttpByteRange& range,
size_t total_size) {
std::string first = base::NumberToString(range.first_byte_position());
std::string last = base::NumberToString(range.last_byte_position());
std::string total = base::NumberToString(total_size);
return base::StrCat({"bytes ", first, "-", last, "/", total});
}
// An `EmbeddedTestServer` request handler function.
//
// Knows how to respond to CORS and PNA preflight requests, as well as regular
// and range requests.
//
// Route: /echorange?<body>
std::unique_ptr<net::test_server::HttpResponse> HandleRangeRequest(
const net::test_server::HttpRequest& request) {
std::optional<std::string_view> query =
StripPrefix(request.relative_url, "/echorange?");
if (!query) {
return nullptr;
}
auto response = std::make_unique<net::test_server::BasicHttpResponse>();
constexpr std::pair<std::string_view, std::string_view> kCopiedHeaders[] = {
{"Origin", "Access-Control-Allow-Origin"},
{"Access-Control-Request-Private-Network",
"Access-Control-Allow-Private-Network"},
{"Access-Control-Request-Headers", "Access-Control-Allow-Headers"},
};
for (const auto& pair : kCopiedHeaders) {
const std::string* value = FindRequestHeader(request, pair.first);
if (value) {
response->AddCustomHeader(pair.second, *value);
}
}
// No body for a preflight response.
if (request.method == net::test_server::METHOD_OPTIONS) {
response->AddCustomHeader("Access-Control-Max-Age", "60");
return response;
}
// Cache-Control: max-age=X does not work for range request caching. Use a
// strong ETag instead, along with a last modified date. Both are required.
response->AddCustomHeader("ETag", "foo");
response->AddCustomHeader("Last-Modified", "Fri, 1 Apr 2022 12:34:56 UTC");
const std::string* range_header = FindRequestHeader(request, "Range");
if (!range_header) {
// Not a range request. Respond with 200 and the whole query as the body.
response->set_content(*query);
return response;
}
std::vector<net::HttpByteRange> ranges;
if (!net::HttpUtil::ParseRangeHeader(*range_header, &ranges) ||
ranges.size() != 1) {
response->set_code(net::HTTP_BAD_REQUEST);
return response;
}
net::HttpByteRange& range = ranges[0];
if (!range.ComputeBounds(query->size())) {
response->set_code(net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE);
return response;
}
response->set_code(net::HTTP_PARTIAL_CONTENT);
response->AddCustomHeader("Content-Range",
GetContentRangeHeader(range, query->size()));
response->set_content(query->substr(range.first_byte_position(),
range.last_byte_position() + 1));
return response;
}
// A `net::EmbeddedTestServer` that pretends to be in a given IP address space.
//
// NOTE(titouan): The IP address space overrides CLI switch is copied to utility
// processes when said processes are started. Thus if any server is instantiated
// after the network process has started, updates we make to our own CLI
// switches will not propagate to the network process, yielding inconsistent
// results.
class FakeAddressSpaceServer {
public:
FakeAddressSpaceServer(net::EmbeddedTestServer::Type type,
net::test_server::HttpConnection::Protocol protocol,
network::mojom::IPAddressSpace ip_address_space,
const base::FilePath& test_data_path)
: server_(type, protocol) {
// Use a certificate valid for multiple domains, which we can use to
// distinguish `local`, `private` and `public` address spaces.
server_.SetSSLConfig(net::EmbeddedTestServer::CERT_TEST_NAMES);
server_.SetConnectionListener(&connection_counter_);
server_.RegisterRequestMonitor(request_observer_.BindCallback());
server_.RegisterRequestHandler(base::BindRepeating(&HandleRangeRequest));
server_.AddDefaultHandlers(test_data_path);
CHECK(server_.Start());
// Set up the command line in order for this server to be considered a part
// of `ip_address_space`, irrespective of the actual IP it binds to.
base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
std::string switch_str = command_line.GetSwitchValueASCII(
network::switches::kIpAddressSpaceOverrides);
// If `switch_str` was empty, we prepend an empty value by unconditionally
// adding a comma before the new entry. This empty value is ignored by the
// switch parsing logic.
base::StrAppend(&switch_str,
{
",",
server_.host_port_pair().ToString(),
"=",
IPAddressSpaceToSwitchValue(ip_address_space),
});
command_line.AppendSwitchASCII(network::switches::kIpAddressSpaceOverrides,
switch_str);
}
// Returns the underlying test server.
net::EmbeddedTestServer& Get() { return server_; }
// Returns the total number of sockets accepted by this server.
int ConnectionCount() const { return connection_counter_.count(); }
const RequestObserver& request_observer() const { return request_observer_; }
private:
static std::string_view IPAddressSpaceToSwitchValue(
network::mojom::IPAddressSpace space) {
switch (space) {
case network::mojom::IPAddressSpace::kLocal:
return "loopback";
case network::mojom::IPAddressSpace::kPrivate:
return "local";
case network::mojom::IPAddressSpace::kPublic:
return "public";
default:
ADD_FAILURE() << "Unhandled address space " << space;
return "";
}
}
ConnectionCounter connection_counter_;
RequestObserver request_observer_;
net::EmbeddedTestServer server_;
};
} // namespace
// This being an integration/browser test, we concentrate on a few behaviors
// relevant to Private Network Access:
//
// - testing the values of important properties on top-level documents:
// - address space
// - secure context bit
// - private network request policy
// - testing the inheritance semantics of these properties
// - testing the correct handling of the CSP: treat-as-public-address directive
// - testing that subresource requests are subject to PNA checks
// - and a few other odds and ends
//
// We use the `--ip-address-space-overrides` command-line switch to test against
// `private` and `public` address spaces, even though all responses are actually
// served from localhost. Combined with host resolver rules, this lets us define
// three different domains that map to the different address spaces:
//
// - `a.test` is `local`
// - `b.test` is `private`
// - `c.test` is `public`
//
// We also have unit tests that test all possible combinations of source and
// destination IP address spaces in services/network/url_loader_unittest.cc.
class PrivateNetworkAccessBrowserTestBase : public ContentBrowserTest {
public:
RenderFrameHostImpl* root_frame_host() {
return static_cast<RenderFrameHostImpl*>(
shell()->web_contents()->GetPrimaryMainFrame());
}
protected:
// Allows subclasses to construct instances with different features enabled.
explicit PrivateNetworkAccessBrowserTestBase(
const std::vector<base::test::FeatureRef>& enabled_features,
const std::vector<base::test::FeatureRef>& disabled_features)
: insecure_local_server_(
net::EmbeddedTestServer::TYPE_HTTP,
net::test_server::HttpConnection::Protocol::kHttp1,
network::mojom::IPAddressSpace::kLocal,
GetTestDataFilePath()),
insecure_private_server_(
net::EmbeddedTestServer::TYPE_HTTP,
net::test_server::HttpConnection::Protocol::kHttp1,
network::mojom::IPAddressSpace::kPrivate,
GetTestDataFilePath()),
insecure_public_server_(
net::EmbeddedTestServer::TYPE_HTTP,
net::test_server::HttpConnection::Protocol::kHttp1,
network::mojom::IPAddressSpace::kPublic,
GetTestDataFilePath()),
secure_local_server_(net::EmbeddedTestServer::TYPE_HTTPS,
net::test_server::HttpConnection::Protocol::kHttp1,
network::mojom::IPAddressSpace::kLocal,
GetTestDataFilePath()),
secure_private_server_(
net::EmbeddedTestServer::TYPE_HTTPS,
net::test_server::HttpConnection::Protocol::kHttp1,
network::mojom::IPAddressSpace::kPrivate,
GetTestDataFilePath()),
secure_public_server_(
net::EmbeddedTestServer::TYPE_HTTPS,
net::test_server::HttpConnection::Protocol::kHttp1,
network::mojom::IPAddressSpace::kPublic,
GetTestDataFilePath()) {
feature_list_.InitWithFeatures(enabled_features, disabled_features);
}
void SetUpOnMainThread() override {
ContentBrowserTest::SetUpOnMainThread();
// Rules must be added on the main thread, otherwise `AddRule()` segfaults.
host_resolver()->AddRule(kLocalHost, "127.0.0.1");
host_resolver()->AddRule(kOtherLocalHost, "127.0.0.1");
host_resolver()->AddRule(kPrivateHost, "127.0.0.1");
host_resolver()->AddRule(kPublicHost, "127.0.0.1");
}
const FakeAddressSpaceServer& InsecureLocalServer() const {
return insecure_local_server_;
}
const FakeAddressSpaceServer& InsecurePrivateServer() const {
return insecure_private_server_;
}
const FakeAddressSpaceServer& InsecurePublicServer() const {
return insecure_public_server_;
}
const FakeAddressSpaceServer& SecureLocalServer() const {
return secure_local_server_;
}
const FakeAddressSpaceServer& SecurePrivateServer() const {
return secure_private_server_;
}
const FakeAddressSpaceServer& SecurePublicServer() const {
return secure_public_server_;
}
GURL InsecureLocalURL(const std::string& path) {
return insecure_local_server_.Get().GetURL(kLocalHost, path);
}
GURL InsecurePrivateURL(const std::string& path) {
return insecure_private_server_.Get().GetURL(kPrivateHost, path);
}
GURL InsecurePublicURL(const std::string& path) {
return insecure_public_server_.Get().GetURL(kPublicHost, path);
}
GURL SecureLocalURL(const std::string& path) {
return secure_local_server_.Get().GetURL(kLocalHost, path);
}
GURL OtherSecureLocalURL(const std::string& path) {
return secure_local_server_.Get().GetURL(kOtherLocalHost, path);
}
GURL SecurePrivateURL(const std::string& path) {
return secure_private_server_.Get().GetURL(kPrivateHost, path);
}
GURL SecurePublicURL(const std::string& path) {
return secure_public_server_.Get().GetURL(kPublicHost, path);
}
GURL NullIPURL(const std::string& path) {
return insecure_public_server_.Get().GetURL("0.0.0.0", path);
}
private:
base::test::ScopedFeatureList feature_list_;
FakeAddressSpaceServer insecure_local_server_;
FakeAddressSpaceServer insecure_private_server_;
FakeAddressSpaceServer insecure_public_server_;
FakeAddressSpaceServer secure_local_server_;
FakeAddressSpaceServer secure_private_server_;
FakeAddressSpaceServer secure_public_server_;
};
// Test with insecure private network subresource requests from the `public`
// address space blocked and preflights otherwise enabled but not enforced.
class PrivateNetworkAccessBrowserTest
: public PrivateNetworkAccessBrowserTestBase {
public:
PrivateNetworkAccessBrowserTest()
: PrivateNetworkAccessBrowserTestBase(
{
features::kBlockInsecurePrivateNetworkRequests,
features::kPrivateNetworkAccessSendPreflights,
},
{
network::features::kLocalNetworkAccessChecks,
}) {}
};
// This definition is required as raw initializer lists aren't allowed in the
// ternary ? operator.
using FeatureVec = std::vector<base::test::FeatureRef>;
// This is the same as PrivateNetworkAccessBrowserTest, but runs each test
// twice, once with kOriginKeyedProcessesByDefault explicitly enabled, and once
// with it explicitly disabled. The tests implemented using this class involve
// sandboxed data: frames whose SiteInfo creation may vary depending on whether
// the feature is enabled or not.
class PrivateNetworkAccessSandboxedDataBrowserTest
: public PrivateNetworkAccessBrowserTestBase,
public testing::WithParamInterface<bool> {
public:
PrivateNetworkAccessSandboxedDataBrowserTest()
: PrivateNetworkAccessBrowserTestBase(
GetParam() ? FeatureVec({
features::kBlockInsecurePrivateNetworkRequests,
features::kPrivateNetworkAccessSendPreflights,
features::kOriginKeyedProcessesByDefault,
})
: FeatureVec({
features::kBlockInsecurePrivateNetworkRequests,
features::kPrivateNetworkAccessSendPreflights,
}),
GetParam() ? FeatureVec({
network::features::kLocalNetworkAccessChecks,
})
: FeatureVec({
features::kOriginKeyedProcessesByDefault,
network::features::kLocalNetworkAccessChecks,
})) {}
};
class PrivateNetworkAccessBrowserTestWithBlockInsteadOfWarnOption
: public PrivateNetworkAccessBrowserTest,
public testing::WithParamInterface<bool> {};
class PrivateNetworkAccessBrowserTestDisableWebSecurity
: public PrivateNetworkAccessBrowserTest {
public:
PrivateNetworkAccessBrowserTestDisableWebSecurity() = default;
protected:
void SetUpCommandLine(base::CommandLine* command_line) override {
PrivateNetworkAccessBrowserTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kDisableWebSecurity);
}
};
// Test with insecure private network subresource requests blocked, including
// from the `private` address space.
class PrivateNetworkAccessBrowserTestBlockFromPrivate
: public PrivateNetworkAccessBrowserTestBase {
public:
PrivateNetworkAccessBrowserTestBlockFromPrivate()
: PrivateNetworkAccessBrowserTestBase(
{
features::kBlockInsecurePrivateNetworkRequests,
features::kBlockInsecurePrivateNetworkRequestsFromPrivate,
features::kPrivateNetworkAccessRespectPreflightResults,
},
{
network::features::kLocalNetworkAccessChecks,
}) {}
};
// Test with insecure private network subresource requests blocked, including
// from the `private` address space.
class PrivateNetworkAccessBrowserTestBlockFromUnknown
: public PrivateNetworkAccessBrowserTestBase {
public:
PrivateNetworkAccessBrowserTestBlockFromUnknown()
: PrivateNetworkAccessBrowserTestBase(
{
features::kBlockInsecurePrivateNetworkRequests,
features::kBlockInsecurePrivateNetworkRequestsFromUnknown,
features::kPrivateNetworkAccessRespectPreflightResults,
},
{
network::features::kLocalNetworkAccessChecks,
}) {}
};
// Test with PNA checks for iframes enabled.
class PrivateNetworkAccessBrowserTestForNavigations
: public PrivateNetworkAccessBrowserTestBase {
public:
PrivateNetworkAccessBrowserTestForNavigations()
: PrivateNetworkAccessBrowserTestBase(
{
features::kBlockInsecurePrivateNetworkRequests,
features::kBlockInsecurePrivateNetworkRequestsFromPrivate,
features::kPrivateNetworkAccessForNavigations,
features::kPrivateNetworkAccessRespectPreflightResults,
},
{
network::features::kLocalNetworkAccessChecks,
}) {}
};
// Test with PNA checks for navigations enabled in warning-only mode.
class PrivateNetworkAccessBrowserTestForNavigationsWarningOnly
: public PrivateNetworkAccessBrowserTestForNavigations {
private:
base::test::ScopedFeatureList feature_list_{
features::kPrivateNetworkAccessForNavigationsWarningOnly};
};
// Test with the feature to send preflights (unenforced) disabled, and insecure
// private network subresource requests blocked.
class PrivateNetworkAccessBrowserTestNoPreflights
: public PrivateNetworkAccessBrowserTestBase {
public:
PrivateNetworkAccessBrowserTestNoPreflights()
: PrivateNetworkAccessBrowserTestBase(
{
features::kBlockInsecurePrivateNetworkRequests,
},
{
features::kPrivateNetworkAccessSendPreflights,
network::features::kLocalNetworkAccessChecks,
}) {}
};
// Test with the feature to send preflights (enforced) enabled, and insecure
// private network subresource requests blocked.
class PrivateNetworkAccessBrowserTestRespectPreflightResults
: public PrivateNetworkAccessBrowserTestBase {
public:
PrivateNetworkAccessBrowserTestRespectPreflightResults()
: PrivateNetworkAccessBrowserTestBase(
{
features::kBlockInsecurePrivateNetworkRequests,
features::kPrivateNetworkAccessRespectPreflightResults,
},
{
network::features::kLocalNetworkAccessChecks,
}) {}
};
// Test with PNA checks for worker-related fetches enabled.
class PrivateNetworkAccessBrowserTestForWorkers
: public PrivateNetworkAccessBrowserTestBase {
public:
PrivateNetworkAccessBrowserTestForWorkers()
: PrivateNetworkAccessBrowserTestBase(
{
features::kBlockInsecurePrivateNetworkRequests,
features::kPrivateNetworkAccessForWorkers,
},
{
features::kPrivateNetworkAccessForWorkersWarningOnly,
network::features::kLocalNetworkAccessChecks,
}) {}
};
// Test with PNA checks for worker-related fetches enabled and preflight
// enforcement enabled.
class PrivateNetworkAccessBrowserTestRespectPreflightResultsForWorkers
: public PrivateNetworkAccessBrowserTestBase {
public:
PrivateNetworkAccessBrowserTestRespectPreflightResultsForWorkers()
: PrivateNetworkAccessBrowserTestBase(
{
features::kBlockInsecurePrivateNetworkRequests,
features::kPrivateNetworkAccessRespectPreflightResults,
features::kPrivateNetworkAccessForWorkers,
},
{
features::kPrivateNetworkAccessForWorkersWarningOnly,
network::features::kLocalNetworkAccessChecks,
}) {}
};
// Test with PNA checks for worker-related fetches enabled in warning-only mode,
// including preflights.
class
PrivateNetworkAccessBrowserTestRespectPreflightResultsForWorkersWarningOnly
: public PrivateNetworkAccessBrowserTestBase {
public:
PrivateNetworkAccessBrowserTestRespectPreflightResultsForWorkersWarningOnly()
: PrivateNetworkAccessBrowserTestBase(
{
features::kBlockInsecurePrivateNetworkRequests,
features::kPrivateNetworkAccessRespectPreflightResults,
features::kPrivateNetworkAccessForWorkers,
features::kPrivateNetworkAccessForWorkersWarningOnly,
},
{
network::features::kLocalNetworkAccessChecks,
}) {}
};
// Test with insecure private network requests allowed.
class PrivateNetworkAccessBrowserTestNoBlocking
: public PrivateNetworkAccessBrowserTestBase {
public:
PrivateNetworkAccessBrowserTestNoBlocking()
: PrivateNetworkAccessBrowserTestBase(
{},
{
features::kBlockInsecurePrivateNetworkRequests,
features::kBlockInsecurePrivateNetworkRequestsFromPrivate,
features::kPrivateNetworkAccessForNavigations,
features::kPrivateNetworkAccessForWorkers,
features::kPrivateNetworkAccessSendPreflights,
network::features::kLocalNetworkAccessChecks,
}) {}
};
// ===========================
// CLIENT SECURITY STATE TESTS
// ===========================
//
// These tests verify the contents of `ClientSecurityState` for top-level
// documents in various different circumstances.
// This test verifies the contents of the ClientSecurityState for the initial
// empty document in a new main frame created by the browser.
//
// Note: the renderer-created main frame case is exercised by the
// OpeneeInherits* tests below.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForInitialEmptyDoc) {
// Start a navigation. This forces the RenderFrameHost to initialize its
// RenderFrame. The navigation is then cancelled by a HTTP 204 code.
// We're left with a RenderFrameHost containing the default
// ClientSecurityState values.
//
// Serve the response from a secure public server, to confirm that none of
// the connection's properties are reflected in the committed document, which
// is not a secure context and belongs to the `local` address space.
EXPECT_TRUE(
NavigateToURLAndExpectNoCommit(shell(), SecurePublicURL("/nocontent")));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::CrossOriginEmbedderPolicyValue::kNone,
security_state->cross_origin_embedder_policy.value);
EXPECT_EQ(network::mojom::PrivateNetworkRequestPolicy::kBlock,
security_state->private_network_request_policy);
// Browser-created empty main frames are trusted to access the local network,
// if they execute code injected via DevTools, WebView APIs or extensions.
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
// This test verifies the contents of the ClientSecurityState for `about:blank`
// in a new main frame created by the browser.
//
// Note: the renderer-created main frame case is exercised by the Openee
// inheritance tests below.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForAboutBlank) {
EXPECT_TRUE(NavigateToURL(shell(), GURL("about:blank")));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForDataURL) {
EXPECT_TRUE(NavigateToURL(shell(), GURL("data:text/html,foo")));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kUnknown,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForFileURL) {
EXPECT_TRUE(NavigateToURL(shell(), GetTestUrl("", "empty.html")));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForInsecureLocalAddress) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForInsecurePrivateAddress) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePrivateURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kPrivate,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForInsecurePublicAddress) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForSecureLocalAddress) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForSecurePrivateAddress) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePrivateURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kPrivate,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForSecurePublicAddress) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
// Tests that a top-level navigation to 0.0.0.0 is in the kLocal address space.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForNullIP) {
if constexpr (BUILDFLAG(IS_WIN)) {
GTEST_SKIP() << "0.0.0.0 behavior varies across platforms and is "
"unreachable on Windows.";
}
EXPECT_TRUE(NavigateToURL(shell(), NullIPURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
class PrivateNetworkAccessBrowserTestNullIPKillswitch
: public PrivateNetworkAccessBrowserTestBase {
public:
PrivateNetworkAccessBrowserTestNullIPKillswitch()
: PrivateNetworkAccessBrowserTestBase(
{
network::features::kTreatNullIPAsPublicAddressSpace,
},
{
network::features::kLocalNetworkAccessChecks,
}) {}
};
// Tests that a top-level navigation to 0.0.0.0 is in the kPublic address space
// when a killswitch is enabled to specifically treat it as public.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestNullIPKillswitch,
ClientSecurityStateForNullIPKillswitch) {
if constexpr (BUILDFLAG(IS_WIN)) {
GTEST_SKIP() << "0.0.0.0 behavior varies across platforms and is "
"unreachable on Windows.";
}
EXPECT_TRUE(NavigateToURL(shell(), NullIPURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForTreatAsPublicAddress) {
EXPECT_TRUE(
NavigateToURL(shell(), SecureLocalURL(kTreatAsPublicAddressPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForTreatAsPublicAddressReportOnly) {
EXPECT_TRUE(NavigateToURL(
shell(),
SecureLocalURL("/set-header?Content-Security-Policy-Report-Only: "
"treat-as-public-address")));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForCachedSecureLocalDocument) {
// Navigate to the cacheable document in order to cache it, then navigate
// away.
const GURL url = SecureLocalURL(kCacheablePath);
EXPECT_TRUE(NavigateToURL(shell(), url));
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
// Navigate to the cached document.
ResourceLoadObserver observer(shell());
EXPECT_TRUE(NavigateToURL(shell(), url));
observer.WaitForResourceCompletion(url);
blink::mojom::ResourceLoadInfoPtr* info = observer.GetResource(url);
ASSERT_TRUE(info);
ASSERT_TRUE(*info);
EXPECT_TRUE((*info)->was_cached);
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForCachedInsecurePublicDocument) {
// Navigate to the cacheable document in order to cache it, then navigate
// away.
const GURL url = InsecurePublicURL(kCacheablePath);
EXPECT_TRUE(NavigateToURL(shell(), url));
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
// Navigate to the cached document.
ResourceLoadObserver observer(shell());
EXPECT_TRUE(NavigateToURL(shell(), url));
observer.WaitForResourceCompletion(url);
blink::mojom::ResourceLoadInfoPtr* info = observer.GetResource(url);
ASSERT_TRUE(info);
ASSERT_TRUE(*info);
EXPECT_TRUE((*info)->was_cached);
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
// This test verifies that the chrome:// scheme is considered local for the
// purpose of Private Network Access.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForSpecialSchemeChromeURL) {
// Not all chrome:// hosts are available in content/ but ukm is one of them.
EXPECT_TRUE(NavigateToURL(shell(), GURL("chrome://ukm")));
EXPECT_TRUE(
root_frame_host()->GetLastCommittedURL().SchemeIs(kChromeUIScheme));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
// The view-source:// scheme should only ever appear in the display URL. It
// shouldn't affect the IPAddressSpace computation. This test verifies that we
// end up with the response IPAddressSpace.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForSpecialSchemeViewSourcePublic) {
const GURL url = SecurePublicURL(kDefaultPath);
EXPECT_TRUE(NavigateToURL(shell(), GURL("view-source:" + url.spec())));
EXPECT_FALSE(
root_frame_host()->GetLastCommittedURL().SchemeIs(kViewSourceScheme));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
// Variation of above test with a private address.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForSpecialSchemeViewSourcePrivate) {
const GURL url = SecurePrivateURL(kDefaultPath);
EXPECT_TRUE(NavigateToURL(shell(), GURL("view-source:" + url.spec())));
EXPECT_FALSE(
root_frame_host()->GetLastCommittedURL().SchemeIs(kViewSourceScheme));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kPrivate,
security_state->ip_address_space);
}
// The chrome-error:// scheme should only ever appear in origins. It shouldn't
// affect the IPAddressSpace computation. This test verifies that we end up with
// the response IPAddressSpace. Error pages should not be considered secure
// contexts however.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForSpecialSchemeChromeErrorPublic) {
EXPECT_FALSE(NavigateToURL(shell(), SecurePublicURL("/empty404.html")));
EXPECT_FALSE(
root_frame_host()->GetLastCommittedURL().SchemeIs(kChromeErrorScheme));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
// Variation of above test with a private address.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
ClientSecurityStateForSpecialSchemeChromeErrorPrivate) {
EXPECT_FALSE(NavigateToURL(shell(), SecurePrivateURL("/empty404.html")));
EXPECT_FALSE(
root_frame_host()->GetLastCommittedURL().SchemeIs(kChromeErrorScheme));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kPrivate,
security_state->ip_address_space);
}
// ========================
// INHERITANCE TEST HELPERS
// ========================
namespace {
// Creates a blob containing dummy HTML, then returns its URL.
// Executes javascript to do so in |frame_host|, which must not be nullptr.
GURL CreateBlobURL(RenderFrameHostImpl* frame_host) {
// Define a variable to avoid awkward `ExtractString()` indentation.
EvalJsResult result = EvalJs(frame_host, R"(
const blob = new Blob(["foo"], {type: "text/html"});
URL.createObjectURL(blob)
)");
return GURL(result.ExtractString());
}
// Executes |script| to add a new child iframe to the given |parent| document.
//
// |parent| must not be nullptr.
//
// Returns a pointer to the child frame host.
RenderFrameHostImpl* AddChildWithScript(RenderFrameHostImpl* parent,
const std::string& script) {
size_t initial_child_count = parent->child_count();
EXPECT_EQ(true, ExecJs(parent, script));
EXPECT_EQ(parent->child_count(), initial_child_count + 1);
if (parent->child_count() < initial_child_count + 1) {
return nullptr;
}
return parent->child_at(initial_child_count)->current_frame_host();
}
RenderFrameHostImpl* GetFirstChild(RenderFrameHostImpl& parent) {
CHECK_NE(parent.child_count(), 0ul);
return parent.child_at(0)->current_frame_host();
}
// Adds a child iframe sourced from `url` to the given `parent` document and
// waits for it to load. Returns the child RFHI.
//
// `parent` must not be nullptr.
RenderFrameHostImpl* AddChildFromURL(RenderFrameHostImpl* parent,
std::string_view url) {
constexpr std::string_view kScriptTemplate = R"(
new Promise((resolve) => {
const iframe = document.createElement("iframe");
iframe.src = $1;
iframe.onload = _ => { resolve(true); };
document.body.appendChild(iframe);
})
)";
return AddChildWithScript(parent, JsReplace(kScriptTemplate, url));
}
// Convenience overload for absolute URLs.
RenderFrameHostImpl* AddChildFromURL(RenderFrameHostImpl* parent,
const GURL& url) {
return AddChildFromURL(parent, url.spec());
}
// Adds a child iframe sourced from `url` to the given `parent` document.
// Does not wait for the child frame to load - this must be done separately.
//
// `parent` must not be nullptr.
void AddChildFromURLWithoutWaiting(RenderFrameHostImpl* parent,
std::string_view url) {
// Define a variable for better indentation.
constexpr std::string_view kScriptTemplate = R"(
const child = document.createElement("iframe");
child.src = $1;
document.body.appendChild(child);
)";
EXPECT_EQ(true, ExecJs(parent, JsReplace(kScriptTemplate, url)));
}
// Convenience overload for absolute URLs.
void AddChildFromURLWithoutWaiting(RenderFrameHostImpl* parent,
const GURL& url) {
return AddChildFromURLWithoutWaiting(parent, url.spec());
}
RenderFrameHostImpl* AddChildFromAboutBlank(RenderFrameHostImpl* parent) {
return AddChildFromURL(parent, "about:blank");
}
RenderFrameHostImpl* AddChildInitialEmptyDoc(RenderFrameHostImpl* parent) {
return AddChildWithScript(parent, R"(
const iframe = document.createElement("iframe");
iframe.src = "/nocontent"; // Returns 204 NO CONTENT, thus no doc commits.
document.body.appendChild(iframe);
true // Do not wait for iframe.onload, which never fires.
)");
}
RenderFrameHostImpl* AddChildFromSrcdoc(RenderFrameHostImpl* parent) {
return AddChildWithScript(parent, R"(
new Promise((resolve) => {
const iframe = document.createElement("iframe");
iframe.srcdoc = "foo";
iframe.onload = _ => { resolve(true); };
document.body.appendChild(iframe);
})
)");
}
RenderFrameHostImpl* AddChildFromDataURL(RenderFrameHostImpl* parent) {
return AddChildFromURL(parent, "data:text/html,foo");
}
RenderFrameHostImpl* AddChildFromJavascriptURL(RenderFrameHostImpl* parent) {
return AddChildFromURL(parent, "javascript:'foo'");
}
RenderFrameHostImpl* AddChildFromBlob(RenderFrameHostImpl* parent) {
GURL blob_url = CreateBlobURL(parent);
return AddChildFromURL(parent, blob_url);
}
RenderFrameHostImpl* AddSandboxedChildFromURL(RenderFrameHostImpl* parent,
const GURL& url) {
std::string script_template = R"(
new Promise((resolve) => {
const iframe = document.createElement("iframe");
iframe.src = $1;
iframe.sandbox = "";
iframe.onload = _ => { resolve(true); };
document.body.appendChild(iframe);
})
)";
return AddChildWithScript(parent, JsReplace(script_template, url));
}
RenderFrameHostImpl* AddSandboxedChildFromAboutBlank(
RenderFrameHostImpl* parent) {
return AddSandboxedChildFromURL(parent, GURL("about:blank"));
}
RenderFrameHostImpl* AddSandboxedChildInitialEmptyDoc(
RenderFrameHostImpl* parent) {
return AddChildWithScript(parent, R"(
const iframe = document.createElement("iframe");
iframe.src = "/nocontent"; // Returns 204 NO CONTENT, thus no doc commits.
iframe.sandbox = "";
document.body.appendChild(iframe);
true // Do not wait for iframe.onload, which never fires.
)");
}
RenderFrameHostImpl* AddSandboxedChildFromSrcdoc(RenderFrameHostImpl* parent) {
return AddChildWithScript(parent, R"(
new Promise((resolve) => {
const iframe = document.createElement("iframe");
iframe.srcdoc = "foo";
iframe.sandbox = "";
iframe.onload = _ => { resolve(true); };
document.body.appendChild(iframe);
})
)");
}
RenderFrameHostImpl* AddSandboxedChildFromDataURL(RenderFrameHostImpl* parent) {
return AddSandboxedChildFromURL(parent, GURL("data:text/html,foo"));
}
RenderFrameHostImpl* AddSandboxedChildFromBlob(RenderFrameHostImpl* parent) {
GURL blob_url = CreateBlobURL(parent);
return AddSandboxedChildFromURL(parent, blob_url);
}
// Returns the main frame RenderFrameHostImpl in the given |shell|.
//
// |shell| must not be nullptr.
//
// Helper for OpenWindow*().
RenderFrameHostImpl* GetPrimaryMainFrameHostImpl(Shell* shell) {
return static_cast<RenderFrameHostImpl*>(
shell->web_contents()->GetPrimaryMainFrame());
}
// Opens a new window from within |parent|, pointed at the given |url|.
// Waits until the openee window has navigated to |url|, then returns a pointer
// to its main frame RenderFrameHostImpl.
//
// |parent| must not be nullptr.
RenderFrameHostImpl* OpenWindowFromURL(RenderFrameHostImpl* parent,
const GURL& url) {
return GetPrimaryMainFrameHostImpl(OpenPopup(parent, url, "_blank"));
}
RenderFrameHostImpl* OpenWindowFromAboutBlank(RenderFrameHostImpl* parent) {
return OpenWindowFromURL(parent, GURL("about:blank"));
}
// Same as above, but with the "noopener" window feature.
RenderFrameHostImpl* OpenWindowFromAboutBlankNoOpener(
RenderFrameHostImpl* parent) {
// Setting the "noopener" window feature makes `window.open()` return `null`.
constexpr bool kNoExpectReturnFromWindowOpen = false;
return GetPrimaryMainFrameHostImpl(OpenPopup(parent, GURL("about:blank"),
"_blank", "noopener",
kNoExpectReturnFromWindowOpen));
}
RenderFrameHostImpl* OpenWindowFromURLExpectNoCommit(
RenderFrameHostImpl* parent,
const GURL& url,
std::string_view features = "") {
ShellAddedObserver observer;
std::string_view script_template = R"(
window.open($1, "_blank", $2);
)";
EXPECT_TRUE(ExecJs(parent, JsReplace(script_template, url, features)));
return GetPrimaryMainFrameHostImpl(observer.GetShell());
}
RenderFrameHostImpl* OpenWindowInitialEmptyDoc(RenderFrameHostImpl* parent) {
// Note: We do not use OpenWindowFromURL() because we do not want to wait for
// a navigation - none will commit.
return OpenWindowFromURLExpectNoCommit(parent, GURL("/nocontent"));
}
// Same as above, but with the "noopener" window feature.
RenderFrameHostImpl* OpenWindowInitialEmptyDocNoOpener(
RenderFrameHostImpl* parent) {
// Note: We do not use OpenWindowFromURL() because we do not want to wait for
// a navigation - none will commit.
return OpenWindowFromURLExpectNoCommit(parent, GURL("/nocontent"),
"noopener");
}
GURL JavascriptURL(std::string_view script) {
return GURL(base::StrCat({"javascript:", script}));
}
RenderFrameHostImpl* OpenWindowFromJavascriptURL(
RenderFrameHostImpl* parent,
std::string_view script = "'foo'") {
// Note: We do not use OpenWindowFromURL() because we do not want to wait for
// a navigation, since the `javascript:` URL will not commit (`about:blank`
// will).
return OpenWindowFromURLExpectNoCommit(parent, JavascriptURL(script));
}
// Same as above, but with the "noopener" window feature.
RenderFrameHostImpl* OpenWindowFromJavascriptURLNoOpener(
RenderFrameHostImpl* parent,
std::string_view script) {
// Note: We do not use OpenWindowFromURL() because we do not want to wait for
// a navigation - none will commit.
return OpenWindowFromURLExpectNoCommit(parent, JavascriptURL(script),
"noopener");
}
RenderFrameHostImpl* OpenWindowFromBlob(RenderFrameHostImpl* parent) {
GURL blob_url = CreateBlobURL(parent);
return OpenWindowFromURL(parent, blob_url);
}
} // namespace
// ===============================
// ADDRESS SPACE INHERITANCE TESTS
// ===============================
//
// These tests verify that `ClientSecurityState.ip_address_space` is correctly
// inherited by child iframes and openee documents for a variety of URLs with
// local schemes.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsAddressSpaceForAboutBlankFromPublic) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromAboutBlank(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsAddressSpaceForAboutBlankFromLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromAboutBlank(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsAddressSpaceForAboutBlankFromPublic) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromAboutBlank(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsAddressSpaceForAboutBlankFromLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromAboutBlank(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
// This test verifies that a newly-opened window targeting `about:blank`
// inherits its address space from the opener. In this case, the opener's
// address space is `public`.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeInheritsAddressSpaceForAboutBlankFromPublic) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowFromAboutBlank(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
// This test verifies that a newly-opened window targeting `about:blank`
// inherits its address space from the opener. In this case, the opener's
// address space is `local`.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeInheritsAddressSpaceForAboutBlankFromLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowFromAboutBlank(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
// This test verifies that a newly-opened window targeting `about:blank`,
// opened with the "noopener" feature, has its address space set to `local`
// regardless of the address space of the opener.
//
// Compare and contrast against the above tests without "noopener".
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeNoOpenerAddressSpaceForAboutBlankIsLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* window =
OpenWindowFromAboutBlankNoOpener(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsAddressSpaceForInitialEmptyDocFromPublic) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildInitialEmptyDoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsAddressSpaceForInitialEmptyDocFromLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildInitialEmptyDoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsAddressSpaceForInitialEmptyDocFromPublic) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildInitialEmptyDoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsAddressSpaceForInitialEmptyDocFromLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildInitialEmptyDoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
// This test verifies that a newly-opened window containing the initial empty
// document inherits its address space from the opener. In this case, the
// opener's address space is `public`.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeInheritsAddressSpaceForInitialEmptyDocFromPublic) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowInitialEmptyDoc(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
// This test verifies that a newly-opened window containing the initial empty
// document inherits its address space from the opener. In this case, the
// opener's address space is `local`.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeInheritsAddressSpaceForInitialEmptyDocFromLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowInitialEmptyDoc(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
// This test verifies that a newly-opened window containing the initial empty
// document, opened with the "noopener" feature, has its address space set to
// `local` regardless of the address space of the opener.
//
// Compare and contrast against the above tests without "noopener".
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeNoOpenerAddressSpaceForInitialEmptyDocIsLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* window =
OpenWindowInitialEmptyDocNoOpener(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsAddressSpaceForAboutSrcdocFromPublic) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromSrcdoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsAddressSpaceForAboutSrcdocFromLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromSrcdoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsAddressSpaceForAboutSrcdocFromPublic) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromSrcdoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsAddressSpaceForAboutSrcdocFromLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromSrcdoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsAddressSpaceForDataURLFromPublic) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromDataURL(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsAddressSpaceForDataURLFromLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromDataURL(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_P(
PrivateNetworkAccessSandboxedDataBrowserTest,
SandboxedIframeInheritsAddressSpaceForDataURLFromPublic) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromDataURL(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_P(PrivateNetworkAccessSandboxedDataBrowserTest,
SandboxedIframeInheritsAddressSpaceForDataURLFromLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromDataURL(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsAddressSpaceForJavascriptURLFromPublic) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddChildFromJavascriptURL(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsAddressSpaceForJavascriptURLFromLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddChildFromJavascriptURL(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeInheritsAddressSpaceForJavascriptURLFromPublic) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowFromJavascriptURL(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeInheritsAddressSpaceForJavascriptURLFromLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowFromJavascriptURL(
root_frame_host(), "var injectedCodeWasExecuted = true");
ASSERT_NE(nullptr, window);
// The Javascript in the URL got executed in the new window.
EXPECT_EQ(true, EvalJs(window, "injectedCodeWasExecuted"));
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeNoOpenerAddressSpaceForJavascriptURLIsLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowFromJavascriptURLNoOpener(
root_frame_host(), "var injectedCodeWasExecuted = true");
ASSERT_NE(nullptr, window);
// The Javascript in the URL was not executed in the new window. This ensures
// it is safe to classify the new window as `local` without allowing the
// opener to execute arbitrary JS in the `local` address space.
EXPECT_EQ("undefined", EvalJs(window, "typeof injectedCodeWasExecuted"));
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsAddressSpaceForBlobURLFromPublic) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromBlob(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsAddressSpaceForBlobURLFromLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromBlob(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsAddressSpaceForBlobURLFromPublic) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromBlob(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsAddressSpaceForBlobURLFromLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromBlob(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeInheritsAddressSpaceForBlobURLFromPublic) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowFromBlob(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeInheritsAddressSpaceForBlobURLFromLocal) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowFromBlob(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(network::mojom::IPAddressSpace::kLocal,
security_state->ip_address_space);
}
// ================================
// SECURE CONTEXT INHERITANCE TESTS
// ================================
//
// These tests verify that `ClientSecurityState.is_web_secure_context` is
// correctly inherited by child iframes and openee documents for a variety of
// URLs with local schemes.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsSecureContextForAboutBlankFromSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromAboutBlank(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsSecureContextForAboutBlankFromInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromAboutBlank(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsSecureContextForAboutBlankFromSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromAboutBlank(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsSecureContextForAboutBlankFromInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromAboutBlank(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeInheritsSecureContextForAboutBlankFromSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowFromAboutBlank(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeInheritsSecureContextForAboutBlankFromInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowFromAboutBlank(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
IframeInheritsSecureContextForInitialEmptyDocFromSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildInitialEmptyDoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
IframeInheritsSecureContextForInitialEmptyDocFromInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildInitialEmptyDoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsSecureContextForInitialEmptyDocFromSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildInitialEmptyDoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsSecureContextForInitialEmptyDocFromInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildInitialEmptyDoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
OpeneeInheritsSecureContextForInitialEmptyDocFromSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowInitialEmptyDoc(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
OpeneeInheritsSecureContextForInitialEmptyDocFromInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowInitialEmptyDoc(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsSecureContextForAboutSrcdocFromSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromSrcdoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsSecureContextForAboutSrcdocFromInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromSrcdoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsSecureContextForAboutSrcdocFromSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromSrcdoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsSecureContextForAboutSrcdocFromInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromSrcdoc(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsSecureContextForDataURLFromSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromDataURL(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsSecureContextForDataURLFromInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromDataURL(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsSecureContextForDataURLFromSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromDataURL(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_P(
PrivateNetworkAccessSandboxedDataBrowserTest,
SandboxedIframeInheritsSecureContextForDataURLFromInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromDataURL(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsSecureContextForJavascriptURLFromSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddChildFromJavascriptURL(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
IframeInheritsSecureContextForJavascriptURLFromInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddChildFromJavascriptURL(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
OpeneeInheritsSecureContextForJavascriptURLFromInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowFromJavascriptURL(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeInheritsSecureContextForJavascriptURLFromSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowFromJavascriptURL(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsSecureContextForBlobURLFromSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromBlob(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeInheritsSecureContextForBlobURLFromInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame = AddChildFromBlob(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsSecureContextForBlobURLFromSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromBlob(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
SandboxedIframeInheritsSecureContextForBlobURLFromInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromBlob(root_frame_host());
ASSERT_NE(nullptr, child_frame);
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeInheritsSecureContextForBlobURLFromSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowFromBlob(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
OpeneeInheritsSecureContextForBlobURLFromInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
RenderFrameHostImpl* window = OpenWindowFromBlob(root_frame_host());
ASSERT_NE(nullptr, window);
const network::mojom::ClientSecurityStatePtr security_state =
window->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
}
// ====================================
// PRIVATE NETWORK REQUEST POLICY TESTS
// ====================================
//
// These tests verify the correct setting of
// `ClientSecurityState.private_network_request_policy` in various situations.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestDisableWebSecurity,
PrivateNetworkPolicyIsAllowInsecure) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kAllow);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestDisableWebSecurity,
PrivateNetworkPolicyIsAllowSecure) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kAllow);
}
// This test verifies that with the blocking feature disabled, the private
// network request policy used by RenderFrameHostImpl is to warn about requests
// from non-secure contexts.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestNoBlocking,
PrivateNetworkPolicyIsWarnByDefault) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kWarn);
}
// This test verifies that with the blocking feature disabled, the private
// network request policy used by RenderFrameHostImpl is to send unenforced
// preflight requests from secure contexts.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestNoBlocking,
PrivateNetworkPolicyIsWarnByDefaultForSecureContexts) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kAllow);
}
INSTANTIATE_TEST_SUITE_P(
,
PrivateNetworkAccessBrowserTestWithBlockInsteadOfWarnOption,
testing::Values(false, true));
INSTANTIATE_TEST_SUITE_P(,
PrivateNetworkAccessSandboxedDataBrowserTest,
testing::Bool(),
[](const testing::TestParamInfo<bool>& info) {
return info.param
? "OriginKeyedProcessesByDefault_disabled"
: "OriginKeyedProcessesByDefault_enabled";
});
// This test verifies that by default, the private network request policy used
// by RenderFrameHostImpl for requests is set to block requests from non-secure
// contexts in the `public` address space.
IN_PROC_BROWSER_TEST_P(
PrivateNetworkAccessBrowserTestWithBlockInsteadOfWarnOption,
PrivateNetworkPolicyIsBlockByDefaultForInsecurePublic) {
PolicyTestContentBrowserClient client;
bool block_instead_of_warn = GetParam();
if (block_instead_of_warn) {
client.SetBlockInsteadOfWarn();
}
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kBlock);
}
// This test verifies that by default, the private network request policy used
// by RenderFrameHostImpl for requests is set to allow requests from non-secure
// contexts in the `private` address space with a warning.
IN_PROC_BROWSER_TEST_P(
PrivateNetworkAccessBrowserTestWithBlockInsteadOfWarnOption,
PrivateNetworkPolicyForInsecurePrivate) {
PolicyTestContentBrowserClient client;
bool block_instead_of_warn = GetParam();
if (block_instead_of_warn) {
client.SetBlockInsteadOfWarn();
}
EXPECT_TRUE(NavigateToURL(shell(), InsecurePrivateURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
block_instead_of_warn
? network::mojom::PrivateNetworkRequestPolicy::kBlock
: network::mojom::PrivateNetworkRequestPolicy::kWarn);
}
// This test verifies that when the right feature is enabled, the private
// network request policy used by RenderFrameHostImpl for requests is set to
// block requests from non-secure contexts in the private address space.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestBlockFromPrivate,
PrivateNetworkPolicyIsBlockForInsecurePrivate) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePrivateURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kBlock);
}
// This test verifies that by default, the private network request policy used
// by RenderFrameHostImpl for requests is set to allow requests from non-secure
// contexts in the `unknown` address space.
IN_PROC_BROWSER_TEST_P(
PrivateNetworkAccessBrowserTestWithBlockInsteadOfWarnOption,
PrivateNetworkPolicyIsAllowByDefaultForInsecureUnknown) {
PolicyTestContentBrowserClient client;
bool block_instead_of_warn = GetParam();
if (block_instead_of_warn) {
client.SetBlockInsteadOfWarn();
}
EXPECT_TRUE(NavigateToURL(shell(), GURL("data:text/html,foo")));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kAllow);
}
// This test verifies that when the right feature is enabled, the private
// network request policy used by RenderFrameHostImpl for requests is set to
// block requests from non-secure contexts in the `unknown` address space.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestBlockFromUnknown,
PrivateNetworkPolicyIsBlockForInsecureUnknown) {
EXPECT_TRUE(NavigateToURL(shell(), GURL("data:text/html,foo")));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kBlock);
}
// This test verifies that by default, the private network request policy used
// by RenderFrameHostImpl for requests is set to allow requests from secure
// contexts.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestNoPreflights,
PrivateNetworkPolicyIsAllowByDefaultForSecureContexts) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kAllow);
}
// This test verifies that when sending preflights is enabled, the private
// network request policy for secure contexts is `kPreflightWarn`.
IN_PROC_BROWSER_TEST_P(
PrivateNetworkAccessBrowserTestWithBlockInsteadOfWarnOption,
PrivateNetworkPolicyForSecureContexts) {
PolicyTestContentBrowserClient client;
bool block_instead_of_warn = GetParam();
if (block_instead_of_warn) {
client.SetBlockInsteadOfWarn();
}
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
block_instead_of_warn
? network::mojom::PrivateNetworkRequestPolicy::kPreflightBlock
: network::mojom::PrivateNetworkRequestPolicy::kPreflightWarn);
}
// This test verifies that blocking insecure private network requests from the
// `kPublic` address space takes precedence over sending preflight requests.
IN_PROC_BROWSER_TEST_P(
PrivateNetworkAccessBrowserTestWithBlockInsteadOfWarnOption,
PrivateNetworkPolicyIsBlockForInsecurePublic) {
PolicyTestContentBrowserClient client;
bool block_instead_of_warn = GetParam();
if (block_instead_of_warn) {
client.SetBlockInsteadOfWarn();
}
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kBlock);
}
// This test verifies that when enforcing preflights is enabled, the private
// network request policy for secure contexts is `kPreflightBlock`.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestRespectPreflightResults,
PrivateNetworkPolicyIsPreflightBlockForSecureContexts) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kPreflightBlock);
}
// This test verifies that when enforcing preflights is enabled, the private
// network request policy for non-secure contexts in the `kPrivate` address
// space is `kPreflightBlock`.
// This checks that as long as the "block from insecure private" feature flag
// is not enabled, we will only show warnings for these requests.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestRespectPreflightResults,
PrivateNetworkPolicyIsWarnForInsecurePrivate) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePrivateURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kWarn);
}
// This test verifies that blocking insecure private network requests from the
// `kPublic` address space takes precedence over enforcing preflight requests.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestRespectPreflightResults,
PrivateNetworkPolicyIsBlockForInsecurePublic) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kBlock);
}
// This test verifies that child frames with distinct origins from their parent
// do not inherit their private network request policy, which is based on the
// origin of the child document instead.
// TODO (crbug.com/324679506) : Fix the test.
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
DISABLED_PrivateNetworkRequestPolicyCalculatedPerOrigin) {
GURL url = InsecurePublicURL(kDefaultPath);
PolicyTestContentBrowserClient client;
client.SetAllowInsecurePrivateNetworkRequestsFrom(url::Origin::Create(url));
EXPECT_TRUE(NavigateToURL(shell(), url));
RenderFrameHostImpl* child_frame =
AddChildFromURL(root_frame_host(), InsecureLocalURL(kDefaultPath));
network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kBlock);
}
// This test verifies that the initial empty document, which inherits its origin
// from the document creator, also inherits its private network request policy.
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
PrivateNetworkRequestPolicyInheritedWithOriginForInitialEmptyDoc) {
GURL url = InsecurePublicURL(kDefaultPath);
PolicyTestContentBrowserClient client;
client.SetAllowInsecurePrivateNetworkRequestsFrom(url::Origin::Create(url));
EXPECT_TRUE(NavigateToURL(shell(), url));
RenderFrameHostImpl* child_frame = AddChildInitialEmptyDoc(root_frame_host());
network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kAllow);
}
// This test verifies that `about:blank` iframes, which inherit their origin
// from the navigation initiator, also inherit their private network request
// policy.
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
PrivateNetworkRequestPolicyInheritedWithOriginForAboutBlank) {
GURL url = InsecurePublicURL(kDefaultPath);
PolicyTestContentBrowserClient client;
client.SetAllowInsecurePrivateNetworkRequestsFrom(url::Origin::Create(url));
EXPECT_TRUE(NavigateToURL(shell(), url));
RenderFrameHostImpl* child_frame = AddChildFromAboutBlank(root_frame_host());
network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kAllow);
}
// This test verifies that `data:` iframes, which commit an opaque origin
// derived from the navigation initiator's origin, do not inherit their private
// network request policy.
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
PrivateNetworkRequestPolicyNotInheritedWithOriginForDataURL) {
GURL url = InsecurePublicURL(kDefaultPath);
PolicyTestContentBrowserClient client;
client.SetAllowInsecurePrivateNetworkRequestsFrom(url::Origin::Create(url));
EXPECT_TRUE(NavigateToURL(shell(), url));
RenderFrameHostImpl* child_frame = AddChildFromDataURL(root_frame_host());
network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kBlock);
}
// This test verifies that sandboxed iframes, which commit an opaque origin
// derived from the navigation initiator's origin, do not inherit their private
// network request policy.
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
PrivateNetworkRequestPolicyNotInheritedForSandboxedInitialEmptyDoc) {
GURL url = InsecurePublicURL(kDefaultPath);
PolicyTestContentBrowserClient client;
client.SetAllowInsecurePrivateNetworkRequestsFrom(url::Origin::Create(url));
EXPECT_TRUE(NavigateToURL(shell(), url));
RenderFrameHostImpl* child_frame =
AddSandboxedChildInitialEmptyDoc(root_frame_host());
network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kBlock);
}
// This test verifies that sandboxed iframes, which commit an opaque origin
// derived from the navigation initiator's origin, do not inherit their private
// network request policy. "about:blank" behaves slightly differently from the
// initial empty doc in code, but should have the same policy in the end.
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
PrivateNetworkRequestPolicyNotInheritedForSandboxedAboutBlank) {
GURL url = InsecurePublicURL(kDefaultPath);
PolicyTestContentBrowserClient client;
client.SetAllowInsecurePrivateNetworkRequestsFrom(url::Origin::Create(url));
EXPECT_TRUE(NavigateToURL(shell(), url));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromAboutBlank(root_frame_host());
network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kBlock);
}
// This test verifies that error pages have a set private network request
// policy of `kAllow` irrespective of the navigation initiator.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
PrivateNetworkRequestPolicyIsAllowForErrorPage) {
GURL url = InsecurePublicURL(kDefaultPath);
EXPECT_TRUE(NavigateToURL(shell(), url));
RenderFrameHostImpl* child_frame =
AddChildFromURL(root_frame_host(), "/close-socket");
network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_FALSE(security_state->is_web_secure_context);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kAllow);
}
// ==================================================
// SECURE CONTEXT RESTRICTION DEPRECATION TRIAL TESTS
// ==================================================
//
// These tests verify the correct behavior of `private_network_request_policy`
// in the face of the `PrivateNetworkAccessNonSecureContextsAllowed` deprecation
// trial.
// Test with insecure private network requests blocked, excluding navigations.
class PrivateNetworkAccessDeprecationTrialDisabledBrowserTest
: public PrivateNetworkAccessBrowserTestBase {
public:
PrivateNetworkAccessDeprecationTrialDisabledBrowserTest()
: PrivateNetworkAccessBrowserTestBase(
{
features::kBlockInsecurePrivateNetworkRequests,
},
{
features::kBlockInsecurePrivateNetworkRequestsDeprecationTrial,
network::features::kLocalNetworkAccessChecks,
}) {}
};
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessDeprecationTrialDisabledBrowserTest,
OriginEnabledDoesNothing) {
DeprecationTrialURLLoaderInterceptor interceptor;
EXPECT_TRUE(NavigateToURL(shell(), interceptor.EnabledUrl()));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kBlock);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
DeprecationTrialOriginEnabled) {
DeprecationTrialURLLoaderInterceptor interceptor;
EXPECT_TRUE(NavigateToURL(shell(), interceptor.EnabledUrl()));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kAllow);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
DeprecationTrialOriginDisabled) {
DeprecationTrialURLLoaderInterceptor interceptor;
EXPECT_TRUE(NavigateToURL(shell(), interceptor.DisabledUrl()));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kBlock);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
DeprecationTrialSettingInheritedByInitialEmptyDoc) {
DeprecationTrialURLLoaderInterceptor interceptor;
EXPECT_TRUE(NavigateToURL(shell(), interceptor.EnabledUrl()));
RenderFrameHostImpl* child_frame = AddChildInitialEmptyDoc(root_frame_host());
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
// TODO(crbug.com/40058599): Expect `kAllow` here once inheritance is
// properly implemented.
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kBlock);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
DeprecationTrialSettingInheritedByAboutBlank) {
DeprecationTrialURLLoaderInterceptor interceptor;
EXPECT_TRUE(NavigateToURL(shell(), interceptor.EnabledUrl()));
RenderFrameHostImpl* child_frame = AddChildFromAboutBlank(root_frame_host());
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
// TODO(crbug.com/40058599): Expect `kAllow` here once inheritance is
// properly implemented.
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kBlock);
}
// `data:` URLs do not inherit their navigation initiator's origin, so they
// should not inherit deprecation trials.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
DeprecationTrialSettingNotInheritedByDataURL) {
DeprecationTrialURLLoaderInterceptor interceptor;
EXPECT_TRUE(NavigateToURL(shell(), interceptor.EnabledUrl()));
RenderFrameHostImpl* child_frame = AddChildFromDataURL(root_frame_host());
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kBlock);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
DeprecationTrialSettingNotInheritedBySandboxedIframe) {
DeprecationTrialURLLoaderInterceptor interceptor;
EXPECT_TRUE(NavigateToURL(shell(), interceptor.EnabledUrl()));
RenderFrameHostImpl* child_frame =
AddSandboxedChildFromAboutBlank(root_frame_host());
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kBlock);
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
DeprecationTrialSettingNotInheritedByErrorPage) {
DeprecationTrialURLLoaderInterceptor interceptor;
EXPECT_TRUE(NavigateToURL(shell(), interceptor.EnabledUrl()));
RenderFrameHostImpl* child_frame =
AddChildFromURL(root_frame_host(), "/close-socket");
// The iframe committed an error page.
EXPECT_EQ(GURL(kUnreachableWebDataURL),
EvalJs(child_frame, "document.location.href"));
EXPECT_TRUE(child_frame->GetLastCommittedOrigin().opaque());
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
// TODO(crbug.com/40747546): Expect `kBlock` once error pages have
// stricter policies, or decide that this is right and remove this test.
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kAllow);
}
// =======================
// SUBRESOURCE FETCH TESTS
// =======================
//
// These tests verify the behavior of the browser when fetching subresources
// across IP address spaces. When the right features are enabled, private
// network requests are blocked.
// This test mimics the tests below, with all blocking features disabled. It
// verifies that by default requests:
// - from an insecure page with the "treat-as-public-address" CSP directive
// - to a local IP address
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestNoBlocking,
PrivateNetworkRequestIsNotBlockedByDefault) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
// Check that the page can load a local resource.
EXPECT_EQ(true, EvalJs(root_frame_host(),
FetchSubresourceScript(InsecureLocalURL(kCorsPath))));
}
// Check that the `--disable-web-security` command-line switch disables PNA
// checks.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestDisableWebSecurity,
PrivateNetworkRequestIsNotBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
// Check that the page can load a local resource.
EXPECT_EQ(true, EvalJs(root_frame_host(),
FetchSubresourceScript(InsecureLocalURL(kCorsPath))));
}
// This test verifies that when preflights are disabled, requests:
// - from a secure page with the "treat-as-public-address" CSP directive
// - to a local IP address
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FromSecureTreatAsPublicToLocalIsNotBlocked) {
EXPECT_TRUE(
NavigateToURL(shell(), SecureLocalURL(kTreatAsPublicAddressPath)));
// Check that the page can load a local resource. We load it from a secure
// origin to avoid running afoul of mixed content restrictions.
EXPECT_EQ(true,
EvalJs(root_frame_host(),
FetchSubresourceScript(OtherSecureLocalURL(kCorsPath))));
}
// This test verifies that when preflights are disabled, requests:
// - from a secure page served from a public IP address
// - to a local IP address
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestNoPreflights,
FromSecurePublicToLocalIsNotBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
// Check that the page can load a local resource. We load it from a secure
// origin to avoid running afoul of mixed content restrictions.
EXPECT_EQ(true, EvalJs(root_frame_host(),
FetchSubresourceScript(SecureLocalURL(kCorsPath))));
}
// This test verifies that when preflights are sent but not enforced, requests:
// - from a secure page served from a public IP address
// - to a local IP address
// - for which the target server does not respond OK to the preflight request
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FromSecurePublicToLocalIsNotBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
// Check that the page can load a local resource.
//
// We load the resource from a secure origin to avoid running afoul of mixed
// content restrictions.
EXPECT_EQ(true, EvalJs(root_frame_host(),
FetchSubresourceScript(SecureLocalURL(kCorsPath))));
}
// This test verifies that when preflights are sent and enforced, requests:
// - from a secure page served from a public IP address
// - to a local IP address
// - when the target server does not respond OK to the preflight request
// are blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestRespectPreflightResults,
FromSecurePublicToLocalIsBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
// We load the resource from a secure origin to avoid running afoul of mixed
// content restrictions.
EXPECT_EQ(false, EvalJs(root_frame_host(),
FetchSubresourceScript(SecureLocalURL(kCorsPath))));
}
// This test verifies that when preflights are disabled, requests:
// - from a secure page served from a private IP address
// - to a local IP address
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestNoPreflights,
FromSecurePrivateToLocalIsNotBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePrivateURL(kDefaultPath)));
// Check that the page can load a local resource. We load it from a secure
// origin to avoid running afoul of mixed content restrictions.
EXPECT_EQ(true, EvalJs(root_frame_host(),
FetchSubresourceScript(SecureLocalURL(kCorsPath))));
}
// This test verifies that when preflights are sent but not enforced, requests:
// - from a secure page served from a private IP address
// - to a local IP address
// - for which the target server does not respond OK to the preflight request
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FromSecurePrivateToLocalIsNotBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePrivateURL(kDefaultPath)));
// Check that the page can load a local resource.
//
// We load it from a secure origin to avoid running afoul of mixed content
// restrictions.
EXPECT_EQ(true, EvalJs(root_frame_host(),
FetchSubresourceScript(SecureLocalURL(kCorsPath))));
}
// This test verifies that when preflights are sent and enforced, requests:
// - from a secure page served from a private IP address
// - to a local IP address
// - for which the target server does not respond OK to the preflight request
// are blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestRespectPreflightResults,
FromSecurePrivateToLocalIsBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePrivateURL(kDefaultPath)));
// We load the resource from a secure origin to avoid running afoul of mixed
// content restrictions.
EXPECT_EQ(false, EvalJs(root_frame_host(),
FetchSubresourceScript(SecureLocalURL(kCorsPath))));
}
// This test verifies that when preflights are disabled, requests:
// - from a secure page served from a local IP address
// - to a local IP address
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestNoPreflights,
FromSecureLocalToLocalIsNotBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
// Check that the page can load a local resource. We load it from a secure
// origin to avoid running afoul of mixed content restrictions.
EXPECT_EQ(true,
EvalJs(root_frame_host(),
FetchSubresourceScript(OtherSecureLocalURL(kCorsPath))));
}
// This test verifies that when preflights are sent but not enforced, requests:
// - from a secure page served from a local IP address
// - to a local IP address
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FromSecureLocalToLocalIsNotBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
// Check that the page can load a local resource. We load it from a secure
// origin to avoid running afoul of mixed content restrictions.
EXPECT_EQ(true,
EvalJs(root_frame_host(),
FetchSubresourceScript(OtherSecureLocalURL(kCorsPath))));
}
// This test verifies that when preflights are sent and enforced, requests:
// - from a secure page served from a local IP address
// - to a local IP address
// - for which the target server does not respond OK to the preflight request
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestRespectPreflightResults,
FromSecureLocalToLocalIsNotBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
// Check that the page can load a local resource. We load it from a secure
// origin to avoid running afoul of mixed content restrictions.
EXPECT_EQ(true,
EvalJs(root_frame_host(),
FetchSubresourceScript(OtherSecureLocalURL(kCorsPath))));
}
// This test verifies that when preflights are sent but not enforced, requests:
// - from a secure page served from a local IP address
// - to a local IP address
// - for which the target server responds OK to the preflight request
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FromSecurePublicToLocalPreflightOK) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
// Check that the page can load a local resource. We load it from a secure
// origin to avoid running afoul of mixed content restrictions.
EXPECT_EQ(true, EvalJs(root_frame_host(),
FetchSubresourceScript(SecureLocalURL(kPnaPath))));
}
// This test verifies that when preflights are sent and enforced, requests:
// - from a secure page served from a local IP address
// - to a local IP address
// - for which the target server responds OK to the preflight request
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestRespectPreflightResults,
FromSecurePublicToLocalPreflightOK) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
// Check that the page can load a local resource. We load it from a secure
// origin to avoid running afoul of mixed content restrictions.
EXPECT_EQ(true, EvalJs(root_frame_host(),
FetchSubresourceScript(SecureLocalURL(kPnaPath))));
}
// TODO(crbug.com/40221632): Re-enable this test
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestRespectPreflightResults,
DISABLED_PreflightConnectionReusedHttp1) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
EXPECT_EQ(true, EvalJs(root_frame_host(),
FetchSubresourceScript(SecureLocalURL(kPnaPath))));
// Expect 3 requests, but only 2 connections:
//
// 1. The initial request opens a connection, then is cancelled when the
// private network request is detected.
// 2. The preflight request likely reuses this connection.
// 3. The actual request opens a new connection (the embedded test server does
// not handle keep-alives).
//
// The socket opened by the initial request is returned to the socket pools
// asynchronously, so there is the potential for a race condition here, if
// the following requests are sent very quickly. Sometimes the preflight
// request might not reuse the initial connection and opens its own. In those
// cases, it is extremely likely that the final request will reuse the first
// socket.
//
// TODO(crbug.com/40221632): Find out why the connection is not re-used
// on Mac 11. Likely culprit is some kind of race condition, since the socket
// closure during 1) above is not synchronized with 2) and 3).
#if BUILDFLAG(IS_MAC)
int connection_count = SecureLocalServer().ConnectionCount();
EXPECT_GE(connection_count, 2); // At least 2 connections.
EXPECT_LE(connection_count, 3); // No more than 3 connections.
#else
EXPECT_EQ(SecureLocalServer().ConnectionCount(), 2);
#endif
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestRespectPreflightResults,
PreflightConnectionReusedHttp2) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
ConnectionCounter counter;
net::EmbeddedTestServer http2_server(
net::EmbeddedTestServer::TYPE_HTTPS,
net::test_server::HttpConnection::Protocol::kHttp2);
http2_server.SetConnectionListener(&counter);
http2_server.AddDefaultHandlers(GetTestDataFilePath());
ASSERT_TRUE(http2_server.Start());
EXPECT_EQ(true,
EvalJs(root_frame_host(),
FetchSubresourceScript(http2_server.GetURL(kPnaPath))));
EXPECT_EQ(counter.count(), 1);
}
// This test verifies that when the right feature is enabled but the content
// browser client overrides it, requests:
// - from an insecure page with the "treat-as-public-address" CSP directive
// - to a local IP address
// are not blocked.
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
FromInsecureTreatAsPublicToLocalWithPolicySetToAllowIsNotBlocked) {
GURL url = InsecureLocalURL(kTreatAsPublicAddressPath);
PolicyTestContentBrowserClient client;
client.SetAllowInsecurePrivateNetworkRequestsFrom(url::Origin::Create(url));
EXPECT_TRUE(NavigateToURL(shell(), url));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kAllow);
// Check that the page can load a local resource.
EXPECT_EQ(true, EvalJs(root_frame_host(),
FetchSubresourceScript(InsecureLocalURL(kCorsPath))));
}
// This test verifies that when the right feature is enabled, requests:
// - from an insecure page with the "treat-as-public-address" CSP directive
// - to a local IP address
// are blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FromInsecureTreatAsPublicToLocalIsBlocked) {
EXPECT_TRUE(
NavigateToURL(shell(), InsecureLocalURL(kTreatAsPublicAddressPath)));
// Check that the page cannot load a local resource.
EXPECT_EQ(false, EvalJs(root_frame_host(),
FetchSubresourceScript(InsecureLocalURL(kCorsPath))));
}
// This test verifies that when the right feature is enabled, requests:
// - from an insecure page served by a public IP address
// - to local IP addresses
// are blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FromInsecurePublicToLocalIsBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
// Check that the page cannot load a local resource.
EXPECT_EQ(false, EvalJs(root_frame_host(),
FetchSubresourceScript(InsecureLocalURL(kCorsPath))));
}
// This test verifies that when the right feature is disabled, requests:
// - from an insecure page served by a private IP address
// - to local IP addresses
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FromInsecurePrivateToLocalIsNotBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePrivateURL(kDefaultPath)));
// Check that the page can load a local resource.
EXPECT_EQ(true, EvalJs(root_frame_host(),
FetchSubresourceScript(InsecureLocalURL(kCorsPath))));
}
// This test verifies that when the right feature is enabled, requests:
// - from an insecure page served by a private IP address
// - to local IP addresses
// are blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestBlockFromPrivate,
FromInsecurePrivateToLocalIsBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePrivateURL(kDefaultPath)));
// Check that the page cannot load a local resource.
EXPECT_EQ(false, EvalJs(root_frame_host(),
FetchSubresourceScript(InsecureLocalURL(kCorsPath))));
}
// This test verifies that when the right feature is enabled, requests:
// - from an insecure page served by a local IP address
// - to local IP addresses
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FromInsecureLocalToLocalIsNotBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
// Check that the page can load a local resource.
EXPECT_EQ(true, EvalJs(root_frame_host(),
FetchSubresourceScript(InsecureLocalURL(kCorsPath))));
}
// This test verifies that when the right feature is enabled, requests:
// - from a secure page with the "treat-as-public-address" CSP directive
// - embedded in an insecure page served from a local IP address
// - to local IP addresses
// are blocked.
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTest,
FromSecurePublicEmbeddedInInsecureLocalToLocalIsBlocked) {
// First navigate to an insecure page served by a local IP address.
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
// Then embed a secure public iframe.
std::string script = JsReplace(
R"(
const iframe = document.createElement("iframe");
iframe.src = $1;
document.body.appendChild(iframe);
)",
SecurePublicURL(kDefaultPath));
EXPECT_TRUE(ExecJs(root_frame_host(), script));
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
ASSERT_EQ(1ul, root_frame_host()->child_count());
RenderFrameHostImpl* child_frame =
root_frame_host()->child_at(0)->current_frame_host();
const network::mojom::ClientSecurityStatePtr security_state =
child_frame->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
// Even though the iframe document was loaded from a secure connection, the
// context is deemed insecure because it was embedded by an insecure context.
EXPECT_FALSE(security_state->is_web_secure_context);
// The address space of the document, however, is not influenced by the
// parent's address space.
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
// Check that the iframe cannot load a local resource.
EXPECT_EQ(false, EvalJs(child_frame,
FetchSubresourceScript(InsecureLocalURL(kCorsPath))));
}
// This test verifies that even when the right feature is enabled, requests:
// - from a non-secure context in the `local` IP address space
// - to a subresource cached from a `local` IP address
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FromInsecureLocalToCachedLocalIsNotBlocked) {
GURL target = InsecureLocalURL(kCacheablePath);
// Cache the resource first. The server receives a GET request.
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
EXPECT_EQ(true, EvalJs(root_frame_host(), FetchSubresourceScript(target)));
EXPECT_THAT(
InsecureLocalServer().request_observer().RequestMethodsForUrl(target),
ElementsAre(METHOD_GET));
// Check that the page can still load the subresource from cache. The server
// does not receive any new request.
EXPECT_EQ(true, EvalJs(root_frame_host(), FetchSubresourceScript(target)));
EXPECT_THAT(
InsecureLocalServer().request_observer().RequestMethodsForUrl(target),
ElementsAre(METHOD_GET));
}
// This test verifies that when the right feature is enabled, requests:
// - from a non-secure context in the `public` IP address space
// - to a subresource cached from a `local` IP address
// are blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FromInsecurePublicToCachedLocalIsBlocked) {
GURL target = InsecureLocalURL(kCacheablePath);
// Cache the resource first, by fetching it from a document in the same IP
// address space. The server receives a GET request.
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
EXPECT_EQ(true, EvalJs(root_frame_host(), FetchSubresourceScript(target)));
EXPECT_THAT(
InsecureLocalServer().request_observer().RequestMethodsForUrl(target),
ElementsAre(METHOD_GET));
// Now navigate to a document in the `public` address space belonging to the
// same site as the previous document (this will use the same cache key).
EXPECT_TRUE(
NavigateToURL(shell(), InsecureLocalURL(kTreatAsPublicAddressPath)));
// Check that the page cannot load the resource, even from cache. The server
// does not receive any new request.
EXPECT_EQ(false, EvalJs(root_frame_host(), FetchSubresourceScript(target)));
EXPECT_THAT(
InsecureLocalServer().request_observer().RequestMethodsForUrl(target),
ElementsAre(METHOD_GET));
}
// This test verifies that when preflights are sent and enforced, requests:
// - from a secure context in the `local` IP address space
// - to a subresource cached from a `local` IP address
// - for which the target server does not respond OK to the preflight request
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestRespectPreflightResults,
FromSecureLocalToCachedLocalIsNotBlocked) {
GURL target = SecureLocalURL(kCacheablePath);
// Cache the resource first. The server receives a GET request.
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
EXPECT_EQ(true, EvalJs(root_frame_host(), FetchSubresourceScript(target)));
EXPECT_THAT(
SecureLocalServer().request_observer().RequestMethodsForUrl(target),
ElementsAre(METHOD_GET));
// Check that the page can still load the subresource from cache. The server
// does not receive any new request.
EXPECT_EQ(true, EvalJs(root_frame_host(), FetchSubresourceScript(target)));
EXPECT_THAT(
SecureLocalServer().request_observer().RequestMethodsForUrl(target),
ElementsAre(METHOD_GET));
}
// This test verifies that when preflights are sent but not enforced, requests:
// - from a secure page served in the `public` IP address space
// - to a subresource cached from a `local` IP address
// - for which the target server does not respond OK to the preflight request
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FromSecurePublicToCachedLocalIsNotBlocked) {
GURL target = OtherSecureLocalURL(kCacheableCorsPath);
// Cache the resource first.
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
EXPECT_EQ(true, EvalJs(root_frame_host(), FetchSubresourceScript(target)));
EXPECT_THAT(
SecureLocalServer().request_observer().RequestMethodsForUrl(target),
ElementsAre(METHOD_GET));
EXPECT_TRUE(
NavigateToURL(shell(), SecureLocalURL(kTreatAsPublicAddressPath)));
// Check that the page can still load the subresource from cache.
EXPECT_EQ(true, EvalJs(root_frame_host(), FetchSubresourceScript(target)));
// The server receives a preflight request because the preflight response is
// not cached, but no second GET request.
EXPECT_THAT(
SecureLocalServer().request_observer().RequestMethodsForUrl(target),
ElementsAre(METHOD_GET, METHOD_OPTIONS));
}
// This test verifies that when preflights are sent and enforced, requests:
// - from a secure page served in the `public` IP address space
// - to a subresource cached from a `local` IP address
// - for which the target server does not respond OK to the preflight request
// are blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestRespectPreflightResults,
FromSecurePublicToCachedLocalIsBlocked) {
GURL target = OtherSecureLocalURL(kCacheableCorsPath);
// Cache the resource first.
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
EXPECT_EQ(true, EvalJs(root_frame_host(), FetchSubresourceScript(target)));
EXPECT_THAT(
SecureLocalServer().request_observer().RequestMethodsForUrl(target),
ElementsAre(METHOD_GET));
EXPECT_TRUE(
NavigateToURL(shell(), SecureLocalURL(kTreatAsPublicAddressPath)));
// Check that the page cannot load the subresource from cache.
EXPECT_EQ(false, EvalJs(root_frame_host(), FetchSubresourceScript(target)));
// The server receives a preflight request because the preflight response is
// not cached, but no second GET request.
EXPECT_THAT(
SecureLocalServer().request_observer().RequestMethodsForUrl(target),
ElementsAre(METHOD_GET, METHOD_OPTIONS));
}
// This test verifies that when preflights are sent and enforced, requests:
// - from a secure page served in the `public` IP address space
// - to a subresource cached from a `local` IP address
// - for which the target server responds OK to the preflight request
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestRespectPreflightResults,
FromSecurePublicToCachedLocalIsNotBlocked) {
GURL target = OtherSecureLocalURL(kCacheablePnaPath);
// Cache the resource first.
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
EXPECT_EQ(true, EvalJs(root_frame_host(), FetchSubresourceScript(target)));
EXPECT_THAT(
SecureLocalServer().request_observer().RequestMethodsForUrl(target),
ElementsAre(METHOD_GET));
EXPECT_TRUE(
NavigateToURL(shell(), SecureLocalURL(kTreatAsPublicAddressPath)));
// Check that the page can still load the subresource from cache.
EXPECT_EQ(true, EvalJs(root_frame_host(), FetchSubresourceScript(target)));
// The server receives a preflight request because the preflight response is
// not cached, but no second GET request.
EXPECT_THAT(
SecureLocalServer().request_observer().RequestMethodsForUrl(target),
ElementsAre(METHOD_GET, METHOD_OPTIONS));
}
// This test verifies that even with the blocking feature disabled, an insecure
// page in the `local` address space cannot fetch a `file:` URL.
//
// This is relevant to Private Network Access, since `file:` URLs are considered
// to be in the `local` IP address space.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestNoBlocking,
InsecurePageCannotRequestFile) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
// Check that the page cannot load a `file:` URL.
EXPECT_EQ(false, EvalJs(root_frame_host(), FetchSubresourceScript(GetTestUrl(
"", "empty.html"))));
}
// This test verifies that even with the blocking feature disabled, a secure
// page in the `local` address space cannot fetch a `file:` URL.
//
// This is relevant to Private Network Access, since `file:` URLs are considered
// to be in the `local` IP address space.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestNoBlocking,
SecurePageCannotRequestFile) {
EXPECT_TRUE(NavigateToURL(shell(), SecureLocalURL(kDefaultPath)));
// Check that the page cannot load a `file:` URL.
EXPECT_EQ(false, EvalJs(root_frame_host(), FetchSubresourceScript(GetTestUrl(
"", "empty.html"))));
}
// This test verifies that if a page redirects after responding to a private
// network request to a server in a different address space, the request does
// not fail.
// Regression test for https://crbug.com/1293891.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest, Redirect) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePrivateURL(kDefaultPath)));
GURL target =
SecureLocalURL("/server-redirect?" + SecurePrivateURL(kCorsPath).spec());
EXPECT_EQ(true, EvalJs(root_frame_host(), FetchSubresourceScript(target)));
}
// This test verifies that if a request is made for a resource of which a
// partial prefix range of bytes was cached, a preflight is correctly sent for
// the non-cached portion, and the renderer does not crash.
// Regression test for https://crbug.com/1279376.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest, PrefixRangePreflight) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
const GURL url = SecureLocalURL("/echorange?this-is-a-test");
constexpr std::string_view kFetchRangeScript = R"(
(async () => {
const url = $1;
const range = $2;
const headers = {};
if (range) {
headers["Range"] = range;
}
const response = await fetch(url, { headers });
const body = await response.text();
return body;
})()
)";
// Cache a portion of the target resource.
EXPECT_EQ("this", EvalJs(root_frame_host(),
JsReplace(kFetchRangeScript, url, "bytes=0-3")));
// The server received a preflight request, followed by a GET request.
EXPECT_THAT(SecureLocalServer().request_observer().RequestMethodsForUrl(url),
ElementsAre(net::test_server::METHOD_OPTIONS,
net::test_server::METHOD_GET));
// Fetch the whole resource.
EXPECT_EQ(
"this-is-a-test",
EvalJs(root_frame_host(), JsReplace(kFetchRangeScript, url, "bytes=0-")));
// The server received a single GET request for the non-cached suffix. The
// preflight response was previously cached, so there is no second preflight.
EXPECT_THAT(
SecureLocalServer().request_observer().RequestMethodsForUrl(url),
ElementsAre(net::test_server::METHOD_OPTIONS,
net::test_server::METHOD_GET, net::test_server::METHOD_GET));
}
// =========================
// WORKER SCRIPT FETCH TESTS
// =========================
namespace {
// Path to a worker script that posts a message to its creator once loaded.
constexpr char kWorkerScriptPath[] = "/workers/post_ready.js";
// Same as above, but with PNA headers set correctly for preflight requests.
constexpr char kWorkerScriptWithPnaHeadersPath[] =
"/workers/post_ready_with_pna_headers.js";
// Instantiates a dedicated worker script from `path`.
// If it loads successfully, the worker should post a message to its creator to
// signal success.
std::string FetchWorkerScript(std::string_view path) {
constexpr char kTemplate[] = R"(
new Promise((resolve) => {
const worker = new Worker($1);
worker.addEventListener("message", () => resolve(true));
worker.addEventListener("error", () => resolve(false));
})
)";
return JsReplace(kTemplate, path);
}
// Path to a worker script that posts a message to each client that connects.
constexpr char kSharedWorkerScriptPath[] = "/workers/shared_post_ready.js";
// Same as above, but with PNA headers set correctly for preflight requests.
constexpr char kSharedWorkerScriptWithPnaHeadersPath[] =
"/workers/shared_post_ready_with_pna_headers.js";
// Instantiates a shared worker script from `path`.
// If it loads successfully, the worker should post a message to each client
// that connects to it to signal success.
std::string FetchSharedWorkerScript(std::string_view path) {
constexpr char kTemplate[] = R"(
new Promise((resolve) => {
const worker = new SharedWorker($1);
worker.port.addEventListener("message", () => resolve(true));
worker.addEventListener("error", () => resolve(false));
worker.port.start();
})
)";
return JsReplace(kTemplate, path);
}
// TODO(crbug.com/40290702): Remove this and replace calls below with
// calls to `EXPECT_EQ` directly once Shared Workers are supported on Android.
void ExpectFetchSharedWorkerScriptResult(bool expected,
const EvalJsResult& result) {
#if !BUILDFLAG(IS_ANDROID)
EXPECT_EQ(expected, result);
#else
EXPECT_NE("", result.error);
#endif
}
} // namespace
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FetchWorkerFromInsecureTreatAsPublicToLocal) {
EXPECT_TRUE(
NavigateToURL(shell(), InsecureLocalURL(kTreatAsPublicAddressPath)));
EXPECT_EQ(true,
EvalJs(root_frame_host(), FetchWorkerScript(kWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestForWorkers,
FetchWorkerFromInsecureTreatAsPublicToLocal) {
EXPECT_TRUE(
NavigateToURL(shell(), InsecureLocalURL(kTreatAsPublicAddressPath)));
EXPECT_EQ(false,
EvalJs(root_frame_host(), FetchWorkerScript(kWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTestRespectPreflightResultsForWorkers,
FetchWorkerFromInsecureTreatAsPublicToLocal) {
EXPECT_TRUE(
NavigateToURL(shell(), InsecureLocalURL(kTreatAsPublicAddressPath)));
EXPECT_EQ(false,
EvalJs(root_frame_host(), FetchWorkerScript(kWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTestRespectPreflightResultsForWorkersWarningOnly,
FetchWorkerFromInsecurePublicToLocal) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
EXPECT_EQ(true,
EvalJs(root_frame_host(), FetchWorkerScript(kWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FetchWorkerFromSecureTreatAsPublicToLocal) {
EXPECT_TRUE(
NavigateToURL(shell(), SecureLocalURL(kTreatAsPublicAddressPath)));
EXPECT_EQ(true,
EvalJs(root_frame_host(), FetchWorkerScript(kWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestForWorkers,
FetchWorkerFromSecureTreatAsPublicToLocal) {
EXPECT_TRUE(
NavigateToURL(shell(), SecureLocalURL(kTreatAsPublicAddressPath)));
EXPECT_EQ(true,
EvalJs(root_frame_host(), FetchWorkerScript(kWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTestRespectPreflightResultsForWorkers,
FetchWorkerFromSecureTreatAsPublicToLocal) {
EXPECT_TRUE(
NavigateToURL(shell(), SecureLocalURL(kTreatAsPublicAddressPath)));
// The request is exempt from Private Network Access checks because it is
// same-origin and the origin is potentially trustworthy.
EXPECT_EQ(true,
EvalJs(root_frame_host(), FetchWorkerScript(kWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTestRespectPreflightResultsForWorkersWarningOnly,
FetchWorkerFromSecurePublicToLocalFailedPreflight) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
EXPECT_EQ(true,
EvalJs(root_frame_host(), FetchWorkerScript(kWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTestRespectPreflightResultsForWorkers,
FetchWorkerFromSecureTreatAsPublicToLocalSuccess) {
EXPECT_TRUE(
NavigateToURL(shell(), SecureLocalURL(kTreatAsPublicAddressPath)));
EXPECT_EQ(true, EvalJs(root_frame_host(),
FetchWorkerScript(kWorkerScriptWithPnaHeadersPath)));
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FetchSharedWorkerFromInsecureTreatAsPublicToLocal) {
EXPECT_TRUE(
NavigateToURL(shell(), InsecureLocalURL(kTreatAsPublicAddressPath)));
ExpectFetchSharedWorkerScriptResult(
true, EvalJs(root_frame_host(),
FetchSharedWorkerScript(kSharedWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestForWorkers,
FetchSharedWorkerFromInsecureTreatAsPublicToLocal) {
EXPECT_TRUE(
NavigateToURL(shell(), InsecureLocalURL(kTreatAsPublicAddressPath)));
ExpectFetchSharedWorkerScriptResult(
false, EvalJs(root_frame_host(),
FetchSharedWorkerScript(kSharedWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTestRespectPreflightResultsForWorkers,
FetchSharedWorkerFromInsecureTreatAsPublicToLocal) {
EXPECT_TRUE(
NavigateToURL(shell(), InsecureLocalURL(kTreatAsPublicAddressPath)));
ExpectFetchSharedWorkerScriptResult(
false, EvalJs(root_frame_host(),
FetchSharedWorkerScript(kSharedWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTestRespectPreflightResultsForWorkersWarningOnly,
FetchSharedWorkerFromInsecurePublicToLocal) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
ExpectFetchSharedWorkerScriptResult(
true, EvalJs(root_frame_host(),
FetchSharedWorkerScript(kSharedWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
FetchSharedWorkerFromSecureTreatAsPublicToLocal) {
EXPECT_TRUE(
NavigateToURL(shell(), SecureLocalURL(kTreatAsPublicAddressPath)));
ExpectFetchSharedWorkerScriptResult(
true, EvalJs(root_frame_host(),
FetchSharedWorkerScript(kSharedWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestForWorkers,
FetchSharedWorkerFromSecureTreatAsPublicToLocal) {
EXPECT_TRUE(
NavigateToURL(shell(), SecureLocalURL(kTreatAsPublicAddressPath)));
ExpectFetchSharedWorkerScriptResult(
true, EvalJs(root_frame_host(),
FetchSharedWorkerScript(kSharedWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTestRespectPreflightResultsForWorkers,
FetchSharedWorkerFromSecureTreatAsPublicToLocal) {
EXPECT_TRUE(
NavigateToURL(shell(), SecureLocalURL(kTreatAsPublicAddressPath)));
// The request is exempt from Private Network Access checks because it is
// same-origin and the origin is potentially trustworthy.
ExpectFetchSharedWorkerScriptResult(
true, EvalJs(root_frame_host(),
FetchSharedWorkerScript(kSharedWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTestRespectPreflightResultsForWorkersWarningOnly,
FetchSharedWorkerFromSecurePublicToLocalFailedPreflight) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
ExpectFetchSharedWorkerScriptResult(
true, EvalJs(root_frame_host(),
FetchSharedWorkerScript(kSharedWorkerScriptPath)));
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTestRespectPreflightResultsForWorkers,
FetchSharedWorkerFromSecureTreatAsPublicToLocalSuccess) {
EXPECT_TRUE(
NavigateToURL(shell(), SecureLocalURL(kTreatAsPublicAddressPath)));
ExpectFetchSharedWorkerScriptResult(
true,
EvalJs(root_frame_host(),
FetchSharedWorkerScript(kSharedWorkerScriptWithPnaHeadersPath)));
}
// ======================
// NAVIGATION FETCH TESTS
// ======================
//
// These tests verify the behavior of the browser when navigating across IP
// address spaces.
//
// Iframe navigations are effectively treated as subresource fetches of the
// initiator document: they are handled by checking the resource's address space
// against the initiator document's address space.
//
// Top-level navigations are never blocked.
//
// TODO(crbug.com/40149351): Revisit this when top-level navigations are
// subject to Private Network Access checks.
// When the `PrivateNetworkAccessForIframes` feature is disabled, iframe fetches
// are not subject to PNA checks.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeFromInsecurePublicToLocalIsNotBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
GURL url = InsecureLocalURL("/empty.html");
TestNavigationManager child_navigation_manager(shell()->web_contents(), url);
AddChildFromURLWithoutWaiting(root_frame_host(), url);
ASSERT_TRUE(child_navigation_manager.WaitForNavigationFinished());
// Check that the child iframe navigated successfully.
EXPECT_TRUE(child_navigation_manager.was_successful());
EXPECT_EQ(url, EvalJs(GetFirstChild(*root_frame_host()),
"document.location.href"));
EXPECT_THAT(
InsecureLocalServer().request_observer().RequestMethodsForUrl(url),
ElementsAre(METHOD_GET));
}
// When the `PrivateNetworkAccessForIframes` feature is disabled, iframe fetches
// are not subject to PNA checks.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTest,
IframeFromSecurePublicToLocalIsNotBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
GURL url = SecureLocalURL("/empty.html");
TestNavigationManager child_navigation_manager(shell()->web_contents(), url);
AddChildFromURLWithoutWaiting(root_frame_host(), url);
ASSERT_TRUE(child_navigation_manager.WaitForNavigationFinished());
// Check that the child iframe navigated successfully.
EXPECT_TRUE(child_navigation_manager.was_successful());
EXPECT_EQ(url, EvalJs(GetFirstChild(*root_frame_host()),
"document.location.href"));
EXPECT_THAT(SecureLocalServer().request_observer().RequestMethodsForUrl(url),
ElementsAre(METHOD_GET));
}
// This test verifies that when iframe support is enabled in warning-only mode,
// iframe requests:
// - from an insecure page served from a public IP address
// - to a local IP address
// are not blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestForNavigationsWarningOnly,
IframeFromInsecurePublicToLocalIsNotBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
GURL url = InsecureLocalURL("/empty.html");
TestNavigationManager child_navigation_manager(shell()->web_contents(), url);
AddChildFromURLWithoutWaiting(root_frame_host(), url);
ASSERT_TRUE(child_navigation_manager.WaitForNavigationFinished());
// Check that the child iframe fetched successfully.
EXPECT_TRUE(child_navigation_manager.was_successful());
EXPECT_EQ(url, EvalJs(GetFirstChild(*root_frame_host()),
"document.location.href"));
EXPECT_THAT(
InsecureLocalServer().request_observer().RequestMethodsForUrl(url),
ElementsAre(METHOD_GET));
}
// This test verifies that when the right feature is enabled, iframe requests:
// - from an insecure page served from a public IP address
// - to a local IP address
// are blocked.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestForNavigations,
IframeFromInsecurePublicToLocalIsBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
GURL url = InsecureLocalURL("/empty.html");
TestNavigationManager child_navigation_manager(shell()->web_contents(), url);
AddChildFromURLWithoutWaiting(root_frame_host(), url);
ASSERT_TRUE(child_navigation_manager.WaitForNavigationFinished());
// Check that the child iframe failed to fetch.
EXPECT_FALSE(child_navigation_manager.was_successful());
RenderFrameHostImpl* child_frame = GetFirstChild(*root_frame_host());
EXPECT_EQ(GURL(kUnreachableWebDataURL),
EvalJs(child_frame, "document.location.href"));
// The frame committed an error page but retains the original URL so that
// reloading the page does the right thing. The committed origin on the other
// hand is opaque, which it would not be if the navigation had succeeded.
EXPECT_EQ(url, child_frame->GetLastCommittedURL());
EXPECT_TRUE(child_frame->GetLastCommittedOrigin().opaque());
// Blocked before we ever sent a request.
EXPECT_THAT(
InsecureLocalServer().request_observer().RequestMethodsForUrl(url),
IsEmpty());
}
// Same as above, testing the "treat-as-public-address" CSP directive.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestForNavigations,
IframeFromInsecureTreatAsPublicToLocalIsBlocked) {
EXPECT_TRUE(
NavigateToURL(shell(), InsecureLocalURL(kTreatAsPublicAddressPath)));
GURL url = InsecureLocalURL("/empty.html");
TestNavigationManager child_navigation_manager(shell()->web_contents(), url);
AddChildFromURLWithoutWaiting(root_frame_host(), url);
ASSERT_TRUE(child_navigation_manager.WaitForNavigationFinished());
// Check that the child iframe failed to fetch.
EXPECT_FALSE(child_navigation_manager.was_successful());
}
// This test verifies that when an iframe navigation fails due to PNA, the
// iframe navigates to an error page, even if it had previously committed a
// document.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestForNavigations,
FailedNavigationCommitsErrorPage) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
// First add a child frame, which successfully commits a document.
AddChildFromURL(root_frame_host(), "/empty.html");
GURL url = InsecureLocalURL("/empty.html");
TestNavigationManager child_navigation_manager(shell()->web_contents(), url);
// Then try to navigate that frame in a way that fails PNA checks.
EXPECT_TRUE(ExecJs(
root_frame_host(),
JsReplace("document.getElementsByTagName('iframe')[0].src = $1;", url)));
ASSERT_TRUE(child_navigation_manager.WaitForNavigationFinished());
// Check that the child iframe failed to fetch.
EXPECT_FALSE(child_navigation_manager.was_successful());
RenderFrameHostImpl* child_frame = GetFirstChild(*root_frame_host());
EXPECT_EQ(GURL(kUnreachableWebDataURL),
EvalJs(child_frame, "document.location.href"));
// The frame committed an error page but retains the original URL so that
// reloading the page does the right thing. The committed origin on the other
// hand is opaque, which it would not be if the navigation had succeeded.
EXPECT_EQ(url, child_frame->GetLastCommittedURL());
EXPECT_TRUE(child_frame->GetLastCommittedOrigin().opaque());
// Blocked before we ever sent a request.
EXPECT_THAT(
InsecureLocalServer().request_observer().RequestMethodsForUrl(url),
IsEmpty());
}
// This test verifies that when iframe support is enabled in warning-only mode,
// iframe requests:
// - from a secure page served from a public IP address
// - to a local IP address
// are preceded by a preflight request which is allowed to fail.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestForNavigationsWarningOnly,
IframeFromSecurePublicToLocalIsNotBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
GURL url = SecureLocalURL("/empty.html");
TestNavigationManager child_navigation_manager(shell()->web_contents(), url);
AddChildFromURLWithoutWaiting(root_frame_host(), url);
ASSERT_TRUE(child_navigation_manager.WaitForNavigationFinished());
EXPECT_TRUE(child_navigation_manager.was_successful());
EXPECT_EQ(url, EvalJs(GetFirstChild(*root_frame_host()),
"document.location.href"));
// A preflight request first, then the GET request.
EXPECT_THAT(SecureLocalServer().request_observer().RequestMethodsForUrl(url),
ElementsAre(METHOD_OPTIONS, METHOD_GET));
}
// This test verifies that when the right feature is enabled, iframe requests:
// - from a secure page served from a public IP address
// - to a local IP address
// are preceded by a preflight request which must succeed.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestForNavigations,
IframeFromSecurePublicToLocalIsBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
GURL url = SecureLocalURL("/empty.html");
TestNavigationManager child_navigation_manager(shell()->web_contents(), url);
AddChildFromURLWithoutWaiting(root_frame_host(), url);
ASSERT_TRUE(child_navigation_manager.WaitForNavigationFinished());
// Check that the child iframe failed to fetch.
EXPECT_FALSE(child_navigation_manager.was_successful());
RenderFrameHostImpl* child_frame = GetFirstChild(*root_frame_host());
EXPECT_EQ(GURL(kUnreachableWebDataURL),
EvalJs(child_frame, "document.location.href"));
// The frame committed an error page but retains the original URL so that
// reloading the page does the right thing. The committed origin on the other
// hand is opaque, which it would not be if the navigation had succeeded.
EXPECT_EQ(url, child_frame->GetLastCommittedURL());
EXPECT_TRUE(child_frame->GetLastCommittedOrigin().opaque());
// A preflight request only.
EXPECT_THAT(SecureLocalServer().request_observer().RequestMethodsForUrl(url),
ElementsAre(METHOD_OPTIONS));
}
// This test verifies that when the right feature is enabled, iframe requests:
// - from a secure page served from a public IP address
// - to a local IP address
// are preceded by a preflight request, to which the server must respond
// correctly.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestForNavigations,
IframeFromSecurePublicToLocalIsNotBlocked) {
GURL initiator_url = SecurePublicURL(kDefaultPath);
EXPECT_TRUE(NavigateToURL(shell(), initiator_url));
GURL url =
SecureLocalURL(MakePnaPathForIframe(url::Origin::Create(initiator_url)));
TestNavigationManager child_navigation_manager(shell()->web_contents(), url);
AddChildFromURLWithoutWaiting(root_frame_host(), url);
ASSERT_TRUE(child_navigation_manager.WaitForNavigationFinished());
// Check that the child iframe navigated successfully.
EXPECT_TRUE(child_navigation_manager.was_successful());
RenderFrameHostImpl* child_frame = GetFirstChild(*root_frame_host());
EXPECT_EQ(url, EvalJs(child_frame, "document.location.href"));
EXPECT_EQ(url, child_frame->GetLastCommittedURL());
// A preflight request first, then the GET request.
EXPECT_THAT(SecureLocalServer().request_observer().RequestMethodsForUrl(url),
ElementsAre(METHOD_OPTIONS, METHOD_GET));
}
// Same as above, testing the "treat-as-public-address" CSP directive.
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestForNavigations,
IframeFromSecureTreatAsPublicToLocalIsNotBlocked) {
GURL initiator_url = SecureLocalURL(kTreatAsPublicAddressPath);
EXPECT_TRUE(NavigateToURL(shell(), initiator_url));
GURL url = OtherSecureLocalURL(
MakePnaPathForIframe(url::Origin::Create(initiator_url)));
TestNavigationManager child_navigation_manager(shell()->web_contents(), url);
AddChildFromURLWithoutWaiting(root_frame_host(), url);
ASSERT_TRUE(child_navigation_manager.WaitForNavigationFinished());
// Check that the child iframe navigated successfully.
EXPECT_TRUE(child_navigation_manager.was_successful());
// A preflight request first, then the GET request.
EXPECT_THAT(SecureLocalServer().request_observer().RequestMethodsForUrl(url),
ElementsAre(METHOD_OPTIONS, METHOD_GET));
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTestForNavigations,
FormSubmissionFromInsecurePublicToLocalIsBlockedInMainFrame) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
GURL url = InsecureLocalURL(kDefaultPath);
TestNavigationManager navigation_manager(shell()->web_contents(), url);
std::string_view script_template = R"(
const form = document.createElement("form");
form.action = $1;
form.method = "post";
document.body.appendChild(form);
form.submit();
)";
EXPECT_TRUE(ExecJs(root_frame_host(), JsReplace(script_template, url)));
ASSERT_TRUE(navigation_manager.WaitForNavigationFinished());
// Check that the form submission was blocked.
EXPECT_FALSE(navigation_manager.was_successful());
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTestForNavigations,
FormSubmissionFromInsecurePublicToLocalIsBlockedInChildFrame) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
GURL url = InsecureLocalURL(kDefaultPath);
TestNavigationManager navigation_manager(shell()->web_contents(), url);
std::string_view script_template = R"(
const iframe = document.createElement("iframe");
document.body.appendChild(iframe);
const childDoc = iframe.contentDocument;
const form = childDoc.createElement("form");
form.action = $1;
form.method = "post";
childDoc.body.appendChild(form);
form.submit();
)";
EXPECT_TRUE(ExecJs(root_frame_host(), JsReplace(script_template, url)));
ASSERT_TRUE(navigation_manager.WaitForNavigationFinished());
// Check that the child iframe was blocked.
EXPECT_FALSE(navigation_manager.was_successful());
ASSERT_EQ(1ul, root_frame_host()->child_count());
RenderFrameHostImpl* child_frame =
root_frame_host()->child_at(0)->current_frame_host();
// Failed navigation.
EXPECT_EQ(GURL(kUnreachableWebDataURL),
EvalJs(child_frame, "document.location.href"));
// The URL is the form target URL, to allow for reloading.
// The origin is opaque though, a symptom of the failed navigation.
EXPECT_EQ(url, child_frame->GetLastCommittedURL());
EXPECT_TRUE(child_frame->GetLastCommittedOrigin().opaque());
}
IN_PROC_BROWSER_TEST_F(
PrivateNetworkAccessBrowserTestForNavigations,
FormSubmissionGetFromInsecurePublicToLocalIsBlockedInChildFrame) {
EXPECT_TRUE(NavigateToURL(shell(), InsecurePublicURL(kDefaultPath)));
GURL target_url = InsecureLocalURL(kDefaultPath);
// The page navigates to `url` followed by an empty query: '?'.
GURL expected_url = GURL(target_url.spec() + "?");
TestNavigationManager navigation_manager(shell()->web_contents(),
expected_url);
std::string_view script_template = R"(
const iframe = document.createElement("iframe");
document.body.appendChild(iframe);
const childDoc = iframe.contentDocument;
const form = childDoc.createElement("form");
form.action = $1;
form.method = "get";
childDoc.body.appendChild(form);
form.submit();
)";
EXPECT_TRUE(
ExecJs(root_frame_host(), JsReplace(script_template, target_url)));
ASSERT_TRUE(navigation_manager.WaitForNavigationFinished());
// Check that the child iframe was blocked.
EXPECT_FALSE(navigation_manager.was_successful());
ASSERT_EQ(1ul, root_frame_host()->child_count());
RenderFrameHostImpl* child_frame =
root_frame_host()->child_at(0)->current_frame_host();
// Failed navigation.
EXPECT_EQ(GURL(kUnreachableWebDataURL),
EvalJs(child_frame, "document.location.href"));
// The URL is the form target URL, to allow for reloading.
// The origin is opaque though, a symptom of the failed navigation.
EXPECT_EQ(expected_url, child_frame->GetLastCommittedURL());
EXPECT_TRUE(child_frame->GetLastCommittedOrigin().opaque());
}
IN_PROC_BROWSER_TEST_F(PrivateNetworkAccessBrowserTestForNavigations,
SiblingNavigationFromInsecurePublicToLocalIsBlocked) {
EXPECT_TRUE(NavigateToURL(shell(), InsecureLocalURL(kDefaultPath)));
// Named targeting only works if the initiator is one of:
//
// - the target's parent -> uninteresting
// - the target's opener -> implies the target is a main frame
// - same-origin with the target -> the only option left
//
// Thus we use CSP: treat-as-public-address to place the initiator in a
// different IP address space as its same-origin target.
GURL initiator_url = InsecureLocalURL(kTreatAsPublicAddressPath);
GURL target_url = InsecureLocalURL(kDefaultPath);
constexpr std::string_view kScriptTemplate = R"(
function addChild(name, src) {
return new Promise((resolve) => {
const iframe = document.createElement("iframe");
iframe.name = name;
iframe.src = src;
iframe.onload = () => resolve(iframe);
document.body.appendChild(iframe);
});
}
Promise.all([
addChild("initiator", $1),
addChild("target", "/empty.html"),
]).then(() => true);
)";
EXPECT_EQ(true, EvalJs(root_frame_host(),
JsReplace(kScriptTemplate, initiator_url)));
ASSERT_EQ(2ul, root_frame_host()->child_count());
RenderFrameHostImpl* initiator = root_frame_host()->child_at(0)->current_frame_host();
EXPECT_EQ(initiator->GetLastCommittedURL(), initiator_url);
TestNavigationManager navigation_manager(shell()->web_contents(), target_url);
EXPECT_TRUE(
ExecJs(initiator, JsReplace("window.open($1, 'target')", target_url)));
ASSERT_TRUE(navigation_manager.WaitForNavigationFinished());
// Check that the child iframe was blocked.
EXPECT_FALSE(navigation_manager.was_successful());
// Request was blocked before it was even sent.
EXPECT_THAT(
SecureLocalServer().request_observer().RequestMethodsForUrl(target_url),
IsEmpty());
}
class LocalNetworkAccessBrowserTest
: public PrivateNetworkAccessBrowserTestBase {
public:
LocalNetworkAccessBrowserTest()
: PrivateNetworkAccessBrowserTestBase(
{
network::features::kLocalNetworkAccessChecks,
},
{}) {}
};
IN_PROC_BROWSER_TEST_F(LocalNetworkAccessBrowserTest, CheckSecurityState) {
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kPermissionWarn);
}
IN_PROC_BROWSER_TEST_F(LocalNetworkAccessBrowserTest, CheckBlockInsteadOfWarn) {
PolicyTestContentBrowserClient client;
client.SetBlockInsteadOfWarn();
EXPECT_TRUE(NavigateToURL(shell(), SecurePublicURL(kDefaultPath)));
const network::mojom::ClientSecurityStatePtr security_state =
root_frame_host()->BuildClientSecurityState();
ASSERT_FALSE(security_state.is_null());
EXPECT_TRUE(security_state->is_web_secure_context);
EXPECT_EQ(network::mojom::IPAddressSpace::kPublic,
security_state->ip_address_space);
EXPECT_EQ(security_state->private_network_request_policy,
network::mojom::PrivateNetworkRequestPolicy::kPermissionBlock);
}
} // namespace content
|