summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/loader/threadable_loader.cc
blob: 4fed84a7914dddcefa37e6d96c67d2b471d9893d (plain)
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
/*
 * Copyright (C) 2011, 2012 Google Inc. All rights reserved.
 * Copyright (C) 2013, Intel Corporation
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "third_party/blink/renderer/core/loader/threadable_loader.h"

#include <memory>

#include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h"
#include "services/network/public/cpp/cors/cors_error_status.h"
#include "services/network/public/mojom/cors.mojom-blink.h"
#include "services/network/public/mojom/fetch_api.mojom-blink.h"
#include "third_party/blink/public/common/service_worker/service_worker_utils.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/public/platform/web_security_origin.h"
#include "third_party/blink/public/platform/web_url_request.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/frame_console.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/local_frame_client.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/core/inspector/inspector_network_agent.h"
#include "third_party/blink/renderer/core/inspector/inspector_trace_events.h"
#include "third_party/blink/renderer/core/loader/base_fetch_context.h"
#include "third_party/blink/renderer/core/loader/frame_loader.h"
#include "third_party/blink/renderer/core/loader/threadable_loader_client.h"
#include "third_party/blink/renderer/core/probe/core_probes.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/platform/exported/wrapped_resource_request.h"
#include "third_party/blink/renderer/platform/heap/heap.h"
#include "third_party/blink/renderer/platform/heap/self_keep_alive.h"
#include "third_party/blink/renderer/platform/loader/cors/cors.h"
#include "third_party/blink/renderer/platform/loader/cors/cors_error_string.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_client_settings_object.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_parameters.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher_properties.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_loader.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_loader_options.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_request.h"
#include "third_party/blink/renderer/platform/weborigin/scheme_registry.h"
#include "third_party/blink/renderer/platform/weborigin/security_origin.h"
#include "third_party/blink/renderer/platform/weborigin/security_policy.h"
#include "third_party/blink/renderer/platform/wtf/assertions.h"
#include "third_party/blink/renderer/platform/wtf/shared_buffer.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"

namespace blink {

namespace {

// Fetch API Spec: https://fetch.spec.whatwg.org/#cors-preflight-fetch-0
AtomicString CreateAccessControlRequestHeadersHeader(
    const HTTPHeaderMap& headers) {
  Vector<String> filtered_headers = cors::CorsUnsafeRequestHeaderNames(headers);

  if (!filtered_headers.size())
    return g_null_atom;

  // Sort header names lexicographically.
  std::sort(filtered_headers.begin(), filtered_headers.end(),
            WTF::CodeUnitCompareLessThan);
  StringBuilder header_buffer;
  for (const String& header : filtered_headers) {
    if (!header_buffer.IsEmpty())
      header_buffer.Append(",");
    header_buffer.Append(header);
  }

  return header_buffer.ToAtomicString();
}

}  // namespace

// DetachedClient is a ThreadableLoaderClient for a "detached"
// ThreadableLoader. It's for fetch requests with keepalive set, so
// it keeps itself alive during loading.
class ThreadableLoader::DetachedClient final
    : public GarbageCollected<DetachedClient>,
      public ThreadableLoaderClient {
  USING_GARBAGE_COLLECTED_MIXIN(DetachedClient);

 public:
  explicit DetachedClient(ThreadableLoader* loader)
      : self_keep_alive_(PERSISTENT_FROM_HERE, this), loader_(loader) {}
  ~DetachedClient() override {}

  void DidFinishLoading(uint64_t identifier) override {
    self_keep_alive_.Clear();
  }
  void DidFail(const ResourceError&) override { self_keep_alive_.Clear(); }
  void DidFailRedirectCheck() override { self_keep_alive_.Clear(); }
  void Trace(Visitor* visitor) override {
    visitor->Trace(loader_);
    ThreadableLoaderClient::Trace(visitor);
  }

 private:
  SelfKeepAlive<DetachedClient> self_keep_alive_;
  // Keep it alive.
  const Member<ThreadableLoader> loader_;
};

class ThreadableLoader::AssignOnScopeExit final {
  STACK_ALLOCATED();

 public:
  AssignOnScopeExit(const KURL& from, KURL* to) : from_(from), to_(to) {}
  ~AssignOnScopeExit() { *to_ = from_; }

 private:
  const KURL& from_;
  KURL* to_;

  DISALLOW_COPY_AND_ASSIGN(AssignOnScopeExit);
};

// Max number of CORS redirects handled in ThreadableLoader.
// See https://fetch.spec.whatwg.org/#http-redirect-fetch.
// //net/url_request/url_request.cc and
// //services/network/cors/cors_url_loader.cc also implement the same logic
// separately.
static const int kMaxRedirects = 20;

// static
std::unique_ptr<ResourceRequest>
ThreadableLoader::CreateAccessControlPreflightRequest(
    const ResourceRequest& request,
    const SecurityOrigin* origin) {
  const KURL& request_url = request.Url();

  DCHECK(request_url.User().IsEmpty());
  DCHECK(request_url.Pass().IsEmpty());

  std::unique_ptr<ResourceRequest> preflight_request =
      std::make_unique<ResourceRequest>(request_url);
  preflight_request->SetHttpMethod(http_names::kOPTIONS);
  preflight_request->SetHttpHeaderField(http_names::kAccessControlRequestMethod,
                                        request.HttpMethod());
  preflight_request->SetMode(network::mojom::RequestMode::kCors);
  preflight_request->SetPriority(request.Priority());
  preflight_request->SetRequestContext(request.GetRequestContext());
  preflight_request->SetCredentialsMode(network::mojom::CredentialsMode::kOmit);
  preflight_request->SetSkipServiceWorker(true);
  preflight_request->SetReferrerString(request.ReferrerString());
  preflight_request->SetReferrerPolicy(request.GetReferrerPolicy());

  if (request.IsExternalRequest()) {
    preflight_request->SetHttpHeaderField(
        http_names::kAccessControlRequestExternal, "true");
  }

  const AtomicString request_headers =
      CreateAccessControlRequestHeadersHeader(request.HttpHeaderFields());
  if (request_headers != g_null_atom) {
    preflight_request->SetHttpHeaderField(
        http_names::kAccessControlRequestHeaders, request_headers);
  }

  if (origin)
    preflight_request->SetHTTPOrigin(origin);

  return preflight_request;
}

// static
std::unique_ptr<ResourceRequest>
ThreadableLoader::CreateAccessControlPreflightRequestForTesting(
    const ResourceRequest& request) {
  return CreateAccessControlPreflightRequest(request, nullptr);
}

ThreadableLoader::ThreadableLoader(
    ExecutionContext& execution_context,
    ThreadableLoaderClient* client,
    const ResourceLoaderOptions& resource_loader_options,
    ResourceFetcher* resource_fetcher)
    : client_(client),
      execution_context_(execution_context),
      resource_fetcher_(resource_fetcher),
      resource_loader_options_(resource_loader_options),
      out_of_blink_cors_(RuntimeEnabledFeatures::OutOfBlinkCorsEnabled()),
      async_(resource_loader_options.synchronous_policy ==
             kRequestAsynchronously),
      request_context_(mojom::RequestContextType::UNSPECIFIED),
      request_mode_(network::mojom::RequestMode::kSameOrigin),
      credentials_mode_(network::mojom::CredentialsMode::kOmit),
      timeout_timer_(execution_context_->GetTaskRunner(TaskType::kNetworking),
                     this,
                     &ThreadableLoader::DidTimeout),
      redirect_limit_(kMaxRedirects),
      redirect_mode_(network::mojom::RedirectMode::kFollow),
      override_referrer_(false) {
  DCHECK(client);
  if (!resource_fetcher_) {
    if (auto* scope = DynamicTo<WorkerGlobalScope>(*execution_context_))
      scope->EnsureFetcher();
    resource_fetcher_ = execution_context_->Fetcher();
  }
}

void ThreadableLoader::Start(ResourceRequest request) {
  original_security_origin_ = security_origin_ = request.RequestorOrigin();
  // Setting an outgoing referer is only supported in the async code path.
  DCHECK(async_ ||
         request.ReferrerString() == Referrer::ClientReferrerString());

  bool cors_enabled = cors::IsCorsEnabledRequestMode(request.GetMode());

  // kPreventPreflight can be used only when the CORS is enabled.
  DCHECK(request.CorsPreflightPolicy() ==
             network::mojom::CorsPreflightPolicy::kConsiderPreflight ||
         cors_enabled);

  initial_request_url_ = request.Url();
  last_request_url_ = initial_request_url_;
  request_context_ = request.GetRequestContext();
  request_mode_ = request.GetMode();
  credentials_mode_ = request.GetCredentialsMode();
  redirect_mode_ = request.GetRedirectMode();

  if (request.GetMode() == network::mojom::RequestMode::kNoCors) {
    SECURITY_CHECK(cors::IsNoCorsAllowedContext(request_context_));
  }
  cors_flag_ = cors::CalculateCorsFlag(request.Url(), GetSecurityOrigin(),
                                       request.IsolatedWorldOrigin().get(),
                                       request.GetMode());

  // The CORS flag variable is not yet used at the step in the spec that
  // corresponds to this line, but divert |cors_flag_| here for convenience.
  if (cors_flag_ &&
      request.GetMode() == network::mojom::RequestMode::kSameOrigin) {
    ThreadableLoaderClient* client = client_;
    Clear();
    client->DidFail(ResourceError(
        request.Url(), network::CorsErrorStatus(
                           network::mojom::CorsError::kDisallowedByMode)));
    return;
  }

  request_started_ = base::TimeTicks::Now();

  // Save any headers on the request here. If this request redirects
  // cross-origin, we cancel the old request create a new one, and copy these
  // headers.
  request_headers_ = request.HttpHeaderFields();
  report_upload_progress_ = request.ReportUploadProgress();

  // Set the service worker mode to none if "bypass for network" in DevTools is
  // enabled.
  bool should_bypass_service_worker = false;
  probe::ShouldBypassServiceWorker(execution_context_,
                                   &should_bypass_service_worker);
  if (should_bypass_service_worker)
    request.SetSkipServiceWorker(true);

  // Process the CORS protocol inside the ThreadableLoader for the
  // following cases:
  //
  // - When the request is sync or the protocol is unsupported since we can
  //   assume that any service worker (SW) is skipped for such requests by
  //   content/ code.
  // - When |GetSkipServiceWorker()| is true, any SW will be skipped.
  // - If we're not yet controlled by a SW, then we're sure that this
  //   request won't be intercepted by a SW. In case we end up with
  //   sending a CORS preflight request, the actual request to be sent later
  //   may be intercepted. This is taken care of in LoadPreflightRequest() by
  //   setting |GetSkipServiceWorker()| to true.
  //
  // From the above analysis, you can see that the request can never be
  // intercepted by a SW inside this if-block. It's because:
  // - |GetSkipServiceWorker()| needs to be false, and
  // - we're controlled by a SW at this point
  // to allow a SW to intercept the request. Even when the request gets issued
  // asynchronously after performing the CORS preflight, it doesn't get
  // intercepted since LoadPreflightRequest() sets the flag to kNone in advance.
  const bool is_controlled_by_service_worker =
      resource_fetcher_->IsControlledByServiceWorker() ==
      blink::mojom::ControllerServiceWorkerMode::kControlled;
  if (!async_ || request.GetSkipServiceWorker() ||
      !SchemeRegistry::ShouldTreatURLSchemeAsAllowingServiceWorkers(
          request.Url().Protocol()) ||
      !is_controlled_by_service_worker) {
    DispatchInitialRequest(request);
    return;
  }

  if (!out_of_blink_cors_ &&
      cors::IsCorsEnabledRequestMode(request.GetMode())) {
    // Save the request to fallback_request_for_service_worker to use when the
    // service worker doesn't handle (call respondWith()) a CORS enabled
    // request.
    fallback_request_for_service_worker_.CopyFrom(request);
    // Skip the service worker for the fallback request.
    fallback_request_for_service_worker_.SetSkipServiceWorker(true);
  }

  LoadRequest(request, resource_loader_options_);
}

void ThreadableLoader::DispatchInitialRequest(ResourceRequest& request) {
  if (out_of_blink_cors_ || (!request.IsExternalRequest() && !cors_flag_)) {
    LoadRequest(request, resource_loader_options_);
    return;
  }

  DCHECK(cors::IsCorsEnabledRequestMode(request.GetMode()) ||
         request.IsExternalRequest());

  MakeCrossOriginAccessRequest(request);
}

void ThreadableLoader::PrepareCrossOriginRequest(
    ResourceRequest& request) const {
  if (GetSecurityOrigin())
    request.SetHTTPOrigin(GetSecurityOrigin());

  if (override_referrer_) {
    request.SetReferrerString(referrer_after_redirect_.referrer);
    request.SetReferrerPolicy(referrer_after_redirect_.referrer_policy);
  }
}

void ThreadableLoader::LoadPreflightRequest(
    const ResourceRequest& actual_request,
    const ResourceLoaderOptions& actual_options) {
  std::unique_ptr<ResourceRequest> preflight_request =
      CreateAccessControlPreflightRequest(actual_request, GetSecurityOrigin());

  actual_request_.CopyFrom(actual_request);
  actual_options_ = actual_options;

  // Explicitly set |skip_service_worker| to true here. Although the page is
  // not controlled by a SW at this point, a new SW may be controlling the
  // page when this actual request gets sent later. We should not send the
  // actual request to the SW. See https://crbug.com/604583.
  actual_request_.SetSkipServiceWorker(true);

  // Create a ResourceLoaderOptions for preflight.
  ResourceLoaderOptions preflight_options = actual_options;

  LoadRequest(*preflight_request, preflight_options);
}

void ThreadableLoader::MakeCrossOriginAccessRequest(
    const ResourceRequest& request) {
  DCHECK(cors::IsCorsEnabledRequestMode(request.GetMode()) ||
         request.IsExternalRequest());
  DCHECK(client_);
  DCHECK(!GetResource());

  // Cross-origin requests are only allowed certain registered schemes. We would
  // catch this when checking response headers later, but there is no reason to
  // send a request, preflighted or not, that's guaranteed to be denied.
  if (!SchemeRegistry::ShouldTreatURLSchemeAsCorsEnabled(
          request.Url().Protocol())) {
    DispatchDidFail(ResourceError(
        request.Url(), network::CorsErrorStatus(
                           network::mojom::CorsError::kCorsDisabledScheme)));
    return;
  }

  // Non-secure origins may not make "external requests":
  // https://wicg.github.io/cors-rfc1918/#integration-fetch
  String error_message;
  // TODO(yhirano): Consider moving this branch elsewhere.
  if (!execution_context_->IsSecureContext(error_message) &&
      request.IsExternalRequest()) {
    // TODO(yhirano): Fix the link.
    DispatchDidFail(ResourceError::CancelledDueToAccessCheckError(
        request.Url(), ResourceRequestBlockedReason::kOrigin,
        "Requests to internal network resources are not allowed "
        "from non-secure contexts (see https://goo.gl/Y0ZkNV). "
        "This is an experimental restriction which is part of "
        "'https://mikewest.github.io/cors-rfc1918/'."));
    return;
  }

  ResourceRequest cross_origin_request;
  cross_origin_request.CopyFrom(request);
  ResourceLoaderOptions cross_origin_options(resource_loader_options_);

  cross_origin_request.RemoveUserAndPassFromURL();

  // Enforce the CORS preflight for checking the Access-Control-Allow-External
  // header. The CORS preflight cache doesn't help for this purpose.
  if (request.IsExternalRequest()) {
    LoadPreflightRequest(cross_origin_request, cross_origin_options);
    return;
  }

  if (request.GetMode() !=
      network::mojom::RequestMode::kCorsWithForcedPreflight) {
    if (request.CorsPreflightPolicy() ==
        network::mojom::CorsPreflightPolicy::kPreventPreflight) {
      PrepareCrossOriginRequest(cross_origin_request);
      LoadRequest(cross_origin_request, cross_origin_options);
      return;
    }

    DCHECK_EQ(request.CorsPreflightPolicy(),
              network::mojom::CorsPreflightPolicy::kConsiderPreflight);

    // We use ContainsOnlyCorsSafelistedOrForbiddenHeaders() here since
    // |request| may have been modified in the process of loading (not from
    // the user's input). For example, referrer. We need to accept them. For
    // security, we must reject forbidden headers/methods at the point we
    // accept user's input. Not here.
    if (cors::IsCorsSafelistedMethod(request.HttpMethod()) &&
        cors::ContainsOnlyCorsSafelistedOrForbiddenHeaders(
            request.HttpHeaderFields())) {
      PrepareCrossOriginRequest(cross_origin_request);
      LoadRequest(cross_origin_request, cross_origin_options);
      return;
    }
  }

  // Now, we need to check that the request passes the CORS preflight either by
  // issuing a CORS preflight or based on an entry in the CORS preflight cache.

  bool should_ignore_preflight_cache = false;
  // Prevent use of the CORS preflight cache when instructed by the DevTools
  // not to use caches.
  probe::ShouldForceCorsPreflight(execution_context_,
                                  &should_ignore_preflight_cache);
  if (should_ignore_preflight_cache ||
      !cors::CheckIfRequestCanSkipPreflight(
          GetSecurityOrigin()->ToString(), cross_origin_request.Url(),
          cross_origin_request.GetCredentialsMode(),
          cross_origin_request.HttpMethod(),
          cross_origin_request.HttpHeaderFields())) {
    LoadPreflightRequest(cross_origin_request, cross_origin_options);
    return;
  }

  // We don't want any requests that could involve a CORS preflight to get
  // intercepted by a foreign SW, even if we have the result of the preflight
  // cached already. See https://crbug.com/674370.
  cross_origin_request.SetSkipServiceWorker(true);

  PrepareCrossOriginRequest(cross_origin_request);
  LoadRequest(cross_origin_request, cross_origin_options);
}

ThreadableLoader::~ThreadableLoader() {}

void ThreadableLoader::SetTimeout(const base::TimeDelta& timeout) {
  timeout_ = timeout;

  // |request_started_| <= base::TimeTicks() indicates loading is either not yet
  // started or is already finished, and thus we don't need to do anything with
  // timeout_timer_.
  if (request_started_ <= base::TimeTicks()) {
    DCHECK(!timeout_timer_.IsActive());
    return;
  }

  DCHECK(async_);
  timeout_timer_.Stop();

  // At the time of this method's implementation, it is only ever called for an
  // inflight request by XMLHttpRequest.
  //
  // The XHR request says to resolve the time relative to when the request
  // was initially sent, however other uses of this method may need to
  // behave differently, in which case this should be re-arranged somehow.
  if (!timeout_.is_zero()) {
    base::TimeDelta elapsed_time = base::TimeTicks::Now() - request_started_;
    base::TimeDelta resolved_time =
        std::max(timeout_ - elapsed_time, base::TimeDelta());
    timeout_timer_.StartOneShot(resolved_time, FROM_HERE);
  }
}

void ThreadableLoader::Cancel() {
  // Cancel can re-enter, and therefore |resource()| might be null here as a
  // result.
  if (!client_ || !GetResource()) {
    Clear();
    return;
  }

  DispatchDidFail(ResourceError::CancelledError(GetResource()->Url()));
}

void ThreadableLoader::Detach() {
  Resource* resource = GetResource();
  if (!resource)
    return;
  detached_ = true;
  client_ = MakeGarbageCollected<DetachedClient>(this);
}

void ThreadableLoader::SetDefersLoading(bool value) {
  if (GetResource() && GetResource()->Loader())
    GetResource()->Loader()->SetDefersLoading(value);
}

void ThreadableLoader::Clear() {
  client_ = nullptr;
  timeout_timer_.Stop();
  request_started_ = base::TimeTicks();
  if (GetResource())
    checker_.WillRemoveClient();
  ClearResource();
}

// In this method, we can clear |request| to tell content::WebURLLoaderImpl of
// Chromium not to follow the redirect. This works only when this method is
// called by RawResource::willSendRequest(). If called by
// RawResource::didAddClient(), clearing |request| won't be propagated to
// content::WebURLLoaderImpl. So, this loader must also get detached from the
// resource by calling clearResource().
// TODO(toyoshim): Implement OOR-CORS mode specific redirect code.
bool ThreadableLoader::RedirectReceived(
    Resource* resource,
    const ResourceRequest& new_request,
    const ResourceResponse& redirect_response) {
  DCHECK(client_);
  DCHECK_EQ(resource, GetResource());
  {
    AssignOnScopeExit assign_on_scope_exit(new_request.Url(),
                                           &last_request_url_);

    checker_.RedirectReceived();

    const KURL& new_url = new_request.Url();
    const KURL& original_url = redirect_response.CurrentRequestUrl();

    if (out_of_blink_cors_)
      return client_->WillFollowRedirect(new_url, redirect_response);

    if (!actual_request_.IsNull()) {
      ReportResponseReceived(resource->InspectorId(), redirect_response);

      HandlePreflightFailure(
          original_url,
          network::CorsErrorStatus(
              network::mojom::CorsError::kPreflightDisallowedRedirect));
      return false;
    }

    if (cors_flag_) {
      if (const auto error_status = cors::CheckAccess(
              original_url, redirect_response.HttpHeaderFields(),
              new_request.GetCredentialsMode(), *GetSecurityOrigin())) {
        DispatchDidFail(ResourceError(original_url, *error_status));
        return false;
      }
    }

    if (redirect_mode_ == network::mojom::RedirectMode::kError) {
      bool follow = client_->WillFollowRedirect(new_url, redirect_response);
      DCHECK(!follow);
      return false;
    }

    if (redirect_mode_ == network::mojom::RedirectMode::kManual) {
      auto redirect_response_to_pass = redirect_response;
      redirect_response_to_pass.SetType(
          network::mojom::FetchResponseType::kOpaqueRedirect);
      bool follow =
          client_->WillFollowRedirect(new_url, redirect_response_to_pass);
      DCHECK(!follow);
      return false;
    }

    DCHECK_EQ(redirect_mode_, network::mojom::RedirectMode::kFollow);

    if (redirect_limit_ <= 0) {
      ThreadableLoaderClient* client = client_;
      Clear();
      auto* message = MakeGarbageCollected<ConsoleMessage>(
          mojom::ConsoleMessageSource::kNetwork,
          mojom::ConsoleMessageLevel::kError,
          "Failed to load resource: net::ERR_TOO_MANY_REDIRECTS",
          SourceLocation::Capture(original_url, 0, 0));
      execution_context_->AddConsoleMessage(message);
      client->DidFailRedirectCheck();
      return false;
    }
    --redirect_limit_;

    auto redirect_response_to_pass = redirect_response;
    redirect_response_to_pass.SetType(response_tainting_);

    // Allow same origin requests to continue after allowing clients to audit
    // the redirect.
    if (!(cors_flag_ ||
          cors::CalculateCorsFlag(new_url, GetSecurityOrigin(),
                                  new_request.IsolatedWorldOrigin().get(),
                                  new_request.GetMode()))) {
      bool follow =
          client_->WillFollowRedirect(new_url, redirect_response_to_pass);
      response_tainting_ = cors::CalculateResponseTainting(
          new_url, new_request.GetMode(), GetSecurityOrigin(),
          new_request.IsolatedWorldOrigin().get(), CorsFlag::Unset);
      return follow;
    }

    probe::DidReceiveCorsRedirectResponse(
        execution_context_, resource->InspectorId(),
        GetFrame() ? GetFrame()->Loader().GetDocumentLoader() : nullptr,
        redirect_response_to_pass, resource);

    if (auto error_status = cors::CheckRedirectLocation(
            new_url, request_mode_, GetSecurityOrigin(),
            cors_flag_ ? CorsFlag::Set : CorsFlag::Unset)) {
      DispatchDidFail(ResourceError(original_url, *error_status));
      return false;
    }

    if (!client_->WillFollowRedirect(new_url, redirect_response_to_pass))
      return false;

    // FIXME: consider combining this with CORS redirect handling performed by
    // CrossOriginAccessControl::handleRedirect().
    if (GetResource())
      checker_.WillRemoveClient();
    ClearResource();

    // If
    // - CORS flag is set, and
    // - the origin of the redirect target URL is not same origin with the
    //   origin of the current request's URL
    // set the source origin to a unique opaque origin.
    //
    // See https://fetch.spec.whatwg.org/#http-redirect-fetch.
    if (cors_flag_) {
      scoped_refptr<const SecurityOrigin> original_origin =
          SecurityOrigin::Create(original_url);
      scoped_refptr<const SecurityOrigin> new_origin =
          SecurityOrigin::Create(new_url);
      if (!original_origin->IsSameOriginWith(new_origin.get()))
        security_origin_ = SecurityOrigin::CreateUniqueOpaque();
    }

    // Set |cors_flag_| so that further logic (corresponds to the main fetch in
    // the spec) will be performed with CORS flag set.
    // See https://fetch.spec.whatwg.org/#http-redirect-fetch.
    cors_flag_ = true;

    // Save the referrer to use when following the redirect.
    override_referrer_ = true;
    referrer_after_redirect_ =
        Referrer(new_request.ReferrerString(), new_request.GetReferrerPolicy());
  }
  // We're initiating a new request (for redirect), so update
  // |last_request_url_| by destroying |assign_on_scope_exit|.

  ResourceRequest cross_origin_request;
  cross_origin_request.CopyFrom(new_request);
  cross_origin_request.SetInitialUrlForResourceTiming(initial_request_url_);

  // Remove any headers that may have been added by the network layer that cause
  // access control to fail.
  cross_origin_request.SetReferrerString(Referrer::NoReferrer());
  cross_origin_request.SetReferrerPolicy(
      network::mojom::ReferrerPolicy::kDefault);
  cross_origin_request.ClearHTTPOrigin();
  cross_origin_request.ClearHTTPUserAgent();
  // Add any request headers which we previously saved from the
  // original request.
  for (const auto& header : request_headers_)
    cross_origin_request.SetHttpHeaderField(header.key, header.value);
  cross_origin_request.SetReportUploadProgress(report_upload_progress_);
  MakeCrossOriginAccessRequest(cross_origin_request);

  return false;
}

void ThreadableLoader::RedirectBlocked() {
  checker_.RedirectBlocked();

  // Tells the client that a redirect was received but not followed (for an
  // unknown reason).
  ThreadableLoaderClient* client = client_;
  Clear();
  client->DidFailRedirectCheck();
}

void ThreadableLoader::DataSent(Resource* resource,
                                uint64_t bytes_sent,
                                uint64_t total_bytes_to_be_sent) {
  DCHECK(client_);
  DCHECK_EQ(resource, GetResource());
  DCHECK(async_);

  checker_.DataSent();
  client_->DidSendData(bytes_sent, total_bytes_to_be_sent);
}

void ThreadableLoader::DataDownloaded(Resource* resource,
                                      uint64_t data_length) {
  DCHECK(client_);
  DCHECK_EQ(resource, GetResource());
  DCHECK(actual_request_.IsNull());

  checker_.DataDownloaded();
  client_->DidDownloadData(data_length);
}

void ThreadableLoader::DidDownloadToBlob(Resource* resource,
                                         scoped_refptr<BlobDataHandle> blob) {
  DCHECK(client_);
  DCHECK_EQ(resource, GetResource());

  checker_.DidDownloadToBlob();
  client_->DidDownloadToBlob(std::move(blob));
}

void ThreadableLoader::HandlePreflightResponse(
    const ResourceResponse& response) {
  base::Optional<network::CorsErrorStatus> cors_error_status =
      cors::CheckPreflightAccess(
          response.CurrentRequestUrl(), response.HttpStatusCode(),
          response.HttpHeaderFields(), actual_request_.GetCredentialsMode(),
          *GetSecurityOrigin());
  if (cors_error_status) {
    HandlePreflightFailure(response.CurrentRequestUrl(), *cors_error_status);
    return;
  }

  base::Optional<network::CorsErrorStatus> error_status;
  if (actual_request_.IsExternalRequest()) {
    error_status = cors::CheckExternalPreflight(response.HttpHeaderFields());
    if (error_status) {
      HandlePreflightFailure(response.CurrentRequestUrl(), *error_status);
      return;
    }
  }

  String access_control_error_description;
  error_status = cors::EnsurePreflightResultAndCacheOnSuccess(
      response.HttpHeaderFields(), GetSecurityOrigin()->ToString(),
      actual_request_.Url(), actual_request_.HttpMethod(),
      actual_request_.HttpHeaderFields(), actual_request_.GetCredentialsMode());
  if (error_status)
    HandlePreflightFailure(response.CurrentRequestUrl(), *error_status);
}

void ThreadableLoader::ReportResponseReceived(
    uint64_t identifier,
    const ResourceResponse& response) {
  LocalFrame* frame = GetFrame();
  if (!frame)
    return;
  DocumentLoader* loader = frame->Loader().GetDocumentLoader();
  probe::DidReceiveResourceResponse(probe::ToCoreProbeSink(execution_context_),
                                    identifier, loader, response,
                                    GetResource());
  frame->Console().ReportResourceResponseReceived(loader, identifier, response);
}

void ThreadableLoader::ResponseReceived(Resource* resource,
                                        const ResourceResponse& response) {
  DCHECK_EQ(resource, GetResource());
  DCHECK(client_);

  checker_.ResponseReceived();

  // TODO(toyoshim): Support OOR-CORS preflight and Service Worker case.
  // Note that CORS-preflight is usually handled in the Network Service side,
  // but still done in Blink side when it is needed on redirects.
  // https://crbug.com/736308.
  if (out_of_blink_cors_ && !response.WasFetchedViaServiceWorker()) {
    DCHECK(actual_request_.IsNull());
    fallback_request_for_service_worker_ = ResourceRequest();
    client_->DidReceiveResponse(resource->InspectorId(), response);
    return;
  }

  // Code path for legacy Blink CORS.
  if (!actual_request_.IsNull()) {
    ReportResponseReceived(resource->InspectorId(), response);
    HandlePreflightResponse(response);
    return;
  }

  if (response.WasFetchedViaServiceWorker()) {
    if (response.WasFallbackRequiredByServiceWorker()) {
      // At this point we must have m_fallbackRequestForServiceWorker. (For
      // SharedWorker the request won't be CORS or CORS-with-preflight,
      // therefore fallback-to-network is handled in the browser process when
      // the ServiceWorker does not call respondWith().)
      DCHECK(!fallback_request_for_service_worker_.IsNull());
      ReportResponseReceived(resource->InspectorId(), response);
      LoadFallbackRequestForServiceWorker();
      return;
    }

    // It's possible that we issue a fetch with request with non "no-cors"
    // mode but get an opaque filtered response if a service worker is involved.
    // We dispatch a CORS failure for the case.
    // TODO(yhirano): This is probably not spec conformant. Fix it after
    // https://github.com/w3c/preload/issues/100 is addressed.
    if (request_mode_ != network::mojom::RequestMode::kNoCors &&
        response.GetType() == network::mojom::FetchResponseType::kOpaque) {
      DispatchDidFail(
          ResourceError(response.CurrentRequestUrl(),
                        network::CorsErrorStatus(
                            network::mojom::CorsError::kInvalidResponse)));
      return;
    }

    fallback_request_for_service_worker_ = ResourceRequest();
    client_->DidReceiveResponse(resource->InspectorId(), response);
    return;
  }

  // Even if the request met the conditions to get handled by a Service Worker
  // in the constructor of this class (and therefore
  // |fallback_request_for_service_worker_| is set), the Service Worker may skip
  // processing the request. Only if the request is same origin, the skipped
  // response may come here (wasFetchedViaServiceWorker() returns false) since
  // such a request doesn't have to go through the CORS algorithm by calling
  // loadFallbackRequestForServiceWorker().
  DCHECK(fallback_request_for_service_worker_.IsNull() ||
         GetSecurityOrigin()->CanRequest(
             fallback_request_for_service_worker_.Url()));
  fallback_request_for_service_worker_ = ResourceRequest();

  if (cors_flag_) {
    base::Optional<network::CorsErrorStatus> access_error = cors::CheckAccess(
        response.CurrentRequestUrl(), response.HttpHeaderFields(),
        credentials_mode_, *GetSecurityOrigin());
    if (access_error) {
      ReportResponseReceived(resource->InspectorId(), response);
      DispatchDidFail(
          ResourceError(response.CurrentRequestUrl(), *access_error));
      return;
    }
  }

  DCHECK_EQ(&response, &resource->GetResponse());
  resource->SetResponseType(response_tainting_);
  DCHECK_EQ(response.GetType(), response_tainting_);
  client_->DidReceiveResponse(resource->InspectorId(), response);
}

void ThreadableLoader::ResponseBodyReceived(Resource*, BytesConsumer& body) {
  checker_.ResponseBodyReceived();

  client_->DidStartLoadingResponseBody(body);
}

void ThreadableLoader::SetSerializedCachedMetadata(Resource*,
                                                   const uint8_t* data,
                                                   size_t size) {
  checker_.SetSerializedCachedMetadata();

  if (!actual_request_.IsNull())
    return;
  client_->DidReceiveCachedMetadata(reinterpret_cast<const char*>(data),
                                    SafeCast<int>(size));
}

void ThreadableLoader::DataReceived(Resource* resource,
                                    const char* data,
                                    size_t data_length) {
  DCHECK_EQ(resource, GetResource());
  DCHECK(client_);

  checker_.DataReceived();

  // Preflight data should be invisible to clients.
  if (!actual_request_.IsNull())
    return;

  DCHECK(fallback_request_for_service_worker_.IsNull());

  // TODO(junov): Fix the ThreadableLoader ecosystem to use size_t. Until then,
  // we use safeCast to trap potential overflows.
  client_->DidReceiveData(data, SafeCast<unsigned>(data_length));
}

void ThreadableLoader::NotifyFinished(Resource* resource) {
  DCHECK(client_);
  DCHECK_EQ(resource, GetResource());

  checker_.NotifyFinished(resource);

  if (resource->ErrorOccurred()) {
    DispatchDidFail(resource->GetResourceError());
    return;
  }

  DCHECK(fallback_request_for_service_worker_.IsNull());

  if (!actual_request_.IsNull()) {
    DCHECK(actual_request_.IsExternalRequest() || cors_flag_);
    LoadActualRequest();
    return;
  }

  ThreadableLoaderClient* client = client_;
  // Protect the resource in |didFinishLoading| in order not to release the
  // downloaded file.
  Persistent<Resource> protect = GetResource();
  Clear();
  client->DidFinishLoading(resource->InspectorId());
}

void ThreadableLoader::DidTimeout(TimerBase* timer) {
  DCHECK(async_);
  DCHECK_EQ(timer, &timeout_timer_);
  // clearResource() may be called in clear() and some other places. clear()
  // calls stop() on |timeout_|. In the other places, the resource is set
  // again. If the creation fails, clear() is called. So, here, resource() is
  // always non-nullptr.
  DCHECK(GetResource());
  // When |client_| is set to nullptr only in clear() where |timeout_|
  // is stopped. So, |client_| is always non-nullptr here.
  DCHECK(client_);

  DispatchDidFail(ResourceError::TimeoutError(GetResource()->Url()));
}

void ThreadableLoader::LoadFallbackRequestForServiceWorker() {
  if (GetResource())
    checker_.WillRemoveClient();
  ClearResource();
  ResourceRequest fallback_request;
  fallback_request.CopyFrom(fallback_request_for_service_worker_);
  fallback_request_for_service_worker_.CopyFrom(ResourceRequest());
  DispatchInitialRequest(fallback_request);
}

void ThreadableLoader::LoadActualRequest() {
  ResourceRequest actual_request;
  actual_request.CopyFrom(actual_request_);
  ResourceLoaderOptions actual_options = actual_options_;
  actual_request_.CopyFrom(ResourceRequest());
  actual_options_ = ResourceLoaderOptions();

  if (GetResource())
    checker_.WillRemoveClient();
  ClearResource();

  PrepareCrossOriginRequest(actual_request);
  LoadRequest(actual_request, actual_options);
}

void ThreadableLoader::HandlePreflightFailure(
    const KURL& url,
    const network::CorsErrorStatus& error_status) {
  // Prevent NotifyFinished() from bypassing access check.
  actual_request_ = ResourceRequest();

  DispatchDidFail(ResourceError(url, error_status));
}

void ThreadableLoader::DispatchDidFail(const ResourceError& error) {
  if (!out_of_blink_cors_ && error.CorsErrorStatus()) {
    String message = cors::GetErrorString(
        *error.CorsErrorStatus(), initial_request_url_, last_request_url_,
        *GetSecurityOrigin(), ResourceType::kRaw,
        resource_loader_options_.initiator_info.name);
    execution_context_->AddConsoleMessage(MakeGarbageCollected<ConsoleMessage>(
        mojom::ConsoleMessageSource::kJavaScript,
        mojom::ConsoleMessageLevel::kError, std::move(message)));
  }
  Resource* resource = GetResource();
  if (resource)
    resource->SetResponseType(network::mojom::FetchResponseType::kError);
  ThreadableLoaderClient* client = client_;
  Clear();
  client->DidFail(error);
}

void ThreadableLoader::LoadRequest(
    ResourceRequest& request,
    ResourceLoaderOptions resource_loader_options) {
  resource_loader_options.cors_handling_by_resource_fetcher =
      kDisableCorsHandlingByResourceFetcher;

  if (out_of_blink_cors_) {
    if (request.GetCredentialsMode() ==
        network::mojom::CredentialsMode::kOmit) {
      // See comments at network::ResourceRequest::credentials_mode.
      request.SetAllowStoredCredentials(false);
    }
  } else {
    if (actual_request_.IsNull()) {
      response_tainting_ = cors::CalculateResponseTainting(
          request.Url(), request.GetMode(), GetSecurityOrigin(),
          request.IsolatedWorldOrigin().get(),
          cors_flag_ ? CorsFlag::Set : CorsFlag::Unset);
      request.SetAllowStoredCredentials(cors::CalculateCredentialsFlag(
          request.GetCredentialsMode(), response_tainting_));
    } else {
      request.SetAllowStoredCredentials(false);
    }
  }

  request.SetRequestorOrigin(original_security_origin_);

  if (!actual_request_.IsNull())
    resource_loader_options.data_buffering_policy = kBufferData;

  if (!timeout_.is_zero()) {
    if (!async_) {
      request.SetTimeoutInterval(timeout_);
    } else if (!timeout_timer_.IsActive()) {
      // The timer can be active if this is the actual request of a
      // CORS-with-preflight request.
      timeout_timer_.StartOneShot(timeout_, FROM_HERE);
    }
  }

  FetchParameters new_params(std::move(request), resource_loader_options);
  DCHECK(!GetResource());

  checker_.WillAddClient();
  if (request.GetRequestContext() == mojom::RequestContextType::VIDEO ||
      request.GetRequestContext() == mojom::RequestContextType::AUDIO) {
    DCHECK(async_);
    RawResource::FetchMedia(new_params, resource_fetcher_, this);
  } else if (request.GetRequestContext() ==
             mojom::RequestContextType::MANIFEST) {
    DCHECK(async_);
    RawResource::FetchManifest(new_params, resource_fetcher_, this);
  } else if (async_) {
    RawResource::Fetch(new_params, resource_fetcher_, this);
  } else {
    RawResource::FetchSynchronously(new_params, resource_fetcher_, this);
  }
}

const SecurityOrigin* ThreadableLoader::GetSecurityOrigin() const {
  return security_origin_ ? security_origin_.get()
                          : resource_fetcher_->GetProperties()
                                .GetFetchClientSettingsObject()
                                .GetSecurityOrigin();
}

LocalFrame* ThreadableLoader::GetFrame() const {
  auto* window = DynamicTo<LocalDOMWindow>(execution_context_.Get());
  return window ? window->GetFrame() : nullptr;
}

void ThreadableLoader::Trace(Visitor* visitor) {
  visitor->Trace(execution_context_);
  visitor->Trace(client_);
  visitor->Trace(resource_fetcher_);
  RawResourceClient::Trace(visitor);
}

}  // namespace blink