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(servo_rand::random_uuid()))
44 }
45
46 pub fn new_opaque_data_url_worker() -> ImmutableOrigin {
48 ImmutableOrigin::Opaque(OpaqueOrigin::SecureWorkerFromDataUrl(
49 servo_rand::random_uuid(),
50 ))
51 }
52
53 pub fn scheme(&self) -> Option<&str> {
54 match *self {
55 ImmutableOrigin::Opaque(_) => None,
56 ImmutableOrigin::Tuple(ref scheme, _, _) => Some(&**scheme),
57 }
58 }
59
60 pub fn host(&self) -> Option<&Host> {
61 match *self {
62 ImmutableOrigin::Opaque(_) => None,
63 ImmutableOrigin::Tuple(_, ref host, _) => Some(host),
64 }
65 }
66
67 pub fn port(&self) -> Option<u16> {
68 match *self {
69 ImmutableOrigin::Opaque(_) => None,
70 ImmutableOrigin::Tuple(_, _, port) => Some(port),
71 }
72 }
73
74 pub fn into_url_origin(self) -> Origin {
75 match self {
76 ImmutableOrigin::Opaque(_) => Origin::new_opaque(),
77 ImmutableOrigin::Tuple(scheme, host, port) => Origin::Tuple(scheme, host, port),
78 }
79 }
80
81 pub fn is_tuple(&self) -> bool {
84 match *self {
85 ImmutableOrigin::Opaque(..) => false,
86 ImmutableOrigin::Tuple(..) => true,
87 }
88 }
89
90 pub fn is_potentially_trustworthy(&self) -> bool {
92 if matches!(self, ImmutableOrigin::Opaque(_)) {
94 return false;
95 }
96
97 if let ImmutableOrigin::Tuple(scheme, host, _) = self {
98 if scheme == "https" || scheme == "wss" {
100 return true;
101 }
102 if scheme == "file" {
104 return true;
105 }
106
107 if let Ok(ip_addr) = host.to_string().parse::<IpAddr>() {
110 return ip_addr.is_loopback();
111 }
112 if let Host::Domain(domain) = host {
118 if domain == "localhost" || domain.ends_with(".localhost") {
119 return true;
120 }
121 }
122 }
123 false
125 }
126
127 pub fn ascii_serialization(&self) -> String {
129 self.clone().into_url_origin().ascii_serialization()
130 }
131}
132
133#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
135pub enum OpaqueOrigin {
136 Opaque(Uuid),
137 SecureWorkerFromDataUrl(Uuid),
141}
142malloc_size_of_is_0!(OpaqueOrigin);
143
144#[derive(Clone, Debug, Deserialize, Serialize)]
146pub struct MutableOrigin(Rc<(ImmutableOrigin, RefCell<Option<Host>>)>);
147
148malloc_size_of_is_0!(MutableOrigin);
149
150impl MutableOrigin {
151 pub fn new(origin: ImmutableOrigin) -> MutableOrigin {
152 MutableOrigin(Rc::new((origin, RefCell::new(None))))
153 }
154
155 pub fn immutable(&self) -> &ImmutableOrigin {
156 &(self.0).0
157 }
158
159 pub fn is_tuple(&self) -> bool {
160 self.immutable().is_tuple()
161 }
162
163 pub fn scheme(&self) -> Option<&str> {
164 self.immutable().scheme()
165 }
166
167 pub fn host(&self) -> Option<&Host> {
168 self.immutable().host()
169 }
170
171 pub fn port(&self) -> Option<u16> {
172 self.immutable().port()
173 }
174
175 pub fn same_origin(&self, other: &MutableOrigin) -> bool {
176 self.immutable() == other.immutable()
177 }
178
179 pub fn same_origin_domain(&self, other: &MutableOrigin) -> bool {
180 if let Some(ref self_domain) = *(self.0).1.borrow() {
181 if let Some(ref other_domain) = *(other.0).1.borrow() {
182 self_domain == other_domain &&
183 self.immutable().scheme() == other.immutable().scheme()
184 } else {
185 false
186 }
187 } else {
188 self.immutable().same_origin_domain(other)
189 }
190 }
191
192 pub fn domain(&self) -> Option<Host> {
193 (self.0).1.borrow().clone()
194 }
195
196 pub fn set_domain(&self, domain: Host) {
197 *(self.0).1.borrow_mut() = Some(domain);
198 }
199
200 pub fn has_domain(&self) -> bool {
201 (self.0).1.borrow().is_some()
202 }
203
204 pub fn effective_domain(&self) -> Option<Host> {
205 self.immutable()
206 .host()
207 .map(|host| self.domain().unwrap_or_else(|| host.clone()))
208 }
209}