From 22d2a4626d08cac79c509e28fbdf141c8d92c511 Mon Sep 17 00:00:00 2001 From: Victor Pellan Date: Wed, 7 Jan 2026 11:55:42 +0100 Subject: [PATCH 1/4] Add >100mb check for stable config files --- libdd-library-config/src/lib.rs | 104 +++++++++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 10 deletions(-) diff --git a/libdd-library-config/src/lib.rs b/libdd-library-config/src/lib.rs index 61f535b304..0f63b5d8a0 100644 --- a/libdd-library-config/src/lib.rs +++ b/libdd-library-config/src/lib.rs @@ -543,13 +543,27 @@ impl Configurator { debug_messages.push(format!("\tlocal: {path_local:?}")); debug_messages.push(format!("\tfleet: {path_managed:?}")); } + let local_config = match fs::File::open(path_local) { - Ok(file) => match self.parse_stable_config_file(file) { - LoggedResult::Ok(config, logs) => { - debug_messages.extend(logs); - config + Ok(file) => { + match file.metadata() { + Ok(metadata) => { + // Fail if the file is > 100mb + if metadata.len() > 1024 * 1024 * 100 { + let anyhow_error = anyhow::anyhow!("Local file is too large (> 100mb)"); + let logged_result = LoggedResult::Err(anyhow_error); + return logged_result; + } + } + Err(e) => return LoggedResult::Err(anyhow::Error::from(e).context("failed to get file metadata")), + } + match self.parse_stable_config_file(file) { + LoggedResult::Ok(config, logs) => { + debug_messages.extend(logs); + config + } + LoggedResult::Err(e) => return LoggedResult::Err(e), } - LoggedResult::Err(e) => return LoggedResult::Err(e), }, Err(e) if e.kind() == io::ErrorKind::NotFound => StableConfig::default(), Err(e) => { @@ -559,12 +573,24 @@ impl Configurator { } }; let fleet_config = match fs::File::open(path_managed) { - Ok(file) => match self.parse_stable_config_file(file) { - LoggedResult::Ok(config, logs) => { - debug_messages.extend(logs); - config + Ok(file) => { + match file.metadata() { + Ok(metadata) => { + // Fail if the file is > 100mb + if metadata.len() > 1024 * 1024 * 100 { + let test = anyhow::anyhow!("Fleet file is too large (> 100mb)"); + return LoggedResult::Err(test); + } + } + Err(e) => return LoggedResult::Err(anyhow::Error::from(e).context("failed to get file metadata")), + } + match self.parse_stable_config_file(file) { + LoggedResult::Ok(config, logs) => { + debug_messages.extend(logs); + config + } + LoggedResult::Err(e) => return LoggedResult::Err(e), } - LoggedResult::Err(e) => return LoggedResult::Err(e), }, Err(e) if e.kind() == io::ErrorKind::NotFound => StableConfig::default(), Err(e) => { @@ -865,6 +891,64 @@ mod tests { } } + #[test] + fn test_large_local_file() { + let configurator = Configurator::new(true); + let mut temp_local_file = tempfile::NamedTempFile::new().unwrap(); + let temp_fleet_file = tempfile::NamedTempFile::new().unwrap(); + let mut large_file = Vec::new(); + for _ in 0..(1024 * 1024 * 100 + 1) { + large_file.push(b'a'); + } + temp_local_file.write_all(&large_file).unwrap(); + let temp_local_path = temp_local_file.into_temp_path(); + let temp_fleet_path = temp_fleet_file.into_temp_path(); + let result = configurator.get_config_from_file( + temp_local_path.to_str().unwrap().as_ref(), + temp_fleet_path.to_str().unwrap().as_ref(), + &ProcessInfo { + args: vec![b"-jar HelloWorld.jar".to_vec()], + envp: vec![b"ENV=VAR".to_vec()], + language: b"java".to_vec(), + }, + ); + match result { + LoggedResult::Ok(..) => panic!("Expected error"), + LoggedResult::Err(e) => { + assert_eq!(e.to_string(), "Local file is too large (> 100mb)".to_string()); + } + } + } + + #[test] + fn test_large_fleet_file() { + let configurator = Configurator::new(true); + let temp_local_file = tempfile::NamedTempFile::new().unwrap(); + let mut temp_fleet_file = tempfile::NamedTempFile::new().unwrap(); + let mut large_file = Vec::new(); + for _ in 0..(1024 * 1024 * 100 + 1) { + large_file.push(b'a'); + } + temp_fleet_file.write_all(&large_file).unwrap(); + let temp_local_path = temp_local_file.into_temp_path(); + let temp_fleet_path = temp_fleet_file.into_temp_path(); + let result = configurator.get_config_from_file( + temp_local_path.to_str().unwrap().as_ref(), + temp_fleet_path.to_str().unwrap().as_ref(), + &ProcessInfo { + args: vec![b"-jar HelloWorld.jar".to_vec()], + envp: vec![b"ENV=VAR".to_vec()], + language: b"java".to_vec(), + }, + ); + match result { + LoggedResult::Ok(..) => panic!("Expected error"), + LoggedResult::Err(e) => { + assert_eq!(e.to_string(), "Fleet file is too large (> 100mb)".to_string()); + } + } + } + #[test] fn test_local_host_global_config() { use LibraryConfigSource::*; From 2050aa9f5094602aa4f2fef68c01bd93dcbda256 Mon Sep 17 00:00:00 2001 From: Victor Pellan Date: Wed, 7 Jan 2026 14:11:22 +0100 Subject: [PATCH 2/4] fix format and clippy --- libdd-library-config/src/lib.rs | 36 ++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/libdd-library-config/src/lib.rs b/libdd-library-config/src/lib.rs index 0f63b5d8a0..4530c27967 100644 --- a/libdd-library-config/src/lib.rs +++ b/libdd-library-config/src/lib.rs @@ -555,7 +555,11 @@ impl Configurator { return logged_result; } } - Err(e) => return LoggedResult::Err(anyhow::Error::from(e).context("failed to get file metadata")), + Err(e) => { + return LoggedResult::Err( + anyhow::Error::from(e).context("failed to get file metadata"), + ) + } } match self.parse_stable_config_file(file) { LoggedResult::Ok(config, logs) => { @@ -564,7 +568,7 @@ impl Configurator { } LoggedResult::Err(e) => return LoggedResult::Err(e), } - }, + } Err(e) if e.kind() == io::ErrorKind::NotFound => StableConfig::default(), Err(e) => { return LoggedResult::Err( @@ -582,7 +586,11 @@ impl Configurator { return LoggedResult::Err(test); } } - Err(e) => return LoggedResult::Err(anyhow::Error::from(e).context("failed to get file metadata")), + Err(e) => { + return LoggedResult::Err( + anyhow::Error::from(e).context("failed to get file metadata"), + ) + } } match self.parse_stable_config_file(file) { LoggedResult::Ok(config, logs) => { @@ -591,7 +599,7 @@ impl Configurator { } LoggedResult::Err(e) => return LoggedResult::Err(e), } - }, + } Err(e) if e.kind() == io::ErrorKind::NotFound => StableConfig::default(), Err(e) => { return LoggedResult::Err( @@ -896,10 +904,7 @@ mod tests { let configurator = Configurator::new(true); let mut temp_local_file = tempfile::NamedTempFile::new().unwrap(); let temp_fleet_file = tempfile::NamedTempFile::new().unwrap(); - let mut large_file = Vec::new(); - for _ in 0..(1024 * 1024 * 100 + 1) { - large_file.push(b'a'); - } + let large_file = vec![b'a'; 1024 * 1024 * 100 + 1]; temp_local_file.write_all(&large_file).unwrap(); let temp_local_path = temp_local_file.into_temp_path(); let temp_fleet_path = temp_fleet_file.into_temp_path(); @@ -915,7 +920,10 @@ mod tests { match result { LoggedResult::Ok(..) => panic!("Expected error"), LoggedResult::Err(e) => { - assert_eq!(e.to_string(), "Local file is too large (> 100mb)".to_string()); + assert_eq!( + e.to_string(), + "Local file is too large (> 100mb)".to_string() + ); } } } @@ -925,10 +933,7 @@ mod tests { let configurator = Configurator::new(true); let temp_local_file = tempfile::NamedTempFile::new().unwrap(); let mut temp_fleet_file = tempfile::NamedTempFile::new().unwrap(); - let mut large_file = Vec::new(); - for _ in 0..(1024 * 1024 * 100 + 1) { - large_file.push(b'a'); - } + let large_file = vec![b'a'; 1024 * 1024 * 100 + 1]; temp_fleet_file.write_all(&large_file).unwrap(); let temp_local_path = temp_local_file.into_temp_path(); let temp_fleet_path = temp_fleet_file.into_temp_path(); @@ -944,7 +949,10 @@ mod tests { match result { LoggedResult::Ok(..) => panic!("Expected error"), LoggedResult::Err(e) => { - assert_eq!(e.to_string(), "Fleet file is too large (> 100mb)".to_string()); + assert_eq!( + e.to_string(), + "Fleet file is too large (> 100mb)".to_string() + ); } } } From 68ccd52f0fac9722485ed22c3e2838bb5edee18d Mon Sep 17 00:00:00 2001 From: Victor Pellan Date: Mon, 12 Jan 2026 15:52:40 +0100 Subject: [PATCH 3/4] Change return type from Error to Ok with logs --- libdd-library-config/src/lib.rs | 97 +++++++++++++++------------------ 1 file changed, 44 insertions(+), 53 deletions(-) diff --git a/libdd-library-config/src/lib.rs b/libdd-library-config/src/lib.rs index 4530c27967..0647560e6f 100644 --- a/libdd-library-config/src/lib.rs +++ b/libdd-library-config/src/lib.rs @@ -550,9 +550,16 @@ impl Configurator { Ok(metadata) => { // Fail if the file is > 100mb if metadata.len() > 1024 * 1024 * 100 { - let anyhow_error = anyhow::anyhow!("Local file is too large (> 100mb)"); - let logged_result = LoggedResult::Err(anyhow_error); - return logged_result; + debug_messages.push("failed to read local config file: file is too large (> 100mb)".to_string()); + StableConfig::default() + } else { + match self.parse_stable_config_file(file) { + LoggedResult::Ok(config, logs) => { + debug_messages.extend(logs); + config + } + LoggedResult::Err(e) => return LoggedResult::Err(e), + } } } Err(e) => { @@ -561,13 +568,6 @@ impl Configurator { ) } } - match self.parse_stable_config_file(file) { - LoggedResult::Ok(config, logs) => { - debug_messages.extend(logs); - config - } - LoggedResult::Err(e) => return LoggedResult::Err(e), - } } Err(e) if e.kind() == io::ErrorKind::NotFound => StableConfig::default(), Err(e) => { @@ -582,8 +582,16 @@ impl Configurator { Ok(metadata) => { // Fail if the file is > 100mb if metadata.len() > 1024 * 1024 * 100 { - let test = anyhow::anyhow!("Fleet file is too large (> 100mb)"); - return LoggedResult::Err(test); + debug_messages.push("failed to read fleet config file: file is too large (> 100mb)".to_string()); + StableConfig::default() + } else { + match self.parse_stable_config_file(file) { + LoggedResult::Ok(config, logs) => { + debug_messages.extend(logs); + config + } + LoggedResult::Err(e) => return LoggedResult::Err(e), + } } } Err(e) => { @@ -592,13 +600,6 @@ impl Configurator { ) } } - match self.parse_stable_config_file(file) { - LoggedResult::Ok(config, logs) => { - debug_messages.extend(logs); - config - } - LoggedResult::Err(e) => return LoggedResult::Err(e), - } } Err(e) if e.kind() == io::ErrorKind::NotFound => StableConfig::default(), Err(e) => { @@ -900,40 +901,12 @@ mod tests { } #[test] - fn test_large_local_file() { + fn test_large_files() { let configurator = Configurator::new(true); let mut temp_local_file = tempfile::NamedTempFile::new().unwrap(); - let temp_fleet_file = tempfile::NamedTempFile::new().unwrap(); - let large_file = vec![b'a'; 1024 * 1024 * 100 + 1]; - temp_local_file.write_all(&large_file).unwrap(); - let temp_local_path = temp_local_file.into_temp_path(); - let temp_fleet_path = temp_fleet_file.into_temp_path(); - let result = configurator.get_config_from_file( - temp_local_path.to_str().unwrap().as_ref(), - temp_fleet_path.to_str().unwrap().as_ref(), - &ProcessInfo { - args: vec![b"-jar HelloWorld.jar".to_vec()], - envp: vec![b"ENV=VAR".to_vec()], - language: b"java".to_vec(), - }, - ); - match result { - LoggedResult::Ok(..) => panic!("Expected error"), - LoggedResult::Err(e) => { - assert_eq!( - e.to_string(), - "Local file is too large (> 100mb)".to_string() - ); - } - } - } - - #[test] - fn test_large_fleet_file() { - let configurator = Configurator::new(true); - let temp_local_file = tempfile::NamedTempFile::new().unwrap(); let mut temp_fleet_file = tempfile::NamedTempFile::new().unwrap(); let large_file = vec![b'a'; 1024 * 1024 * 100 + 1]; + temp_local_file.write_all(&large_file).unwrap(); temp_fleet_file.write_all(&large_file).unwrap(); let temp_local_path = temp_local_file.into_temp_path(); let temp_fleet_path = temp_fleet_file.into_temp_path(); @@ -947,13 +920,31 @@ mod tests { }, ); match result { - LoggedResult::Ok(..) => panic!("Expected error"), - LoggedResult::Err(e) => { + LoggedResult::Ok(configs, logs) => { + assert_eq!(configs, vec![]); assert_eq!( - e.to_string(), - "Fleet file is too large (> 100mb)".to_string() + logs, + vec![ + "Reading stable configuration from files:", + format!("\tlocal: \"{path}\"", path = temp_local_path.to_str().unwrap()).as_str(), + format!("\tfleet: \"{path}\"", path = temp_fleet_path.to_str().unwrap()).as_str(), + "failed to read local config file: file is too large (> 100mb)", + "failed to read fleet config file: file is too large (> 100mb)", + "\tProcess args:", + "\t\t\"-jar HelloWorld.jar\"", + "\tProcess language: \"java\"", + "No selector matched for source LocalStableConfig", + "Called library_config_common_component:", + "\tsource: LocalStableConfig", + "\tconfigurator: Configurator { debug_logs: true }", + "No selector matched for source FleetStableConfig", + "Called library_config_common_component:", + "\tsource: FleetStableConfig", + "\tconfigurator: Configurator { debug_logs: true }" + ] ); } + LoggedResult::Err(_) => panic!("Expected success"), } } From 2162cd7eeed94ab131557515177c1a7ebc7883b8 Mon Sep 17 00:00:00 2001 From: Victor Pellan Date: Mon, 12 Jan 2026 16:29:07 +0100 Subject: [PATCH 4/4] Format + try to fix test for windows --- libdd-library-config/src/lib.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/libdd-library-config/src/lib.rs b/libdd-library-config/src/lib.rs index 0647560e6f..24c5d16a3b 100644 --- a/libdd-library-config/src/lib.rs +++ b/libdd-library-config/src/lib.rs @@ -550,7 +550,10 @@ impl Configurator { Ok(metadata) => { // Fail if the file is > 100mb if metadata.len() > 1024 * 1024 * 100 { - debug_messages.push("failed to read local config file: file is too large (> 100mb)".to_string()); + debug_messages.push( + "failed to read local config file: file is too large (> 100mb)" + .to_string(), + ); StableConfig::default() } else { match self.parse_stable_config_file(file) { @@ -582,7 +585,10 @@ impl Configurator { Ok(metadata) => { // Fail if the file is > 100mb if metadata.len() > 1024 * 1024 * 100 { - debug_messages.push("failed to read fleet config file: file is too large (> 100mb)".to_string()); + debug_messages.push( + "failed to read fleet config file: file is too large (> 100mb)" + .to_string(), + ); StableConfig::default() } else { match self.parse_stable_config_file(file) { @@ -828,7 +834,7 @@ mod utils { #[cfg(test)] mod tests { - use std::{collections::HashMap, io::Write}; + use std::{collections::HashMap, io::Write, path::Path}; use super::{Configurator, LoggedResult, ProcessInfo}; use crate::{ @@ -919,6 +925,8 @@ mod tests { language: b"java".to_vec(), }, ); + let local_path: &Path = temp_local_path.to_str().unwrap().as_ref(); + let fleet_path: &Path = temp_fleet_path.to_str().unwrap().as_ref(); match result { LoggedResult::Ok(configs, logs) => { assert_eq!(configs, vec![]); @@ -926,8 +934,8 @@ mod tests { logs, vec![ "Reading stable configuration from files:", - format!("\tlocal: \"{path}\"", path = temp_local_path.to_str().unwrap()).as_str(), - format!("\tfleet: \"{path}\"", path = temp_fleet_path.to_str().unwrap()).as_str(), + format!("\tlocal: {local_path:?}").as_str(), + format!("\tfleet: {fleet_path:?}").as_str(), "failed to read local config file: file is too large (> 100mb)", "failed to read fleet config file: file is too large (> 100mb)", "\tProcess args:",