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
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/network/url_loader.h"
#include <algorithm>
#include <limits>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/containers/enum_set.h"
#include "base/containers/fixed_flat_set.h"
#include "base/feature_list.h"
#include "base/files/file.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/not_fatal_until.h"
#include "base/numerics/safe_conversions.h"
#include "base/sequence_checker.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/string_view_util.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/thread_annotations.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/typed_macros.h"
#include "base/unguessable_token.h"
#include "build/build_config.h"
#include "mojo/public/cpp/bindings/shared_remote.h"
#include "mojo/public/cpp/system/simple_watcher.h"
#include "net/base/completion_once_callback.h"
#include "net/base/features.h"
#include "net/base/isolation_info.h"
#include "net/base/load_flags.h"
#include "net/base/load_timing_info.h"
#include "net/base/load_timing_internal_info.h"
#include "net/base/mime_sniffer.h"
#include "net/base/net_error_details.h"
#include "net/base/net_errors.h"
#include "net/base/proxy_chain.h"
#include "net/base/schemeful_site.h"
#include "net/base/task/task_runner.h"
#include "net/base/transport_info.h"
#include "net/base/upload_data_stream.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_inclusion_status.h"
#include "net/cookies/cookie_setting_override.h"
#include "net/cookies/cookie_store.h"
#include "net/cookies/cookie_util.h"
#include "net/cookies/site_for_cookies.h"
#include "net/cookies/static_cookie_policy.h"
#include "net/device_bound_sessions/session.h"
#include "net/dns/public/secure_dns_policy.h"
#include "net/filter/filter_source_stream.h"
#include "net/http/http_connection_info.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_util.h"
#include "net/http/structured_headers.h"
#include "net/log/net_log_source_type.h"
#include "net/log/net_log_util.h"
#include "net/log/net_log_with_source.h"
#include "net/ssl/client_cert_store.h"
#include "net/ssl/ssl_connection_status_flags.h"
#include "net/ssl/ssl_info.h"
#include "net/ssl/ssl_private_key.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/redirect_info.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "services/network/accept_ch_frame_interceptor.h"
#include "services/network/ad_heuristic_cookie_overrides.h"
#include "services/network/cookie_settings.h"
#include "services/network/devtools_durable_msg_writer.h"
#include "services/network/file_opener_for_upload.h"
#include "services/network/orb/orb_impl.h"
#include "services/network/public/cpp/client_hints.h"
#include "services/network/public/cpp/constants.h"
#include "services/network/public/cpp/cors/cors.h"
#include "services/network/public/cpp/cors/origin_access_list.h"
#include "services/network/public/cpp/cross_origin_resource_policy.h"
#include "services/network/public/cpp/empty_url_loader_client.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/header_util.h"
#include "services/network/public/cpp/ip_address_space_util.h"
#include "services/network/public/cpp/loading_params.h"
#include "services/network/public/cpp/net_adapters.h"
#include "services/network/public/cpp/network_switches.h"
#include "services/network/public/cpp/parsed_headers.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/sri_message_signatures.h"
#include "services/network/public/mojom/client_security_state.mojom.h"
#include "services/network/public/mojom/cookie_access_observer.mojom.h"
#include "services/network/public/mojom/cookie_manager.mojom.h"
#include "services/network/public/mojom/devtools_observer.mojom.h"
#include "services/network/public/mojom/early_hints.mojom.h"
#include "services/network/public/mojom/fetch_api.mojom.h"
#include "services/network/public/mojom/http_raw_headers.mojom.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/public/mojom/network_context_client.mojom.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
#include "services/network/public/mojom/url_loader_network_service_observer.mojom-data-view.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "services/network/resource_scheduler/resource_scheduler_client.h"
#include "services/network/sec_header_helpers.h"
#include "services/network/shared_dictionary/shared_dictionary_access_checker.h"
#include "services/network/shared_dictionary/shared_dictionary_manager.h"
#include "services/network/shared_dictionary/shared_dictionary_storage.h"
#include "services/network/shared_resource_checker.h"
#include "services/network/shared_storage/shared_storage_request_helper.h"
#include "services/network/slop_bucket.h"
#include "services/network/ssl_private_key_proxy.h"
#include "services/network/throttling/scoped_throttling_token.h"
#include "services/network/throttling/throttling_controller.h"
#include "services/network/throttling/throttling_network_interceptor.h"
#include "services/network/trust_tokens/trust_token_request_helper.h"
#include "services/network/trust_tokens/trust_token_url_loader_interceptor.h"
#include "services/network/url_loader_factory.h"
#include "services/network/url_loader_util.h"
#include "third_party/abseil-cpp/absl/container/inlined_vector.h"
#include "url/origin.h"
namespace network {
namespace {
BASE_FEATURE(kDelayedCookieNotification, base::FEATURE_DISABLED_BY_DEFAULT);
// Cannot use 0, because this means "default" in
// mojo::core::Core::CreateDataPipe
constexpr size_t kBlockedBodyAllocationSize = 1;
// Size to allocate for `discard_buffer_`.
constexpr size_t kDiscardBufferSize = 128 * 1024;
constexpr char kActivateStorageAccessHeader[] = "activate-storage-access";
bool ShouldNotifyAboutCookie(net::CookieInclusionStatus status) {
// Notify about cookies actually used, and those blocked by preferences ---
// for purposes of cookie UI --- as well those carrying warnings pertaining to
// SameSite features and cookies with non-ASCII domain attributes, in order to
// issue a deprecation warning for them.
// Filter out tentative secure source scheme warnings. They're used for netlog
// debugging and not something we want to inform cookie observers about.
status.RemoveWarningReason(
net::CookieInclusionStatus::WarningReason::
WARN_TENTATIVELY_ALLOWING_SECURE_SOURCE_SCHEME);
return status.IsInclude() || status.ShouldWarn() ||
status.HasExclusionReason(net::CookieInclusionStatus::ExclusionReason::
EXCLUDE_USER_PREFERENCES) ||
status.HasExclusionReason(net::CookieInclusionStatus::ExclusionReason::
EXCLUDE_THIRD_PARTY_PHASEOUT) ||
status.HasExclusionReason(net::CookieInclusionStatus::ExclusionReason::
EXCLUDE_DOMAIN_NON_ASCII) ||
status.HasExclusionReason(net::CookieInclusionStatus::ExclusionReason::
EXCLUDE_PORT_MISMATCH) ||
status.HasExclusionReason(net::CookieInclusionStatus::ExclusionReason::
EXCLUDE_SCHEME_MISMATCH);
}
scoped_refptr<RefCountedDeviceBoundSessionAccessObserverRemote>
MaybeInitializeDeviceBoundSessionAccessObserverSharedRemote(
ObserverWrapper<mojom::DeviceBoundSessionAccessObserver>& observer,
URLLoaderContext& context) {
if (!base::FeatureList::IsEnabled(
features::kDeviceBoundSessionAccessObserverSharedRemote)) {
return nullptr;
}
auto remote = observer.TakeRemote();
if (remote) {
return base::MakeRefCounted<
RefCountedDeviceBoundSessionAccessObserverRemote>(std::move(remote));
}
return context.GetDeviceBoundSessionAccessObserverSharedRemote();
}
net::HttpRequestHeaders AttachCookies(const net::HttpRequestHeaders& headers,
const std::string& cookies_from_browser) {
DCHECK(!cookies_from_browser.empty());
// Parse the existing cookie line.
std::string old_cookies = headers.GetHeader(net::HttpRequestHeaders::kCookie)
.value_or(std::string());
net::cookie_util::ParsedRequestCookies parsed_cookies;
net::cookie_util::ParseRequestCookieLine(old_cookies, &parsed_cookies);
net::cookie_util::ParsedRequestCookies parsed_cookies_from_browser;
net::cookie_util::ParseRequestCookieLine(cookies_from_browser,
&parsed_cookies_from_browser);
// Add the browser cookies to the request.
for (const auto& cookie : parsed_cookies_from_browser) {
DCHECK(!cookie.first.empty());
// Ensure we're not adding duplicate cookies.
auto it = std::find_if(
parsed_cookies.begin(), parsed_cookies.end(),
[&cookie](const net::cookie_util::ParsedRequestCookie& old_cookie) {
return old_cookie.first == cookie.first;
});
if (it != parsed_cookies.end())
continue;
parsed_cookies.emplace_back(cookie.first, cookie.second);
}
net::HttpRequestHeaders updated_headers = headers;
std::string updated_cookies =
net::cookie_util::SerializeRequestCookieLine(parsed_cookies);
updated_headers.SetHeader(net::HttpRequestHeaders::kCookie, updated_cookies);
return updated_headers;
}
std::vector<network::mojom::HttpRawHeaderPairPtr>
ResponseHeaderToRawHeaderPairs(
const net::HttpResponseHeaders& response_headers) {
std::vector<network::mojom::HttpRawHeaderPairPtr> header_array;
size_t iterator = 0;
std::string name, value;
while (response_headers.EnumerateHeaderLines(&iterator, &name, &value)) {
header_array.emplace_back(std::in_place, std::move(name), std::move(value));
}
return header_array;
}
bool IncludesValidLoadField(const net::HttpResponseHeaders* headers) {
if (!headers) {
return false;
}
std::optional<std::string> header_value =
headers->GetNormalizedHeader(kActivateStorageAccessHeader);
if (!header_value) {
return false;
}
const std::optional<net::structured_headers::ParameterizedItem> item =
net::structured_headers::ParseItem(*header_value);
if (!item.has_value()) {
return false;
}
return item->item.is_token() && item->item.GetString() == "load";
}
mojo::SharedRemote<mojom::DeviceBoundSessionAccessObserver> Clone(
mojom::DeviceBoundSessionAccessObserver& observer) {
TRACE_EVENT("loading", "CloneDeviceBoundSessionAccessObserver");
base::ScopedUmaHistogramTimer timer(
"NetworkService.URLLoader.CloneDeviceBoundSessionAccessObserver",
base::ScopedUmaHistogramTimer::ScopedHistogramTiming::kMicrosecondTimes);
mojo::SharedRemote<mojom::DeviceBoundSessionAccessObserver> new_observer;
observer.Clone(new_observer.BindNewPipeAndPassReceiver());
return new_observer;
}
int32_t PopulateOptions(int32_t initial_options,
bool is_orb_enabled,
bool has_devtools_request_id) {
int32_t options = initial_options;
if (options & mojom::kURLLoadOptionReadAndDiscardBody) {
CHECK(!(options & mojom::kURLLoadOptionSniffMimeType))
<< "options ReadAndDiscardBody and SniffMimeType cannot be used "
"together";
if (is_orb_enabled) {
// TODO(ricea): Make ReadAndDiscardBody and ORB work together.
LOG(WARNING) << "Disabling ReadAndDiscardBody because ORB is enabled";
options &= ~mojom::kURLLoadOptionReadAndDiscardBody;
}
}
if (has_devtools_request_id) {
options |= mojom::kURLLoadOptionSendSSLInfoWithResponse |
mojom::kURLLoadOptionSendSSLInfoForCertificateError;
}
return options;
}
const scoped_refptr<base::SingleThreadTaskRunner>& TaskRunner(
net::RequestPriority priority) {
if (features::kNetworkServiceTaskSchedulerURLLoader.Get()) {
return net::GetTaskRunner(priority);
}
return base::SingleThreadTaskRunner::GetCurrentDefault();
}
} // namespace
URLLoader::MaybeSyncURLLoaderClient::MaybeSyncURLLoaderClient(
mojo::PendingRemote<mojom::URLLoaderClient> mojo_client,
base::WeakPtr<mojom::URLLoaderClient> sync_client)
: mojo_client_(std::move(mojo_client)),
sync_client_(std::move(sync_client)) {}
URLLoader::MaybeSyncURLLoaderClient::~MaybeSyncURLLoaderClient() = default;
void URLLoader::MaybeSyncURLLoaderClient::Reset() {
mojo_client_.reset();
sync_client_.reset();
}
mojo::PendingReceiver<mojom::URLLoaderClient>
URLLoader::MaybeSyncURLLoaderClient::BindNewPipeAndPassReceiver() {
sync_client_.reset();
return mojo_client_.BindNewPipeAndPassReceiver();
}
mojom::URLLoaderClient* URLLoader::MaybeSyncURLLoaderClient::Get() {
if (sync_client_)
return sync_client_.get();
if (mojo_client_)
return mojo_client_.get();
return nullptr;
}
URLLoader::URLLoader(
URLLoaderContext& context,
DeleteCallback delete_callback,
mojo::PendingReceiver<mojom::URLLoader> url_loader_receiver,
int32_t options,
const ResourceRequest& request,
mojo::PendingRemote<mojom::URLLoaderClient> url_loader_client,
base::WeakPtr<mojom::URLLoaderClient> sync_url_loader_client,
const net::NetworkTrafficAnnotationTag& traffic_annotation,
base::StrictNumeric<int32_t> request_id,
int keepalive_request_size,
base::WeakPtr<KeepaliveStatisticsRecorder> keepalive_statistics_recorder,
std::unique_ptr<TrustTokenRequestHelperFactory> trust_token_helper_factory,
SharedDictionaryManager* shared_dictionary_manager,
std::unique_ptr<SharedDictionaryAccessChecker> shared_dictionary_checker,
ObserverWrapper<mojom::CookieAccessObserver> cookie_observer,
ObserverWrapper<mojom::TrustTokenAccessObserver> trust_token_observer,
ObserverWrapper<mojom::URLLoaderNetworkServiceObserver>
url_loader_network_observer,
ObserverWrapper<mojom::DevToolsObserver> devtools_observer,
ObserverWrapper<mojom::DeviceBoundSessionAccessObserver>
device_bound_session_observer,
mojo::PendingRemote<mojom::AcceptCHFrameObserver> accept_ch_frame_observer,
bool shared_storage_writable_eligible,
SharedResourceChecker& shared_resource_checker,
std::unique_ptr<DevtoolsDurableMessageWriter> maybe_durable_message_writer)
: url_request_context_(context.GetUrlRequestContext()),
network_context_client_(context.GetNetworkContextClient()),
delete_callback_(std::move(delete_callback)),
resource_type_(request.resource_type),
is_load_timing_enabled_(request.enable_load_timing),
factory_params_(context.GetFactoryParams()),
coep_reporter_(context.GetCoepReporter()),
dip_reporter_(context.GetDipReporter()),
request_id_(request_id),
keepalive_request_size_(keepalive_request_size),
keepalive_(request.keepalive),
client_security_state_(
request.trusted_params
? request.trusted_params->client_security_state.Clone()
: nullptr),
do_not_prompt_for_login_(request.do_not_prompt_for_login),
receiver_(this, std::move(url_loader_receiver)),
url_loader_client_(std::move(url_loader_client),
std::move(sync_url_loader_client)),
writable_handle_watcher_(FROM_HERE,
mojo::SimpleWatcher::ArmingPolicy::MANUAL,
TaskRunner(request.priority)),
peer_closed_handle_watcher_(FROM_HERE,
mojo::SimpleWatcher::ArmingPolicy::MANUAL,
TaskRunner(request.priority)),
per_factory_orb_state_(context.GetMutableOrbState()),
devtools_request_id_(request.devtools_request_id),
options_(PopulateOptions(options,
factory_params_->is_orb_enabled,
!!devtools_request_id())),
request_mode_(request.mode),
request_credentials_mode_(request.credentials_mode),
has_user_activation_(request.trusted_params &&
request.trusted_params->has_user_activation),
request_destination_(request.destination),
expected_public_keys_(request.expected_public_keys),
resource_scheduler_client_(context.GetResourceSchedulerClient()),
keepalive_statistics_recorder_(std::move(keepalive_statistics_recorder)),
fetch_window_id_(request.fetch_window_id),
private_network_access_interceptor_(request,
GetClientSecurityState(),
options_),
trust_token_interceptor_(TrustTokenUrlLoaderInterceptor::MaybeCreate(
std::move(trust_token_helper_factory))),
shared_dictionary_checker_(std::move(shared_dictionary_checker)),
origin_access_list_(context.GetOriginAccessList()),
cookie_observer_(std::move(cookie_observer)),
trust_token_observer_(std::move(trust_token_observer)),
url_loader_network_observer_(std::move(url_loader_network_observer)),
devtools_observer_(std::move(devtools_observer)),
device_bound_session_observer_(std::move(device_bound_session_observer)),
device_bound_session_observer_shared_remote_(
MaybeInitializeDeviceBoundSessionAccessObserverSharedRemote(
device_bound_session_observer_,
context)),
shared_storage_request_helper_(
std::make_unique<SharedStorageRequestHelper>(
shared_storage_writable_eligible,
url_loader_network_observer_.get())),
ad_auction_event_record_request_helper_(
request.attribution_reporting_eligibility,
url_loader_network_observer_.get()),
has_fetch_streaming_upload_body_(
url_loader_util::HasFetchStreamingUploadBody(request)),
accept_ch_frame_interceptor_(AcceptCHFrameInterceptor::MaybeCreate(
std::move(accept_ch_frame_observer),
request.trusted_params ? request.trusted_params->enabled_client_hints
: std::nullopt)),
allow_cookies_from_browser_(
request.trusted_params &&
request.trusted_params->allow_cookies_from_browser),
cookies_from_browser_(allow_cookies_from_browser_
? url_loader_util::GetCookiesFromHeaders(
request.headers,
request.cors_exempt_headers)
: std::string()),
include_request_cookies_with_response_(
request.trusted_params &&
request.trusted_params->include_request_cookies_with_response),
include_load_timing_internal_info_with_response_(
request.trusted_params.has_value()),
provide_data_use_updates_(context.DataUseUpdatesEnabled()),
partial_decoder_decoding_buffer_size_(net::kMaxBytesToSniff),
permissions_policy_(request.permissions_policy),
durable_message_writer_(std::move(maybe_durable_message_writer)) {
DCHECK(delete_callback_);
if (options_ & mojom::kURLLoadOptionReadAndDiscardBody) {
if (!factory_params_->is_orb_enabled) {
discard_buffer_ =
base::MakeRefCounted<net::IOBufferWithSize>(kDiscardBufferSize);
}
}
mojom::TrustedURLLoaderHeaderClient* url_loader_header_client =
context.GetUrlLoaderHeaderClient();
if (url_loader_header_client &&
(options_ & mojom::kURLLoadOptionUseHeaderClient)) {
if (options_ & mojom::kURLLoadOptionAsCorsPreflight) {
url_loader_header_client->OnLoaderForCorsPreflightCreated(
request, header_client_.BindNewPipeAndPassReceiver());
} else {
url_loader_header_client->OnLoaderCreated(
request_id_, header_client_.BindNewPipeAndPassReceiver());
}
// Make sure the loader dies if |header_client_| has an error, otherwise
// requests can hang.
header_client_.set_disconnect_handler(
base::BindOnce(&URLLoader::OnMojoDisconnect, base::Unretained(this)));
}
receiver_.set_disconnect_handler(
base::BindOnce(&URLLoader::OnMojoDisconnect, base::Unretained(this)));
// If the request is to a URL that we can determine is an LNA request from
// just the URL, then trigger the LNA prompt. We only trigger this for request
// where GetAddressSpaceFromUrl() returns a value as those would also trigger
// if and when we move the LNA check to after hostname resolution but before
// connection.
const mojom::ClientSecurityState* client_security_state =
GetClientSecurityState();
if (client_security_state &&
client_security_state->private_network_request_policy ==
mojom::PrivateNetworkRequestPolicy::kPermissionBlock &&
url_loader_network_observer_) {
std::optional<mojom::IPAddressSpace> url_address_space =
GetAddressSpaceFromUrl(request.url);
if (url_address_space) {
PrivateNetworkAccessChecker lna_checker(request, client_security_state,
options_);
if (lna_checker.CheckAddressSpace(*url_address_space) ==
PrivateNetworkAccessCheckResult::kLNAPermissionRequired) {
// This passes in `TransportType::kDirect`, regardless of how the
// request may end up being connected -- the cases where we know this
// is an LNA request from the URL alone are ones where we have high
// confidence in triggering the LNA prompt.
//
// Ignoring the result of the permission here because the point of this
// call is to get the permission prompt shown if the permission is
// "prompt". Later LNA checks will check the permission and use the
// the result.
url_loader_network_observer_->OnLocalNetworkAccessPermissionRequired(
mojom::TransportType::kDirect, *url_address_space,
base::BindOnce([](mojom::LocalNetworkAccessResult result) {}));
}
}
}
url_request_ = url_request_context_->CreateRequest(
request.url, request.priority, this, traffic_annotation,
/*is_for_websockets=*/false, request.net_log_create_info);
TRACE_EVENT("loading", "URLLoader::URLLoader",
net::NetLogWithSourceToFlow(url_request_->net_log()));
// Set up UserData (pointing to `this`) first.
url_request_->SetUserData(kUserDataKey,
std::make_unique<UnownedPointer>(this));
// Configure the main request parameters. This must happen after setting
// UserData, as `ConfigureUrlRequest` might internally retrieve data (e.g.,
// PermissionsPolicy) via the `url_request_`'s UserData pointer.
url_loader_util::ConfigureUrlRequest(request, *factory_params_,
*origin_access_list_, *url_request_,
shared_resource_checker);
if (context.ShouldRequireIsolationInfo()) {
DCHECK(!url_request_->isolation_info().IsEmpty());
}
SetUpUrlRequestCallbacks(shared_dictionary_manager);
throttling_token_ = network::ScopedThrottlingToken::MaybeCreate(
url_request_->net_log().source().id, request.throttling_profile_id);
if (keepalive_ && keepalive_statistics_recorder_) {
keepalive_statistics_recorder_->OnLoadStarted(
*factory_params_->top_frame_id, keepalive_request_size_);
}
if (request.net_log_reference_info) {
// Log source object that created the request, if available.
url_request_->net_log().AddEventReferencingSource(
net::NetLogEventType::CREATED_BY,
request.net_log_reference_info.value());
}
// Resolve elements from request_body and prepare upload data.
if (request.request_body.get()) {
OpenFilesForUpload(request);
return;
}
ProcessOutboundTrustTokenInterceptor(request);
}
void URLLoader::SetUpUrlRequestCallbacks(
SharedDictionaryManager* shared_dictionary_manager) {
url_request_->SetRequestHeadersCallback(base::BindRepeating(
&URLLoader::SetRawRequestHeadersAndNotify, base::Unretained(this)));
if (shared_dictionary_checker_) {
url_request_->SetIsSharedDictionaryReadAllowedCallback(base::BindRepeating(
&URLLoader::IsSharedDictionaryReadAllowed, base::Unretained(this)));
}
if (devtools_request_id()) {
url_request_->SetResponseHeadersCallback(base::BindRepeating(
&URLLoader::SetRawResponseHeaders, base::Unretained(this)));
}
url_request_->SetEarlyResponseHeadersCallback(base::BindRepeating(
&URLLoader::NotifyEarlyResponse, base::Unretained(this)));
if (shared_dictionary_manager) {
url_request_->SetSharedDictionaryGetter(
shared_dictionary_manager->MaybeCreateSharedDictionaryGetter(
url_request_->load_flags(), request_destination_));
}
// Device bound session access can happen asynchronously as a result
// of this URLRequest. So pass a refcounted shared Remote that will outlive
// `this`.
if (device_bound_session_observer_shared_remote_) {
url_request_->SetDeviceBoundSessionAccessCallback(base::BindRepeating(
[](scoped_refptr<RefCountedDeviceBoundSessionAccessObserverRemote>
shared_remote,
const net::device_bound_sessions::SessionAccess& access) {
shared_remote.get()->data->OnDeviceBoundSessionAccessed(access);
},
device_bound_session_observer_shared_remote_));
} else if (device_bound_session_observer_) {
// This is just for the experiment to measure the impact on the
// DeviceBoundSessionAccessObserverSharedRemote feature.
// TODO(crbug.com/407680127): Remove this and when the feature is enabled
// and the feature flag is removed.
url_request_->SetDeviceBoundSessionAccessCallback(base::BindRepeating(
&mojom::DeviceBoundSessionAccessObserver::OnDeviceBoundSessionAccessed,
Clone(*device_bound_session_observer_)));
}
}
void URLLoader::OpenFilesForUpload(const ResourceRequest& request) {
std::vector<base::FilePath> paths;
for (const auto& element : *request.request_body.get()->elements()) {
if (element.type() == mojom::DataElementDataView::Tag::kFile) {
paths.push_back(element.As<network::DataElementFile>().path());
}
}
if (paths.empty()) {
SetUpUpload(request, std::vector<base::File>());
return;
}
if (!network_context_client_) {
DLOG(ERROR) << "URLLoader couldn't upload a file because no "
"NetworkContextClient is set.";
// Defer calling NotifyCompleted to make sure the URLLoader finishes
// initializing before getting deleted.
TaskRunner(url_request_->priority())
->PostTask(FROM_HERE, base::BindOnce(&URLLoader::NotifyCompleted,
weak_ptr_factory_.GetWeakPtr(),
net::ERR_ACCESS_DENIED));
return;
}
url_request_->LogBlockedBy("Opening Files");
file_opener_for_upload_ = std::make_unique<FileOpenerForUpload>(
std::move(paths), url_request_->url(), factory_params_->process_id,
network_context_client_,
base::BindOnce(&URLLoader::SetUpUpload, base::Unretained(this), request));
file_opener_for_upload_->Start();
}
void URLLoader::SetUpUpload(
const ResourceRequest& request,
base::expected<std::vector<base::File>, net::Error> file_open_result) {
if (file_opener_for_upload_) {
file_opener_for_upload_.reset();
// This corresponds to the LogBlockedBy call made just before creating
// FileOpenerForUpload.
url_request_->LogUnblocked();
}
if (!file_open_result.has_value()) {
// Defer calling NotifyCompleted to make sure the URLLoader finishes
// initializing before getting deleted.
TaskRunner(url_request_->priority())
->PostTask(FROM_HERE, base::BindOnce(&URLLoader::NotifyCompleted,
weak_ptr_factory_.GetWeakPtr(),
file_open_result.error()));
return;
}
scoped_refptr<base::SequencedTaskRunner> task_runner =
base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE});
url_request_->set_upload(url_loader_util::CreateUploadDataStream(
request.request_body.get(), file_open_result.value(), task_runner.get()));
if (request.enable_upload_progress) {
upload_progress_tracker_ = std::make_unique<UploadProgressTracker>(
FROM_HERE,
base::BindRepeating(&URLLoader::SendUploadProgress,
base::Unretained(this)),
url_request_.get());
}
ProcessOutboundTrustTokenInterceptor(request);
}
void URLLoader::ProcessOutboundTrustTokenInterceptor(
const ResourceRequest& request) {
// If no Trust Token parameters are specified, proceed to the next
// interceptor.
if (!request.trust_token_params) {
ProcessOutboundSharedStorageInterceptor();
return;
}
// If trust_token_params exist, the interceptor MUST have been created in the
// URLLoader constructor.
CHECK(trust_token_interceptor_);
// Ask the interceptor if any special load flags are needed.
url_request_->SetLoadFlags(url_request_->load_flags() |
trust_token_interceptor_->GetAdditionalLoadFlags(
request.trust_token_params.value()));
// Delegate the Begin phase of the Trust Token operation to the interceptor.
// The interceptor will asynchronously handle helper creation, calling Begin,
// and determining the outcome.
trust_token_interceptor_->BeginOperation(
request.trust_token_params->operation, url_request_->url(),
url_request_->isolation_info().top_frame_origin().value_or(url::Origin()),
url_request_->extra_request_headers(), request.trust_token_params.value(),
url_request_->net_log(),
// Provide a getter for the TrustTokenAccessObserver.
base::BindOnce(
[](base::WeakPtr<URLLoader> weak_ptr)
-> mojom::TrustTokenAccessObserver* {
return weak_ptr ? weak_ptr->trust_token_observer_.get() : nullptr;
},
weak_ptr_factory_.GetWeakPtr()),
// Provide a getter for the DevTools reporting callback.
base::BindOnce(
[](base::WeakPtr<URLLoader> weak_ptr)
-> base::OnceCallback<void(mojom::TrustTokenOperationResultPtr)> {
if (weak_ptr && weak_ptr->devtools_observer_.get() &&
weak_ptr->devtools_request_id_) {
return base::BindOnce(
&mojom::DevToolsObserver::OnTrustTokenOperationDone,
base::Unretained(weak_ptr->devtools_observer_.get()),
*weak_ptr->devtools_request_id_);
}
return base::OnceCallback<void(
mojom::TrustTokenOperationResultPtr)>();
},
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&URLLoader::OnDoneBeginningTrustTokenOperation,
weak_ptr_factory_.GetWeakPtr()));
}
void URLLoader::OnDoneBeginningTrustTokenOperation(
base::expected<net::HttpRequestHeaders, net::Error> result) {
// If `result` does not have a value, the operation failed or completed
// locally.
if (!result.has_value()) {
// Defer calling NotifyCompleted to make sure the URLLoader
// finishes initializing before getting deleted.
TaskRunner(url_request_->priority())
->PostTask(FROM_HERE, base::BindOnce(&URLLoader::NotifyCompleted,
weak_ptr_factory_.GetWeakPtr(),
result.error()));
return;
}
// Operation succeeded and returned headers to add/overwrite.
// Apply the headers provided by the interceptor.
for (const auto& header_pair : result->GetHeaderVector()) {
url_request_->SetExtraRequestHeaderByName(
header_pair.key, header_pair.value, /*overwrite=*/true);
}
// Trust Token outbound processing is done, proceed to the next interceptor.
ProcessOutboundSharedStorageInterceptor();
}
void URLLoader::ProcessOutboundSharedStorageInterceptor() {
DCHECK(shared_storage_request_helper_);
shared_storage_request_helper_->ProcessOutgoingRequest(*url_request_);
ScheduleStart();
}
void URLLoader::ScheduleStart() {
TRACE_EVENT("loading", "URLLoader::ScheduleStart",
net::NetLogWithSourceToFlow(url_request_->net_log()));
bool defer = false;
if (resource_scheduler_client_) {
resource_scheduler_request_handle_ =
resource_scheduler_client_->ScheduleRequest(
!(options_ & mojom::kURLLoadOptionSynchronous), url_request_.get());
resource_scheduler_request_handle_->set_resume_callback(
base::BindOnce(&URLLoader::ResumeStart, base::Unretained(this)));
resource_scheduler_request_handle_->WillStartRequest(&defer);
}
if (defer)
url_request_->LogBlockedBy("ResourceScheduler");
else
url_request_->Start();
}
URLLoader::~URLLoader() {
TRACE_EVENT("loading", "URLLoader::~URLLoader",
net::NetLogWithSourceToFlow(url_request_->net_log()));
if (keepalive_ && keepalive_statistics_recorder_) {
keepalive_statistics_recorder_->OnLoadFinished(
*factory_params_->top_frame_id, keepalive_request_size_);
}
if (!cookie_access_details_.empty()) {
// In case the response wasn't received successfully sent the call now.
// Note `cookie_observer_` is guaranteed non-null since
// `cookie_access_details_` is only appended to when it is valid.
cookie_observer_->OnCookiesAccessed(std::move(cookie_access_details_));
}
}
// static
const void* const URLLoader::kUserDataKey = &URLLoader::kUserDataKey;
void URLLoader::FollowRedirect(
const std::vector<std::string>& removed_headers,
const net::HttpRequestHeaders& modified_headers,
const net::HttpRequestHeaders& modified_cors_exempt_headers,
const std::optional<GURL>& new_url) {
if (!deferred_redirect_url_) {
NOTREACHED();
}
// Note: There are some ordering dependencies here.
// `CalculateStorageAccessStatus` depends on
// `url_request->cookie_setting_overrides()`. `SetFetchMetadataHeaders`
// depends on `url_request_->storage_access_status()`.
if (request_credentials_mode_ == mojom::CredentialsMode::kInclude) {
url_request_->set_storage_access_status(
url_request_->CalculateStorageAccessStatus());
} else {
url_request_->reset_storage_access_status();
}
// We may need to clear out old Sec- prefixed request headers. We'll attempt
// to do this before we re-add any.
MaybeRemoveSecHeaders(*url_request_, *deferred_redirect_url_);
SetFetchMetadataHeaders(*url_request_, request_mode_, has_user_activation_,
request_destination_, *deferred_redirect_url_,
*factory_params_, *origin_access_list_,
request_credentials_mode_);
ResetRawHeadersForRedirect();
// Removing headers can't make the set of pre-existing headers unsafe, but
// adding headers can.
if (!AreRequestHeadersSafe(modified_headers) ||
!AreRequestHeadersSafe(modified_cors_exempt_headers)) {
NotifyCompleted(net::ERR_INVALID_ARGUMENT);
// |this| may have been deleted.
return;
}
// Store any cookies passed from the browser process to later attach them to
// the request.
if (allow_cookies_from_browser_) {
cookies_from_browser_ = url_loader_util::GetCookiesFromHeaders(
modified_headers, modified_cors_exempt_headers);
}
// Reset the state of the PNA checker - redirects should be treated like new
// requests by the same client.
private_network_access_interceptor_.ResetForRedirect(
new_url ? *new_url : *deferred_redirect_url_);
// Propagate removal or restoration of shared storage eligiblity to the helper
// if the "Sec-Shared-Storage-Writable" request header has been removed or
// restored.
DCHECK(shared_storage_request_helper_);
shared_storage_request_helper_->UpdateSharedStorageWritableEligible(
removed_headers, modified_headers);
deferred_redirect_url_.reset();
new_redirect_url_ = new_url;
net::HttpRequestHeaders merged_modified_headers = modified_headers;
merged_modified_headers.MergeFrom(modified_cors_exempt_headers);
url_request_->FollowDeferredRedirect(removed_headers,
merged_modified_headers);
new_redirect_url_.reset();
}
void URLLoader::SetPriority(net::RequestPriority priority,
int32_t intra_priority_value) {
if (url_request_ && resource_scheduler_client_) {
resource_scheduler_client_->ReprioritizeRequest(
url_request_.get(), priority, intra_priority_value);
}
}
int URLLoader::OnConnected(net::URLRequest* url_request,
const net::TransportInfo& info,
net::CompletionOnceCallback callback) {
DCHECK_EQ(url_request, url_request_.get());
// Delegate the PNA check to the interceptor.
net::Error net_error = private_network_access_interceptor_.OnConnected(
url_request_->url(), info,
// Callback getter for async continuation:
base::BindOnce(
[](URLLoader* self, const net::TransportInfo* info_ptr,
net::CompletionOnceCallback* callback)
-> base::OnceCallback<void(net::Error)> {
return base::BindOnce(
&URLLoader::
ProcessLocalNetworkAccessPermissionResultOnConnected,
// Safe to use Unretained, as this callback will be owned by the
// interceptor which is owned by the URLLoader.
base::Unretained(self), *info_ptr, std::move(*callback));
},
// Safe to use Unretained, as this callback must be called
// synchronously.
base::Unretained(this), base::Unretained(&info),
base::Unretained(&callback)),
// Callback to set CORS error status:
base::BindOnce(
[](URLLoader* self, CorsErrorStatus cors_error_status) {
self->cors_error_status_ = std::move(cors_error_status);
},
// Safe to use Unretained, as this callback must be called
// synchronously.
base::Unretained(this)),
url_request_->net_log(), devtools_observer_.get(), devtools_request_id(),
url_loader_network_observer_.get());
// If PNA failed synchronously or requires async LNA (ERR_IO_PENDING), return
// now.
if (net_error != net::OK) {
return net_error;
}
// PNA passed synchronously. Proceed to Accept-CH handling.
return ProcessAcceptCHFrameOnConnected(info, std::move(callback));
}
void URLLoader::ProcessLocalNetworkAccessPermissionResultOnConnected(
const net::TransportInfo& info,
net::CompletionOnceCallback callback,
net::Error pna_result) {
// If LNA permission was denied, complete the request with the error.
if (pna_result != net::OK) {
std::move(callback).Run(pna_result);
return;
}
// LNA granted. Proceed to the next step: Accept-CH handling.
std::pair<net::CompletionOnceCallback, net::CompletionOnceCallback> split =
base::SplitOnceCallback(std::move(callback));
int result = ProcessAcceptCHFrameOnConnected(info, std::move(split.first));
if (result != net::ERR_IO_PENDING) {
std::move(split.second).Run(result);
}
}
int URLLoader::ProcessAcceptCHFrameOnConnected(
const net::TransportInfo& info,
net::CompletionOnceCallback callback) {
TRACE_EVENT("loading", "URLLoader::ProcessAcceptCHFrameOnConnected",
net::NetLogWithSourceToFlow(url_request_->net_log()), "url",
url_request_->url());
if (info.negotiated_protocol == net::NextProto::kProtoHTTP2) {
base::UmaHistogramBoolean("Net.URLLoader.AcceptCHFrameReceivedOnHttp2",
!info.accept_ch_frame.empty());
} else if (info.negotiated_protocol == net::NextProto::kProtoQUIC) {
base::UmaHistogramBoolean("Net.URLLoader.AcceptCHFrameReceivedOnHttp3",
!info.accept_ch_frame.empty());
}
if (!accept_ch_frame_interceptor_) {
return net::OK;
}
return accept_ch_frame_interceptor_->OnConnected(
url_request_->url(), info.accept_ch_frame,
url_request_->extra_request_headers(), std::move(callback));
}
mojom::URLResponseHeadPtr URLLoader::BuildResponseHead() const {
CHECK(request_cookies_.empty() || include_request_cookies_with_response_);
return url_loader_util::BuildResponseHead(
*url_request_, request_cookies_,
private_network_access_interceptor_.ClientAddressSpace(),
private_network_access_interceptor_.ResponseAddressSpace().value_or(
mojom::IPAddressSpace::kUnknown),
options_, ShouldSetLoadWithStorageAccess(), is_load_timing_enabled_,
include_load_timing_internal_info_with_response_,
/*response_start=*/base::TimeTicks::Now(), devtools_observer_.get(),
devtools_request_id().value_or(""));
}
void URLLoader::OnReceivedRedirect(net::URLRequest* url_request,
const net::RedirectInfo& redirect_info,
bool* defer_redirect) {
DCHECK(url_request == url_request_.get());
DCHECK(!deferred_redirect_url_);
deferred_redirect_url_ = std::make_unique<GURL>(redirect_info.new_url);
TRACE_EVENT("loading", "URLLoader::OnReceivedRedirect",
net::NetLogWithSourceToFlow(url_request_->net_log()), "new_url",
deferred_redirect_url_);
// Send the redirect response to the client, allowing them to inspect it and
// optionally follow the redirect.
*defer_redirect = true;
mojom::URLResponseHeadPtr response = BuildResponseHead();
DispatchOnRawResponse();
ReportFlaggedResponseCookies(false);
// Enforce the Cross-Origin-Resource-Policy (CORP) header.
const CrossOriginEmbedderPolicy kEmptyCoep;
const CrossOriginEmbedderPolicy& cross_origin_embedder_policy =
factory_params_->client_security_state
? factory_params_->client_security_state->cross_origin_embedder_policy
: kEmptyCoep;
const DocumentIsolationPolicy kEmptyDip;
const DocumentIsolationPolicy& document_isolation_policy =
factory_params_->client_security_state
? factory_params_->client_security_state->document_isolation_policy
: kEmptyDip;
if (std::optional<mojom::BlockedByResponseReason> blocked_reason =
CrossOriginResourcePolicy::IsBlocked(
url_request_->url(), url_request_->original_url(),
url_request_->initiator(), *response, request_mode_,
request_destination_, cross_origin_embedder_policy,
coep_reporter_, document_isolation_policy, dip_reporter_)) {
CompleteBlockedResponse(net::ERR_BLOCKED_BY_RESPONSE, false,
blocked_reason);
// TODO(crbug.com/40054032): Close the socket here.
// For more details see https://crbug.com/1154250#c17.
// Item 2 discusses redirect handling.
//
// "url_request_->AbortAndCloseConnection()" should ideally close the
// socket, but unfortunately, URLRequestHttpJob caches redirects in a way
// that ignores their response bodies, since they'll never be read. It does
// this by calling HttpCache::Transaction::StopCaching(), which also has the
// effect of detaching the HttpNetworkTransaction, which owns the socket,
// from the HttpCache::Transaction. To fix this, we'd either need to call
// StopCaching() later in the process, or make the HttpCache::Transaction
// continue to hang onto the HttpNetworkTransaction after this call.
DeleteSelf();
return;
}
url_loader_util::SetRequestCredentials(
redirect_info.new_url, factory_params_->client_security_state,
request_mode_, request_credentials_mode_, url_request_->initiator(),
*url_request_);
// Clear the Cookie header to ensure that cookies passed in through the
// `ResourceRequest` do not persist across redirects.
url_request_.get()->RemoveRequestHeaderByName(
net::HttpRequestHeaders::kCookie);
cookies_from_browser_.clear();
request_cookies_.clear();
const url::Origin origin = url::Origin::Create(url_request_->url());
const url::Origin pending_origin = url::Origin::Create(redirect_info.new_url);
if (!origin.IsSameOriginWith(pending_origin)) {
url_request_->cookie_setting_overrides().Remove(
net::CookieSettingOverride::kStorageAccessGrantEligibleViaHeader);
url_request_->cookie_setting_overrides().Remove(
net::CookieSettingOverride::kStorageAccessGrantEligible);
}
DCHECK_EQ(emitted_devtools_raw_request_, emitted_devtools_raw_response_);
response->emitted_extra_info = emitted_devtools_raw_request_;
ad_auction_event_record_request_helper_.HandleResponse(
*url_request_, GetPermissionsPolicy());
ProcessInboundSharedStorageInterceptorOnReceivedRedirect(redirect_info,
std::move(response));
}
void URLLoader::ProcessInboundSharedStorageInterceptorOnReceivedRedirect(
const net::RedirectInfo& redirect_info,
mojom::URLResponseHeadPtr response) {
DCHECK(shared_storage_request_helper_);
auto split = base::SplitOnceCallback(base::BindOnce(
&URLLoader::ContinueOnReceiveRedirect, weak_ptr_factory_.GetWeakPtr(),
redirect_info, std::move(response)));
if (!shared_storage_request_helper_->ProcessIncomingResponse(
*url_request_, std::move(split.first))) {
std::move(split.second).Run();
}
}
void URLLoader::ContinueOnReceiveRedirect(
const net::RedirectInfo& redirect_info,
mojom::URLResponseHeadPtr response) {
DCHECK(response);
url_loader_client_.Get()->OnReceiveRedirect(redirect_info,
std::move(response));
}
void URLLoader::OnAuthRequired(net::URLRequest* url_request,
const net::AuthChallengeInfo& auth_info) {
if (has_fetch_streaming_upload_body_) {
NotifyCompleted(net::ERR_FAILED);
// |this| may have been deleted.
return;
}
if (!url_loader_network_observer_) {
OnAuthCredentials(std::nullopt);
return;
}
if (do_not_prompt_for_login_) {
OnAuthCredentials(std::nullopt);
return;
}
DCHECK(!auth_challenge_responder_receiver_.is_bound());
url_loader_network_observer_->OnAuthRequired(
fetch_window_id_, request_id_, url_request_->url(), first_auth_attempt_,
auth_info, url_request->response_headers(),
auth_challenge_responder_receiver_.BindNewPipeAndPassRemote());
auth_challenge_responder_receiver_.set_disconnect_handler(
base::BindOnce(&URLLoader::DeleteSelf, base::Unretained(this)));
first_auth_attempt_ = false;
}
void URLLoader::OnCertificateRequested(net::URLRequest* unused,
net::SSLCertRequestInfo* cert_info) {
DCHECK(!client_cert_responder_receiver_.is_bound());
if (!url_loader_network_observer_) {
CancelRequest();
return;
}
// Set up mojo endpoints for ClientCertificateResponder and bind to the
// Receiver. This enables us to receive messages regarding the client
// certificate selection.
url_loader_network_observer_->OnCertificateRequested(
fetch_window_id_, cert_info,
client_cert_responder_receiver_.BindNewPipeAndPassRemote());
client_cert_responder_receiver_.set_disconnect_handler(
base::BindOnce(&URLLoader::CancelRequest, base::Unretained(this)));
}
void URLLoader::OnSSLCertificateError(net::URLRequest* request,
int net_error,
const net::SSLInfo& ssl_info,
bool fatal) {
if (!url_loader_network_observer_) {
OnSSLCertificateErrorResponse(ssl_info, net_error);
return;
}
url_loader_network_observer_->OnSSLCertificateError(
url_request_->url(), net_error, ssl_info, fatal,
base::BindOnce(&URLLoader::OnSSLCertificateErrorResponse,
weak_ptr_factory_.GetWeakPtr(), ssl_info));
}
void URLLoader::ProcessInboundSharedStorageInterceptorOnResponseStarted() {
DCHECK(shared_storage_request_helper_);
if (!shared_storage_request_helper_->ProcessIncomingResponse(
*url_request_, base::BindOnce(&URLLoader::ContinueOnResponseStarted,
weak_ptr_factory_.GetWeakPtr()))) {
ContinueOnResponseStarted();
}
}
void URLLoader::OnResponseStarted(net::URLRequest* url_request, int net_error) {
DCHECK(url_request == url_request_.get());
has_received_response_ = true;
if (keepalive_) {
base::UmaHistogramEnumeration(
"FetchKeepAlive.Requests2.Network",
internal::FetchKeepAliveRequestNetworkMetricType::kOnResponse);
}
// Wait to report for main frame navigations. This is because handling the
// cookie notification contends with ReadyToCommitNavigation, which is in the
// critical path for loading. Additionally, cookie observers for navigations
// now outlive the NavigationRequest.
bool delay_cookie_call =
url_request_->isolation_info().IsMainFrameRequest() &&
base::FeatureList::IsEnabled(kDelayedCookieNotification);
ReportFlaggedResponseCookies(!delay_cookie_call);
if (net_error != net::OK) {
NotifyCompleted(net_error);
// |this| may have been deleted.
return;
}
response_ = BuildResponseHead();
DispatchOnRawResponse();
ad_auction_event_record_request_helper_.HandleResponse(
*url_request_, GetPermissionsPolicy());
// Parse and remove the Trust Tokens response headers, if any are expected,
// potentially failing the request if an error occurs.
if (response_ && response_->headers && trust_token_interceptor_) {
DCHECK(response_);
trust_token_interceptor_->FinalizeOperation(
*response_->headers.get(),
base::BindOnce(&URLLoader::OnDoneFinalizingTrustTokenOperation,
weak_ptr_factory_.GetWeakPtr()));
// |this| may have been deleted.
return;
}
ProcessInboundSharedStorageInterceptorOnResponseStarted();
}
void URLLoader::OnDoneFinalizingTrustTokenOperation(net::Error error) {
if (error != net::OK) {
NotifyCompleted(error);
// |this| may have been deleted.
return;
}
ProcessInboundSharedStorageInterceptorOnResponseStarted();
}
void URLLoader::ContinueOnResponseStarted() {
// Do not account header bytes when reporting received body bytes to client.
reported_total_encoded_bytes_ = url_request_->GetTotalReceivedBytes();
if (upload_progress_tracker_) {
upload_progress_tracker_->OnUploadCompleted();
upload_progress_tracker_ = nullptr;
}
if (!(options_ & mojom::kURLLoadOptionReadAndDiscardBody)) {
MojoCreateDataPipeOptions options;
options.struct_size = sizeof(MojoCreateDataPipeOptions);
options.flags = MOJO_CREATE_DATA_PIPE_FLAG_NONE;
options.element_num_bytes = 1;
options.capacity_num_bytes = GetDataPipeDefaultAllocationSize(
DataPipeAllocationSize::kLargerSizeIfPossible);
MojoResult result =
mojo::CreateDataPipe(&options, response_body_stream_, consumer_handle_);
if (result != MOJO_RESULT_OK) {
NotifyCompleted(net::ERR_INSUFFICIENT_RESOURCES);
return;
}
CHECK(response_body_stream_.is_valid());
CHECK(consumer_handle_.is_valid());
peer_closed_handle_watcher_.Watch(
response_body_stream_.get(), MOJO_HANDLE_SIGNAL_PEER_CLOSED,
base::BindRepeating(&URLLoader::OnResponseBodyStreamConsumerClosed,
base::Unretained(this)));
peer_closed_handle_watcher_.ArmOrNotify();
writable_handle_watcher_.Watch(
response_body_stream_.get(), MOJO_HANDLE_SIGNAL_WRITABLE,
base::BindRepeating(&URLLoader::OnResponseBodyStreamReady,
base::Unretained(this)));
}
// Enforce the Cross-Origin-Resource-Policy (CORP) header.
const CrossOriginEmbedderPolicy kEmptyCoep;
const CrossOriginEmbedderPolicy& cross_origin_embedder_policy =
factory_params_->client_security_state
? factory_params_->client_security_state->cross_origin_embedder_policy
: kEmptyCoep;
const DocumentIsolationPolicy kEmptyDip;
const DocumentIsolationPolicy& document_isolation_policy =
factory_params_->client_security_state
? factory_params_->client_security_state->document_isolation_policy
: kEmptyDip;
if (std::optional<mojom::BlockedByResponseReason> blocked_reason =
CrossOriginResourcePolicy::IsBlocked(
url_request_->url(), url_request_->original_url(),
url_request_->initiator(), *response_, request_mode_,
request_destination_, cross_origin_embedder_policy,
coep_reporter_, document_isolation_policy, dip_reporter_)) {
CompleteBlockedResponse(net::ERR_BLOCKED_BY_RESPONSE, false,
blocked_reason);
// Close the socket associated with the request, to prevent leaking
// information.
url_request_->AbortAndCloseConnection();
DeleteSelf();
return;
}
// Enforce SRI-compliant HTTP Message Signature headers.
//
// https://wicg.github.io/signature-based-sri/
if (std::optional<mojom::BlockedByResponseReason> blocked_reason =
MaybeBlockResponseForSRIMessageSignature(
*url_request_, *response_, expected_public_keys_,
devtools_observer_.get(), devtools_request_id().value_or(""))) {
CompleteBlockedResponse(net::ERR_BLOCKED_BY_RESPONSE, false,
blocked_reason);
// Close the socket associated with the request, to prevent leaking
// information.
url_request_->AbortAndCloseConnection();
DeleteSelf();
return;
}
// Enforce ad-auction-only signals -- the renderer process isn't allowed
// to read auction-only signals for ad auctions; only the browser process
// is allowed to read those, and only the browser process can issue trusted
// requests.
// TODO(crbug.com/40269364): Remove old names once API users have migrated to
// new names.
if (!factory_params_->is_trusted && response_->headers) {
std::optional<std::string> auction_only =
response_->headers->GetNormalizedHeader("Ad-Auction-Only");
if (!auction_only) {
auction_only =
response_->headers->GetNormalizedHeader("X-FLEDGE-Auction-Only");
}
if (auction_only &&
base::EqualsCaseInsensitiveASCII(*auction_only, "true")) {
CompleteBlockedResponse(net::ERR_BLOCKED_BY_RESPONSE, false);
url_request_->AbortAndCloseConnection();
DeleteSelf();
return;
}
}
// Figure out if we need to sniff (for MIME type detection or for Opaque
// Response Blocking / ORB).
if (factory_params_->is_orb_enabled) {
// TODO(ricea): Make ORB and ReadAndDiscardBody work together if necessary.
CHECK(!(options_ & mojom::kURLLoadOptionReadAndDiscardBody))
<< "ORB is incompatible with the ReadAndDiscardBody option";
orb_analyzer_ = orb::ResponseAnalyzer::Create(&*per_factory_orb_state_);
is_more_orb_sniffing_needed_ = true;
auto decision =
orb_analyzer_->Init(url_request_->url(), url_request_->initiator(),
request_mode_, request_destination_, *response_);
if (MaybeBlockResponseForOrb(decision)) {
return;
}
}
if ((options_ & mojom::kURLLoadOptionSniffMimeType)) {
if (ShouldSniffContent(url_request_->url(), *response_)) {
// We're going to look at the data before deciding what the content type
// is. That means we need to delay sending the response started IPC.
VLOG(1) << "Will sniff content for mime type: " << url_request_->url();
is_more_mime_sniffing_needed_ = true;
mime_type_before_sniffing_ = response_->mime_type;
} else if (response_->mime_type.empty()) {
// Ugg. The server told us not to sniff the content but didn't give us
// a mime type. What's a browser to do? Turns out, we're supposed to
// treat the response as "text/plain". This is the most secure option.
response_->mime_type.assign("text/plain");
}
}
// If client-side content decoding is requested, store the types of decoding
// to be used with the Durable Message so it can decode on retrieval.
if (durable_message_writer_) {
durable_message_writer_->SetClientDecodingTypes(
response_->client_side_content_decoding_types);
}
// If client-side content decoding is requested and either ORB or MIME
// sniffing is needed, use PartialDecoder to get decoded data for sniffing.
if (!response_->client_side_content_decoding_types.empty() &&
(is_more_orb_sniffing_needed_ || is_more_mime_sniffing_needed_)) {
// Create a PartialDecoder to decode the response body up to a certain limit
// for sniffing purposes.
partial_decoder_ = std::make_unique<PartialDecoder>(
base::BindRepeating(
[](base::WeakPtr<net::URLRequest> url_request, net::IOBuffer* dest,
int dest_size) {
CHECK(url_request);
return url_request->Read(dest, dest_size);
},
url_request_->GetWeakPtr()),
response_->client_side_content_decoding_types,
partial_decoder_decoding_buffer_size_);
// Start reading decoded data from the partial decoder.
ReadDecodedDataFromPartialDecoder();
return;
}
StartReading();
}
void URLLoader::ReadDecodedDataFromPartialDecoder() {
// Keep reading from the partial decoder until it signals pending or
// completes.
while (partial_decoder_) {
// Attempt to read more decoded data.
int result = partial_decoder_->ReadDecodedDataMore(
base::BindOnce(&URLLoader::OnReadDecodedDataFromPartialDecoder,
base::Unretained(this)));
if (result == net::ERR_IO_PENDING) {
// If the read is pending, return and wait for the callback.
return;
}
// Process the result immediately.
CheckPartialDecoderResult(result);
if (partial_decoder_result_) {
// Sniffing is complete, and we have the raw data. Stop using the
// partial decoder and proceed to read the rest of the response.
StartReading();
return;
}
}
}
void URLLoader::OnReadDecodedDataFromPartialDecoder(int result) {
// This is the callback from `PartialDecoder::ReadDecodedDataMore`.
// Process the result of the partial decode.
CheckPartialDecoderResult(result);
if (partial_decoder_result_) {
// Sniffing is complete. Proceed to read the full response.
StartReading();
} else if (partial_decoder_) {
// More sniffing might be needed, continue reading from the partial decoder.
ReadDecodedDataFromPartialDecoder();
}
}
void URLLoader::CheckPartialDecoderResult(int result) {
if (result < 0) {
// The partial decoder failed. Stop decoding and report the error.
partial_decoder_.reset();
// Defer calling NotifyCompleted to make sure the caller can still access
// |this|.
TaskRunner(url_request_->priority())
->PostTask(FROM_HERE,
base::BindOnce(&URLLoader::NotifyCompleted,
weak_ptr_factory_.GetWeakPtr(), result));
return;
}
// Check if we should stop sniffing after processing the current chunk of
// decoded data. This happens if the decoder returns 0 (end of stream) or
// if the decoded buffer is full.
const bool stop_sniffing_after_processing_current_data =
(result == 0 || !partial_decoder_->HasRemainingBuffer());
// Get a view of the decoded data, limited to the maximum sniffing size.
// Capping the size to net::kMaxBytesToSniff for tests which have called
// set_partial_decoder_decoding_buffer_size_for_testing().
const std::string_view decoded_data_to_sniff =
base::as_string_view(partial_decoder_->decoded_data())
.substr(0, net::kMaxBytesToSniff);
if (is_more_mime_sniffing_needed_) {
// Perform MIME sniffing using the decoded data.
CHECK(mime_type_before_sniffing_.has_value());
std::string new_type;
is_more_mime_sniffing_needed_ = !net::SniffMimeType(
decoded_data_to_sniff, url_request_->url(),
mime_type_before_sniffing_.value(),
net::ForceSniffFileUrlsForHtml::kDisabled, &new_type);
response_->mime_type = std::move(new_type);
response_->did_mime_sniff = true;
if (stop_sniffing_after_processing_current_data) {
is_more_mime_sniffing_needed_ = false;
}
}
if (is_more_orb_sniffing_needed_) {
// Perform ORB sniffing using the decoded data.
orb::ResponseAnalyzer::Decision orb_decision =
result == 0 ? orb::ResponseAnalyzer::Decision::kSniffMore
: orb_analyzer_->Sniff(decoded_data_to_sniff);
if (orb_decision == orb::ResponseAnalyzer::Decision::kSniffMore &&
stop_sniffing_after_processing_current_data) {
// If more sniffing was requested but we've reached the limit, get the
// final decision for the sniffed data.
orb_decision = orb_analyzer_->HandleEndOfSniffableResponseBody();
DCHECK_NE(orb::ResponseAnalyzer::Decision::kSniffMore, orb_decision);
}
if (MaybeBlockResponseForOrb(orb_decision)) {
partial_decoder_.reset();
return;
}
}
// If no more sniffing is needed, retrieve the accumulated raw data from the
// partial decoder.
if (!is_more_orb_sniffing_needed_ && !is_more_mime_sniffing_needed_) {
partial_decoder_result_ = std::move(*partial_decoder_).TakeResult();
}
}
void URLLoader::ReadMore() {
CHECK_NE(url_read_state_, URLReadState::kURLReadInProgress);
url_read_state_ = URLReadState::kWaitMojoPipeWritable;
if (partial_decoder_result_) {
// If we have buffered raw data from the partial decoder, send that first.
while (partial_decoder_result_->HasRawData()) {
MojoResult result = NetToMojoPendingBuffer::BeginWrite(
&response_body_stream_, &pending_write_);
switch (result) {
case MOJO_RESULT_OK:
break;
case MOJO_RESULT_SHOULD_WAIT:
// The Mojo data pipe is full. Wait for it to become writable.
// While a SlopBucket would be ideal when enabled, it's omitted here
// for simplicity.
//
// This scenario occurs when the PartialDecoder reads raw data
// exceeding the Mojo data pipe's capacity. However, since the
// PartialDecoder only reads decompressed data up to
// net::kMaxBytesToSniff, this situation is extremely rare in
// real-world scenarios.
writable_handle_watcher_.ArmOrNotify();
return;
default:
// The response body stream is in a bad state. This happens when the
// consumer handle of the body was synchronously released in
// URLLoaderClient::OnReceiveResponse().
NotifyCompleted(net::ERR_FAILED);
return;
}
// Copy data from the raw buffer into the Mojo pipe buffer.
pending_write_buffer_offset_ = partial_decoder_result_->ConsumeRawData(
base::as_writable_byte_span(*pending_write_));
CHECK(pending_write_buffer_offset_);
MaybeCollectDurableMessage(0, pending_write_buffer_offset_);
CompletePendingWrite(true);
}
// Check if the partial decoder finished with a specific status.
if (partial_decoder_result_->completion_status()) {
NotifyCompleted(*partial_decoder_result_->completion_status());
return;
}
partial_decoder_result_.reset();
}
// Once the MIME type is sniffed, all data is sent as soon as it is read from
// the network.
DCHECK(consumer_handle_.is_valid() || !pending_write_);
// TODO(ricea): Refactor this method and DidRead() to reduce duplication.
if (options_ & mojom::kURLLoadOptionReadAndDiscardBody) {
url_read_state_ = URLReadState::kURLReadInProgress;
int bytes_read =
url_request_->Read(discard_buffer_.get(), discard_buffer_->size());
if (bytes_read != net::ERR_IO_PENDING) {
DidRead(bytes_read, /*completed_synchronously=*/true,
/*into_slop_bucket=*/false);
// `this` may have been deleted.
}
return;
}
if (!pending_write_.get()) {
// TODO: we should use the abstractions in MojoAsyncResourceHandler.
DCHECK_EQ(0u, pending_write_buffer_offset_);
MojoResult result = NetToMojoPendingBuffer::BeginWrite(
&response_body_stream_, &pending_write_);
mojo_begin_write_count_for_uma_++;
switch (result) {
case MOJO_RESULT_OK:
break;
case MOJO_RESULT_SHOULD_WAIT: {
CHECK(!pending_write_);
bool should_wait = true;
if (base::FeatureList::IsEnabled(kSlopBucket) && !slop_bucket_) {
slop_bucket_ = SlopBucket::RequestSlopBucket(url_request_.get());
was_slop_bucket_enabled_ = true;
}
if (slop_bucket_ && !slop_bucket_->read_in_progress() &&
!slop_bucket_->IsComplete()) {
// Read into the slop bucket while we're waiting for the mojo data
// pipe to empty out.
std::optional<int> bytes_read_maybe = slop_bucket_->AttemptRead();
if (bytes_read_maybe.has_value()) {
url_read_state_ = URLReadState::kURLReadInProgress;
should_wait = false;
int bytes_read = bytes_read_maybe.value();
if (bytes_read != net::ERR_IO_PENDING) {
// DidRead() will not delete `this` when `into_slop_bucket` is
// true, so it is safe to access member variables after this call.
DidRead(bytes_read, /*completed_synchronously=*/true,
/*into_slop_bucket=*/true);
}
}
} // The pipe is full. We need to wait for it to have more space.
if (should_wait) {
mojo_blocked_write_count_for_uma_++;
}
writable_handle_watcher_.ArmOrNotify();
return;
}
default:
// The response body stream is in a bad state. Bail.
NotifyCompleted(net::ERR_FAILED);
return;
}
pending_write_buffer_size_ = pending_write_->size();
DCHECK_GT(static_cast<uint32_t>(std::numeric_limits<int>::max()),
pending_write_buffer_size_);
if (consumer_handle_.is_valid()) {
DCHECK_GE(pending_write_buffer_size_,
static_cast<uint32_t>(net::kMaxBytesToSniff));
}
// We may be able to fill up the buffer from the slop bucket.
if (slop_bucket_) {
const size_t consumed =
slop_bucket_->Consume(base::as_writable_byte_span(*pending_write_));
if (consumed) {
// TODO(ricea): Refactor the way pending writes work so we don't need to
// poke a value into `pending_write_buffer_offset_` here.
pending_write_buffer_offset_ = consumed;
CompletePendingWrite(true);
ReadMoreAsync();
return;
} else if (slop_bucket_->read_in_progress()) {
// There were no bytes available, but a read is in progress. Need to
// prevent starting another read until it completes.
CompletePendingWrite(true);
return;
} else if (slop_bucket_->IsComplete()) {
// The SlopBucket didn't have any bytes available. It has finished
// reading from the URLRequest and now the render process has caught up,
// so we can notify completion.
CompletePendingWrite(true);
NotifyCompleted(slop_bucket_->completion_code());
// `this` is deleted.
return;
}
// Nothing was available. Read from `url_request_` as usual.
}
}
CHECK(!slop_bucket_ || !slop_bucket_->IsComplete());
auto buf = base::MakeRefCounted<NetToMojoIOBuffer>(
pending_write_, pending_write_buffer_offset_);
url_read_state_ = URLReadState::kURLReadInProgress;
int bytes_read = url_request_->Read(
buf.get(), static_cast<int>(pending_write_buffer_size_ -
pending_write_buffer_offset_));
if (bytes_read != net::ERR_IO_PENDING) {
DidRead(bytes_read, /*completed_synchronously=*/true,
/*into_slop_bucket=*/false);
// |this| may have been deleted.
}
}
void URLLoader::ReadMoreAsync() {
CHECK_EQ(url_read_state_, URLReadState::kWaitMojoPipeWritable);
url_read_state_ = URLReadState::kReadMoreTaskPosted;
TaskRunner(url_request_->priority())
->PostTask(FROM_HERE, base::BindOnce(&URLLoader::ReadMore,
weak_ptr_factory_.GetWeakPtr()));
}
// Handles the completion of a read. `num_bytes` is the number of bytes read, 0
// if we reached the end of the response body, or a net::Error otherwise.
// `completed_synchronously` is true if the call to URLRequest::Read did not
// return net::ERR_IO_PENDING. `into_slop_bucket` is true if this was actually a
// read into `slop_bucket_` and not into our own buffer.
void URLLoader::DidRead(int num_bytes,
bool completed_synchronously,
bool into_slop_bucket) {
CHECK_EQ(url_read_state_, URLReadState::kURLReadInProgress);
url_read_state_ = URLReadState::kWaitMojoPipeWritable;
size_t new_data_offset = pending_write_buffer_offset_;
MaybeCollectDurableMessage(new_data_offset, num_bytes);
if (num_bytes > 0) {
if (!into_slop_bucket) {
pending_write_buffer_offset_ += num_bytes;
}
// Only notify client of download progress if we're done sniffing and
// started sending response.
if (!consumer_handle_.is_valid()) {
int64_t total_encoded_bytes = url_request_->GetTotalReceivedBytes();
if (ShouldSendTransferSizeUpdated()) {
int64_t delta = total_encoded_bytes - reported_total_encoded_bytes_;
DCHECK_LE(0, delta);
if (delta) {
url_loader_client_.Get()->OnTransferSizeUpdated(delta);
}
}
reported_total_encoded_bytes_ = total_encoded_bytes;
}
}
bool complete_read = true;
if (consumer_handle_.is_valid()) {
// `consumer_handle_` is only valid when we are sniffing. Sniffing is only
// applied to the first 1024 bytes. The mojo data pipe is always larger than
// 1024 bytes, therefore it will never fill up while we are sniffing.
// Therefore a SlopBucket will not have been created yet and
// `into_slop_bucket` can't be true.
CHECK(!into_slop_bucket);
// |pending_write_| may be null if the job self-aborts due to a suspend;
// this will have |consumer_handle_| valid when the loader is paused.
if (pending_write_) {
// Limit sniffing to the first net::kMaxBytesToSniff.
size_t data_length = pending_write_buffer_offset_;
if (data_length > net::kMaxBytesToSniff)
data_length = net::kMaxBytesToSniff;
std::string_view data(pending_write_->buffer(), data_length);
bool stop_sniffing_after_processing_current_data =
(num_bytes <= 0 ||
pending_write_buffer_offset_ >= net::kMaxBytesToSniff);
if (is_more_mime_sniffing_needed_) {
CHECK(mime_type_before_sniffing_.has_value());
std::string new_type;
is_more_mime_sniffing_needed_ = !net::SniffMimeType(
data, url_request_->url(), mime_type_before_sniffing_.value(),
net::ForceSniffFileUrlsForHtml::kDisabled, &new_type);
// SniffMimeType() returns false if there is not enough data to
// determine the mime type. However, even if it returns false, it
// returns a new type that is probably better than the current one.
response_->mime_type.assign(new_type);
response_->did_mime_sniff = true;
if (stop_sniffing_after_processing_current_data)
is_more_mime_sniffing_needed_ = false;
}
if (is_more_orb_sniffing_needed_) {
orb::ResponseAnalyzer::Decision orb_decision =
orb::ResponseAnalyzer::Decision::kSniffMore;
// `has_new_data_to_sniff` can be false at the end-of-stream.
bool has_new_data_to_sniff = new_data_offset < data.length();
if (has_new_data_to_sniff)
orb_decision = orb_analyzer_->Sniff(data);
if (orb_decision == orb::ResponseAnalyzer::Decision::kSniffMore &&
stop_sniffing_after_processing_current_data) {
orb_decision = orb_analyzer_->HandleEndOfSniffableResponseBody();
DCHECK_NE(orb::ResponseAnalyzer::Decision::kSniffMore, orb_decision);
}
if (MaybeBlockResponseForOrb(orb_decision)) {
return;
}
}
}
if (!is_more_mime_sniffing_needed_ && !is_more_orb_sniffing_needed_) {
SendResponseToClient();
} else {
complete_read = false;
}
}
if (num_bytes <= 0) {
// There may be no |pending_write_| if a URLRequestJob cancelled itself in
// URLRequestJob::OnSuspend() after receiving headers, while there was no
// pending read.
// TODO(mmenke): That case is rather unfortunate - something should be done
// at the socket layer instead, both to make for a better API (Only complete
// reads when there's a pending read), and to cover all TCP socket uses,
// since the concern is the effect that entering suspend mode has on
// sockets. See https://crbug.com/651120.
if (pending_write_) {
CHECK(!into_slop_bucket);
CompletePendingWrite(num_bytes == 0);
}
// If we are reading into the SlopBucket then notification of completion
// will be postponed until the data has been forwarded to the mojo data
// pipe.
if (!into_slop_bucket) {
NotifyCompleted(num_bytes);
// |this| will have been deleted.
}
return;
}
if (complete_read && !into_slop_bucket) {
CompletePendingWrite(true /* success */);
}
if (completed_synchronously) {
ReadMoreAsync();
} else {
ReadMore();
}
}
void URLLoader::OnReadCompleted(net::URLRequest* url_request, int bytes_read) {
DCHECK(url_request == url_request_.get());
if (partial_decoder_ && partial_decoder_->read_in_progress()) {
// When `partial_decoder_` is reading the body from `url_request`, pass the
// result to `partial_decoder_`.
partial_decoder_->OnReadRawDataCompleted(bytes_read);
return;
}
bool into_slop_bucket = false;
if (slop_bucket_ && slop_bucket_->read_in_progress()) {
slop_bucket_->OnReadCompleted(bytes_read);
into_slop_bucket = true;
}
DidRead(bytes_read, /*completed_synchronously=*/false, into_slop_bucket);
// |this| may have been deleted.
}
int URLLoader::OnBeforeStartTransaction(
const net::HttpRequestHeaders& headers,
net::NetworkDelegate::OnBeforeStartTransactionCallback callback) {
const net::HttpRequestHeaders* used_headers = &headers;
net::HttpRequestHeaders headers_with_bonus_cookies;
if (!cookies_from_browser_.empty()) {
headers_with_bonus_cookies = AttachCookies(headers, cookies_from_browser_);
used_headers = &headers_with_bonus_cookies;
}
if (include_request_cookies_with_response_) {
request_cookies_.clear();
std::string cookie_header =
used_headers->GetHeader(net::HttpRequestHeaders::kCookie)
.value_or(std::string());
net::cookie_util::ParseRequestCookieLine(cookie_header, &request_cookies_);
}
if (header_client_) {
header_client_->OnBeforeSendHeaders(
*used_headers,
base::BindOnce(&URLLoader::OnBeforeSendHeadersComplete,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
return net::ERR_IO_PENDING;
}
// Additional cookies were added to the existing headers, so `callback` must
// be invoked to ensure that the cookies are included in the request.
if (!cookies_from_browser_.empty()) {
CHECK_EQ(used_headers, &headers_with_bonus_cookies);
TaskRunner(url_request_->priority())
->PostTask(FROM_HERE,
base::BindOnce(std::move(callback), net::OK,
std::move(headers_with_bonus_cookies)));
return net::ERR_IO_PENDING;
}
return net::OK;
}
int URLLoader::OnHeadersReceived(
net::CompletionOnceCallback callback,
const net::HttpResponseHeaders* original_response_headers,
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
const net::IPEndPoint& endpoint,
std::optional<GURL>* preserve_fragment_on_redirect_url,
const std::optional<net::SSLInfo>& ssl_info) {
if (header_client_) {
header_client_->OnHeadersReceived(
original_response_headers->raw_headers(), endpoint, ssl_info,
base::BindOnce(&URLLoader::OnHeadersReceivedComplete,
weak_ptr_factory_.GetWeakPtr(), std::move(callback),
override_response_headers,
preserve_fragment_on_redirect_url));
return net::ERR_IO_PENDING;
}
return net::OK;
}
net::LoadState URLLoader::GetLoadState() const {
return url_request_->GetLoadState().state;
}
net::UploadProgress URLLoader::GetUploadProgress() const {
return url_request_->GetUploadProgress();
}
int32_t URLLoader::GetProcessId() const {
return factory_params_->process_id;
}
uint32_t URLLoader::GetResourceType() const {
return resource_type_;
}
bool URLLoader::CookiesDisabled() const {
return options_ & mojom::kURLLoadOptionBlockAllCookies;
}
bool URLLoader::AllowCookie(const net::CanonicalCookie& cookie,
const GURL& url,
const net::SiteForCookies& site_for_cookies) const {
if (cookie.IsPartitioned() && !CookiesDisabled()) {
return true;
}
return AllowFullCookies(url, site_for_cookies);
}
bool URLLoader::AllowFullCookies(
const GURL& url,
const net::SiteForCookies& site_for_cookies) const {
net::StaticCookiePolicy::Type policy =
net::StaticCookiePolicy::ALLOW_ALL_COOKIES;
if (CookiesDisabled()) {
policy = net::StaticCookiePolicy::BLOCK_ALL_COOKIES;
} else if (options_ & mojom::kURLLoadOptionBlockThirdPartyCookies) {
policy = net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES;
} else {
return true;
}
return net::StaticCookiePolicy(policy).CanAccessCookies(
url, site_for_cookies) == net::OK;
}
// static
URLLoader* URLLoader::ForRequest(const net::URLRequest& request) {
auto* pointer =
static_cast<UnownedPointer*>(request.GetUserData(kUserDataKey));
if (!pointer)
return nullptr;
return pointer->get();
}
void URLLoader::OnAuthCredentials(
const std::optional<net::AuthCredentials>& credentials) {
auth_challenge_responder_receiver_.reset();
if (!credentials.has_value()) {
url_request_->CancelAuth();
} else {
// CancelAuth will proceed to the body, so cookies only need to be reported
// here.
ReportFlaggedResponseCookies(false);
url_request_->SetAuth(credentials.value());
}
}
void URLLoader::ContinueWithCertificate(
const scoped_refptr<net::X509Certificate>& x509_certificate,
const std::string& provider_name,
const std::vector<uint16_t>& algorithm_preferences,
mojo::PendingRemote<mojom::SSLPrivateKey> ssl_private_key) {
client_cert_responder_receiver_.reset();
auto key = base::MakeRefCounted<SSLPrivateKeyProxy>(
provider_name, algorithm_preferences, std::move(ssl_private_key));
url_request_->ContinueWithCertificate(std::move(x509_certificate),
std::move(key));
}
void URLLoader::ContinueWithoutCertificate() {
client_cert_responder_receiver_.reset();
url_request_->ContinueWithCertificate(nullptr, nullptr);
}
void URLLoader::CancelRequest() {
client_cert_responder_receiver_.reset();
url_request_->CancelWithError(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
}
void URLLoader::CancelRequestIfNonceMatchesAndUrlNotExempted(
const base::UnguessableToken& nonce,
const std::set<GURL>& exemptions) {
if (url_request_->isolation_info().nonce() == nonce) {
if (!exemptions.contains(
url_request_->original_url().GetWithoutFilename())) {
url_request_->CancelWithError(net::ERR_NETWORK_ACCESS_REVOKED);
}
}
}
void URLLoader::NotifyCompleted(int error_code) {
// Ensure sending the final upload progress message here, since
// OnResponseCompleted can be called without OnResponseStarted on cancellation
// or error cases.
if (upload_progress_tracker_) {
upload_progress_tracker_->OnUploadCompleted();
upload_progress_tracker_ = nullptr;
}
auto total_received = url_request_->GetTotalReceivedBytes();
auto total_sent = url_request_->GetTotalSentBytes();
if (total_received > 0) {
base::UmaHistogramCustomCounts("DataUse.BytesReceived3.Delegate",
total_received, 50, 10 * 1000 * 1000, 50);
mojo_begin_write_count_for_uma_ =
std::max(mojo_begin_write_count_for_uma_, 1);
const int proportion = mojo_blocked_write_count_for_uma_ * 100 /
mojo_begin_write_count_for_uma_;
base::UmaHistogramPercentage(
"Net.URLLoader.ProportionOfWritesBlockedByMojo", proportion);
if (was_slop_bucket_enabled_) {
base::UmaHistogramPercentage(
"Net.URLLoader.SlopBucket.ProportionOfWritesBlockedByMojo.All",
proportion);
using CacheEntryStatus = net::HttpResponseInfo::CacheEntryStatus;
const CacheEntryStatus& cache_entry_status =
url_request_->response_info().cache_entry_status;
if (cache_entry_status != CacheEntryStatus::ENTRY_USED &&
cache_entry_status != CacheEntryStatus::ENTRY_VALIDATED) {
base::UmaHistogramPercentage(
"Net.URLLoader.SlopBucket.ProportionOfWritesBlockedByMojo.NoCache",
proportion);
}
}
}
if (total_sent > 0) {
UMA_HISTOGRAM_COUNTS_1M("DataUse.BytesSent3.Delegate", total_sent);
}
url_loader_util::MaybeRecordSharedDictionaryUsedResponseMetrics(
error_code, request_destination_, url_request_->response_info(),
shared_dictionary_allowed_check_passed_);
if ((total_received > 0 || total_sent > 0)) {
if (url_loader_network_observer_ && provide_data_use_updates_) {
url_loader_network_observer_->OnDataUseUpdate(
url_request_->traffic_annotation().unique_id_hash_code,
base::ByteSize(base::checked_cast<uint64_t>(total_received)),
base::ByteSize(base::checked_cast<uint64_t>(total_sent)));
}
}
if (url_loader_client_.Get()) {
if (consumer_handle_.is_valid())
SendResponseToClient();
URLLoaderCompletionStatus status;
status.error_code = error_code;
if (error_code == net::ERR_QUIC_PROTOCOL_ERROR) {
net::NetErrorDetails details;
url_request_->PopulateNetErrorDetails(&details);
status.extended_error_code = details.quic_connection_error;
} else if (error_code == net::ERR_INCONSISTENT_IP_ADDRESS_SPACE) {
// The error code is only used internally, translate it into a CORS error.
DCHECK(cors_error_status_.has_value());
status.error_code = net::ERR_FAILED;
}
status.exists_in_cache = url_request_->response_info().was_cached;
status.completion_time = base::TimeTicks::Now();
status.encoded_data_length = url_request_->GetTotalReceivedBytes();
status.encoded_body_length = url_request_->GetRawBodyBytes();
status.decoded_body_length = total_written_bytes_;
status.resolve_error_info =
url_request_->response_info().resolve_error_info;
if (trust_token_interceptor_ && trust_token_interceptor_->status()) {
status.trust_token_operation_status = *trust_token_interceptor_->status();
}
status.cors_error_status = cors_error_status_;
if ((options_ & mojom::kURLLoadOptionSendSSLInfoForCertificateError) &&
net::IsCertStatusError(url_request_->ssl_info().cert_status)) {
status.ssl_info = url_request_->ssl_info();
}
url_loader_client_.Get()->OnComplete(status);
}
DeleteSelf();
}
void URLLoader::OnMojoDisconnect() {
NotifyCompleted(net::ERR_FAILED);
}
void URLLoader::OnResponseBodyStreamConsumerClosed(MojoResult result) {
NotifyCompleted(net::ERR_FAILED);
}
void URLLoader::OnResponseBodyStreamReady(MojoResult result) {
if (result != MOJO_RESULT_OK) {
NotifyCompleted(net::ERR_FAILED);
return;
}
if (url_read_state_ == URLReadState::kWaitMojoPipeWritable) {
ReadMore();
}
}
void URLLoader::DeleteSelf() {
std::move(delete_callback_).Run(this);
}
void URLLoader::SendResponseToClient() {
TRACE_EVENT("loading", "network::URLLoader::SendResponseToClient",
net::NetLogWithSourceToFlow(url_request_->net_log()), "url",
url_request_->url());
DCHECK_EQ(emitted_devtools_raw_request_, emitted_devtools_raw_response_);
response_->emitted_extra_info = emitted_devtools_raw_request_;
url_loader_client_.Get()->OnReceiveResponse(
response_->Clone(), std::move(consumer_handle_), std::nullopt);
}
void URLLoader::CompletePendingWrite(bool success) {
if (success && pending_write_) {
// The write can only be completed immediately in case of a success, since
// doing so invalidates memory of any attached NetToMojoIOBuffer's; but in
// case of an abort, particularly one caused by a suspend, the failure may
// be delivered to URLLoader while the disk_cache layer is still hanging on
// to the now-invalid IOBuffer in some worker thread trying to commit it to
// disk. In case of an error, this will have to wait till everything is
// destroyed.
response_body_stream_ =
pending_write_->Complete(pending_write_buffer_offset_);
}
total_written_bytes_ += pending_write_buffer_offset_;
pending_write_ = nullptr;
pending_write_buffer_offset_ = 0;
}
void URLLoader::SetRawResponseHeaders(
scoped_refptr<const net::HttpResponseHeaders> headers) {
raw_response_headers_ = headers;
}
void URLLoader::NotifyEarlyResponse(
scoped_refptr<const net::HttpResponseHeaders> headers) {
DCHECK(!has_received_response_);
DCHECK(url_loader_client_.Get());
DCHECK(headers);
DCHECK_EQ(headers->response_code(), 103);
// Calculate IP address space.
mojom::ParsedHeadersPtr parsed_headers =
PopulateParsedHeaders(headers.get(), url_request_->url());
net::IPEndPoint transaction_endpoint;
bool has_endpoint =
url_request_->GetTransactionRemoteEndpoint(&transaction_endpoint);
DCHECK(has_endpoint);
CalculateClientAddressSpaceParams params{
.client_address_space_inherited_from_service_worker = std::nullopt,
.parsed_headers = &parsed_headers,
.remote_endpoint = &transaction_endpoint,
};
mojom::IPAddressSpace ip_address_space =
CalculateClientAddressSpace(url_request_->url(), params);
mojom::ReferrerPolicy referrer_policy = ParseReferrerPolicy(*headers);
url_loader_client_.Get()->OnReceiveEarlyHints(mojom::EarlyHints::New(
std::move(parsed_headers), referrer_policy, ip_address_space));
MaybeNotifyEarlyResponseToDevtools(*headers);
}
void URLLoader::MaybeNotifyEarlyResponseToDevtools(
const net::HttpResponseHeaders& headers) {
if (!devtools_observer_ || !devtools_request_id()) {
return;
}
devtools_observer_->OnEarlyHintsResponse(
devtools_request_id().value(), ResponseHeaderToRawHeaderPairs(headers));
}
void URLLoader::SetRawRequestHeadersAndNotify(
net::HttpRawRequestHeaders headers) {
// If we have emitted_devtools_raw_request_, don't notify DevTools
// to prevent duplicate ExtraInfo events.
if (!emitted_devtools_raw_request_ && devtools_observer_ &&
devtools_request_id()) {
std::vector<network::mojom::HttpRawHeaderPairPtr> header_array;
header_array.reserve(headers.headers().size());
for (const auto& header : headers.headers()) {
network::mojom::HttpRawHeaderPairPtr pair =
network::mojom::HttpRawHeaderPair::New();
pair->key = header.first;
pair->value = header.second;
header_array.push_back(std::move(pair));
}
DispatchOnRawRequest(std::move(header_array));
}
raw_request_line_size_ = headers.request_line().size();
// Each header's format is "{key}: {value}\r\n" so add 4 bytes for each
// header.
raw_request_headers_size_ = headers.headers().size() * 4;
for (const auto& [key, value] : headers.headers()) {
raw_request_headers_size_ += key.size() + value.size();
}
if (cookie_observer_) {
std::vector<mojom::CookieOrLineWithAccessResultPtr> reported_cookies;
for (const auto& cookie_with_access_result :
url_request_->maybe_sent_cookies()) {
if (ShouldNotifyAboutCookie(
cookie_with_access_result.access_result.status)) {
reported_cookies.push_back(mojom::CookieOrLineWithAccessResult::New(
mojom::CookieOrLine::NewCookie(cookie_with_access_result.cookie),
cookie_with_access_result.access_result));
}
}
if (!reported_cookies.empty()) {
cookie_access_details_.emplace_back(mojom::CookieAccessDetails::New(
mojom::CookieAccessDetails::Type::kRead, url_request_->url(),
url_request_->isolation_info().frame_origin(),
url_request_->isolation_info().top_frame_origin().value_or(
url::Origin()),
url_request_->site_for_cookies(), std::move(reported_cookies),
devtools_request_id(), url_request_->ad_tagged(),
url_request_->cookie_setting_overrides()));
}
}
}
bool URLLoader::IsSharedDictionaryReadAllowed() {
shared_dictionary_allowed_check_passed_ =
shared_dictionary_checker_->CheckAllowedToReadAndReport(
url_request_->url(), url_request_->site_for_cookies(),
url_request_->isolation_info(), url_request_->cookie_partition_key());
return shared_dictionary_allowed_check_passed_;
}
void URLLoader::DispatchOnRawRequest(
std::vector<network::mojom::HttpRawHeaderPairPtr> headers) {
DCHECK(devtools_observer_ && devtools_request_id());
net::LoadTimingInfo load_timing_info;
url_request_->GetLoadTimingInfo(&load_timing_info);
emitted_devtools_raw_request_ = true;
bool is_main_frame_navigation =
url_request_->isolation_info().IsMainFrameRequest() ||
url_request_->force_main_frame_for_same_site_cookies();
net::SchemefulSite request_site(url_request_->url());
// TODO when crbug.com/40093296 "Don't trust |site_for_cookies| provided by
// the renderer" is fixed. Update the FromNetworkIsolationKey method to use
// url_request_->isolation_info().site_for_cookies() instead of
// url_request_->site_for_cookies().
std::optional<net::CookiePartitionKey> partition_key =
net::CookiePartitionKey::FromNetworkIsolationKey(
url_request_->isolation_info().network_isolation_key(),
url_request_->site_for_cookies(), request_site,
is_main_frame_navigation);
std::optional<bool> site_has_cookie_in_other_partition;
if (partition_key.has_value()) {
site_has_cookie_in_other_partition =
url_request_->context()->cookie_store()->SiteHasCookieInOtherPartition(
request_site, partition_key.value());
}
network::mojom::OtherPartitionInfoPtr other_partition_info = nullptr;
if (site_has_cookie_in_other_partition.has_value()) {
other_partition_info = network::mojom::OtherPartitionInfo::New();
other_partition_info->site_has_cookie_in_other_partition =
*site_has_cookie_in_other_partition;
}
std::optional<base::UnguessableToken> applied_network_conditions_id;
if (throttling_token_) {
ThrottlingNetworkInterceptor* interceptor =
ThrottlingController::GetInterceptor(throttling_token_->source_id(),
url_request_->url());
if (interceptor) {
applied_network_conditions_id = interceptor->conditions().rule_id();
}
}
devtools_observer_->OnRawRequest(
devtools_request_id().value(), url_request_->maybe_sent_cookies(),
std::move(headers), load_timing_info.request_start,
private_network_access_interceptor_.CloneClientSecurityState(),
std::move(other_partition_info),
std::move(applied_network_conditions_id));
}
void URLLoader::DispatchOnRawResponse() {
if (!emitted_devtools_raw_request_) {
// TODO(ortuno): not sure why emitting of metrics is gated upon request not
// having been dispatched to DevTools, but this has been so since it raw
// header size metrics have been introduced by https://crrev.com/c/5824030.
if (url_request_->response_headers()) {
// Record request metrics here instead of in NotifyCompleted to account
// for redirects.
url_loader_util::RecordURLLoaderRequestMetrics(
*url_request_, raw_request_line_size_, raw_request_headers_size_);
}
// If there were no raw request headers, we assume no raw response headers
// either, to make client logic simpler.
// TODO(caseq): ensure this is actually an invariant?
return;
}
// Per `if (emitted_devtools_raw_request_)` above.
CHECK(devtools_observer_);
CHECK(devtools_request_id());
if (!url_request_->response_headers()) {
return;
}
const net::HttpResponseHeaders* response_headers =
raw_response_headers_ ? raw_response_headers_.get()
: url_request_->response_headers();
std::vector<network::mojom::HttpRawHeaderPairPtr> header_array =
ResponseHeaderToRawHeaderPairs(*response_headers);
// Only send the "raw" header text when the headers were actually send in
// text form (i.e. not QUIC or SPDY)
std::optional<std::string> raw_response_headers;
const net::HttpResponseInfo& response_info = url_request_->response_info();
if (!response_info.DidUseQuic() && !response_info.was_fetched_via_spdy) {
raw_response_headers =
std::make_optional(net::HttpUtil::ConvertHeadersBackToHTTPResponse(
response_headers->raw_headers()));
}
emitted_devtools_raw_response_ = true;
devtools_observer_->OnRawResponse(
devtools_request_id().value(), url_request_->maybe_stored_cookies(),
std::move(header_array), raw_response_headers,
private_network_access_interceptor_.ResponseAddressSpace().value_or(
mojom::IPAddressSpace::kUnknown),
response_headers->response_code(), url_request_->cookie_partition_key());
}
void URLLoader::SendUploadProgress(const net::UploadProgress& progress) {
url_loader_client_.Get()->OnUploadProgress(
progress.position(), progress.size(),
base::BindOnce(&URLLoader::OnUploadProgressACK,
weak_ptr_factory_.GetWeakPtr()));
}
void URLLoader::OnUploadProgressACK() {
if (upload_progress_tracker_)
upload_progress_tracker_->OnAckReceived();
}
void URLLoader::OnSSLCertificateErrorResponse(const net::SSLInfo& ssl_info,
int net_error) {
if (net_error == net::OK) {
url_request_->ContinueDespiteLastError();
return;
}
url_request_->CancelWithSSLError(net_error, ssl_info);
}
bool URLLoader::HasDataPipe() const {
return pending_write_ || response_body_stream_.is_valid();
}
void URLLoader::ResumeStart() {
url_request_->LogUnblocked();
url_request_->Start();
}
void URLLoader::OnBeforeSendHeadersComplete(
net::NetworkDelegate::OnBeforeStartTransactionCallback callback,
int result,
const std::optional<net::HttpRequestHeaders>& headers) {
if (include_request_cookies_with_response_ && headers) {
request_cookies_.clear();
std::string cookie_header =
headers->GetHeader(net::HttpRequestHeaders::kCookie)
.value_or(std::string());
net::cookie_util::ParseRequestCookieLine(cookie_header, &request_cookies_);
}
std::move(callback).Run(result, headers);
}
void URLLoader::OnHeadersReceivedComplete(
net::CompletionOnceCallback callback,
scoped_refptr<net::HttpResponseHeaders>* out_headers,
std::optional<GURL>* out_preserve_fragment_on_redirect_url,
int result,
const std::optional<std::string>& headers,
const std::optional<GURL>& preserve_fragment_on_redirect_url) {
if (headers) {
*out_headers =
base::MakeRefCounted<net::HttpResponseHeaders>(headers.value());
}
*out_preserve_fragment_on_redirect_url = preserve_fragment_on_redirect_url;
std::move(callback).Run(result);
}
void URLLoader::CompleteBlockedResponse(
int error_code,
bool should_report_orb_blocking,
std::optional<mojom::BlockedByResponseReason> reason) {
if (has_received_response_) {
// The response headers and body shouldn't yet be sent to the
// URLLoaderClient.
CHECK(response_);
CHECK(consumer_handle_.is_valid() ||
(options_ & mojom::kURLLoadOptionReadAndDiscardBody));
}
// Tell the URLLoaderClient that the response has been completed.
URLLoaderCompletionStatus status;
status.error_code = error_code;
status.completion_time = base::TimeTicks::Now();
status.encoded_data_length = 0;
status.encoded_body_length = 0;
status.decoded_body_length = 0;
status.should_report_orb_blocking = should_report_orb_blocking;
status.blocked_by_response_reason = reason;
url_loader_client_.Get()->OnComplete(status);
// Reset the connection to the URLLoaderClient. This helps ensure that we
// won't accidentally leak any data to the renderer from this point on.
url_loader_client_.Reset();
}
URLLoader::BlockResponseForOrbResult URLLoader::BlockResponseForOrb() {
// ORB should only do work after the response headers have been received.
DCHECK(has_received_response_);
// Caller should have set up a OrbAnalyzer for BlockResponseForOrb to be
// able to do its job.
DCHECK(orb_analyzer_);
// The response headers and body shouldn't yet be sent to the URLLoaderClient.
DCHECK(response_);
DCHECK(consumer_handle_.is_valid());
// Send stripped headers to the real URLLoaderClient.
orb::SanitizeBlockedResponseHeaders(*response_);
// Determine error code. This essentially handles the "ORB v0.1" and "ORB
// v0.2" difference.
int blocked_error_code =
(orb_analyzer_->ShouldHandleBlockedResponseAs() ==
orb::ResponseAnalyzer::BlockedResponseHandling::kEmptyResponse)
? net::OK
: net::ERR_BLOCKED_BY_ORB;
// Send empty body to the real URLLoaderClient. This preserves "ORB v0.1"
// behaviour and will also go away once
// OpaqueResponseBlockingErrorsForAllFetches is perma-enabled.
if (blocked_error_code == net::OK) {
mojo::ScopedDataPipeProducerHandle producer_handle;
mojo::ScopedDataPipeConsumerHandle consumer_handle;
MojoResult result = mojo::CreateDataPipe(kBlockedBodyAllocationSize,
producer_handle, consumer_handle);
if (result != MOJO_RESULT_OK) {
// Defer calling NotifyCompleted to make sure the caller can still access
// |this|.
TaskRunner(url_request_->priority())
->PostTask(FROM_HERE,
base::BindOnce(&URLLoader::NotifyCompleted,
weak_ptr_factory_.GetWeakPtr(),
net::ERR_INSUFFICIENT_RESOURCES));
return kWillCancelRequest;
}
producer_handle.reset();
// Tell the real URLLoaderClient that the response has been completed.
url_loader_client_.Get()->OnReceiveResponse(
response_->Clone(), std::move(consumer_handle), std::nullopt);
}
// At this point, orb_analyzer_ has done its duty. We'll reset it now
// to force UMA reporting to happen earlier, to support easier testing.
bool should_report_blocked_response =
orb_analyzer_->ShouldReportBlockedResponse();
orb_analyzer_.reset();
CompleteBlockedResponse(blocked_error_code, should_report_blocked_response);
// Close the socket associated with the request, to prevent leaking
// information.
url_request_->AbortAndCloseConnection();
// Delete self and cancel the request - the caller doesn't need to continue.
//
// DeleteSelf is posted asynchronously, to make sure that the callers (e.g.
// URLLoader::OnResponseStarted and/or URLLoader::DidRead instance methods)
// can still safely dereference |this|.
TaskRunner(url_request_->priority())
->PostTask(FROM_HERE, base::BindOnce(&URLLoader::DeleteSelf,
weak_ptr_factory_.GetWeakPtr()));
return kWillCancelRequest;
}
bool URLLoader::MaybeBlockResponseForOrb(
orb::ResponseAnalyzer::Decision orb_decision) {
DCHECK(orb_analyzer_);
DCHECK(is_more_orb_sniffing_needed_);
bool will_cancel = false;
switch (orb_decision) {
case network::orb::ResponseAnalyzer::Decision::kBlock: {
will_cancel = BlockResponseForOrb() == kWillCancelRequest;
orb_analyzer_.reset();
is_more_orb_sniffing_needed_ = false;
break;
}
case network::orb::ResponseAnalyzer::Decision::kAllow:
orb_analyzer_.reset();
is_more_orb_sniffing_needed_ = false;
break;
case network::orb::ResponseAnalyzer::Decision::kSniffMore:
break;
}
DCHECK_EQ(is_more_orb_sniffing_needed_, !!orb_analyzer_);
return will_cancel;
}
void URLLoader::ReportFlaggedResponseCookies(bool call_cookie_observer) {
if (!cookie_observer_) {
return;
}
std::vector<mojom::CookieOrLineWithAccessResultPtr> reported_cookies;
for (const auto& cookie_line_and_access_result :
url_request_->maybe_stored_cookies()) {
if (ShouldNotifyAboutCookie(
cookie_line_and_access_result.access_result.status)) {
mojom::CookieOrLinePtr cookie_or_line;
if (cookie_line_and_access_result.cookie.has_value()) {
cookie_or_line = mojom::CookieOrLine::NewCookie(
cookie_line_and_access_result.cookie.value());
} else {
cookie_or_line = mojom::CookieOrLine::NewCookieString(
cookie_line_and_access_result.cookie_string);
}
reported_cookies.push_back(mojom::CookieOrLineWithAccessResult::New(
std::move(cookie_or_line),
cookie_line_and_access_result.access_result));
}
}
if (!reported_cookies.empty()) {
cookie_access_details_.emplace_back(mojom::CookieAccessDetails::New(
mojom::CookieAccessDetails::Type::kChange, url_request_->url(),
url_request_->isolation_info().frame_origin(),
url_request_->isolation_info().top_frame_origin().value_or(
url::Origin()),
url_request_->site_for_cookies(), std::move(reported_cookies),
devtools_request_id(), url_request_->ad_tagged(),
url_request_->cookie_setting_overrides()));
if (call_cookie_observer) {
cookie_observer_->OnCookiesAccessed(std::move(cookie_access_details_));
}
}
}
void URLLoader::StartReading() {
if (!is_more_mime_sniffing_needed_ && !is_more_orb_sniffing_needed_) {
// Treat feed types as text/plain.
if (response_->mime_type == "application/rss+xml" ||
response_->mime_type == "application/atom+xml") {
response_->mime_type.assign("text/plain");
}
SendResponseToClient();
}
// Start reading...
ReadMore();
}
bool URLLoader::ShouldForceIgnoreSiteForCookies(
const ResourceRequest& request) {
// Ignore site for cookies in requests from an initiator covered by the
// same-origin-policy exclusions in `origin_access_list_` (typically requests
// initiated by Chrome Extensions).
if (request.request_initiator.has_value() &&
cors::OriginAccessList::AccessState::kAllowed ==
origin_access_list_->CheckAccessState(
request.request_initiator.value(), request.url)) {
return true;
}
// Convert `site_for_cookies` into an origin (an opaque origin if
// `net::SiteForCookies::IsNull()` returns true).
//
// Note that `site_for_cookies` is a _site_ rather than an _origin_, but for
// Chrome Extensions the _site_ and _origin_ of a host are the same extension
// id. Thanks to this, for Chrome Extensions, we can pass a _site_ into
// OriginAccessChecks (which normally expect an _origin_).
url::Origin site_origin =
url::Origin::Create(request.site_for_cookies.RepresentativeUrl());
// If `site_for_cookies` represents an origin that is granted access to the
// initiator and the target by `origin_access_list_` (typically such
// `site_for_cookies` represents a Chrome Extension), then we also should
// force ignoring of site for cookies if the initiator and the target are
// same-site.
//
// Ideally we would walk up the frame tree and check that each ancestor is
// first-party to the main frame (treating the `origin_access_list_`
// exceptions as "first-party"). But walking up the tree is not possible in
// //services/network and so we make do with just checking the direct
// initiator of the request.
//
// We also check same-siteness between the initiator and the requested URL,
// because setting `force_ignore_site_for_cookies` to true causes Strict
// cookies to be attached, and having the initiator be same-site to the
// request URL is a requirement for Strict cookies (see
// net::cookie_util::ComputeSameSiteContext).
if (!site_origin.opaque() && request.request_initiator.has_value()) {
bool site_can_access_target =
cors::OriginAccessList::AccessState::kAllowed ==
origin_access_list_->CheckAccessState(site_origin, request.url);
bool site_can_access_initiator =
cors::OriginAccessList::AccessState::kAllowed ==
origin_access_list_->CheckAccessState(
site_origin, request.request_initiator->GetURL());
net::SiteForCookies site_of_initiator =
net::SiteForCookies::FromOrigin(request.request_initiator.value());
bool are_initiator_and_target_same_site =
site_of_initiator.IsFirstParty(request.url);
if (site_can_access_initiator && site_can_access_target &&
are_initiator_and_target_same_site) {
return true;
}
}
return false;
}
bool URLLoader::ShouldSendTransferSizeUpdated() const {
return devtools_request_id() || url_request_->ad_tagged() ||
!base::FeatureList::IsEnabled(features::kReduceTransferSizeUpdatedIPC);
}
bool URLLoader::ShouldSetLoadWithStorageAccess() const {
CHECK(url_request_);
if (!IncludesValidLoadField(url_request_->response_headers())) {
return false;
}
auto determine_storage_access_load_outcome =
[&]() -> net::cookie_util::ActivateStorageAccessLoadOutcome {
if (!url_request_->storage_access_status().IsSet()) {
url_request_->set_storage_access_status(
url_request_->CalculateStorageAccessStatus());
}
if (!url_request_->storage_access_status()
.GetStatusForThirdPartyContext()) {
return net::cookie_util::ActivateStorageAccessLoadOutcome::
kFailureInvalidStatus;
}
switch (url_request_->storage_access_status()
.GetStatusForThirdPartyContext()
.value()) {
case net::cookie_util::StorageAccessStatus::kNone:
return net::cookie_util::ActivateStorageAccessLoadOutcome::
kFailureInvalidStatus;
case net::cookie_util::StorageAccessStatus::kInactive:
case net::cookie_util::StorageAccessStatus::kActive:
return net::cookie_util::ActivateStorageAccessLoadOutcome::kSuccess;
}
NOTREACHED();
};
auto outcome = determine_storage_access_load_outcome();
base::UmaHistogramEnumeration(
"API.StorageAccessHeader.ActivateStorageAccessLoadOutcome", outcome);
return outcome ==
net::cookie_util::ActivateStorageAccessLoadOutcome::kSuccess;
}
const mojom::ClientSecurityState* URLLoader::GetClientSecurityState() {
return url_loader_util::SelectClientSecurityState(
factory_params_->client_security_state.get(),
client_security_state_.get());
}
void URLLoader::ResetRawHeadersForRedirect() {
emitted_devtools_raw_request_ = false;
emitted_devtools_raw_response_ = false;
raw_request_line_size_ = 0;
raw_request_headers_size_ = 0;
raw_response_headers_ = nullptr;
}
void URLLoader::MaybeCollectDurableMessage(size_t new_data_offset,
int num_bytes) {
if (!pending_write_ || !durable_message_writer_) {
return;
}
if (num_bytes <= 0) {
durable_message_writer_->MarkComplete();
return;
}
int64_t raw_bytes_cur_size = url_request_->GetRawBodyBytes();
int64_t raw_bytes_delta =
raw_bytes_cur_size - devtools_durable_message_raw_size_;
durable_message_writer_->AddBytes(
base::as_byte_span(
base::span(*pending_write_)
.subspan(new_data_offset, static_cast<size_t>(num_bytes))),
raw_bytes_delta);
devtools_durable_message_raw_size_ = raw_bytes_cur_size;
}
} // namespace network
|