1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
pub(crate) mod cors;
pub(crate) mod hosts;
mod matcher;
use crate::types::Error;
use cors::{AccessControlAllowHeaders, AccessControlAllowOrigin};
use hosts::{AllowHosts, Host};
use hyper::header;
use jsonrpsee_utils::http_helpers;
#[derive(Clone, Debug)]
pub struct AccessControl {
allowed_hosts: AllowHosts,
allowed_origins: Option<Vec<AccessControlAllowOrigin>>,
allowed_headers: AccessControlAllowHeaders,
continue_on_invalid_cors: bool,
}
impl AccessControl {
pub fn deny_host(&self, request: &hyper::Request<hyper::Body>) -> bool {
!hosts::is_host_valid(http_helpers::read_header_value(request.headers(), "host"), &self.allowed_hosts)
}
pub fn deny_cors_origin(&self, request: &hyper::Request<hyper::Body>) -> bool {
let header = cors::get_cors_allow_origin(
http_helpers::read_header_value(request.headers(), "origin"),
http_helpers::read_header_value(request.headers(), "host"),
&self.allowed_origins,
)
.map(|origin| {
use self::cors::AccessControlAllowOrigin::*;
match origin {
Value(ref val) => {
header::HeaderValue::from_str(val).unwrap_or_else(|_| header::HeaderValue::from_static("null"))
}
Null => header::HeaderValue::from_static("null"),
Any => header::HeaderValue::from_static("*"),
}
});
header == cors::AllowCors::Invalid && !self.continue_on_invalid_cors
}
pub fn deny_cors_header(&self, request: &hyper::Request<hyper::Body>) -> bool {
let headers = request.headers().keys().map(|name| name.as_str());
let requested_headers = http_helpers::read_header_values(request.headers(), "access-control-request-headers")
.filter_map(|val| val.to_str().ok())
.flat_map(|val| val.split(", "))
.flat_map(|val| val.split(','));
let header = cors::get_cors_allow_headers(headers, requested_headers, &self.allowed_headers, |name| {
header::HeaderValue::from_str(name).unwrap_or_else(|_| header::HeaderValue::from_static("unknown"))
});
header == cors::AllowCors::Invalid && !self.continue_on_invalid_cors
}
}
impl Default for AccessControl {
fn default() -> Self {
Self {
allowed_hosts: AllowHosts::Any,
allowed_origins: None,
allowed_headers: AccessControlAllowHeaders::Any,
continue_on_invalid_cors: false,
}
}
}
#[derive(Debug)]
pub struct AccessControlBuilder {
allowed_hosts: AllowHosts,
allowed_origins: Option<Vec<AccessControlAllowOrigin>>,
allowed_headers: AccessControlAllowHeaders,
continue_on_invalid_cors: bool,
}
impl Default for AccessControlBuilder {
fn default() -> Self {
Self {
allowed_hosts: AllowHosts::Any,
allowed_origins: None,
allowed_headers: AccessControlAllowHeaders::Any,
continue_on_invalid_cors: false,
}
}
}
impl AccessControlBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn allow_all_hosts(mut self) -> Self {
self.allowed_hosts = AllowHosts::Any;
self
}
pub fn allow_all_origins(mut self) -> Self {
self.allowed_headers = AccessControlAllowHeaders::Any;
self
}
pub fn allow_all_headers(mut self) -> Self {
self.allowed_origins = None;
self
}
pub fn set_allowed_hosts<List, H>(mut self, list: List) -> Result<Self, Error>
where
List: IntoIterator<Item = H>,
H: Into<Host>,
{
let allowed_hosts: Vec<Host> = list.into_iter().map(Into::into).collect();
if allowed_hosts.is_empty() {
return Err(Error::EmptyAllowList("Host"));
}
self.allowed_hosts = AllowHosts::Only(allowed_hosts);
Ok(self)
}
pub fn set_allowed_origins<Origin, List>(mut self, list: List) -> Result<Self, Error>
where
List: IntoIterator<Item = Origin>,
Origin: Into<AccessControlAllowOrigin>,
{
let allowed_origins: Vec<AccessControlAllowOrigin> = list.into_iter().map(Into::into).collect();
if allowed_origins.is_empty() {
return Err(Error::EmptyAllowList("Origin"));
}
self.allowed_origins = Some(allowed_origins);
Ok(self)
}
pub fn set_allowed_headers<Header, List>(mut self, list: List) -> Result<Self, Error>
where
List: IntoIterator<Item = Header>,
Header: Into<String>,
{
let allowed_headers: Vec<String> = list.into_iter().map(Into::into).collect();
if allowed_headers.is_empty() {
return Err(Error::EmptyAllowList("Header"));
}
self.allowed_headers = AccessControlAllowHeaders::Only(allowed_headers);
Ok(self)
}
pub fn continue_on_invalid_cors(mut self, continue_on_invalid_cors: bool) -> Self {
self.continue_on_invalid_cors = continue_on_invalid_cors;
self
}
pub fn build(self) -> AccessControl {
AccessControl {
allowed_hosts: self.allowed_hosts,
allowed_origins: self.allowed_origins,
allowed_headers: self.allowed_headers,
continue_on_invalid_cors: self.continue_on_invalid_cors,
}
}
}