use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
const OBSOLETE_SECTIONS: &[&str] = &["sandbox"];
fn warn_obsolete_section(section: &str, path: &Path) {
tracing::warn!(
"config file {} contains an obsolete `{}` section that has no effect",
path.display(),
section
);
if section == "sandbox" {
eprintln!(
"\n\
warning: {} contains a `sandbox:` section.\n\
\n\
These settings never took effect. `enabled`, `memory_limit_mb`,\n\
`cpu_limit_percent`, `network_access` and `filesystem_access` were\n\
parsed and then ignored — no code path has ever read them to confine\n\
anything. A config setting them was not protected by them.\n\
\n\
Templates execute as ordinary child processes with the invoking user's\n\
privileges and full network and filesystem access. Review templates\n\
before running them.\n\
\n\
Remove the `sandbox:` section from {}. For real isolation, run cxg\n\
itself inside a container or VM, as a non-privileged user.\n\
\n\
(The `cxg sandbox` command is unrelated and still works — it manages\n\
per-language dependency environments, not execution confinement.)\n",
path.display(),
path.display()
);
} else {
eprintln!(
"\nwarning: {} contains an obsolete `{}:` section that has no effect. \
Remove it.\n",
path.display(),
section
);
}
}
fn top_level_keys(content: &str, path: &Path) -> Vec<String> {
match path.extension().and_then(|s| s.to_str()) {
Some("yaml") | Some("yml") => serde_yaml::from_str::<serde_yaml::Value>(content)
.ok()
.and_then(|v| {
v.as_mapping().map(|m| {
m.keys()
.filter_map(|k| k.as_str().map(String::from))
.collect()
})
})
.unwrap_or_default(),
Some("toml") => toml::from_str::<toml::Value>(content)
.ok()
.and_then(|v| {
v.as_table()
.map(|t| t.keys().map(|k| k.to_string()).collect())
})
.unwrap_or_default(),
Some("json") => serde_json::from_str::<serde_json::Value>(content)
.ok()
.and_then(|v| {
v.as_object()
.map(|m| m.keys().map(|k| k.to_string()).collect())
})
.unwrap_or_default(),
_ => Vec::new(),
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Config {
#[serde(default)]
pub templates: TemplateConfig,
#[serde(default)]
pub network: NetworkConfig,
#[serde(default)]
pub execution: ExecutionConfig,
}
impl Config {
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
let path = path.as_ref();
let content = std::fs::read_to_string(path)
.map_err(|e| Error::config(format!("Failed to read config file: {}", e)))?;
let config = Self::parse(&content, path)?;
for section in Self::obsolete_sections(&content, path) {
warn_obsolete_section(section, path);
}
Ok(config)
}
fn parse(content: &str, path: &Path) -> Result<Self> {
match path.extension().and_then(|s| s.to_str()) {
Some("yaml") | Some("yml") => serde_yaml::from_str(content)
.map_err(|e| Error::config(format!("Invalid YAML config: {}", e))),
Some("toml") => toml::from_str(content)
.map_err(|e| Error::config(format!("Invalid TOML config: {}", e))),
Some("json") => serde_json::from_str(content)
.map_err(|e| Error::config(format!("Invalid JSON config: {}", e))),
_ => Err(Error::config("Unsupported config file format")),
}
}
pub fn obsolete_sections(content: &str, path: &Path) -> Vec<&'static str> {
let keys = top_level_keys(content, path);
OBSOLETE_SECTIONS
.iter()
.copied()
.filter(|section| keys.iter().any(|key| key == section))
.collect()
}
pub fn from_file_reporting_obsolete<P: AsRef<Path>>(
path: P,
) -> Result<(Self, Vec<&'static str>)> {
let path = path.as_ref();
let content = std::fs::read_to_string(path)
.map_err(|e| Error::config(format!("Failed to read config file: {}", e)))?;
let config = Self::parse(&content, path)?;
let obsolete = Self::obsolete_sections(&content, path);
for section in &obsolete {
warn_obsolete_section(section, path);
}
Ok((config, obsolete))
}
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let path = path.as_ref();
let content = match path.extension().and_then(|s| s.to_str()) {
Some("yaml") | Some("yml") => serde_yaml::to_string(self)
.map_err(|e| Error::config(format!("Failed to serialize to YAML: {}", e)))?,
Some("toml") => toml::to_string_pretty(self)
.map_err(|e| Error::config(format!("Failed to serialize to TOML: {}", e)))?,
Some("json") => serde_json::to_string_pretty(self)
.map_err(|e| Error::config(format!("Failed to serialize to JSON: {}", e)))?,
_ => return Err(Error::config("Unsupported config file format")),
};
std::fs::write(path, content)
.map_err(|e| Error::config(format!("Failed to write config file: {}", e)))?;
Ok(())
}
pub fn validate(&self) -> Result<()> {
if self.execution.parallel_targets == 0 {
return Err(Error::config("Parallel targets must be greater than 0"));
}
if self.network.timeout_secs == 0 {
return Err(Error::config("Timeout must be greater than 0"));
}
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct TemplateConfig {
pub directories: Vec<PathBuf>,
pub timeout_secs: u64,
}
impl Default for TemplateConfig {
fn default() -> Self {
Self {
directories: vec![], timeout_secs: 30,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct NetworkConfig {
pub timeout_secs: u64,
pub user_agent: String,
#[serde(skip)]
pub follow_redirects: bool,
pub max_redirects: usize,
pub connection_pool_size: usize,
pub proxy: Option<String>,
pub rate_limit: Option<u32>,
#[serde(default)]
pub headers: Vec<(String, String)>,
#[serde(default)]
pub cookies: Vec<(String, String)>,
}
impl Default for NetworkConfig {
fn default() -> Self {
Self {
timeout_secs: 10,
user_agent: format!("cert-x-gen/{}", env!("CARGO_PKG_VERSION")),
follow_redirects: false,
max_redirects: 5,
connection_pool_size: 100,
proxy: None,
rate_limit: Some(100),
headers: Vec::new(),
cookies: Vec::new(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct ExecutionConfig {
pub parallel_targets: usize,
pub parallel_templates: usize,
pub max_retries: u32,
pub retry_delay_secs: u64,
pub aggressive_mode: bool,
pub stealth_mode: bool,
}
impl Default for ExecutionConfig {
fn default() -> Self {
Self {
parallel_targets: 50,
parallel_templates: 10,
max_retries: 1,
retry_delay_secs: 1,
aggressive_mode: false,
stealth_mode: false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = Config::default();
assert_eq!(config.execution.parallel_targets, 50);
assert_eq!(config.network.timeout_secs, 10);
}
#[test]
fn test_config_validation() {
let mut config = Config::default();
assert!(config.validate().is_ok());
config.execution.parallel_targets = 0;
assert!(config.validate().is_err());
}
#[test]
fn test_partial_config_uses_defaults_for_omitted_sections() {
let yaml = "network:\n timeout_secs: 20\n";
let config: Config = serde_yaml::from_str(yaml).expect("partial config should load");
assert_eq!(config.network.timeout_secs, 20);
assert_eq!(
config.network.connection_pool_size,
NetworkConfig::default().connection_pool_size
);
assert_eq!(
config.templates.timeout_secs,
TemplateConfig::default().timeout_secs
);
assert_eq!(
config.execution.parallel_targets,
ExecutionConfig::default().parallel_targets
);
assert!(config.validate().is_ok());
}
#[test]
fn test_legacy_sandbox_section_still_loads_and_is_reported() {
let yaml = "network:\n timeout_secs: 20\n\
sandbox:\n enabled: true\n memory_limit_mb: 512\n \
cpu_limit_percent: 80\n network_access: none\n \
filesystem_access: readonly\n";
let path = Path::new("cert-x-gen.yaml");
let config = Config::parse(yaml, path).expect("legacy config must still load");
assert_eq!(config.network.timeout_secs, 20);
assert!(config.validate().is_ok());
assert_eq!(Config::obsolete_sections(yaml, path), vec!["sandbox"]);
}
#[test]
fn test_clean_config_reports_no_obsolete_sections() {
let yaml = "network:\n timeout_secs: 20\n";
assert!(Config::obsolete_sections(yaml, Path::new("cert-x-gen.yaml")).is_empty());
}
#[test]
fn test_obsolete_sections_detected_in_toml_and_json() {
assert_eq!(
Config::obsolete_sections("[sandbox]\nenabled = true\n", Path::new("c.toml")),
vec!["sandbox"]
);
assert_eq!(
Config::obsolete_sections(r#"{"sandbox": {"enabled": true}}"#, Path::new("c.json")),
vec!["sandbox"]
);
}
#[test]
fn test_generated_config_has_no_sandbox_section() {
let yaml = serde_yaml::to_string(&Config::default()).expect("serialize");
assert!(
!yaml.contains("sandbox"),
"generated config still emits a sandbox section:\n{}",
yaml
);
}
#[test]
fn test_empty_config_is_all_defaults() {
let config: Config = serde_yaml::from_str("{}").expect("empty config should load");
assert_eq!(
config.execution.parallel_targets,
Config::default().execution.parallel_targets
);
assert!(config.validate().is_ok());
}
}