From e989548d9fa3557ca408515c2a618d83a4030898 Mon Sep 17 00:00:00 2001 From: Fred Clausen <43556888+fredclausen@users.noreply.github.com> Date: Thu, 28 Jul 2022 09:30:22 -0600 Subject: [PATCH 1/2] Check for alpha characters in host name Also some linting because pre-commit --- .github/workflows/deploy.yml | 2 +- src/config_options.rs | 2 +- src/sanity_checker.rs | 78 ++++++++++++++++++++++++++++-------- 3 files changed, 63 insertions(+), 19 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index c5dae996..f82760db 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -185,7 +185,7 @@ jobs: - uses: mukunku/tag-exists-action@v1.0.0 id: checkTag - with: + with: tag: ${{ steps.release_version.outputs.RELEASE_VERSION }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/src/config_options.rs b/src/config_options.rs index 448d05ea..87ccf2e9 100644 --- a/src/config_options.rs +++ b/src/config_options.rs @@ -191,4 +191,4 @@ mod test { assert_eq!(trace_level_logging, LevelFilter::Trace); assert_eq!(stupid_levels_logging, LevelFilter::Trace); } -} \ No newline at end of file +} diff --git a/src/sanity_checker.rs b/src/sanity_checker.rs index e290b41f..3ae7a6cd 100644 --- a/src/sanity_checker.rs +++ b/src/sanity_checker.rs @@ -7,9 +7,9 @@ // File to verify the sanity of config options +use crate::config_options::Input; use std::net::SocketAddr; use std::str::FromStr; -use crate::config_options::Input; pub fn check_config_option_sanity(config_options: &Input) -> Result<(), String> { let mut is_input_sane = true; @@ -161,22 +161,56 @@ fn check_ports_are_valid_with_host(socket_addresses: &Option>, name: if let Some(sockets) = socket_addresses { if sockets.is_empty() { - error!("{} has been provided, but there are no socket addresses", name); + error!( + "{} has been provided, but there are no socket addresses", + name + ); is_input_sane = false; } for socket in sockets { - let parse_socket = SocketAddr::from_str(socket); - match parse_socket { - Err(parse_error) => { - error!("{}: Failed to validate that {} is a properly formatted socket: {}", name, socket, parse_error); + // FIXME: Just doing a socketaddr parse fails if the host name isn't an IP address. + + // check and see if there are alpha characters in the string + if socket + .chars() + .any(|c| !c.is_numeric() && !c.eq(&'.') && !c.eq(&':')) + { + // split the string on ':' + let socket_parts = socket.split(":").collect::>(); + let port = socket_parts[1]; + // validate the port is numeric and between 1-65535 + if !port.chars().all(|c| c.is_numeric()) { + error!("{} Port is not numeric", name); is_input_sane = false; - }, - Ok(parsed_socket) => { - if parsed_socket.port().eq(&0) { - error!("{}: Socket address is valid, but the port provided is zero!", name); + } else { + match port.parse::() { + Ok(_) => trace!("{} Port is numeric", name), + Err(_) => { + error!("{} Port is not numeric", name); + is_input_sane = false; + } + } + } + } else { + let parse_socket = SocketAddr::from_str(socket); + match parse_socket { + Err(parse_error) => { + error!( + "{}: Failed to validate that {} is a properly formatted socket: {}", + name, socket, parse_error + ); is_input_sane = false; - } else { - trace!("{} is a valid socket address", socket); + } + Ok(parsed_socket) => { + if parsed_socket.port().eq(&0) { + error!( + "{}: Socket address is valid, but the port provided is zero!", + name + ); + is_input_sane = false; + } else { + trace!("{} is a valid socket address", socket); + } } } } @@ -191,12 +225,22 @@ mod test { #[test] fn test_check_ports_are_valid_with_host() { - let valid_hosts: Option> = Some(vec!["127.0.0.1:8008".to_string(), "10.0.0.1:12345".to_string(), "192.168.1.1:65535".to_string()]); - let invalid_hosts: Option> = Some(vec!["127.0.0.1:0".to_string(), "10.0.0.1".to_string(), "192.168.1.1:65536".to_string()]); + let valid_hosts: Option> = Some(vec![ + "127.0.0.1:8008".to_string(), + "10.0.0.1:12345".to_string(), + "192.168.1.1:65535".to_string(), + ]); + let invalid_hosts: Option> = Some(vec![ + "127.0.0.1:0".to_string(), + "10.0.0.1".to_string(), + "192.168.1.1:65536".to_string(), + ]); let empty_host_vec: Option> = Some(vec![]); let valid_hosts_tests: bool = check_ports_are_valid_with_host(&valid_hosts, "valid_hosts"); - let invalid_hosts_tests: bool = check_ports_are_valid_with_host(&invalid_hosts, "invalid_hosts"); - let empty_host_vec_test: bool = check_ports_are_valid_with_host(&empty_host_vec, "empty_vec"); + let invalid_hosts_tests: bool = + check_ports_are_valid_with_host(&invalid_hosts, "invalid_hosts"); + let empty_host_vec_test: bool = + check_ports_are_valid_with_host(&empty_host_vec, "empty_vec"); assert_eq!(valid_hosts_tests, true); assert_eq!(invalid_hosts_tests, false); assert_eq!(empty_host_vec_test, false); @@ -214,4 +258,4 @@ mod test { assert_eq!(invalid_ports_test, false); assert_eq!(empty_ports_test, false); } -} \ No newline at end of file +} From 98faccbfb9badedd7549c25a46ca41da4ad19569 Mon Sep 17 00:00:00 2001 From: Fred Clausen <43556888+fredclausen@users.noreply.github.com> Date: Thu, 28 Jul 2022 09:32:15 -0600 Subject: [PATCH 2/2] bump version --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4d41eb1b..79a13535 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "acars_router" -version = "1.0.1" +version = "1.0.2" dependencies = [ "chrono", "clap", diff --git a/Cargo.toml b/Cargo.toml index 7c020f5b..673e7de6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "acars_router" -version = "1.0.1" +version = "1.0.2" edition = "2021" authors = ["Fred Clausen", "Mike Nye"] description = "ACARS Router: A Utility to ingest ACARS/VDLM2 from many sources, process, and feed out to many consumers."