Spaces:
Runtime error
Runtime error
File size: 37,916 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 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 | use crate::ferron_common::ServerConfig;
use hyper::header::{HeaderName, HeaderValue};
use std::collections::HashSet;
use std::error::Error;
use std::net::IpAddr;
use std::str::FromStr;
use yaml_rust2::{yaml, Yaml};
// Struct to store used configuration properties
struct UsedProperties<'a> {
config: &'a ServerConfig,
properties: HashSet<String>,
}
impl<'a> UsedProperties<'a> {
fn new(config: &'a ServerConfig) -> Self {
UsedProperties {
config,
properties: HashSet::new(),
}
}
fn contains(&mut self, property: &str) -> bool {
self.properties.insert(property.to_string());
!self.config[property].is_badvalue()
}
fn unused(&self) -> Vec<String> {
let empty_hashmap = yaml::Hash::new();
let all_properties = self
.config
.as_hash()
.unwrap_or(&empty_hashmap)
.keys()
.filter_map(|a| a.as_str().map(|a| a.to_string()));
all_properties
.filter(|item| !self.properties.contains(item))
.collect()
}
}
fn validate_ip(ip: &str) -> bool {
let _: IpAddr = match ip.parse() {
Ok(addr) => addr,
Err(_) => return false,
};
true
}
// Internal configuration file validators
pub fn validate_config(
config: ServerConfig,
is_global: bool,
is_location: bool,
modules_optional_builtin: &[String],
) -> Result<Vec<String>, Box<dyn Error + Send + Sync>> {
let mut used_properties = UsedProperties::new(&config);
let domain_badvalue = !used_properties.contains("domain");
let ip_badvalue = !used_properties.contains("ip");
if !domain_badvalue && config["domain"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid domain name"))?
}
if !ip_badvalue {
match config["ip"].as_str() {
Some(ip) => {
if !validate_ip(ip) {
Err(anyhow::anyhow!("Invalid IP address"))?;
}
}
None => {
Err(anyhow::anyhow!("Invalid IP address"))?;
}
}
}
if domain_badvalue && ip_badvalue && !is_global && !is_location {
Err(anyhow::anyhow!(
"A host must either have IP address or domain name specified"
))?;
}
if used_properties.contains("path") {
if !is_location {
Err(anyhow::anyhow!(
"Location path configuration is only allowed in location configuration"
))?;
}
if config["path"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid location path"))?;
}
}
if used_properties.contains("locations") && is_location {
Err(anyhow::anyhow!("Nested locations are not allowed"))?;
}
if used_properties.contains("loadModules") {
if !is_global {
Err(anyhow::anyhow!(
"Module configuration is not allowed in host configuration"
))?
}
if let Some(modules) = config["loadModules"].as_vec() {
let modules_iter = modules.iter();
for module_name_yaml in modules_iter {
if module_name_yaml.as_str().is_none() {
Err(anyhow::anyhow!("Invalid module name"))?
}
}
} else {
Err(anyhow::anyhow!("Invalid module configuration"))?
}
}
if used_properties.contains("port") {
if !is_global {
Err(anyhow::anyhow!(
"HTTP port configuration is not allowed in host configuration"
))?
}
if let Some(port) = config["port"].as_i64() {
if !(0..=65535).contains(&port) {
Err(anyhow::anyhow!("Invalid HTTP port"))?
}
} else if config["port"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid HTTP port"))?
}
}
if used_properties.contains("sport") {
if !is_global {
Err(anyhow::anyhow!(
"HTTPS port configuration is not allowed in host configuration"
))?
}
if let Some(port) = config["sport"].as_i64() {
if !(0..=65535).contains(&port) {
Err(anyhow::anyhow!("Invalid HTTPS port"))?
}
} else if config["sport"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid HTTPS port"))?
}
}
if used_properties.contains("secure") {
if !is_global {
Err(anyhow::anyhow!(
"HTTPS enabling configuration is not allowed in host configuration"
))?
}
if config["secure"].as_bool().is_none() {
Err(anyhow::anyhow!("Invalid HTTPS enabling option value"))?
}
}
if used_properties.contains("enableHTTP2") {
if !is_global {
Err(anyhow::anyhow!(
"HTTP/2 enabling configuration is not allowed in host configuration"
))?
}
if config["enableHTTP2"].as_bool().is_none() {
Err(anyhow::anyhow!("Invalid HTTP/2 enabling option value"))?
}
}
if used_properties.contains("enableHTTP3") {
if !is_global {
Err(anyhow::anyhow!(
"HTTP/3 enabling configuration is not allowed in host configuration"
))?
}
if config["enableHTTP3"].as_bool().is_none() {
Err(anyhow::anyhow!("Invalid HTTP/3 enabling option value"))?
}
}
if used_properties.contains("logFilePath") {
if !is_global {
Err(anyhow::anyhow!(
"Log file configuration is not allowed in host configuration"
))?
}
if config["logFilePath"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid log file path"))?
}
}
if used_properties.contains("errorLogFilePath") {
if !is_global {
Err(anyhow::anyhow!(
"Error log file configuration is not allowed in host configuration"
))?
}
if config["errorLogFilePath"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid error log file path"))?
}
}
if used_properties.contains("cert") {
if !is_global {
Err(anyhow::anyhow!(
"TLS certificate configuration is not allowed in host configuration"
))?
}
if config["cert"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid TLS certificate path"))?
}
}
if used_properties.contains("key") {
if !is_global {
Err(anyhow::anyhow!(
"Private key configuration is not allowed in host configuration"
))?
}
if config["key"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid private key path"))?
}
}
if used_properties.contains("sni") {
if !is_global {
Err(anyhow::anyhow!(
"SNI configuration is not allowed in host configuration"
))?
}
if let Some(sni) = config["sni"].as_hash() {
let sni_hostnames = sni.keys();
for sni_hostname_unknown in sni_hostnames {
if let Some(sni_hostname) = sni_hostname_unknown.as_str() {
if sni[sni_hostname_unknown]["cert"].as_str().is_none() {
Err(anyhow::anyhow!(
"Invalid SNI TLS certificate path for \"{}\"",
sni_hostname
))?
}
if sni[sni_hostname_unknown]["key"].as_str().is_none() {
Err(anyhow::anyhow!(
"Invalid SNI private key certificate path for \"{}\"",
sni_hostname
))?
}
} else {
Err(anyhow::anyhow!("Invalid SNI hostname"))?
}
}
} else {
Err(anyhow::anyhow!("Invalid SNI certificate list"))?
}
}
if used_properties.contains("http2Options") {
if !is_global {
Err(anyhow::anyhow!(
"HTTP/2 configuration is not allowed in host configuration"
))?
}
if config["http2Options"].as_hash().is_some() {
if let Some(initial_window_size) = config["http2Options"]["initialWindowSize"].as_i64() {
if !(0..=2_147_483_647).contains(&initial_window_size) {
Err(anyhow::anyhow!("Invalid HTTP/2 initial window size"))?
}
}
if let Some(max_frame_size) = config["http2Options"]["maxFrameSize"].as_i64() {
if !(16_384..=16_777_215).contains(&max_frame_size) {
Err(anyhow::anyhow!("Invalid HTTP/2 max frame size"))?
}
}
if let Some(max_concurrent_streams) = config["http2Options"]["maxConcurrentStreams"].as_i64()
{
if max_concurrent_streams < 0 {
Err(anyhow::anyhow!("Invalid HTTP/2 max concurrent streams"))?
}
}
if let Some(max_header_list_size) = config["http2Options"]["maxHeaderListSize"].as_i64() {
if max_header_list_size < 0 {
Err(anyhow::anyhow!("Invalid HTTP/2 max header list size"))?
}
}
if !config["http2Options"]["enableConnectProtocol"].is_badvalue()
&& config["http2Options"]["enableConnectProtocol"]
.as_bool()
.is_none()
{
Err(anyhow::anyhow!(
"Invalid HTTP/2 enable connect protocol option"
))?
}
} else {
Err(anyhow::anyhow!("Invalid HTTP/2 options"))?
}
}
if used_properties.contains("useClientCertificate") {
if !is_global {
Err(anyhow::anyhow!(
"Client certificate verfication enabling option is not allowed in host configuration"
))?
}
if config["useClientCertificate"].as_bool().is_none() {
Err(anyhow::anyhow!(
"Invalid client certificate verification enabling option value"
))?
}
}
if used_properties.contains("cipherSuite") {
if !is_global {
Err(anyhow::anyhow!(
"Cipher suite configuration is not allowed in host configuration"
))?
}
if let Some(cipher_suites) = config["cipherSuite"].as_vec() {
let cipher_suites_iter = cipher_suites.iter();
for cipher_suite_name_yaml in cipher_suites_iter {
if cipher_suite_name_yaml.as_str().is_none() {
Err(anyhow::anyhow!("Invalid cipher suite"))?
}
}
} else {
Err(anyhow::anyhow!("Invalid cipher suite configuration"))?
}
}
if used_properties.contains("ecdhCurve") {
if !is_global {
Err(anyhow::anyhow!(
"ECDH curve configuration is not allowed in host configuration"
))?
}
if let Some(ecdh_curves) = config["ecdhCurve"].as_vec() {
let ecdh_curves_iter = ecdh_curves.iter();
for ecdh_curve_name_yaml in ecdh_curves_iter {
if ecdh_curve_name_yaml.as_str().is_none() {
Err(anyhow::anyhow!("Invalid ECDH curve"))?
}
}
} else {
Err(anyhow::anyhow!("Invalid ECDH curve configuration"))?
}
}
if used_properties.contains("tlsMinVersion") {
if !is_global {
Err(anyhow::anyhow!(
"Minimum TLS version is not allowed in host configuration"
))?
}
if config["tlsMinVersion"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid minimum TLS version"))?
}
}
if used_properties.contains("tlsMaxVersion") {
if !is_global {
Err(anyhow::anyhow!(
"Maximum TLS version is not allowed in host configuration"
))?
}
if config["tlsMaxVersion"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid maximum TLS version"))?
}
}
if used_properties.contains("enableOCSPStapling") {
if !is_global {
Err(anyhow::anyhow!(
"OCSP stapling enabling option is not allowed in host configuration"
))?
}
if config["enableOCSPStapling"].as_bool().is_none() {
Err(anyhow::anyhow!(
"Invalid OCSP stapling enabling option value"
))?
}
}
if used_properties.contains("serverAdministratorEmail")
&& config["serverAdministratorEmail"].as_str().is_none()
{
Err(anyhow::anyhow!(
"Invalid server administrator email address"
))?
}
if used_properties.contains("enableIPSpoofing") && config["enableIPSpoofing"].as_bool().is_none()
{
Err(anyhow::anyhow!(
"Invalid X-Forwarded-For enabling option value"
))?
}
if used_properties.contains("disableNonEncryptedServer") {
if !is_global {
Err(anyhow::anyhow!(
"Non-encrypted server disabling option is not allowed in host configuration"
))?
}
if config["disableNonEncryptedServer"].as_bool().is_none() {
Err(anyhow::anyhow!(
"Invalid non-encrypted server disabling option value"
))?
}
}
if used_properties.contains("blocklist") {
if !is_global {
Err(anyhow::anyhow!(
"Block list configuration is not allowed in host configuration"
))?
}
if let Some(blocklist) = config["blocklist"].as_vec() {
let blocklist_iter = blocklist.iter();
for blocklist_entry_yaml in blocklist_iter {
match blocklist_entry_yaml.as_str() {
Some(blocklist_entry) => {
if !validate_ip(blocklist_entry) {
Err(anyhow::anyhow!("Invalid block list entry"))?
}
}
None => Err(anyhow::anyhow!("Invalid block list entry"))?,
}
}
} else {
Err(anyhow::anyhow!("Invalid block list configuration"))?
}
}
if used_properties.contains("environmentVariables") {
if !is_global {
Err(anyhow::anyhow!(
"Environment variable configuration is not allowed in host configuration"
))?
}
if let Some(environment_variables_hash) = config["environmentVariables"].as_hash() {
let environment_variables_hash_iter = environment_variables_hash.iter();
for (var_name, var_value) in environment_variables_hash_iter {
if var_name.as_str().is_none() || var_value.as_str().is_none() {
Err(anyhow::anyhow!("Invalid environment variables"))?
}
}
} else {
Err(anyhow::anyhow!("Invalid environment variables"))?
}
}
if used_properties.contains("disableToHTTPSRedirect")
&& config["disableToHTTPSRedirect"].as_bool().is_none()
{
Err(anyhow::anyhow!(
"Invalid HTTP to HTTPS redirect disabling option value"
))?
}
if used_properties.contains("wwwredirect") && config["wwwredirect"].as_bool().is_none() {
Err(anyhow::anyhow!(
"Invalid to \"www.\" URL redirect disabling option value"
))?
}
if used_properties.contains("customHeaders") {
if let Some(custom_headers_hash) = config["customHeaders"].as_hash() {
let custom_headers_hash_iter = custom_headers_hash.iter();
for (header_name, header_value) in custom_headers_hash_iter {
if let Some(header_name) = header_name.as_str() {
if let Some(header_value) = header_value.as_str() {
if HeaderValue::from_str(header_value).is_err()
|| HeaderName::from_str(header_name).is_err()
{
Err(anyhow::anyhow!("Invalid custom headers"))?
}
} else {
Err(anyhow::anyhow!("Invalid custom headers"))?
}
} else {
Err(anyhow::anyhow!("Invalid custom headers"))?
}
}
} else {
Err(anyhow::anyhow!("Invalid custom headers"))?
}
}
if used_properties.contains("rewriteMap") {
if let Some(rewrite_map) = config["rewriteMap"].as_vec() {
let rewrite_map_iter = rewrite_map.iter();
for rewrite_map_entry_yaml in rewrite_map_iter {
if !rewrite_map_entry_yaml.is_hash() {
Err(anyhow::anyhow!("Invalid URL rewrite map"))?
}
if rewrite_map_entry_yaml["regex"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid URL rewrite map"))?
}
if rewrite_map_entry_yaml["replacement"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid URL rewrite map"))?
}
if !rewrite_map_entry_yaml["isNotFile"].is_badvalue()
&& rewrite_map_entry_yaml["isNotFile"].as_bool().is_none()
{
Err(anyhow::anyhow!("Invalid URL rewrite map"))?
}
if !rewrite_map_entry_yaml["isNotDirectory"].is_badvalue()
&& rewrite_map_entry_yaml["isNotDirectory"].as_bool().is_none()
{
Err(anyhow::anyhow!("Invalid URL rewrite map"))?
}
if !rewrite_map_entry_yaml["allowDoubleSlashes"].is_badvalue()
&& rewrite_map_entry_yaml["allowDoubleSlashes"]
.as_bool()
.is_none()
{
Err(anyhow::anyhow!("Invalid URL rewrite map"))?
}
if !rewrite_map_entry_yaml["last"].is_badvalue()
&& rewrite_map_entry_yaml["last"].as_bool().is_none()
{
Err(anyhow::anyhow!("Invalid URL rewrite map"))?
}
}
} else {
Err(anyhow::anyhow!("Invalid URL rewrite map"))?
}
}
if used_properties.contains("enableRewriteLogging")
&& config["enableRewriteLogging"].as_bool().is_none()
{
Err(anyhow::anyhow!(
"Invalid URL rewrite logging enabling option value"
))?
}
if used_properties.contains("disableTrailingSlashRedirects")
&& config["disableTrailingSlashRedirects"].as_bool().is_none()
{
Err(anyhow::anyhow!(
"Invalid trailing slash redirect disabling option value"
))?
}
if used_properties.contains("users") {
if let Some(users) = config["users"].as_vec() {
let users_iter = users.iter();
for user_yaml in users_iter {
if !user_yaml.is_hash() {
Err(anyhow::anyhow!("Invalid user configuration"))?
}
if user_yaml["name"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid user configuration"))?
}
if user_yaml["pass"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid user configuration"))?
}
}
} else {
Err(anyhow::anyhow!("Invalid user configuration"))?
}
}
if used_properties.contains("nonStandardCodes") {
if let Some(non_standard_codes) = config["nonStandardCodes"].as_vec() {
let non_standard_codes_iter = non_standard_codes.iter();
for non_standard_code_yaml in non_standard_codes_iter {
if !non_standard_code_yaml.is_hash() {
Err(anyhow::anyhow!(
"Invalid non-standard status code configuration"
))?
}
if non_standard_code_yaml["scode"].as_i64().is_none() {
Err(anyhow::anyhow!(
"Invalid non-standard status code configuration"
))?
}
if !non_standard_code_yaml["regex"].is_badvalue()
&& non_standard_code_yaml["regex"].as_str().is_none()
{
Err(anyhow::anyhow!(
"Invalid non-standard status code configuration"
))?
}
if !non_standard_code_yaml["url"].is_badvalue()
&& non_standard_code_yaml["url"].as_str().is_none()
{
Err(anyhow::anyhow!(
"Invalid non-standard status code configuration"
))?
}
if non_standard_code_yaml["regex"].is_badvalue()
&& non_standard_code_yaml["url"].is_badvalue()
{
Err(anyhow::anyhow!(
"Invalid non-standard status code configuration"
))?
}
if !non_standard_code_yaml["realm"].is_badvalue()
&& non_standard_code_yaml["realm"].as_str().is_none()
{
Err(anyhow::anyhow!(
"Invalid non-standard status code configuration"
))?
}
if !non_standard_code_yaml["disableBruteProtection"].is_badvalue()
&& non_standard_code_yaml["disableBruteProtection"]
.as_bool()
.is_none()
{
Err(anyhow::anyhow!(
"Invalid non-standard status code configuration"
))?
}
if !non_standard_code_yaml["userList"].is_badvalue() {
if let Some(users) = non_standard_code_yaml["userList"].as_vec() {
let users_iter = users.iter();
for user_yaml in users_iter {
if user_yaml.as_str().is_none() {
Err(anyhow::anyhow!(
"Invalid non-standard status code configuration"
))?
}
}
} else {
Err(anyhow::anyhow!(
"Invalid non-standard status code configuration"
))?
}
}
if !non_standard_code_yaml["users"].is_badvalue() {
if let Some(users) = non_standard_code_yaml["users"].as_vec() {
let users_iter = users.iter();
for user_yaml in users_iter {
match user_yaml.as_str() {
Some(user) => {
if !validate_ip(user) {
Err(anyhow::anyhow!(
"Invalid non-standard status code configuration"
))?
}
}
None => Err(anyhow::anyhow!(
"Invalid non-standard status code configuration"
))?,
}
}
} else {
Err(anyhow::anyhow!(
"Invalid non-standard status code configuration"
))?
}
}
}
} else {
Err(anyhow::anyhow!(
"Invalid non-standard status code configuration"
))?
}
}
if used_properties.contains("errorPages") {
if let Some(error_pages) = config["errorPages"].as_vec() {
let error_pages_iter = error_pages.iter();
for error_page_yaml in error_pages_iter {
if !error_page_yaml.is_hash() {
Err(anyhow::anyhow!("Invalid custom error page configuration"))?
}
if error_page_yaml["scode"].as_i64().is_none() {
Err(anyhow::anyhow!("Invalid custom error page configuration"))?
}
if error_page_yaml["path"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid custom error page configuration"))?
}
}
} else {
Err(anyhow::anyhow!("Invalid custom error page configuration"))?
}
}
if used_properties.contains("wwwroot") && config["wwwroot"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid webroot"))?
}
if used_properties.contains("enableETag") && config["enableETag"].as_bool().is_none() {
Err(anyhow::anyhow!("Invalid ETag enabling option"))?
}
if used_properties.contains("enableCompression")
&& config["enableCompression"].as_bool().is_none()
{
Err(anyhow::anyhow!("Invalid HTTP compression enabling option"))?
}
if used_properties.contains("enableDirectoryListing")
&& config["enableDirectoryListing"].as_bool().is_none()
{
Err(anyhow::anyhow!("Invalid directory listing enabling option"))?
}
if used_properties.contains("enableAutomaticTLS") {
if !is_global {
Err(anyhow::anyhow!(
"Automatic TLS enabling configuration is not allowed in host configuration"
))?
}
if config["enableAutomaticTLS"].as_bool().is_none() {
Err(anyhow::anyhow!(
"Invalid automatic TLS enabling option value"
))?
}
}
if used_properties.contains("useAutomaticTLSHTTPChallenge") {
if !is_global {
Err(anyhow::anyhow!(
"Automatic TLS HTTP challenge enabling configuration is not allowed in host configuration"
))?
}
if config["useAutomaticTLSHTTPChallenge"].as_bool().is_none() {
Err(anyhow::anyhow!(
"Invalid automatic TLS HTTP challenge enabling option value"
))?
}
}
if used_properties.contains("automaticTLSContactEmail") {
if !is_global {
Err(anyhow::anyhow!(
"Automatic TLS contact email address configuration is not allowed in host configuration"
))?
}
if config["automaticTLSContactEmail"].as_str().is_none() {
Err(anyhow::anyhow!(
"Invalid automatic TLS contact email address"
))?
}
}
if used_properties.contains("automaticTLSContactCacheDirectory") {
if !is_global {
Err(anyhow::anyhow!(
"Automatic TLS cache directory configuration is not allowed in host configuration"
))?
}
if config["automaticTLSContactCacheDirectory"]
.as_str()
.is_none()
{
Err(anyhow::anyhow!(
"Invalid automatic TLS cache directory path"
))?
}
}
if used_properties.contains("automaticTLSLetsEncryptProduction") {
if !is_global {
Err(anyhow::anyhow!(
"Let's Encrypt production endpoint for automatic TLS enabling configuration is not allowed in host configuration"
))?
}
if config["automaticTLSLetsEncryptProduction"]
.as_bool()
.is_none()
{
Err(anyhow::anyhow!(
"Invalid Let's Encrypt production endpoint for automatic TLS enabling option value"
))?
}
}
if used_properties.contains("timeout") {
if !is_global {
Err(anyhow::anyhow!(
"Server timeout configuration is not allowed in host configuration"
))?
}
if !config["timeout"].is_null() {
if let Some(maximum_cache_response_size) = config["timeout"].as_i64() {
if maximum_cache_response_size < 0 {
Err(anyhow::anyhow!("Invalid server timeout"))?
}
} else {
Err(anyhow::anyhow!("Invalid server timeout"))?
}
}
}
for module_optional_builtin in modules_optional_builtin.iter() {
match module_optional_builtin as &str {
#[cfg(feature = "rproxy")]
"rproxy" => {
if used_properties.contains("proxyTo") {
if let Some(proxy_urls) = config["proxyTo"].as_vec() {
let proxy_urls_iter = proxy_urls.iter();
for proxy_url_yaml in proxy_urls_iter {
if proxy_url_yaml.as_str().is_none() {
Err(anyhow::anyhow!("Invalid reverse proxy target URL value"))?
}
}
} else if config["proxyTo"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid reverse proxy target URL value"))?
}
}
if used_properties.contains("secureProxyTo") {
if let Some(proxy_urls) = config["secureProxyTo"].as_vec() {
let proxy_urls_iter = proxy_urls.iter();
for proxy_url_yaml in proxy_urls_iter {
if proxy_url_yaml.as_str().is_none() {
Err(anyhow::anyhow!(
"Invalid secure reverse proxy target URL value"
))?
}
}
} else if config["secureProxyTo"].as_str().is_none() {
Err(anyhow::anyhow!(
"Invalid secure reverse proxy target URL value"
))?
}
}
if used_properties.contains("enableLoadBalancerHealthCheck")
&& config["enableLoadBalancerHealthCheck"].as_bool().is_none()
{
Err(anyhow::anyhow!(
"Invalid load balancer health check enabling option value"
))?
}
if used_properties.contains("loadBalancerHealthCheckMaximumFails") {
if let Some(window) = config["loadBalancerHealthCheckMaximumFails"].as_i64() {
if window < 0 {
Err(anyhow::anyhow!(
"Invalid load balancer health check maximum fails value"
))?
}
} else {
Err(anyhow::anyhow!(
"Invalid load balancer health check maximum fails value"
))?
}
}
if used_properties.contains("loadBalancerHealthCheckWindow") {
if !is_global {
Err(anyhow::anyhow!(
"Load balancer health check window configuration is not allowed in host configuration"
))?
}
if let Some(window) = config["loadBalancerHealthCheckWindow"].as_i64() {
if window < 0 {
Err(anyhow::anyhow!(
"Invalid load balancer health check window value"
))?
}
} else {
Err(anyhow::anyhow!(
"Invalid load balancer health check window value"
))?
}
}
if used_properties.contains("disableProxyCertificateVerification")
&& config["disableProxyCertificateVerification"]
.as_bool()
.is_none()
{
Err(anyhow::anyhow!(
"Invalid proxy certificate verification disabling option value"
))?
}
}
#[cfg(feature = "cache")]
"cache" => {
if used_properties.contains("cacheVaryHeaders") {
if let Some(modules) = config["cacheVaryHeaders"].as_vec() {
let modules_iter = modules.iter();
for module_name_yaml in modules_iter {
if module_name_yaml.as_str().is_none() {
Err(anyhow::anyhow!("Invalid varying cache header"))?
}
}
} else {
Err(anyhow::anyhow!(
"Invalid varying cache headers configuration"
))?
}
}
if used_properties.contains("cacheIgnoreHeaders") {
if let Some(modules) = config["cacheIgnoreHeaders"].as_vec() {
let modules_iter = modules.iter();
for module_name_yaml in modules_iter {
if module_name_yaml.as_str().is_none() {
Err(anyhow::anyhow!("Invalid ignored cache header"))?
}
}
} else {
Err(anyhow::anyhow!(
"Invalid ignored cache headers configuration"
))?
}
}
if used_properties.contains("maximumCacheResponseSize")
&& !config["maximumCacheResponseSize"].is_null()
{
if let Some(maximum_cache_response_size) = config["maximumCacheResponseSize"].as_i64() {
if maximum_cache_response_size < 0 {
Err(anyhow::anyhow!("Invalid maximum cache response size"))?
}
} else {
Err(anyhow::anyhow!("Invalid maximum cache response size"))?
}
}
if used_properties.contains("maximumCacheEntries") {
if !is_global {
Err(anyhow::anyhow!(
"Maximum cache entries configuration is not allowed in host configuration"
))?
}
if !config["maximumCacheEntries"].is_null() {
if let Some(maximum_cache_response_size) = config["maximumCacheEntries"].as_i64() {
if maximum_cache_response_size < 0 {
Err(anyhow::anyhow!("Invalid maximum cache entries"))?
}
} else {
Err(anyhow::anyhow!("Invalid maximum cache entries"))?
}
}
}
}
#[cfg(feature = "cgi")]
"cgi" => {
if used_properties.contains("cgiScriptExtensions") {
if let Some(cgi_script_extensions) = config["cgiScriptExtensions"].as_vec() {
let cgi_script_extensions_iter = cgi_script_extensions.iter();
for cgi_script_extension_yaml in cgi_script_extensions_iter {
if cgi_script_extension_yaml.as_str().is_none() {
Err(anyhow::anyhow!("Invalid CGI script extension"))?
}
}
} else {
Err(anyhow::anyhow!(
"Invalid CGI script extension configuration"
))?
}
}
if used_properties.contains("cgiScriptInterpreters") {
if let Some(cgi_script_interpreters) = config["cgiScriptInterpreters"].as_hash() {
for (cgi_script_interpreter_extension_unknown, cgi_script_interpreter_params_unknown) in
cgi_script_interpreters.iter()
{
if cgi_script_interpreter_extension_unknown.as_str().is_some() {
if !cgi_script_interpreter_params_unknown.is_null() {
if let Some(cgi_script_interpreter_params) =
cgi_script_interpreter_params_unknown.as_vec()
{
let cgi_script_interpreter_params_iter = cgi_script_interpreter_params.iter();
for cgi_script_interpreter_param_yaml in cgi_script_interpreter_params_iter {
if cgi_script_interpreter_param_yaml.as_str().is_none() {
Err(anyhow::anyhow!("Invalid CGI script interpreter parameter"))?
}
}
} else {
Err(anyhow::anyhow!("Invalid CGI script interpreter parameters"))?
}
}
} else {
Err(anyhow::anyhow!("Invalid CGI script interpreter extension"))?
}
}
} else {
Err(anyhow::anyhow!(
"Invalid CGI script interpreter configuration"
))?
}
}
}
#[cfg(feature = "scgi")]
"scgi" => {
if used_properties.contains("scgiTo") && config["scgiTo"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid SCGI target URL value"))?
}
if used_properties.contains("scgiPath") && config["scgiPath"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid SCGI path"))?
}
}
#[cfg(feature = "fcgi")]
"fcgi" => {
if used_properties.contains("fcgiScriptExtensions") {
if let Some(fastcgi_script_extensions) = config["fcgiScriptExtensions"].as_vec() {
let fastcgi_script_extensions_iter = fastcgi_script_extensions.iter();
for fastcgi_script_extension_yaml in fastcgi_script_extensions_iter {
if fastcgi_script_extension_yaml.as_str().is_none() {
Err(anyhow::anyhow!("Invalid CGI script extension"))?
}
}
} else {
Err(anyhow::anyhow!(
"Invalid CGI script extension configuration"
))?
}
}
if used_properties.contains("fcgiTo") && config["fcgiTo"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid FastCGI target URL value"))?
}
if used_properties.contains("fcgiPath") && config["fcgiPath"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid FastCGI path"))?
}
}
#[cfg(feature = "fauth")]
"fauth" => {
if used_properties.contains("authTo") && config["authTo"].as_str().is_none() {
Err(anyhow::anyhow!(
"Invalid forwarded authentication target URL value"
))?
}
if used_properties.contains("forwardedAuthCopyHeaders") {
if let Some(modules) = config["forwardedAuthCopyHeaders"].as_vec() {
let modules_iter = modules.iter();
for module_name_yaml in modules_iter {
if module_name_yaml.as_str().is_none() {
Err(anyhow::anyhow!(
"Invalid forwarded authentication response header to copy"
))?
}
}
} else {
Err(anyhow::anyhow!(
"Invalid forwarded authentication response headers to copy configuration"
))?
}
}
}
#[cfg(feature = "wsgi")]
"wsgi" => {
if used_properties.contains("wsgiApplicationPath")
&& config["wsgiApplicationPath"].as_str().is_none()
{
Err(anyhow::anyhow!("Invalid path to the WSGI application"))?
}
if used_properties.contains("wsgiPath") && config["wsgiPath"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid WSGI request base path"))?
}
if used_properties.contains("wsgiClearModuleImportPath") {
if !is_global {
Err(anyhow::anyhow!(
"WSGI Python module import path clearing option is not allowed in host configuration"
))?
}
if config["wsgiClearModuleImportPath"].as_bool().is_none() {
Err(anyhow::anyhow!(
"Invalid WSGI Python module import path clearing option value"
))?
}
}
}
#[cfg(feature = "wsgid")]
"wsgid" => {
if used_properties.contains("wsgidApplicationPath")
&& config["wsgidApplicationPath"].as_str().is_none()
{
Err(anyhow::anyhow!(
"Invalid path to the WSGI (with pre-forked process pool) application"
))?
}
if used_properties.contains("wsgidPath") && config["wsgidPath"].as_str().is_none() {
Err(anyhow::anyhow!(
"Invalid WSGI (with pre-forked process pool) request base path"
))?
}
}
#[cfg(feature = "asgi")]
"asgi" => {
if used_properties.contains("asgiApplicationPath")
&& config["asgiApplicationPath"].as_str().is_none()
{
Err(anyhow::anyhow!("Invalid path to the ASGI application"))?
}
if used_properties.contains("asgiPath") && config["asgiPath"].as_str().is_none() {
Err(anyhow::anyhow!("Invalid ASGI request base path"))?
}
if used_properties.contains("asgiClearModuleImportPath") {
if !is_global {
Err(anyhow::anyhow!(
"ASGI Python module import path clearing option is not allowed in host configuration"
))?
}
if config["asgiClearModuleImportPath"].as_bool().is_none() {
Err(anyhow::anyhow!(
"Invalid ASGI Python module import path clearing option value"
))?
}
}
}
_ => (),
}
}
Ok(used_properties.unused())
}
pub fn prepare_config_for_validation(
config: &Yaml,
) -> Result<impl Iterator<Item = (Yaml, bool, bool)>, Box<dyn Error + Send + Sync>> {
let mut vector = Vec::new();
if let Some(global_config) = config["global"].as_hash() {
let global_config_yaml = Yaml::Hash(global_config.clone());
vector.push(global_config_yaml);
}
let mut vector2 = Vec::new();
let mut vector3 = Vec::new();
if !config["hosts"].is_badvalue() {
if let Some(hosts) = config["hosts"].as_vec() {
for host in hosts.iter() {
if !host["locations"].is_badvalue() {
if let Some(locations) = host["locations"].as_vec() {
vector3.append(&mut locations.clone());
} else {
return Err(anyhow::anyhow!("Invalid location configuration").into());
}
}
}
vector2 = hosts.clone();
} else {
return Err(anyhow::anyhow!("Invalid virtual host configuration").into());
}
}
let iter = vector
.into_iter()
.map(|item| (item, true, false))
.chain(vector2.into_iter().map(|item| (item, false, false)))
.chain(vector3.into_iter().map(|item| (item, false, true)));
Ok(iter)
}
|