Skip to main content

net/fetch/
methods.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::sync::Arc;
6use std::sync::atomic::{AtomicBool, Ordering};
7use std::{io, mem, str};
8
9use base64::Engine as _;
10use base64::engine::general_purpose;
11use bytes::Bytes;
12use content_security_policy as csp;
13use embedder_traits::resources::{self, Resource};
14use headers::{AccessControlExposeHeaders, ContentType, HeaderMapExt};
15use http::header::{self, HeaderMap, HeaderName, RANGE};
16use http::{HeaderValue, Method, StatusCode};
17use ipc_channel::ipc;
18use log::{debug, trace, warn};
19use malloc_size_of_derive::MallocSizeOf;
20use mime::{self, Mime};
21use net_traits::fetch::headers::{determine_nosniff, extract_mime_type_as_mime};
22use net_traits::filemanager_thread::{FileTokenCheck, RelativePos};
23use net_traits::http_status::HttpStatus;
24use net_traits::policy_container::PolicyContainer;
25use net_traits::request::{
26    BodyChunkRequest, BodyChunkResponse, CredentialsMode, Destination, Initiator,
27    InsecureRequestsPolicy, InternalRequest, Origin, ParserMetadata, RedirectMode, Referrer,
28    Request, RequestBody, RequestId, RequestMode, ResponseTainting, UpgradeInsecureNavigations,
29    is_cors_safelisted_method, is_cors_safelisted_request_header,
30};
31use net_traits::response::{Response, ResponseBody, ResponseType, TerminationReason};
32use net_traits::{
33    FetchTaskTarget, NetworkError, ReferrerPolicy, ResourceAttribute, ResourceFetchTiming,
34    ResourceFetchTimingContainer, ResourceTimeValue, ResourceTimingType, WebSocketDomAction,
35    WebSocketNetworkEvent, set_default_accept_language,
36};
37use parking_lot::Mutex;
38use profile_traits::generic_callback::GenericCallback as ProfileGenericCallback;
39use rustc_hash::FxHashMap;
40use rustls_pki_types::CertificateDer;
41use serde::{Deserialize, Serialize};
42use servo_base::generic_channel::CallbackSetter;
43use servo_base::id::PipelineId;
44use servo_url::{Host, ImmutableOrigin, ServoUrl};
45use tokio::sync::Mutex as TokioMutex;
46use tokio::sync::mpsc::{UnboundedReceiver as TokioReceiver, UnboundedSender as TokioSender};
47
48use crate::connector::CACertificates;
49#[cfg(feature = "devtools")]
50use crate::devtools::{DevtoolsBodyBuffer, body_for_devtools};
51use crate::devtools::{
52    send_early_httprequest_to_devtools, send_response_to_devtools, send_security_info_to_devtools,
53};
54use crate::fetch::fetch_params::{
55    ConsumePreloadedResources, FetchParams, SharedPreloadedResources,
56};
57use crate::filemanager_thread::FileManager;
58use crate::http_loader::{HttpState, determine_requests_referrer, http_fetch, set_default_accept};
59use crate::protocols::{ProtocolRegistry, is_url_potentially_trustworthy};
60use crate::request_interceptor::RequestInterceptor;
61use crate::subresource_integrity::is_response_integrity_valid;
62
63pub type Target<'a> = &'a mut (dyn FetchTaskTarget + Send);
64
65#[derive(Clone, Deserialize, Serialize)]
66pub enum Data {
67    Payload(bytes::Bytes),
68    ContentLength(usize),
69    Done,
70    Cancelled,
71    Error(NetworkError),
72}
73
74pub struct WebSocketChannel {
75    pub sender: ProfileGenericCallback<WebSocketNetworkEvent>,
76    pub receiver: Option<CallbackSetter<WebSocketDomAction>>,
77}
78
79impl WebSocketChannel {
80    pub fn new(
81        sender: ProfileGenericCallback<WebSocketNetworkEvent>,
82        receiver: Option<CallbackSetter<WebSocketDomAction>>,
83    ) -> Self {
84        Self { sender, receiver }
85    }
86}
87
88/// Used to keep track of keep-alive requests
89#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
90pub struct InFlightKeepAliveRecord {
91    pub(crate) request_id: RequestId,
92    /// Used to keep track of size of keep-alive requests.
93    pub(crate) keep_alive_body_length: u64,
94}
95
96pub type SharedInflightKeepAliveRecords =
97    Arc<Mutex<FxHashMap<PipelineId, Vec<InFlightKeepAliveRecord>>>>;
98
99#[derive(Clone)]
100pub struct FetchContext {
101    pub state: Arc<HttpState>,
102    pub user_agent: String,
103    #[cfg(feature = "devtools")]
104    pub devtools_chan: Option<crossbeam_channel::Sender<devtools_traits::DevtoolsControlMsg>>,
105    pub filemanager: FileManager,
106    pub file_token: FileTokenCheck,
107    pub request_interceptor: Arc<TokioMutex<RequestInterceptor>>,
108    pub cancellation_listener: Arc<CancellationListener>,
109    pub protocols: Arc<ProtocolRegistry>,
110    pub websocket_chan: Option<Arc<Mutex<WebSocketChannel>>>,
111    pub ca_certificates: CACertificates<'static>,
112    pub ignore_certificate_errors: bool,
113    pub preloaded_resources: SharedPreloadedResources,
114    pub in_flight_keep_alive_records: SharedInflightKeepAliveRecords,
115}
116
117#[derive(Default)]
118pub struct CancellationListener {
119    cancelled: AtomicBool,
120}
121
122impl CancellationListener {
123    pub(crate) fn cancelled(&self) -> bool {
124        self.cancelled.load(Ordering::Relaxed)
125    }
126
127    pub(crate) fn cancel(&self) {
128        self.cancelled.store(true, Ordering::Relaxed)
129    }
130}
131
132/// Closes the current process request body sender state when the net side fetch invocation ends.
133/// Redirect replay for navigation requests happens in a later fetch invocation with a newly
134/// deserialized "RequestBody", so each invocation owns closing only its local copy.
135pub(crate) struct AutoRequestBodyStreamCloser {
136    body: Option<RequestBody>,
137}
138
139impl AutoRequestBodyStreamCloser {
140    pub(crate) fn new(body: Option<&RequestBody>) -> Self {
141        Self {
142            body: body.cloned(),
143        }
144    }
145
146    pub(crate) fn disarm(&mut self) {
147        self.body = None;
148    }
149}
150
151impl Drop for AutoRequestBodyStreamCloser {
152    fn drop(&mut self) {
153        if let Some(body) = self.body.take() {
154            body.close_stream();
155        }
156    }
157}
158
159/// A manual navigation redirect keeps the same request body alive for a later net side redirect
160/// replay invocation. That later invocation becomes the next lifecycle owner and must close its
161/// local shared sender state once it reaches a terminal response.
162pub(crate) fn transfers_request_body_stream_to_later_manual_redirect(
163    request: &Request,
164    response: &Response,
165) -> bool {
166    request.mode == RequestMode::Navigate &&
167        request.redirect_mode == RedirectMode::Manual &&
168        request.body.is_some() &&
169        !response.is_network_error() &&
170        response
171            .actual_response()
172            .status
173            .try_code()
174            .is_some_and(|status| status.is_redirection())
175}
176
177pub type DoneChannel = Option<(TokioSender<Data>, TokioReceiver<Data>)>;
178
179/// [Fetch](https://fetch.spec.whatwg.org#concept-fetch)
180pub async fn fetch(request: Request, target: Target<'_>, context: &FetchContext) -> Response {
181    // Step 7. Let timingInfo be a new fetch timing info whose start time
182    // and post-redirect start time are the coarsened shared current time
183    // given crossOriginIsolatedCapability,
184    // and render-blocking is set to request’s render-blocking.
185    let timing_info: ResourceFetchTimingContainer =
186        ResourceFetchTiming::new(request.timing_type()).into();
187
188    timing_info.set_attributes(&[
189        ResourceAttribute::FetchStart,
190        ResourceAttribute::StartTime(ResourceTimeValue::FetchStart),
191    ]);
192
193    // Step 8. Let fetchParams be a new fetch params whose request is request,
194    // timing info is timingInfo,
195    // process request body chunk length is processRequestBodyChunkLength,
196    // process request end-of-body is processRequestEndOfBody,
197    // process early hints response is processEarlyHintsResponse,
198    // process response is processResponse,
199    // process response consume body is processResponseConsumeBody,
200    // process response end-of-body is processResponseEndOfBody,
201    // task destination is taskDestination,
202    // and cross-origin isolated capability is crossOriginIsolatedCapability.
203    let mut fetch_params = FetchParams::new(request, timing_info);
204    // Each net side fetch invocation owns closing its local deserialized request-body sender state
205    // once this function returns, even if navigation redirect replay later starts a new fetch with
206    // a fresh "RequestBody" copy.
207    let mut request_body_stream_closer =
208        AutoRequestBodyStreamCloser::new(fetch_params.request.body.as_ref());
209    let request = &mut fetch_params.request;
210
211    // Step 4. Populate request from client given request.
212    request.populate_request_from_client();
213
214    // Step 5. If request’s client is non-null, then:
215    // TODO
216    // Step 5.1. Set taskDestination to request’s client’s global object.
217    // TODO
218    // Step 5.2. Set crossOriginIsolatedCapability to request’s client’s cross-origin isolated capability.
219    // TODO
220
221    // Step 10. If all of the following conditions are true:
222    if
223    // - request’s URL’s scheme is an HTTP(S) scheme
224    matches!(request.current_url().scheme(), "http" | "https")
225        // - request’s mode is "same-origin", "cors", or "no-cors"
226        && matches!(request.mode, RequestMode::SameOrigin | RequestMode::CorsMode | RequestMode::NoCors)
227        // - request’s method is `GET`
228        && matches!(request.method, Method::GET)
229        // - request’s unsafe-request flag is not set or request’s header list is empty
230        && (!request.unsafe_request || request.headers.is_empty())
231    {
232        // - request’s client is not null, and request’s client’s global object is a Window object
233        if let Some(client) = request.client.as_ref() {
234            // Step 10.1. Assert: request’s origin is same origin with request’s client’s origin.
235            assert!(request.origin == client.origin);
236            // Step 10.2. Let onPreloadedResponseAvailable be an algorithm that runs the
237            // following step given a response response: set fetchParams’s preloaded response candidate to response.
238            // Step 10.3. Let foundPreloadedResource be the result of invoking consume a preloaded resource
239            // for request’s client, given request’s URL, request’s destination, request’s mode,
240            // request’s credentials mode, request’s integrity metadata, and onPreloadedResponseAvailable.
241            // Step 10.4. If foundPreloadedResource is true and fetchParams’s preloaded response candidate is null,
242            // then set fetchParams’s preloaded response candidate to "pending".
243            if let Some(candidate) =
244                client.consume_preloaded_resource(request, context.preloaded_resources.clone())
245            {
246                fetch_params.preload_response_candidate = candidate;
247            }
248        }
249    }
250
251    // Step 11. If request’s header list does not contain `Accept`, then:
252    set_default_accept(request);
253
254    // Step 12. If request’s header list does not contain `Accept-Language`, then user agents should
255    // append (`Accept-Language, an appropriate header value) to request’s header list.
256    set_default_accept_language(&mut request.headers);
257
258    // Step 15. If request’s internal priority is null, then use request’s priority, initiator,
259    // destination, and render-blocking in an implementation-defined manner to set request’s
260    // internal priority to an implementation-defined object.
261    // TODO: figure out what a Priority object is.
262
263    // Step 15. If request is a subresource request:
264    //
265    // We only check for keep-alive requests here, since that's currently the only usage
266    let should_track_in_flight_record = request.keep_alive && request.is_subresource_request();
267    let pipeline_id = request.pipeline_id;
268
269    if should_track_in_flight_record {
270        // Step 15.1. Let record be a new fetch record whose request is request
271        // and controller is fetchParams’s controller.
272        let record = InFlightKeepAliveRecord {
273            request_id: request.id,
274            keep_alive_body_length: request.keep_alive_body_length(),
275        };
276        // Step 15.2. Append record to request’s client’s fetch group’s fetch records.
277        let mut in_flight_records = context.in_flight_keep_alive_records.lock();
278        in_flight_records
279            .entry(pipeline_id.expect("Must always set a pipeline ID for keep-alive requests"))
280            .or_default()
281            .push(record);
282    };
283    let request_id = request.id;
284
285    // Step 17: Run main fetch given fetchParams.
286    let response = main_fetch(&mut fetch_params, false, target, &mut None, context).await;
287
288    if transfers_request_body_stream_to_later_manual_redirect(&fetch_params.request, &response) {
289        request_body_stream_closer.disarm();
290    }
291
292    // Mimics <https://fetch.spec.whatwg.org/#done-flag>
293    if should_track_in_flight_record {
294        context
295            .in_flight_keep_alive_records
296            .lock()
297            .get_mut(&pipeline_id.expect("Must always set a pipeline ID for keep-alive requests"))
298            .expect("Must always have initialized tracked requests before starting fetch")
299            .retain(|record| record.request_id != request_id);
300    }
301
302    // Step 18: Return fetchParams’s controller.
303    // TODO: We don't implement fetchParams as defined in the spec
304    response
305}
306
307pub(crate) fn convert_request_to_csp_request(request: &Request) -> Option<csp::Request> {
308    if request.is_internal_request == InternalRequest::Yes {
309        return None;
310    }
311    let origin = match &request.origin {
312        Origin::Client => return None,
313        Origin::Origin(origin) => origin,
314    };
315
316    let csp_request = csp::Request {
317        url: request.url().into_url(),
318        current_url: request.current_url().into_url(),
319        origin: origin.clone().into_url_origin(),
320        redirect_count: request.redirect_count,
321        destination: request.destination,
322        initiator: match request.initiator {
323            Initiator::Download => csp::Initiator::Download,
324            Initiator::ImageSet => csp::Initiator::ImageSet,
325            Initiator::Manifest => csp::Initiator::Manifest,
326            Initiator::Prefetch => csp::Initiator::Prefetch,
327            _ => csp::Initiator::None,
328        },
329        nonce: request.cryptographic_nonce_metadata.clone(),
330        integrity_metadata: request.integrity_metadata.clone(),
331        parser_metadata: match request.parser_metadata {
332            ParserMetadata::ParserInserted => csp::ParserMetadata::ParserInserted,
333            ParserMetadata::NotParserInserted => csp::ParserMetadata::NotParserInserted,
334            ParserMetadata::Default => csp::ParserMetadata::None,
335        },
336    };
337    Some(csp_request)
338}
339
340/// <https://www.w3.org/TR/CSP/#should-block-request>
341pub fn should_request_be_blocked_by_csp(
342    csp_request: &csp::Request,
343    policy_container: &PolicyContainer,
344) -> (csp::CheckResult, Vec<csp::Violation>) {
345    policy_container
346        .csp_list
347        .as_ref()
348        .map(|c| c.should_request_be_blocked(csp_request))
349        .unwrap_or((csp::CheckResult::Allowed, Vec::new()))
350}
351
352/// <https://www.w3.org/TR/CSP/#report-for-request>
353pub fn report_violations_for_request_by_csp(
354    csp_request: &csp::Request,
355    policy_container: &PolicyContainer,
356) -> Vec<csp::Violation> {
357    policy_container
358        .csp_list
359        .as_ref()
360        .map(|c| c.report_violations_for_request(csp_request))
361        .unwrap_or_default()
362}
363
364fn should_response_be_blocked_by_csp(
365    csp_request: &csp::Request,
366    response: &Response,
367    policy_container: &PolicyContainer,
368) -> (csp::CheckResult, Vec<csp::Violation>) {
369    if response.is_network_error() {
370        return (csp::CheckResult::Allowed, Vec::new());
371    }
372    let csp_response = csp::Response {
373        url: response
374            .actual_response()
375            .url()
376            .cloned()
377            // NOTE(pylbrecht): for WebSocket connections, the URL scheme is converted to http(s)
378            // to integrate with fetch(). We need to convert it back to ws(s) to get valid CSP
379            // checks.
380            // https://github.com/w3c/webappsec-csp/issues/532
381            .map(|mut url| {
382                match csp_request.url.scheme() {
383                    "ws" | "wss" => {
384                        url.as_mut_url()
385                            .set_scheme(csp_request.url.scheme())
386                            .expect("failed to set URL scheme");
387                    },
388                    _ => {},
389                };
390                url
391            })
392            .expect("response must have a url")
393            .into_url(),
394        redirect_count: csp_request.redirect_count,
395    };
396    policy_container
397        .csp_list
398        .as_ref()
399        .map(|c| c.should_response_to_request_be_blocked(csp_request, &csp_response))
400        .unwrap_or((csp::CheckResult::Allowed, Vec::new()))
401}
402
403/// [Main fetch](https://fetch.spec.whatwg.org/#concept-main-fetch)
404pub async fn main_fetch(
405    fetch_params: &mut FetchParams,
406    recursive_flag: bool,
407    target: Target<'_>,
408    done_chan: &mut DoneChannel,
409    context: &FetchContext,
410) -> Response {
411    // Step 1: Let request be fetchParam's request.
412    let request = &mut fetch_params.request;
413    send_early_httprequest_to_devtools(request, context);
414    // Step 2: Let response be null.
415    let mut response = None;
416
417    // Servo internal: return a crash error when a crash error page is needed
418    if let Some(ref details) = request.crash {
419        response = Some(Response::network_error(NetworkError::Crash(
420            details.clone(),
421        )));
422    }
423
424    // Step 3: If request’s local-URLs-only flag is set and request’s
425    // current URL is not local, then set response to a network error.
426    if request.local_urls_only &&
427        !matches!(
428            request.current_url().scheme(),
429            "about" | "blob" | "data" | "filesystem"
430        )
431    {
432        response = Some(Response::network_error(NetworkError::UnsupportedScheme));
433    }
434
435    // The request should have a valid policy_container associated with it.
436    // Step 4. Run report Content Security Policy violations for request.
437    let csp_request = convert_request_to_csp_request(request);
438    if let Some(csp_request) = csp_request.as_ref() {
439        // Step 2.2.
440        let violations = report_violations_for_request_by_csp(
441            csp_request,
442            request
443                .policy_container
444                .policy_container()
445                .expect("Always has a valid policycontainer"),
446        );
447
448        if !violations.is_empty() {
449            target.process_csp_violations(request, violations);
450        }
451    };
452
453    // Step 5. Upgrade request to a potentially trustworthy URL, if appropriate.
454    // Step 6. Upgrade a mixed content request to a potentially trustworthy URL, if appropriate.
455    if should_upgrade_request_to_potentially_trustworthy(request, context) ||
456        should_upgrade_mixed_content_request(request, &context.protocols)
457    {
458        trace!(
459            "upgrading {} targeting {:?}",
460            request.current_url(),
461            request.destination
462        );
463        if let Some(new_scheme) = match request.current_url().scheme() {
464            "http" => Some("https"),
465            "ws" => Some("wss"),
466            _ => None,
467        } {
468            request
469                .current_url_mut()
470                .as_mut_url()
471                .set_scheme(new_scheme)
472                .unwrap();
473        }
474    } else {
475        let insecure_requests_policy = request
476            .client
477            .as_ref()
478            .map(|client| client.insecure_requests_policy);
479        trace!(
480            "not upgrading {} targeting {:?} with {:?}",
481            request.current_url(),
482            request.destination,
483            insecure_requests_policy
484        );
485    }
486    if let Some(csp_request) = csp_request.as_ref() {
487        // Step 7. If should request be blocked due to a bad port, should fetching request be blocked
488        // as mixed content, or should request be blocked by Content Security Policy returns blocked,
489        // then set response to a network error.
490
491        let (check_result, violations) = should_request_be_blocked_by_csp(
492            csp_request,
493            request
494                .policy_container
495                .policy_container()
496                .expect("Always has a valid policy container"),
497        );
498
499        if !violations.is_empty() {
500            target.process_csp_violations(request, violations);
501        }
502
503        if check_result == csp::CheckResult::Blocked {
504            warn!("Request blocked by CSP");
505            response = Some(Response::network_error(NetworkError::ContentSecurityPolicy))
506        }
507    };
508    if should_request_be_blocked_due_to_a_bad_port(&request.current_url()) {
509        response = Some(Response::network_error(NetworkError::InvalidPort));
510    }
511    if should_request_be_blocked_as_mixed_content(request, &context.protocols) {
512        response = Some(Response::network_error(NetworkError::MixedContent));
513    }
514
515    // Step 8: If request’s referrer policy is the empty string, then set request’s referrer policy
516    // to request’s policy container’s referrer policy.
517    if request.referrer_policy == ReferrerPolicy::EmptyString {
518        request.referrer_policy = request
519            .policy_container
520            .policy_container()
521            .expect("Always has a valid policycontainer")
522            .get_referrer_policy();
523    }
524
525    // Step 9, If request’s referrer is not "no-referrer", then set request’s referrer to the result
526    // of invoking determine request’s referrer.
527    let referrer_url = match mem::replace(&mut request.referrer, Referrer::NoReferrer) {
528        Referrer::NoReferrer => None,
529        Referrer::ReferrerUrl(referrer_source) | Referrer::Client(referrer_source) => {
530            request.headers.remove(header::REFERER);
531            determine_requests_referrer(
532                request.referrer_policy,
533                referrer_source,
534                request.current_url(),
535            )
536        },
537    };
538    request.referrer = referrer_url.map_or(Referrer::NoReferrer, Referrer::ReferrerUrl);
539
540    // Step 10.
541    context
542        .state
543        .hsts_list
544        .read()
545        .apply_hsts_rules(request.current_url_mut());
546
547    // Step 11. If recursive is false, then run the remaining steps in parallel.
548    // Not applicable: see fetch_async.
549
550    let current_url = request.current_url();
551    let current_scheme = current_url.scheme();
552
553    // Intercept the request and maybe override the response.
554    context
555        .request_interceptor
556        .lock()
557        .await
558        .intercept_request(request, &mut response, &fetch_params.timing_info)
559        .await;
560
561    let mut response = match response {
562        Some(response) => response,
563        // Step 12. If response is null, then set response to the result
564        // of running the steps corresponding to the first matching statement:
565        None => {
566            let same_origin = if let Origin::Origin(ref origin) = request.origin {
567                *origin == request.current_url_with_blob_claim().origin()
568            } else {
569                false
570            };
571
572            // fetchParams’s preloaded response candidate is non-null
573            if let Some((response, preload_id)) =
574                fetch_params.preload_response_candidate.response().await
575            {
576                response.get_resource_timing().inner().preloaded = true;
577                context
578                    .preloaded_resources
579                    .lock()
580                    .unwrap()
581                    .remove(&preload_id);
582                response
583            }
584            // request's current URL's origin is same origin with request's origin, and request's
585            // response tainting is "basic"
586            else if (same_origin && request.response_tainting == ResponseTainting::Basic) ||
587                // request's current URL's scheme is "data"
588                current_scheme == "data" ||
589                // Note: Although it is not part of the specification, we make an exception here
590                // for custom protocols that are explicitly marked as active for fetch.
591                context.protocols.is_fetchable(current_scheme) ||
592                // request's mode is "navigate" or "websocket"
593                matches!(
594                    request.mode,
595                    RequestMode::Navigate | RequestMode::WebSocket { .. }
596                )
597            {
598                // Substep 1. Set request's response tainting to "basic".
599                request.response_tainting = ResponseTainting::Basic;
600
601                // Substep 2. Return the result of running scheme fetch given fetchParams.
602                scheme_fetch(fetch_params, target, done_chan, context).await
603            } else if request.mode == RequestMode::SameOrigin {
604                Response::network_error(NetworkError::CrossOriginResponse)
605            } else if request.mode == RequestMode::NoCors {
606                // Substep 1. If request's redirect mode is not "follow", then return a network error.
607                if request.redirect_mode != RedirectMode::Follow {
608                    Response::network_error(NetworkError::RedirectError)
609                } else {
610                    // Substep 2. Set request's response tainting to "opaque".
611                    request.response_tainting = ResponseTainting::Opaque;
612
613                    // Substep 3. Return the result of running scheme fetch given fetchParams.
614                    scheme_fetch(fetch_params, target, done_chan, context).await
615                }
616            } else if !matches!(current_scheme, "http" | "https") {
617                Response::network_error(NetworkError::UnsupportedScheme)
618            } else if request.use_cors_preflight ||
619                (request.unsafe_request &&
620                    (!is_cors_safelisted_method(&request.method) ||
621                        request.headers.iter().any(|(name, value)| {
622                            !is_cors_safelisted_request_header(&name, &value)
623                        })))
624            {
625                // Substep 1. Set request’s response tainting to "cors".
626                request.response_tainting = ResponseTainting::CorsTainting;
627
628                let origin = request.origin.clone();
629                let url = request.current_url();
630
631                // Substep 2. Let corsWithPreflightResponse be the result of running override fetch
632                // given "http-fetch", fetchParams, and true.
633                let cors_with_preflight_response =
634                    http_fetch(fetch_params, true, true, false, target, done_chan, context).await;
635                // Substep 3. If corsWithPreflightResponse is a network error, then clear cache entries using request.
636                if cors_with_preflight_response.is_network_error() {
637                    context
638                        .state
639                        .cors_cache
640                        .write()
641                        .clear_entries_for_origin_and_url(&origin, &url);
642                }
643                // Substep 4. Return corsWithPreflightResponse.
644                cors_with_preflight_response
645            } else {
646                // Substep 1. Set request’s response tainting to "cors".
647                request.response_tainting = ResponseTainting::CorsTainting;
648
649                // Substep 2. Return the result of running override fetch given "http-fetch" and fetchParams.
650                http_fetch(fetch_params, true, false, false, target, done_chan, context).await
651            }
652        },
653    };
654
655    // Step 13. If recursive is true, then return response.
656    if recursive_flag {
657        return response;
658    }
659
660    // reborrow request to avoid double mutable borrow
661    let request = &mut fetch_params.request;
662
663    // Step 14. If response is not a network error and response is not a filtered response, then:
664    if !response.is_network_error() && response.internal_response.is_none() {
665        // Step 14.1 If request’s response tainting is "cors", then:
666        if request.response_tainting == ResponseTainting::CorsTainting {
667            // Step 14.1.1 Let headerNames be the result of extracting header list values given
668            // `Access-Control-Expose-Headers` and response’s header list.
669            let header_names: Option<Vec<HeaderName>> = response
670                .headers
671                .typed_get::<AccessControlExposeHeaders>()
672                .map(|v| v.iter().collect());
673
674            if let Some(ref list) = header_names {
675                // Step 14.1.2. If request’s credentials mode is not "include" and headerNames
676                // contains `*`, then set response’s CORS-exposed header-name list to all unique
677                // header names in response’s header list.
678                if request.credentials_mode != CredentialsMode::Include &&
679                    list.iter().any(|header| header == "*")
680                {
681                    response.cors_exposed_header_name_list = response
682                        .headers
683                        .iter()
684                        .map(|(name, _)| name.as_str().to_owned())
685                        .collect();
686                } else {
687                    // Step 14.1.3. Otherwise, if headerNames is non-null or failure, then set
688                    // response’s CORS-exposed header-name list to headerNames.
689                    response.cors_exposed_header_name_list =
690                        list.iter().map(|h| h.as_str().to_owned()).collect();
691                }
692            }
693        }
694
695        // Step 14.2 Set response to the following filtered response with response as its internal response,
696        // depending on request’s response tainting:
697        let response_type = match request.response_tainting {
698            ResponseTainting::Basic => ResponseType::Basic,
699            ResponseTainting::CorsTainting => ResponseType::Cors,
700            ResponseTainting::Opaque => ResponseType::Opaque,
701        };
702        response = response.to_filtered(response_type);
703    }
704
705    let internal_error = {
706        // Tests for steps 17 and 18, before step 15 for borrowing concerns.
707        let response_is_network_error = response.is_network_error();
708        let should_replace_with_nosniff_error = !response_is_network_error &&
709            should_be_blocked_due_to_nosniff(request.destination, &response.headers);
710        let should_replace_with_mime_type_error = !response_is_network_error &&
711            should_be_blocked_due_to_mime_type(request.destination, &response.headers);
712        let should_replace_with_mixed_content = !response_is_network_error &&
713            should_response_be_blocked_as_mixed_content(request, &response, &context.protocols);
714        let should_replace_with_csp_error = csp_request.is_some_and(|csp_request| {
715            let (check_result, violations) = should_response_be_blocked_by_csp(
716                &csp_request,
717                &response,
718                request
719                    .policy_container
720                    .policy_container()
721                    .expect("Always has a valid policy container"),
722            );
723            if !violations.is_empty() {
724                target.process_csp_violations(request, violations);
725            }
726            check_result == csp::CheckResult::Blocked
727        });
728
729        // Step 15.
730        let mut network_error_response = response
731            .get_network_error()
732            .cloned()
733            .map(Response::network_error);
734
735        // Step 15. Let internalResponse be response, if response is a network error;
736        // otherwise response’s internal response.
737        let response_is_opaque = response.response_type == ResponseType::Opaque;
738        let internal_response = if let Some(error_response) = network_error_response.as_mut() {
739            error_response
740        } else {
741            response.actual_response_mut()
742        };
743
744        // Step 16. If internalResponse’s URL list is empty, then set it to a clone of request’s URL list.
745        if internal_response.url_list.is_empty() {
746            internal_response.url_list = request
747                .url_list
748                .iter()
749                .map(|locked_url| locked_url.url())
750                .collect();
751        }
752
753        // Step 17. Set internalResponse’s redirect taint to request’s redirect-taint.
754        internal_response.redirect_taint = request.redirect_taint_for_request();
755
756        // TODO Step 18. If request is a navigation request, then set internalResponse’s navigation
757        // timing allow values list to a clone of request’s navigation timing allow values list.
758
759        // TODO Step 19. If request’s timing allow failed flag is unset, then set internalResponse’s
760        // timing allow passed flag.
761
762        // Step 20. If response is not a network error and any of the following returns blocked
763        // * should internalResponse to request be blocked as mixed content
764        // * should internalResponse to request be blocked by Content Security Policy
765        // * should internalResponse to request be blocked due to its MIME type
766        // * should internalResponse to request be blocked due to nosniff
767        let mut blocked_error_response;
768
769        let internal_response = if should_replace_with_nosniff_error {
770            // Defer rebinding result
771            blocked_error_response = Response::network_error(NetworkError::Nosniff);
772            &blocked_error_response
773        } else if should_replace_with_mime_type_error {
774            // Defer rebinding result
775            blocked_error_response =
776                Response::network_error(NetworkError::MimeType("Blocked by MIME type".into()));
777            &blocked_error_response
778        } else if should_replace_with_mixed_content {
779            blocked_error_response = Response::network_error(NetworkError::MixedContent);
780            &blocked_error_response
781        } else if should_replace_with_csp_error {
782            blocked_error_response = Response::network_error(NetworkError::ContentSecurityPolicy);
783            &blocked_error_response
784        } else {
785            internal_response
786        };
787
788        // Step 21. If response’s type is "opaque", internalResponse’s status is a range status,
789        // internalResponse’s range-requested flag is set, and request’s header list does not
790        // contain `Range`, then set response and internalResponse to a network error.
791        // Also checking if internal response is a network error to prevent crash from attemtping to
792        // read status of a network error if we blocked the request above.
793        let internal_response = if !internal_response.is_network_error() &&
794            response_is_opaque &&
795            internal_response.status.is_a_range_status() &&
796            internal_response.range_requested &&
797            !request.headers.contains_key(RANGE)
798        {
799            // Defer rebinding result
800            blocked_error_response =
801                Response::network_error(NetworkError::PartialResponseToNonRangeRequestError);
802            &blocked_error_response
803        } else {
804            internal_response
805        };
806
807        // Step 22. If response is not a network error and either request’s method is `HEAD` or `CONNECT`,
808        // or internalResponse’s status is a null body status, set internalResponse’s body to null and
809        // disregard any enqueuing toward it (if any).
810        // NOTE: We check `internal_response` since we did not mutate `response` in the previous steps.
811        let not_network_error = !response_is_network_error && !internal_response.is_network_error();
812        if not_network_error &&
813            (is_null_body_status(&internal_response.status) ||
814                matches!(request.method, Method::HEAD | Method::CONNECT))
815        {
816            // when Fetch is used only asynchronously, we will need to make sure
817            // that nothing tries to write to the body at this point
818            let mut body = internal_response.body.lock();
819            *body = ResponseBody::Empty;
820        }
821
822        internal_response.get_network_error().cloned()
823    };
824
825    // Execute deferred rebinding of response.
826    if let Some(error) = internal_error {
827        response = Response::network_error(error);
828    }
829
830    // Step 19. If response is not a network error and any of the following returns blocked
831    let mut response_loaded = false;
832    let mut response = if !response.is_network_error() && !request.integrity_metadata.is_empty() {
833        // Step 19.1.
834        wait_for_response(request, &mut response, target, done_chan, context).await;
835        response_loaded = true;
836
837        // Step 19.2.
838        let integrity_metadata = &request.integrity_metadata;
839        if response.termination_reason.is_none() &&
840            !is_response_integrity_valid(integrity_metadata, &response)
841        {
842            Response::network_error(NetworkError::SubresourceIntegrity)
843        } else {
844            response
845        }
846    } else {
847        response
848    };
849
850    // Step 20.
851    if request.synchronous {
852        // process_response is not supposed to be used
853        // by sync fetch, but we overload it here for simplicity
854        target.process_response(request, &response);
855        if !response_loaded {
856            wait_for_response(request, &mut response, target, done_chan, context).await;
857        }
858        // overloaded similarly to process_response
859        target.process_response_eof(request, &response);
860        return response;
861    }
862
863    // Step 21.
864    if request.body.is_some() && matches!(current_scheme, "http" | "https") {
865        // XXXManishearth: We actually should be calling process_request
866        // in http_network_fetch. However, we can't yet follow the request
867        // upload progress, so I'm keeping it here for now and pretending
868        // the body got sent in one chunk
869        target.process_request_body(request);
870    }
871
872    // Step 22.
873    target.process_response(request, &response);
874    // Send Response to Devtools
875    send_response_to_devtools(request, context, &response, None);
876    send_security_info_to_devtools(request, context, &response);
877
878    // Step 23.
879    if !response_loaded {
880        wait_for_response(request, &mut response, target, done_chan, context).await;
881    }
882
883    // Step 24.
884    target.process_response_eof(request, &response);
885    // Send Response to Devtools
886    // This is done after process_response_eof to ensure that the body is fully
887    // processed before sending the response to Devtools.
888    send_response_to_devtools(request, context, &response, None);
889
890    context
891        .state
892        .http_cache
893        .update_awaiting_consumers(request, &response)
894        .await;
895
896    // Steps 25-27.
897    // TODO: remove this line when only asynchronous fetches are used
898    response
899}
900
901async fn wait_for_response(
902    request: &Request,
903    response: &mut Response,
904    target: Target<'_>,
905    done_chan: &mut DoneChannel,
906    context: &FetchContext,
907) {
908    if let Some(ref mut ch) = *done_chan {
909        #[cfg(feature = "devtools")]
910        let mut devtools_body = context
911            .devtools_chan
912            .as_ref()
913            .map(|_| DevtoolsBodyBuffer::default());
914        loop {
915            match ch.1.recv().await {
916                Some(Data::ContentLength(length)) => {
917                    target.process_response_length_hint(request, length);
918                },
919                Some(Data::Payload(bytes)) => {
920                    #[cfg(feature = "devtools")]
921                    if let Some(body) = devtools_body.as_mut() {
922                        body.append_chunk(&bytes);
923                    }
924                    target.process_response_chunk(request, bytes);
925                },
926                Some(Data::Error(network_error)) => {
927                    if network_error == NetworkError::DecompressionError {
928                        response.termination_reason = Some(TerminationReason::Fatal);
929                    }
930                    response.set_network_error(network_error);
931
932                    break;
933                },
934                Some(Data::Done) => {
935                    #[cfg(feature = "devtools")]
936                    let devtools_response_body =
937                        devtools_body.map(DevtoolsBodyBuffer::into_response_body);
938                    #[cfg(feature = "devtools")]
939                    send_response_to_devtools(request, context, response, devtools_response_body);
940                    break;
941                },
942                Some(Data::Cancelled) => {
943                    response.aborted.store(true, Ordering::Release);
944                    break;
945                },
946
947                None => {
948                    panic!("fetch worker should always send Done before terminating");
949                },
950            }
951        }
952    } else {
953        match *response.actual_response().body.lock() {
954            ResponseBody::Done(ref vec) if !vec.is_empty() => {
955                // in case there was no channel to wait for, the body was
956                // obtained synchronously via scheme_fetch for data/file/about/etc
957                // We should still send the body across as a chunk
958                target.process_response_chunk(request, Bytes::copy_from_slice(vec));
959                #[cfg(feature = "devtools")]
960                if context.devtools_chan.is_some() {
961                    // Now that we've replayed the entire cached body,
962                    // notify the DevTools server with the full Response.
963                    let (data, truncated, content_size) = body_for_devtools(vec);
964                    send_response_to_devtools(
965                        request,
966                        context,
967                        response,
968                        Some(devtools_traits::HttpResponseBody {
969                            data: Bytes::from(data),
970                            truncated,
971                            content_size,
972                        }),
973                    );
974                }
975            },
976            ResponseBody::Done(_) | ResponseBody::Empty => {},
977            _ => unreachable!(),
978        }
979    }
980}
981
982/// Range header start and end values.
983pub enum RangeRequestBounds {
984    /// The range bounds are known and set to final values.
985    Final(RelativePos),
986    /// We need extra information to set the range bounds.
987    /// i.e. buffer or file size.
988    Pending(u64),
989}
990
991impl RangeRequestBounds {
992    pub fn get_final(&self, len: Option<u64>) -> Result<RelativePos, &'static str> {
993        match self {
994            RangeRequestBounds::Final(pos) => {
995                if let Some(len) = len &&
996                    pos.start <= len as i64
997                {
998                    return Ok(*pos);
999                }
1000                Err("Tried to process RangeRequestBounds::Final without len")
1001            },
1002            RangeRequestBounds::Pending(offset) => Ok(RelativePos::from_opts(
1003                if let Some(len) = len {
1004                    Some((len - u64::min(len, *offset)) as i64)
1005                } else {
1006                    Some(0)
1007                },
1008                None,
1009            )),
1010        }
1011    }
1012}
1013
1014fn create_blank_reply(url: ServoUrl, timing_type: ResourceTimingType) -> Response {
1015    let mut response = Response::new(url, ResourceFetchTiming::new(timing_type));
1016    response
1017        .headers
1018        .typed_insert(ContentType::from(mime::TEXT_HTML_UTF_8));
1019    *response.body.lock() = ResponseBody::Done(vec![]);
1020    response.status = HttpStatus::default();
1021    response
1022}
1023
1024fn create_about_memory(url: ServoUrl, timing_type: ResourceTimingType) -> Response {
1025    let mut response = Response::new(url, ResourceFetchTiming::new(timing_type));
1026    response
1027        .headers
1028        .typed_insert(ContentType::from(mime::TEXT_HTML_UTF_8));
1029    *response.body.lock() =
1030        ResponseBody::Done(resources::read_bytes(Resource::AboutMemoryHTML).into_vec());
1031    response.status = HttpStatus::default();
1032    response
1033}
1034
1035/// Handle a request from the user interface to ignore validation errors for a certificate.
1036fn handle_allowcert_request(request: &mut Request, context: &FetchContext) -> io::Result<()> {
1037    let error = |string| Err(io::Error::other(string));
1038
1039    let body = match request.body.as_mut() {
1040        Some(body) => body,
1041        None => return error("No body found"),
1042    };
1043
1044    let stream = body.clone_stream();
1045    let mut stream = stream.lock();
1046    let (body_chan, body_port) = ipc::channel().unwrap();
1047    let Some(chunk_requester) = stream.as_mut() else {
1048        log::error!(
1049            "Could not connect to the request body stream because it has already been closed."
1050        );
1051        return Err(std::io::Error::other("Could not send BodyChunkRequest"));
1052    };
1053    chunk_requester
1054        .send(BodyChunkRequest::Connect(body_chan))
1055        .map_err(|error| {
1056            log::error!(
1057                "Could not connect to the request body stream because it has already been closed: {error}"
1058            );
1059            std::io::Error::other("Could not connect to request body stream")
1060        })?;
1061    chunk_requester
1062        .send(BodyChunkRequest::Chunk)
1063        .map_err(|error| {
1064            log::error!(
1065                "Could not request the first request body chunk because the body stream has already been closed: {error}"
1066            );
1067            std::io::Error::other("Could not request request body chunk")
1068        })?;
1069    let body_bytes = match body_port.recv().ok() {
1070        Some(BodyChunkResponse::Chunk(bytes)) => bytes,
1071        _ => return error("Certificate not sent in a single chunk"),
1072    };
1073
1074    let split_idx = match body_bytes.iter().position(|b| *b == b'&') {
1075        Some(split_idx) => split_idx,
1076        None => return error("Could not find ampersand in data"),
1077    };
1078    let (secret, cert_base64) = body_bytes.split_at(split_idx);
1079
1080    let secret = str::from_utf8(secret).ok().and_then(|s| s.parse().ok());
1081    if secret != Some(*net_traits::PRIVILEGED_SECRET) {
1082        return error("Invalid secret sent. Ignoring request");
1083    }
1084
1085    let cert_bytes = match general_purpose::STANDARD_NO_PAD.decode(&cert_base64[1..]) {
1086        Ok(bytes) => bytes,
1087        Err(_) => return error("Could not decode certificate base64"),
1088    };
1089
1090    context
1091        .state
1092        .override_manager
1093        .add_override(&CertificateDer::from_slice(&cert_bytes).into_owned());
1094    Ok(())
1095}
1096
1097/// [Scheme fetch](https://fetch.spec.whatwg.org#scheme-fetch)
1098async fn scheme_fetch(
1099    fetch_params: &mut FetchParams,
1100    target: Target<'_>,
1101    done_chan: &mut DoneChannel,
1102    context: &FetchContext,
1103) -> Response {
1104    // Step 1: If fetchParams is canceled, then return the appropriate network error for fetchParams.
1105
1106    // Step 2: Let request be fetchParams’s request.
1107    let request = &mut fetch_params.request;
1108    let url_and_blob_lock = request.current_url_with_blob_claim();
1109
1110    let scheme = url_and_blob_lock.scheme();
1111    match scheme {
1112        "about" if url_and_blob_lock.path() == "blank" => {
1113            create_blank_reply(url_and_blob_lock.url(), request.timing_type())
1114        },
1115        "about" if url_and_blob_lock.path() == "memory" => {
1116            create_about_memory(url_and_blob_lock.url(), request.timing_type())
1117        },
1118
1119        "chrome" if url_and_blob_lock.path() == "allowcert" => {
1120            if let Err(error) = handle_allowcert_request(request, context) {
1121                warn!("Could not handle allowcert request: {error}");
1122            }
1123            create_blank_reply(url_and_blob_lock.url(), request.timing_type())
1124        },
1125
1126        "http" | "https" => {
1127            http_fetch(
1128                fetch_params,
1129                false,
1130                false,
1131                false,
1132                target,
1133                done_chan,
1134                context,
1135            )
1136            .await
1137        },
1138
1139        _ => match context.protocols.get(scheme) {
1140            Some(handler) => handler.load(request, done_chan, context).await,
1141            None => Response::network_error(NetworkError::UnsupportedScheme),
1142        },
1143    }
1144}
1145
1146fn is_null_body_status(status: &HttpStatus) -> bool {
1147    matches!(
1148        status.try_code(),
1149        Some(StatusCode::SWITCHING_PROTOCOLS) |
1150            Some(StatusCode::NO_CONTENT) |
1151            Some(StatusCode::RESET_CONTENT) |
1152            Some(StatusCode::NOT_MODIFIED)
1153    )
1154}
1155
1156/// <https://fetch.spec.whatwg.org/#should-response-to-request-be-blocked-due-to-nosniff?>
1157pub fn should_be_blocked_due_to_nosniff(
1158    destination: Destination,
1159    response_headers: &HeaderMap,
1160) -> bool {
1161    // Step 1
1162    if !determine_nosniff(response_headers) {
1163        return false;
1164    }
1165
1166    // Step 2
1167    // Note: an invalid MIME type will produce a `None`.
1168    let mime_type = extract_mime_type_as_mime(response_headers);
1169
1170    /// <https://html.spec.whatwg.org/multipage/#scriptingLanguages>
1171    #[inline]
1172    fn is_javascript_mime_type(mime_type: &Mime) -> bool {
1173        let javascript_mime_types: [Mime; 16] = [
1174            "application/ecmascript".parse().unwrap(),
1175            "application/javascript".parse().unwrap(),
1176            "application/x-ecmascript".parse().unwrap(),
1177            "application/x-javascript".parse().unwrap(),
1178            "text/ecmascript".parse().unwrap(),
1179            "text/javascript".parse().unwrap(),
1180            "text/javascript1.0".parse().unwrap(),
1181            "text/javascript1.1".parse().unwrap(),
1182            "text/javascript1.2".parse().unwrap(),
1183            "text/javascript1.3".parse().unwrap(),
1184            "text/javascript1.4".parse().unwrap(),
1185            "text/javascript1.5".parse().unwrap(),
1186            "text/jscript".parse().unwrap(),
1187            "text/livescript".parse().unwrap(),
1188            "text/x-ecmascript".parse().unwrap(),
1189            "text/x-javascript".parse().unwrap(),
1190        ];
1191
1192        javascript_mime_types
1193            .iter()
1194            .any(|mime| mime.type_() == mime_type.type_() && mime.subtype() == mime_type.subtype())
1195    }
1196
1197    match mime_type {
1198        // Step 4
1199        Some(ref mime_type) if destination.is_script_like() => !is_javascript_mime_type(mime_type),
1200        // Step 5
1201        Some(ref mime_type) if destination == Destination::Style => {
1202            mime_type.type_() != mime::TEXT || mime_type.subtype() != mime::CSS
1203        },
1204
1205        None if destination == Destination::Style || destination.is_script_like() => true,
1206        // Step 6
1207        _ => false,
1208    }
1209}
1210
1211/// <https://fetch.spec.whatwg.org/#should-response-to-request-be-blocked-due-to-mime-type?>
1212fn should_be_blocked_due_to_mime_type(
1213    destination: Destination,
1214    response_headers: &HeaderMap,
1215) -> bool {
1216    // Step 1: Let mimeType be the result of extracting a MIME type from response’s header list.
1217    let mime_type: mime::Mime = match extract_mime_type_as_mime(response_headers) {
1218        Some(mime_type) => mime_type,
1219        // Step 2: If mimeType is failure, then return allowed.
1220        None => return false,
1221    };
1222
1223    // Step 3: Let destination be request’s destination.
1224    // Step 4: If destination is script-like and one of the following is true, then return blocked:
1225    //    - mimeType’s essence starts with "audio/", "image/", or "video/".
1226    //    - mimeType’s essence is "text/csv".
1227    // Step 5: Return allowed.
1228    destination.is_script_like() &&
1229        match mime_type.type_() {
1230            mime::AUDIO | mime::VIDEO | mime::IMAGE => true,
1231            mime::TEXT if mime_type.subtype() == mime::CSV => true,
1232            _ => false,
1233        }
1234}
1235
1236/// <https://fetch.spec.whatwg.org/#block-bad-port>
1237pub fn should_request_be_blocked_due_to_a_bad_port(url: &ServoUrl) -> bool {
1238    // Step 1. Let url be request’s current URL.
1239    // NOTE: We receive the request url as an argument
1240
1241    // Step 2. If url’s scheme is an HTTP(S) scheme and url’s port is a bad port, then return blocked.
1242    let is_http_scheme = matches!(url.scheme(), "http" | "https");
1243    let is_bad_port = url.port().is_some_and(is_bad_port);
1244    if is_http_scheme && is_bad_port {
1245        return true;
1246    }
1247
1248    // Step 3. Return allowed.
1249    false
1250}
1251
1252/// <https://w3c.github.io/webappsec-mixed-content/#should-block-fetch>
1253pub fn should_request_be_blocked_as_mixed_content(
1254    request: &Request,
1255    protocol_registry: &ProtocolRegistry,
1256) -> bool {
1257    // Step 1. Return allowed if one or more of the following conditions are met:
1258    // 1.1. Does settings prohibit mixed security contexts?
1259    // returns "Does Not Restrict Mixed Security Contexts" when applied to request’s client.
1260    if do_settings_prohibit_mixed_security_contexts(request) ==
1261        MixedSecurityProhibited::NotProhibited
1262    {
1263        return false;
1264    }
1265
1266    // 1.2. request’s URL is a potentially trustworthy URL.
1267    if is_url_potentially_trustworthy(protocol_registry, &request.current_url()) {
1268        return false;
1269    }
1270
1271    // 1.3. The user agent has been instructed to allow mixed content.
1272
1273    // 1.4. request’s destination is "document", and request’s target browsing context has
1274    // no parent browsing context.
1275    if request.destination == Destination::Document {
1276        // TODO: request's target browsing context has no parent browsing context
1277        return false;
1278    }
1279
1280    true
1281}
1282
1283/// <https://w3c.github.io/webappsec-mixed-content/#should-block-response>
1284pub fn should_response_be_blocked_as_mixed_content(
1285    request: &Request,
1286    response: &Response,
1287    protocol_registry: &ProtocolRegistry,
1288) -> bool {
1289    // Step 1. Return allowed if one or more of the following conditions are met:
1290    // 1.1. Does settings prohibit mixed security contexts? returns Does Not Restrict Mixed Content
1291    // when applied to request’s client.
1292    if do_settings_prohibit_mixed_security_contexts(request) ==
1293        MixedSecurityProhibited::NotProhibited
1294    {
1295        return false;
1296    }
1297
1298    // 1.2. response’s url is a potentially trustworthy URL.
1299    if response
1300        .actual_response()
1301        .url()
1302        .is_some_and(|response_url| is_url_potentially_trustworthy(protocol_registry, response_url))
1303    {
1304        return false;
1305    }
1306
1307    // 1.3. TODO: The user agent has been instructed to allow mixed content.
1308
1309    // 1.4. request’s destination is "document", and request’s target browsing context
1310    // has no parent browsing context.
1311    if request.destination == Destination::Document {
1312        // TODO: if requests target browsing context has no parent browsing context
1313        return false;
1314    }
1315
1316    true
1317}
1318
1319/// <https://fetch.spec.whatwg.org/#bad-port>
1320fn is_bad_port(port: u16) -> bool {
1321    static BAD_PORTS: [u16; 83] = [
1322        0, 1, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 25, 37, 42, 43, 53, 69, 77, 79, 87, 95,
1323        101, 102, 103, 104, 109, 110, 111, 113, 115, 117, 119, 123, 135, 137, 139, 143, 161, 179,
1324        389, 427, 465, 512, 513, 514, 515, 526, 530, 531, 532, 540, 548, 554, 556, 563, 587, 601,
1325        636, 989, 990, 993, 995, 1719, 1720, 1723, 2049, 3659, 4045, 4190, 5060, 5061, 6000, 6566,
1326        6665, 6666, 6667, 6668, 6669, 6679, 6697, 10080,
1327    ];
1328
1329    BAD_PORTS.binary_search(&port).is_ok()
1330}
1331
1332// TODO : Investigate and need to revisit again
1333pub fn is_form_submission_request(request: &Request) -> bool {
1334    let content_type = request.headers.typed_get::<ContentType>();
1335    content_type.is_some_and(|ct| {
1336        let mime: Mime = ct.into();
1337        mime.type_() == mime::APPLICATION && mime.subtype() == mime::WWW_FORM_URLENCODED
1338    })
1339}
1340
1341/// <https://w3c.github.io/webappsec-upgrade-insecure-requests/#upgrade-request>
1342fn should_upgrade_request_to_potentially_trustworthy(
1343    request: &mut Request,
1344    context: &FetchContext,
1345) -> bool {
1346    fn should_upgrade_navigation_request(request: &Request) -> bool {
1347        // Step 2.1 If request is a form submission, skip the remaining substeps, and continue upgrading request.
1348        if is_form_submission_request(request) {
1349            return true;
1350        }
1351
1352        // Step 2.2 If request’s client's target browsing context is a nested browsing context,
1353        // skip the remaining substeps and continue upgrading request.
1354        if request
1355            .client
1356            .as_ref()
1357            .is_some_and(|client| client.is_nested_browsing_context)
1358        {
1359            return true;
1360        }
1361
1362        // Step 2.3  Let tuple be a tuple of request’s URL's host and port.
1363        let tuple = match request.url().origin() {
1364            ImmutableOrigin::Opaque(_) => None,
1365            ImmutableOrigin::Tuple(_, host, port) => Some(UpgradeInsecureNavigations {
1366                host: host.to_string(),
1367                port,
1368            }),
1369        };
1370
1371        // Step 2.4 If tuple is contained in client's upgrade insecure navigations set,
1372        // then skip the remaining substeps, and continue upgrading request.
1373        if let Some(tuple) = tuple &&
1374            request
1375                .client
1376                .as_ref()
1377                .is_some_and(|client| client.upgrade_insecure_navigations_set.contains(&tuple))
1378        {
1379            return true;
1380        }
1381
1382        // Step 2.5 Return without further modifying request
1383        false
1384    }
1385
1386    // Step 1. If request is a navigation request,
1387    if request.is_navigation_request() {
1388        // Append a header named Upgrade-Insecure-Requests with a value of 1 to
1389        // request’s header list if any of the following criteria are met:
1390        // * request’s URL is not a potentially trustworthy URL
1391        // * request’s URL's host is not a preloadable HSTS host
1392        if !is_url_potentially_trustworthy(&context.protocols, &request.current_url()) ||
1393            request
1394                .current_url()
1395                .host_str()
1396                .is_none_or(|host| context.state.hsts_list.read().is_host_secure(host))
1397        {
1398            debug!("Appending the Upgrade-Insecure-Requests header to request’s header list");
1399            request
1400                .headers
1401                .insert("Upgrade-Insecure-Requests", HeaderValue::from_static("1"));
1402        }
1403
1404        if !should_upgrade_navigation_request(request) {
1405            return false;
1406        }
1407    }
1408
1409    // Step 3. Let upgrade state be the result of executing
1410    // §4.2 Should insecure requests be upgraded for client? upon request's client.
1411    // Step 4. If upgrade state is "Do Not Upgrade", return without modifying request.
1412    request
1413        .client
1414        .as_ref()
1415        .is_some_and(|client| client.insecure_requests_policy == InsecureRequestsPolicy::Upgrade)
1416}
1417
1418#[derive(Debug, PartialEq)]
1419pub enum MixedSecurityProhibited {
1420    Prohibited,
1421    NotProhibited,
1422}
1423
1424/// <https://w3c.github.io/webappsec-mixed-content/#categorize-settings-object>
1425fn do_settings_prohibit_mixed_security_contexts(request: &Request) -> MixedSecurityProhibited {
1426    let Some(ref client) = request.client else {
1427        return MixedSecurityProhibited::NotProhibited;
1428    };
1429
1430    let Origin::Origin(ref origin) = client.origin else {
1431        unreachable!("Settings' origin is never a \"client\"");
1432    };
1433
1434    // Step 1. If settings’ origin is a potentially trustworthy origin,
1435    // then return "Prohibits Mixed Security Contexts".
1436    // NOTE: Workers created from a data: url are secure if they were created from secure contexts
1437    if origin.is_potentially_trustworthy() || origin.is_for_data_worker_from_secure_context() {
1438        return MixedSecurityProhibited::Prohibited;
1439    }
1440
1441    // Step 2.2. For each navigable navigable in document’s ancestor navigables:
1442    // Step 2.2.1. If navigable’s active document's origin is a potentially trustworthy origin,
1443    // then return "Prohibits Mixed Security Contexts".
1444    if client.has_trustworthy_ancestor_origin {
1445        return MixedSecurityProhibited::Prohibited;
1446    }
1447
1448    MixedSecurityProhibited::NotProhibited
1449}
1450
1451/// <https://w3c.github.io/webappsec-mixed-content/#upgrade-algorithm>
1452fn should_upgrade_mixed_content_request(
1453    request: &Request,
1454    protocol_registry: &ProtocolRegistry,
1455) -> bool {
1456    let url = request.url();
1457    // Step 1.1 : request’s URL is a potentially trustworthy URL.
1458    if is_url_potentially_trustworthy(protocol_registry, &url) {
1459        return false;
1460    }
1461
1462    // Step 1.2 : request’s URL’s host is an IP address.
1463    match url.host() {
1464        Some(Host::Ipv4(_)) | Some(Host::Ipv6(_)) => return false,
1465        _ => (),
1466    }
1467
1468    // Step 1.3
1469    if do_settings_prohibit_mixed_security_contexts(request) ==
1470        MixedSecurityProhibited::NotProhibited
1471    {
1472        return false;
1473    }
1474
1475    // Step 1.4 : request’s destination is not "image", "audio", or "video".
1476    if !matches!(
1477        request.destination,
1478        Destination::Audio | Destination::Image | Destination::Video
1479    ) {
1480        return false;
1481    }
1482
1483    // Step 1.5 : request’s destination is "image" and request’s initiator is "imageset".
1484    if request.destination == Destination::Image && request.initiator == Initiator::ImageSet {
1485        return false;
1486    }
1487
1488    true
1489}