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    /// <https://html.spec.whatwg.org/multipage/#policy-container-csp-list>
43    pub csp_list: Option<CspList>,
44    /// <https://html.spec.whatwg.org/multipage/#policy-container-embedder-policy>
45    pub embedder_policy: EmbedderPolicy,
46    /// <https://html.spec.whatwg.org/multipage/#policy-container-referrer-policy>
47    pub referrer_policy: ReferrerPolicy,
48}
49
50impl PolicyContainer {
51    pub fn set_csp_list(&mut self, csp_list: Option<CspList>) {
52        self.csp_list = csp_list;
53    }
54
55    pub fn set_referrer_policy(&mut self, referrer_policy: ReferrerPolicy) {
56        self.referrer_policy = referrer_policy;
57    }
58
59    pub fn get_referrer_policy(&self) -> ReferrerPolicy {
60        // https://w3c.github.io/webappsec-referrer-policy/#referrer-policy-empty-string
61        if self.referrer_policy == ReferrerPolicy::EmptyString {
62            return ReferrerPolicy::default();
63        }
64
65        self.referrer_policy
66    }
67}
68
69/// <https://html.spec.whatwg.org/multipage/#embedder-policy>
70#[derive(Clone, Debug, Default, Deserialize, MallocSizeOf, Serialize)]
71pub struct EmbedderPolicy {
72    /// <https://html.spec.whatwg.org/multipage/#embedder-policy-value-2>
73    pub value: EmbedderPolicyValue,
74}
75
76/// <https://html.spec.whatwg.org/multipage/#embedder-policy-value>
77#[derive(Clone, Copy, Debug, Default, Deserialize, MallocSizeOf, Serialize)]
78pub enum EmbedderPolicyValue {
79    /// "unsafe-none"
80    #[default]
81    UnsafeNone,
82    /// "require-corp"
83    RequireCorp,
84    // TODO "credentialless"
85}