Skip to main content

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 std::sync::Arc;
6
7use content_security_policy::CspList;
8use malloc_size_of_derive::MallocSizeOf;
9use serde::{Deserialize, Serialize};
10
11use crate::ReferrerPolicy;
12
13/// When a policy container is associated with a request, it has an additional state of "Client". As
14/// per the spec:
15///
16/// `"client" is changed to a policy container during fetching. It provides a convenient way for
17/// standards to not have to set request’s policy container.`
18///
19/// This can be achieved with an `Option` however this struct is used with the intent to reduce
20/// ambiguity when mapping our implementation to the spec.
21///
22/// <https://fetch.spec.whatwg.org/#concept-request-policy-container>
23#[derive(Clone, Debug, Default, Deserialize, MallocSizeOf, Serialize)]
24pub enum RequestPolicyContainer {
25    #[default]
26    Client,
27    PolicyContainer(#[conditional_malloc_size_of] Arc<PolicyContainer>),
28}
29
30impl RequestPolicyContainer {
31    pub fn policy_container(&self) -> Option<&PolicyContainer> {
32        match self {
33            RequestPolicyContainer::Client => None,
34            RequestPolicyContainer::PolicyContainer(policy_container) => Some(policy_container),
35        }
36    }
37}
38
39/// <https://html.spec.whatwg.org/multipage/#policy-containers>
40#[derive(Clone, Debug, Default, Deserialize, MallocSizeOf, Serialize)]
41pub struct PolicyContainer {
42    #[ignore_malloc_size_of = "Defined in rust-content-security-policy"]
43    /// <https://html.spec.whatwg.org/multipage/#policy-container-csp-list>
44    pub csp_list: Option<CspList>,
45    /// <https://html.spec.whatwg.org/multipage/#policy-container-embedder-policy>
46    pub embedder_policy: EmbedderPolicy,
47    /// <https://html.spec.whatwg.org/multipage/#policy-container-referrer-policy>
48    pub referrer_policy: ReferrerPolicy,
49}
50
51impl PolicyContainer {
52    pub fn set_csp_list(&mut self, csp_list: Option<CspList>) {
53        self.csp_list = csp_list;
54    }
55
56    pub fn set_referrer_policy(&mut self, referrer_policy: ReferrerPolicy) {
57        self.referrer_policy = referrer_policy;
58    }
59
60    pub fn get_referrer_policy(&self) -> ReferrerPolicy {
61        // https://w3c.github.io/webappsec-referrer-policy/#referrer-policy-empty-string
62        if self.referrer_policy == ReferrerPolicy::EmptyString {
63            return ReferrerPolicy::default();
64        }
65
66        self.referrer_policy
67    }
68}
69
70/// <https://html.spec.whatwg.org/multipage/#embedder-policy>
71#[derive(Clone, Debug, Default, Deserialize, MallocSizeOf, Serialize)]
72pub struct EmbedderPolicy {
73    /// <https://html.spec.whatwg.org/multipage/#embedder-policy-value-2>
74    pub value: EmbedderPolicyValue,
75}
76
77/// <https://html.spec.whatwg.org/multipage/#embedder-policy-value>
78#[derive(Clone, Copy, Debug, Default, Deserialize, MallocSizeOf, Serialize)]
79pub enum EmbedderPolicyValue {
80    /// "unsafe-none"
81    #[default]
82    UnsafeNone,
83    /// "require-corp"
84    RequireCorp,
85    // TODO "credentialless"
86}