script/dom/
reportingendpoint.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::collections::HashMap;
6
7use headers::{ContentType, HeaderMapExt};
8use http::HeaderMap;
9use hyper_serde::Serde;
10use malloc_size_of_derive::MallocSizeOf;
11use net_traits::request::{
12    CredentialsMode, Destination, RequestBody, RequestId, RequestMode,
13    create_request_body_with_content,
14};
15use net_traits::{FetchMetadata, NetworkError, ResourceFetchTiming};
16use script_bindings::str::DOMString;
17use serde::Serialize;
18use servo_url::{ImmutableOrigin, ServoUrl};
19
20use crate::dom::bindings::codegen::Bindings::CSPViolationReportBodyBinding::CSPViolationReportBody;
21use crate::dom::bindings::codegen::Bindings::ReportingObserverBinding::Report;
22use crate::dom::bindings::codegen::Bindings::SecurityPolicyViolationEventBinding::SecurityPolicyViolationEventDisposition;
23use crate::dom::bindings::refcounted::Trusted;
24use crate::dom::bindings::root::DomRoot;
25use crate::dom::csp::Violation;
26use crate::dom::csppolicyviolationreport::serialize_disposition;
27use crate::dom::globalscope::GlobalScope;
28use crate::dom::performance::performanceresourcetiming::InitiatorType;
29use crate::fetch::create_a_potential_cors_request;
30use crate::network_listener::{FetchResponseListener, ResourceTimingListener, submit_timing};
31use crate::script_runtime::CanGc;
32
33/// <https://w3c.github.io/reporting/#endpoint>
34#[derive(Clone, Eq, Hash, MallocSizeOf, PartialEq)]
35pub(crate) struct ReportingEndpoint {
36    /// <https://w3c.github.io/reporting/#dom-endpoint-name>
37    name: DOMString,
38    /// <https://w3c.github.io/reporting/#dom-endpoint-url>
39    url: ServoUrl,
40    /// <https://w3c.github.io/reporting/#dom-endpoint-failures>
41    failures: u32,
42}
43
44impl ReportingEndpoint {
45    /// <https://w3c.github.io/reporting/#process-header>
46    pub(crate) fn parse_reporting_endpoints_header(
47        response_url: &ServoUrl,
48        headers: &Option<Serde<HeaderMap>>,
49    ) -> Option<Vec<ReportingEndpoint>> {
50        let headers = headers.as_ref()?;
51        let reporting_headers = headers.get_all("reporting-endpoints");
52        // Step 2. Let parsed header be the result of executing get a structured field value
53        // given "Reporting-Endpoints" and "dictionary" from response’s header list.
54        let mut parsed_header = Vec::new();
55        for header in reporting_headers.iter() {
56            let Some(header_value) = header.to_str().ok() else {
57                continue;
58            };
59            parsed_header.append(&mut header_value.split(",").map(|s| s.trim()).collect());
60        }
61        // Step 3. If parsed header is null, abort these steps.
62        if parsed_header.is_empty() {
63            return None;
64        }
65        // Step 4. Let endpoints be an empty list.
66        let mut endpoints = Vec::new();
67        // Step 5. For each name → value_and_parameters of parsed header:
68        for header in parsed_header {
69            // There could be a '=' in the URL itself (for example query parameters). Therefore, we can't
70            // split on '=', but instead look for the first one.
71            let Some(split_index) = header.find('=') else {
72                continue;
73            };
74            // Step 5.1. Let endpoint url string be the first element of the tuple value_and_parameters.
75            // If endpoint url string is not a string, then continue.
76            let (name, endpoint_url_string) = header.split_at(split_index);
77            let length = endpoint_url_string.len();
78            let endpoint_bytes = endpoint_url_string.as_bytes();
79            // Note that the first character is the '=' and we check for the next and last character to be '"'
80            if length < 3 || endpoint_bytes[1] != b'"' || endpoint_bytes[length - 1] != b'"' {
81                continue;
82            }
83            // The '="' at the start and '"' at the end removed
84            let endpoint_url_value = &endpoint_url_string[2..length - 1];
85            // Step 5.2. Let endpoint url be the result of executing the URL parser on endpoint url string,
86            // with base URL set to response’s url. If endpoint url is failure, then continue.
87            let Ok(endpoint_url) =
88                ServoUrl::parse_with_base(Some(response_url), endpoint_url_value)
89            else {
90                continue;
91            };
92            // Step 5.3. If endpoint url’s origin is not potentially trustworthy, then continue.
93            if !endpoint_url.is_potentially_trustworthy() {
94                continue;
95            }
96            // Step 5.4. Let endpoint be a new endpoint whose properties are set as follows:
97            // Step 5.5. Add endpoint to endpoints.
98            endpoints.push(ReportingEndpoint {
99                name: name.into(),
100                url: endpoint_url,
101                failures: 0,
102            });
103        }
104        Some(endpoints)
105    }
106}
107
108pub(crate) trait SendReportsToEndpoints {
109    /// <https://w3c.github.io/reporting/#send-reports>
110    fn send_reports_to_endpoints(&self, reports: Vec<Report>, endpoints: Vec<ReportingEndpoint>);
111    /// <https://w3c.github.io/reporting/#try-delivery>
112    fn attempt_to_deliver_reports_to_endpoints(
113        &self,
114        endpoint: &ServoUrl,
115        origin: ImmutableOrigin,
116        reports: &[&Report],
117    );
118    /// <https://w3c.github.io/reporting/#serialize-a-list-of-reports-to-json>
119    fn serialize_list_of_reports(reports: &[&Report]) -> Option<RequestBody>;
120}
121
122impl SendReportsToEndpoints for GlobalScope {
123    fn send_reports_to_endpoints(
124        &self,
125        mut reports: Vec<Report>,
126        endpoints: Vec<ReportingEndpoint>,
127    ) {
128        // Step 1. Let endpoint map be an empty map of endpoint objects to lists of report objects.
129        #[allow(clippy::mutable_key_type)]
130        // See `impl Hash for DOMString`.
131        let mut endpoint_map: HashMap<&ReportingEndpoint, Vec<Report>> = HashMap::new();
132        // Step 2. For each report in reports:
133        reports.retain(|report| {
134            // Step 2.1. If there exists an endpoint (endpoint) in context’s endpoints
135            // list whose name is report’s destination:
136            if let Some(endpoint) = endpoints.iter().find(|e| e.name == report.destination) {
137                // Step 2.1.1. Append report to endpoint map’s list of reports for endpoint.
138                endpoint_map
139                    .entry(endpoint)
140                    .or_default()
141                    .push(report.clone());
142                true
143            } else {
144                // Step 2.1.2. Otherwise, remove report from reports.
145                false
146            }
147        });
148        // Step 3. For each (endpoint, report list) pair in endpoint map:
149        for (endpoint, report_list) in endpoint_map.iter() {
150            // Step 3.1. Let origin map be an empty map of origins to lists of report objects.
151            let mut origin_map: HashMap<ImmutableOrigin, Vec<&Report>> = HashMap::new();
152            // Step 3.2. For each report in report list:
153            for report in report_list {
154                let Ok(url) = ServoUrl::parse(&report.url.str()) else {
155                    continue;
156                };
157                // Step 3.2.1. Let origin be the origin of report’s url.
158                let origin = url.origin();
159                // Step 3.2.2. Append report to origin map’s list of reports for origin.
160                origin_map.entry(origin).or_default().push(report);
161            }
162            // Step 3.3. For each (origin, per-origin reports) pair in origin map,
163            // execute the following steps asynchronously:
164            for (origin, origin_report_list) in origin_map.iter() {
165                // Step 3.3.1. Let result be the result of executing
166                // § 3.5.2 Attempt to deliver reports to endpoint on endpoint, origin, and per-origin reports.
167                self.attempt_to_deliver_reports_to_endpoints(
168                    &endpoint.url,
169                    origin.clone(),
170                    origin_report_list,
171                );
172                // Step 3.3.2. If result is "Failure":
173                // TODO(37238)
174                // Step 3.3.2.1. Increment endpoint’s failures.
175                // TODO(37238)
176                // Step 3.3.3. If result is "Remove Endpoint":
177                // TODO(37238)
178                // Step 3.3.3.1 Remove endpoint from context’s endpoints list.
179                // TODO(37238)
180                // Step 3.3.4. Remove each report from reports.
181                // TODO(37238)
182            }
183        }
184    }
185
186    fn attempt_to_deliver_reports_to_endpoints(
187        &self,
188        endpoint: &ServoUrl,
189        origin: ImmutableOrigin,
190        reports: &[&Report],
191    ) {
192        // Step 1. Let body be the result of executing serialize a list of reports to JSON on reports.
193        let request_body = Self::serialize_list_of_reports(reports);
194        // Step 2. Let request be a new request with the following properties [FETCH]:
195        let mut headers = HeaderMap::with_capacity(1);
196        headers.typed_insert(ContentType::from(
197            "application/reports+json".parse::<mime::Mime>().unwrap(),
198        ));
199        let request = create_a_potential_cors_request(
200            None,
201            endpoint.clone(),
202            Destination::Report,
203            None,
204            None,
205            self.get_referrer(),
206            self.insecure_requests_policy(),
207            self.has_trustworthy_ancestor_or_current_origin(),
208            self.policy_container(),
209        )
210        .method(http::Method::POST)
211        .body(request_body)
212        .origin(origin)
213        .mode(RequestMode::CorsMode)
214        .credentials_mode(CredentialsMode::CredentialsSameOrigin)
215        .unsafe_request(true)
216        .headers(headers);
217        // Step 3. Queue a task to fetch request.
218        self.fetch(
219            request,
220            CSPReportEndpointFetchListener {
221                endpoint: endpoint.clone(),
222                global: Trusted::new(self),
223            },
224            self.task_manager().networking_task_source().into(),
225        );
226        // Step 4. Wait for a response (response).
227        // TODO(37238)
228        // Step 5. If response’s status is an OK status (200-299), return "Success".
229        // TODO(37238)
230        // Step 6. If response’s status is 410 Gone [RFC9110], return "Remove Endpoint".
231        // TODO(37238)
232        // Step 7. Return "Failure".
233        // TODO(37238)
234    }
235
236    fn serialize_list_of_reports(reports: &[&Report]) -> Option<RequestBody> {
237        // Step 1. Let collection be an empty list.
238        // Step 2. For each report in reports:
239        let report_body: Vec<SerializedReport> = reports
240            .iter()
241            // Step 2.1. Let data be a map with the following key/value pairs:
242            .map(|r| SerializedReport {
243                // TODO(37238)
244                age: 0,
245                type_: r.type_.to_string(),
246                url: r.url.to_string(),
247                user_agent: "".to_owned(),
248                body: r.body.clone().map(|b| b.into()),
249            })
250            // Step 2.2. Increment report’s attempts.
251            // TODO(37238)
252            // Step 2.3. Append data to collection.
253            .collect();
254        // Step 3. Return the byte sequence resulting from executing serialize an
255        // Infra value to JSON bytes on collection.
256        Some(create_request_body_with_content(
257            &serde_json::to_string(&report_body).unwrap_or("".to_owned()),
258        ))
259    }
260}
261
262#[derive(Serialize)]
263struct SerializedReport {
264    age: u64,
265    #[serde(rename = "type")]
266    type_: String,
267    url: String,
268    user_agent: String,
269    body: Option<CSPReportingEndpointBody>,
270}
271
272#[derive(Clone, Debug, Serialize)]
273#[serde(rename_all = "camelCase")]
274pub(crate) struct CSPReportingEndpointBody {
275    sample: Option<String>,
276    #[serde(rename = "blockedURL")]
277    blocked_url: Option<String>,
278    referrer: Option<String>,
279    status_code: u16,
280    #[serde(rename = "documentURL")]
281    document_url: String,
282    source_file: Option<String>,
283    effective_directive: String,
284    line_number: Option<u32>,
285    column_number: Option<u32>,
286    original_policy: String,
287    #[serde(serialize_with = "serialize_disposition")]
288    disposition: SecurityPolicyViolationEventDisposition,
289}
290
291impl From<CSPViolationReportBody> for CSPReportingEndpointBody {
292    fn from(value: CSPViolationReportBody) -> Self {
293        CSPReportingEndpointBody {
294            sample: value.sample.map(|s| s.to_string()),
295            blocked_url: value.blockedURL.map(|s| s.to_string()),
296            referrer: value.referrer.map(|s| s.to_string()),
297            status_code: value.statusCode,
298            document_url: value.documentURL.to_string(),
299            source_file: value.sourceFile.map(|s| s.to_string()),
300            effective_directive: value.effectiveDirective.to_string(),
301            line_number: value.lineNumber,
302            column_number: value.columnNumber,
303            original_policy: value.originalPolicy.into(),
304            disposition: value.disposition,
305        }
306    }
307}
308
309struct CSPReportEndpointFetchListener {
310    /// Endpoint URL of this request.
311    endpoint: ServoUrl,
312    /// The global object fetching the report uri violation
313    global: Trusted<GlobalScope>,
314}
315
316impl FetchResponseListener for CSPReportEndpointFetchListener {
317    fn process_request_body(&mut self, _: RequestId) {}
318
319    fn process_request_eof(&mut self, _: RequestId) {}
320
321    fn process_response(
322        &mut self,
323        _: RequestId,
324        fetch_metadata: Result<FetchMetadata, NetworkError>,
325    ) {
326        _ = fetch_metadata;
327    }
328
329    fn process_response_chunk(&mut self, _: RequestId, chunk: Vec<u8>) {
330        _ = chunk;
331    }
332
333    fn process_response_eof(
334        self,
335        _: RequestId,
336        response: Result<ResourceFetchTiming, NetworkError>,
337    ) {
338        if let Ok(response) = response {
339            submit_timing(&self, &response, CanGc::note());
340        }
341    }
342
343    fn process_csp_violations(&mut self, _request_id: RequestId, _violations: Vec<Violation>) {}
344}
345
346impl ResourceTimingListener for CSPReportEndpointFetchListener {
347    fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
348        (InitiatorType::Other, self.endpoint.clone())
349    }
350
351    fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
352        self.global.root()
353    }
354}