1use std::cell::RefCell;
6use std::net::IpAddr;
7use std::rc::Rc;
8
9use malloc_size_of::malloc_size_of_is_0;
10use malloc_size_of_derive::MallocSizeOf;
11use serde::{Deserialize, Serialize};
12use url::{Host, Origin};
13use uuid::Uuid;
14
15#[derive(Clone, Debug, Deserialize, Eq, Hash, MallocSizeOf, PartialEq, Serialize)]
17pub enum ImmutableOrigin {
18    Opaque(OpaqueOrigin),
20
21    Tuple(String, Host, u16),
23}
24
25impl ImmutableOrigin {
26    pub fn new(origin: Origin) -> ImmutableOrigin {
27        match origin {
28            Origin::Opaque(_) => ImmutableOrigin::new_opaque(),
29            Origin::Tuple(scheme, host, port) => ImmutableOrigin::Tuple(scheme, host, port),
30        }
31    }
32
33    pub fn same_origin(&self, other: &MutableOrigin) -> bool {
34        self == other.immutable()
35    }
36
37    pub fn same_origin_domain(&self, other: &MutableOrigin) -> bool {
38        !other.has_domain() && self == other.immutable()
39    }
40
41    pub fn new_opaque() -> ImmutableOrigin {
43        ImmutableOrigin::Opaque(OpaqueOrigin::Opaque(Uuid::new_v4()))
44    }
45
46    pub fn new_opaque_data_url_worker() -> ImmutableOrigin {
48        ImmutableOrigin::Opaque(OpaqueOrigin::SecureWorkerFromDataUrl(Uuid::new_v4()))
49    }
50
51    pub fn scheme(&self) -> Option<&str> {
52        match *self {
53            ImmutableOrigin::Opaque(_) => None,
54            ImmutableOrigin::Tuple(ref scheme, _, _) => Some(&**scheme),
55        }
56    }
57
58    pub fn host(&self) -> Option<&Host> {
59        match *self {
60            ImmutableOrigin::Opaque(_) => None,
61            ImmutableOrigin::Tuple(_, ref host, _) => Some(host),
62        }
63    }
64
65    pub fn port(&self) -> Option<u16> {
66        match *self {
67            ImmutableOrigin::Opaque(_) => None,
68            ImmutableOrigin::Tuple(_, _, port) => Some(port),
69        }
70    }
71
72    pub fn into_url_origin(self) -> Origin {
73        match self {
74            ImmutableOrigin::Opaque(_) => Origin::new_opaque(),
75            ImmutableOrigin::Tuple(scheme, host, port) => Origin::Tuple(scheme, host, port),
76        }
77    }
78
79    pub fn is_tuple(&self) -> bool {
82        match *self {
83            ImmutableOrigin::Opaque(..) => false,
84            ImmutableOrigin::Tuple(..) => true,
85        }
86    }
87
88    pub fn is_potentially_trustworthy(&self) -> bool {
90        if matches!(self, ImmutableOrigin::Opaque(_)) {
92            return false;
93        }
94
95        if let ImmutableOrigin::Tuple(scheme, host, _) = self {
96            if scheme == "https" || scheme == "wss" {
98                return true;
99            }
100            if scheme == "file" {
102                return true;
103            }
104
105            if let Ok(ip_addr) = host.to_string().parse::<IpAddr>() {
108                return ip_addr.is_loopback();
109            }
110            if let Host::Domain(domain) = host {
116                if domain == "localhost" || domain.ends_with(".localhost") {
117                    return true;
118                }
119            }
120        }
121        false
123    }
124
125    pub fn ascii_serialization(&self) -> String {
127        self.clone().into_url_origin().ascii_serialization()
128    }
129}
130
131#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
133pub enum OpaqueOrigin {
134    Opaque(Uuid),
135    SecureWorkerFromDataUrl(Uuid),
139}
140malloc_size_of_is_0!(OpaqueOrigin);
141
142#[derive(Clone, Debug, Deserialize, Serialize)]
144pub struct MutableOrigin(Rc<(ImmutableOrigin, RefCell<Option<Host>>)>);
145
146malloc_size_of_is_0!(MutableOrigin);
147
148impl MutableOrigin {
149    pub fn new(origin: ImmutableOrigin) -> MutableOrigin {
150        MutableOrigin(Rc::new((origin, RefCell::new(None))))
151    }
152
153    pub fn immutable(&self) -> &ImmutableOrigin {
154        &(self.0).0
155    }
156
157    pub fn is_tuple(&self) -> bool {
158        self.immutable().is_tuple()
159    }
160
161    pub fn scheme(&self) -> Option<&str> {
162        self.immutable().scheme()
163    }
164
165    pub fn host(&self) -> Option<&Host> {
166        self.immutable().host()
167    }
168
169    pub fn port(&self) -> Option<u16> {
170        self.immutable().port()
171    }
172
173    pub fn same_origin(&self, other: &MutableOrigin) -> bool {
174        self.immutable() == other.immutable()
175    }
176
177    pub fn same_origin_domain(&self, other: &MutableOrigin) -> bool {
178        if let Some(ref self_domain) = *(self.0).1.borrow() {
179            if let Some(ref other_domain) = *(other.0).1.borrow() {
180                self_domain == other_domain &&
181                    self.immutable().scheme() == other.immutable().scheme()
182            } else {
183                false
184            }
185        } else {
186            self.immutable().same_origin_domain(other)
187        }
188    }
189
190    pub fn domain(&self) -> Option<Host> {
191        (self.0).1.borrow().clone()
192    }
193
194    pub fn set_domain(&self, domain: Host) {
195        *(self.0).1.borrow_mut() = Some(domain);
196    }
197
198    pub fn has_domain(&self) -> bool {
199        (self.0).1.borrow().is_some()
200    }
201
202    pub fn effective_domain(&self) -> Option<Host> {
203        self.immutable()
204            .host()
205            .map(|host| self.domain().unwrap_or_else(|| host.clone()))
206    }
207}