Spaces:
Runtime error
Runtime error
File size: 18,935 Bytes
9552aa0 | 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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | use std::error::Error;
use std::sync::Arc;
use std::time::Duration;
use crate::ferron_util::ip_blocklist::IpBlockList;
use crate::ferron_util::ip_match::ip_match;
use crate::ferron_util::match_hostname::match_hostname;
use crate::ferron_util::match_location::match_location;
use crate::ferron_util::non_standard_code_structs::{
NonStandardCode, NonStandardCodesLocationWrap, NonStandardCodesWrap,
};
use crate::ferron_util::ttl_cache::TtlCache;
use crate::ferron_common::{
ErrorLogger, HyperResponse, RequestData, ResponseData, ServerConfig, ServerModule,
ServerModuleHandlers, SocketData,
};
use crate::ferron_common::{HyperUpgraded, WithRuntime};
use async_trait::async_trait;
use base64::{engine::general_purpose, Engine};
use fancy_regex::RegexBuilder;
use http_body_util::{BodyExt, Empty};
use hyper::header::HeaderValue;
use hyper::{header, HeaderMap, Response, StatusCode};
use hyper_tungstenite::HyperWebsocket;
use password_auth::verify_password;
use tokio::runtime::Handle;
use tokio::sync::RwLock;
use yaml_rust2::Yaml;
fn non_standard_codes_config_init(
non_standard_codes_list: &[Yaml],
) -> Result<Vec<NonStandardCode>, anyhow::Error> {
let non_standard_codes_list_iter = non_standard_codes_list.iter();
let mut non_standard_codes_list_vec = Vec::new();
for non_standard_codes_list_entry in non_standard_codes_list_iter {
let status_code: u16 = match non_standard_codes_list_entry["scode"].as_i64() {
Some(scode) => scode.try_into()?,
None => {
return Err(anyhow::anyhow!(
"Non-standard codes must include a status code"
));
}
};
let regex = match non_standard_codes_list_entry["regex"].as_str() {
Some(regex_str) => match RegexBuilder::new(regex_str)
.case_insensitive(cfg!(windows))
.build()
{
Ok(regex) => Some(regex),
Err(err) => {
return Err(anyhow::anyhow!(
"Invalid non-standard code regular expression: {}",
err.to_string()
));
}
},
None => None,
};
let url = non_standard_codes_list_entry["url"]
.as_str()
.map(|s| s.to_string());
if regex.is_none() && url.is_none() {
return Err(anyhow::anyhow!(
"Non-standard codes must either include URL or a matching regular expression"
));
}
let location = non_standard_codes_list_entry["location"]
.as_str()
.map(|s| s.to_string());
let realm = non_standard_codes_list_entry["realm"]
.as_str()
.map(|s| s.to_string());
let disable_brute_force_protection = non_standard_codes_list_entry["disableBruteProtection"]
.as_bool()
.unwrap_or(false);
let user_list = match non_standard_codes_list_entry["userList"].as_vec() {
Some(userlist) => {
let mut new_userlist = Vec::new();
for user_yaml in userlist.iter() {
if let Some(user) = user_yaml.as_str() {
new_userlist.push(user.to_string());
}
}
Some(new_userlist)
}
None => None,
};
let users = match non_standard_codes_list_entry["users"].as_vec() {
Some(users_vec) => {
let mut users_str_vec = Vec::new();
for user_yaml in users_vec.iter() {
if let Some(user) = user_yaml.as_str() {
users_str_vec.push(user);
}
}
let mut users_init = IpBlockList::new();
users_init.load_from_vec(users_str_vec);
Some(users_init)
}
None => None,
};
non_standard_codes_list_vec.push(NonStandardCode::new(
status_code,
url,
regex,
location,
realm,
disable_brute_force_protection,
user_list,
users,
));
}
Ok(non_standard_codes_list_vec)
}
pub fn server_module_init(
config: &ServerConfig,
) -> Result<Box<dyn ServerModule + Send + Sync>, Box<dyn Error + Send + Sync>> {
let mut global_non_standard_codes_list = Vec::new();
let mut host_non_standard_codes_lists = Vec::new();
if let Some(non_standard_codes_list_yaml) = config["global"]["nonStandardCodes"].as_vec() {
global_non_standard_codes_list = non_standard_codes_config_init(non_standard_codes_list_yaml)?;
}
if let Some(hosts) = config["hosts"].as_vec() {
for host_yaml in hosts.iter() {
let domain = host_yaml["domain"].as_str().map(String::from);
let ip = host_yaml["ip"].as_str().map(String::from);
let mut locations = Vec::new();
if let Some(locations_yaml) = host_yaml["locations"].as_vec() {
for location_yaml in locations_yaml.iter() {
if let Some(path_str) = location_yaml["path"].as_str() {
let path = String::from(path_str);
if let Some(non_standard_codes_list_yaml) = location_yaml["nonStandardCodes"].as_vec() {
locations.push(NonStandardCodesLocationWrap::new(
path,
non_standard_codes_config_init(non_standard_codes_list_yaml)?,
));
}
}
}
}
if let Some(non_standard_codes_list_yaml) = host_yaml["nonStandardCodes"].as_vec() {
host_non_standard_codes_lists.push(NonStandardCodesWrap::new(
domain,
ip,
non_standard_codes_config_init(non_standard_codes_list_yaml)?,
locations,
));
} else if !locations.is_empty() {
host_non_standard_codes_lists.push(NonStandardCodesWrap::new(
domain,
ip,
Vec::new(),
locations,
));
}
}
}
Ok(Box::new(NonStandardCodesModule::new(
Arc::new(global_non_standard_codes_list),
Arc::new(host_non_standard_codes_lists),
Arc::new(RwLock::new(TtlCache::new(Duration::new(300, 0)))),
)))
}
struct NonStandardCodesModule {
global_non_standard_codes_list: Arc<Vec<NonStandardCode>>,
host_non_standard_codes_lists: Arc<Vec<NonStandardCodesWrap>>,
brute_force_db: Arc<RwLock<TtlCache<String, u8>>>,
}
impl NonStandardCodesModule {
fn new(
global_non_standard_codes_list: Arc<Vec<NonStandardCode>>,
host_non_standard_codes_lists: Arc<Vec<NonStandardCodesWrap>>,
brute_force_db: Arc<RwLock<TtlCache<String, u8>>>,
) -> Self {
Self {
global_non_standard_codes_list,
host_non_standard_codes_lists,
brute_force_db,
}
}
}
impl ServerModule for NonStandardCodesModule {
fn get_handlers(&self, handle: Handle) -> Box<dyn ServerModuleHandlers + Send> {
Box::new(NonStandardCodesModuleHandlers {
global_non_standard_codes_list: self.global_non_standard_codes_list.clone(),
host_non_standard_codes_lists: self.host_non_standard_codes_lists.clone(),
brute_force_db: self.brute_force_db.clone(),
handle,
})
}
}
fn parse_basic_auth(auth_str: &str) -> Option<(String, String)> {
if let Some(base64_credentials) = auth_str.strip_prefix("Basic ") {
if let Ok(decoded) = general_purpose::STANDARD.decode(base64_credentials) {
if let Ok(decoded_str) = std::str::from_utf8(&decoded) {
let parts: Vec<&str> = decoded_str.splitn(2, ':').collect();
if parts.len() == 2 {
return Some((parts[0].to_string(), parts[1].to_string()));
}
}
}
}
None
}
struct NonStandardCodesModuleHandlers {
global_non_standard_codes_list: Arc<Vec<NonStandardCode>>,
host_non_standard_codes_lists: Arc<Vec<NonStandardCodesWrap>>,
brute_force_db: Arc<RwLock<TtlCache<String, u8>>>,
handle: Handle,
}
#[async_trait]
impl ServerModuleHandlers for NonStandardCodesModuleHandlers {
async fn request_handler(
&mut self,
request: RequestData,
config: &ServerConfig,
socket_data: &SocketData,
error_logger: &ErrorLogger,
) -> Result<ResponseData, Box<dyn Error + Send + Sync>> {
WithRuntime::new(self.handle.clone(), async move {
let hyper_request = request.get_hyper_request();
let global_non_standard_codes_list = self.global_non_standard_codes_list.iter();
let empty_vector = Vec::new();
let another_empty_vector = Vec::new();
let mut host_non_standard_codes_list = empty_vector.iter();
let mut location_non_standard_codes_list = another_empty_vector.iter();
// Should have used a HashMap instead of iterating over an array for better performance...
for host_non_standard_codes_list_wrap in self.host_non_standard_codes_lists.iter() {
if match_hostname(
match &host_non_standard_codes_list_wrap.domain {
Some(value) => Some(value as &str),
None => None,
},
match hyper_request.headers().get(header::HOST) {
Some(value) => value.to_str().ok(),
None => None,
},
) && match &host_non_standard_codes_list_wrap.ip {
Some(value) => ip_match(value as &str, socket_data.remote_addr.ip()),
None => true,
} {
host_non_standard_codes_list =
host_non_standard_codes_list_wrap.non_standard_codes.iter();
if let Ok(path_decoded) = urlencoding::decode(
request
.get_original_url()
.unwrap_or(request.get_hyper_request().uri())
.path(),
) {
for location_wrap in host_non_standard_codes_list_wrap.locations.iter() {
if match_location(&location_wrap.path, &path_decoded) {
location_non_standard_codes_list = location_wrap.non_standard_codes.iter();
break;
}
}
}
break;
}
}
let combined_non_standard_codes_list = global_non_standard_codes_list
.chain(host_non_standard_codes_list)
.chain(location_non_standard_codes_list);
let request_url = format!(
"{}{}",
hyper_request.uri().path(),
match hyper_request.uri().query() {
Some(query) => format!("?{}", query),
None => String::from(""),
}
);
let mut auth_user = None;
for non_standard_code in combined_non_standard_codes_list {
let mut redirect_url = None;
let mut url_matched = false;
if let Some(users) = &non_standard_code.users {
if !users.is_blocked(socket_data.remote_addr.ip()) {
// Don't process this non-standard code
continue;
}
}
if let Some(regex) = &non_standard_code.regex {
let regex_match_option = regex.find(&request_url)?;
if let Some(regex_match) = regex_match_option {
url_matched = true;
if non_standard_code.status_code == 301
|| non_standard_code.status_code == 302
|| non_standard_code.status_code == 307
|| non_standard_code.status_code == 308
{
let matched_text = regex_match.as_str();
if let Some(location) = &non_standard_code.location {
redirect_url = Some(regex.replace(matched_text, location).to_string());
}
}
}
}
if !url_matched {
if let Some(url) = &non_standard_code.url {
if url == hyper_request.uri().path() {
url_matched = true;
if non_standard_code.status_code == 301
|| non_standard_code.status_code == 302
|| non_standard_code.status_code == 307
|| non_standard_code.status_code == 308
{
if let Some(location) = &non_standard_code.location {
redirect_url = Some(format!(
"{}{}",
location,
match hyper_request.uri().query() {
Some(query) => format!("?{}", query),
None => String::from(""),
}
));
}
}
}
}
}
if url_matched {
match non_standard_code.status_code {
301 | 302 | 307 | 308 => {
return Ok(
ResponseData::builder(request)
.response(
Response::builder()
.status(StatusCode::from_u16(non_standard_code.status_code)?)
.header(header::LOCATION, redirect_url.unwrap_or(request_url))
.body(Empty::new().map_err(|e| match e {}).boxed())?,
)
.build(),
);
}
401 => {
let brute_force_db_key = socket_data.remote_addr.ip().to_string();
if !non_standard_code.disable_brute_force_protection {
let rwlock_read = self.brute_force_db.read().await;
let current_attempts = rwlock_read.get(&brute_force_db_key).unwrap_or(0);
if current_attempts >= 10 {
error_logger
.log(&format!(
"Too many failed authorization attempts for client \"{}\"",
socket_data.remote_addr.ip()
))
.await;
return Ok(
ResponseData::builder(request)
.status(StatusCode::TOO_MANY_REQUESTS)
.build(),
);
}
}
let mut header_map = HeaderMap::new();
header_map.insert(
header::WWW_AUTHENTICATE,
HeaderValue::from_str(&format!(
"Basic realm=\"{}\", charset=\"UTF-8\"",
non_standard_code
.realm
.clone()
.unwrap_or("Ferron HTTP Basic Authorization".to_string())
.replace("\\", "\\\\")
.replace("\"", "\\\"")
))?,
);
if let Some(authorization_header_value) =
hyper_request.headers().get(header::AUTHORIZATION)
{
let authorization_str = match authorization_header_value.to_str() {
Ok(str) => str,
Err(_) => {
return Ok(
ResponseData::builder(request)
.status(StatusCode::BAD_REQUEST)
.build(),
);
}
};
if let Some((username, password)) = parse_basic_auth(authorization_str) {
if let Some(users_vec_yaml) = config["users"].as_vec() {
let mut authorized_user = None;
for user_yaml in users_vec_yaml {
if let Some(username_db) = user_yaml["name"].as_str() {
if username_db != username {
continue;
}
if let Some(user_list) = &non_standard_code.user_list {
if !user_list.contains(&username) {
continue;
}
}
if let Some(password_hash_db) = user_yaml["pass"].as_str() {
let password_cloned = password.clone();
let password_hash_db_cloned = password_hash_db.to_string();
// Offload verifying the hash into a separate blocking thread.
let password_valid = tokio::task::spawn_blocking(move || {
verify_password(password_cloned, &password_hash_db_cloned).is_ok()
})
.await?;
if password_valid {
authorized_user = Some(&username);
break;
}
}
}
}
if let Some(authorized_user) = authorized_user {
auth_user = Some(authorized_user.to_owned());
continue;
}
}
if !non_standard_code.disable_brute_force_protection {
let mut rwlock_write = self.brute_force_db.write().await;
rwlock_write.cleanup();
let current_attempts = rwlock_write.get(&brute_force_db_key).unwrap_or(0);
rwlock_write.insert(brute_force_db_key, current_attempts + 1);
}
error_logger
.log(&format!(
"Authorization failed for user \"{}\" and client \"{}\"",
username,
socket_data.remote_addr.ip()
))
.await;
}
}
return Ok(
ResponseData::builder(request)
.status(StatusCode::UNAUTHORIZED)
.headers(header_map)
.build(),
);
}
_ => {
return Ok(
ResponseData::builder(request)
.status(StatusCode::from_u16(non_standard_code.status_code)?)
.build(),
)
}
}
}
}
if auth_user.is_some() {
let (hyper_request, _, original_url) = request.into_parts();
Ok(ResponseData::builder(RequestData::new(hyper_request, auth_user, original_url)).build())
} else {
Ok(ResponseData::builder(request).build())
}
})
.await
}
async fn proxy_request_handler(
&mut self,
request: RequestData,
_config: &ServerConfig,
_socket_data: &SocketData,
_error_logger: &ErrorLogger,
) -> Result<ResponseData, Box<dyn Error + Send + Sync>> {
Ok(ResponseData::builder(request).build())
}
async fn response_modifying_handler(
&mut self,
response: HyperResponse,
) -> Result<HyperResponse, Box<dyn Error + Send + Sync>> {
Ok(response)
}
async fn proxy_response_modifying_handler(
&mut self,
response: HyperResponse,
) -> Result<HyperResponse, Box<dyn Error + Send + Sync>> {
Ok(response)
}
async fn connect_proxy_request_handler(
&mut self,
_upgraded_request: HyperUpgraded,
_connect_address: &str,
_config: &ServerConfig,
_socket_data: &SocketData,
_error_logger: &ErrorLogger,
) -> Result<(), Box<dyn Error + Send + Sync>> {
Ok(())
}
fn does_connect_proxy_requests(&mut self) -> bool {
false
}
async fn websocket_request_handler(
&mut self,
_websocket: HyperWebsocket,
_uri: &hyper::Uri,
_config: &ServerConfig,
_socket_data: &SocketData,
_error_logger: &ErrorLogger,
) -> Result<(), Box<dyn Error + Send + Sync>> {
Ok(())
}
fn does_websocket_requests(&mut self, _config: &ServerConfig, _socket_data: &SocketData) -> bool {
false
}
}
|