net_traits/
policy_container.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 content_security_policy::CspList;
6use malloc_size_of_derive::MallocSizeOf;
7use serde::{Deserialize, Serialize};
8
9use crate::ReferrerPolicy;
10
11/// When a policy container is associated with a request, it has an additional state of "Client". As
12/// per the spec:
13///
14/// `"client" is changed to a policy container during fetching. It provides a convenient way for
15/// standards to not have to set request’s policy container.`
16///
17/// This can be achieved with an `Option` however this struct is used with the intent to reduce
18/// ambiguity when mapping our implementation to the spec.
19///
20/// <https://fetch.spec.whatwg.org/#concept-request-policy-container>
21#[derive(Clone, Debug, Default, Deserialize, MallocSizeOf, Serialize)]
22pub enum RequestPolicyContainer {
23    #[default]
24    Client,
25    PolicyContainer(PolicyContainer),
26}
27
28/// <https://html.spec.whatwg.org/multipage/#policy-containers>
29#[derive(Clone, Debug, Default, Deserialize, MallocSizeOf, Serialize)]
30pub struct PolicyContainer {
31    #[ignore_malloc_size_of = "Defined in rust-content-security-policy"]
32    /// <https://html.spec.whatwg.org/multipage/#policy-container-csp-list>
33    pub csp_list: Option<CspList>,
34    /// <https://html.spec.whatwg.org/multipage/#policy-container-referrer-policy>
35    referrer_policy: ReferrerPolicy,
36    // https://html.spec.whatwg.org/multipage/#policy-container-embedder-policy
37    // TODO: Embedder Policy
38}
39
40impl PolicyContainer {
41    pub fn set_csp_list(&mut self, csp_list: Option<CspList>) {
42        self.csp_list = csp_list;
43    }
44
45    pub fn set_referrer_policy(&mut self, referrer_policy: ReferrerPolicy) {
46        self.referrer_policy = referrer_policy;
47    }
48
49    pub fn get_referrer_policy(&self) -> ReferrerPolicy {
50        // https://w3c.github.io/webappsec-referrer-policy/#referrer-policy-empty-string
51        if self.referrer_policy == ReferrerPolicy::EmptyString {
52            return ReferrerPolicy::default();
53        }
54
55        self.referrer_policy
56    }
57}