cert-x-gen 1.9.2

Advanced Multi-Language Security Scanning Engine
Documentation
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
//! Configuration management for CERT-X-GEN

use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

/// Top-level config sections this build no longer reads.
///
/// `sandbox` held `enabled`, `memory_limit_mb`, `cpu_limit_percent`,
/// `network_access` and `filesystem_access`. Nothing ever enforced any of
/// them — the values were deserialized and, outside one unit test, never read.
/// Unrelated to the `cxg sandbox` command, which manages per-language
/// dependency environments and is unaffected.
const OBSOLETE_SECTIONS: &[&str] = &["sandbox"];

/// Report an obsolete section on stderr.
///
/// Loud on purpose: someone whose config says `sandbox.enabled: true` believes
/// they are hardened. Dropping the keys silently would leave them believing it.
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
        );
    }
}

/// Top-level keys of a config document, or empty if it cannot be parsed as a map.
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(),
    }
}

/// Main configuration structure
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Config {
    /// Template configuration. Omitted → `TemplateConfig::default()`.
    #[serde(default)]
    pub templates: TemplateConfig,
    /// Network configuration. Omitted → `NetworkConfig::default()`.
    #[serde(default)]
    pub network: NetworkConfig,
    /// Execution configuration. Omitted → `ExecutionConfig::default()`.
    #[serde(default)]
    pub execution: ExecutionConfig,
}

impl Config {
    /// Load configuration from file.
    ///
    /// Obsolete sections are not an error: the file still loads, but each one
    /// is reported on stderr (see [`Config::obsolete_sections`]) so nobody is
    /// left believing a setting is doing something it never did.
    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)
    }

    /// Parse configuration from a string, dispatching on the path's extension.
    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")),
        }
    }

    /// Top-level sections present in `content` that this build no longer reads.
    ///
    /// Unknown keys are ignored by the parser, so a config that still sets one
    /// loads cleanly — which is exactly why callers must surface these.
    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()
    }

    /// Load a configuration file and report which obsolete sections it contains.
    ///
    /// Same load semantics as [`Config::from_file`], but the caller gets the
    /// list back instead of only seeing it on stderr.
    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))
    }

    /// Save configuration to file
    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(())
    }

    /// Validate configuration
    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(())
    }
}

/// Template configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct TemplateConfig {
    /// Template directories (for backward compatibility)
    pub directories: Vec<PathBuf>,
    /// Template timeout (seconds)
    pub timeout_secs: u64,
}

impl Default for TemplateConfig {
    fn default() -> Self {
        Self {
            directories: vec![], // Empty - will use discovery system
            timeout_secs: 30,
        }
    }
}

/// Network configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct NetworkConfig {
    /// Request timeout (seconds)
    pub timeout_secs: u64,
    /// User agent string
    pub user_agent: String,
    /// Follow redirects.
    ///
    /// Not a config key: this is runtime plumbing for the `--follow-redirects`
    /// CLI flag, which unconditionally overwrites it (`main.rs`). Skipped from
    /// (de)serialization so a config file cannot set a value the flag ignores.
    #[serde(skip)]
    pub follow_redirects: bool,
    /// Maximum redirects
    pub max_redirects: usize,
    /// Connection pool size
    pub connection_pool_size: usize,
    /// Proxy URL
    pub proxy: Option<String>,
    /// Rate limit (requests per second)
    pub rate_limit: Option<u32>,
    /// Custom headers for HTTP requests
    #[serde(default)]
    pub headers: Vec<(String, String)>,
    /// Cookies for authenticated scans
    #[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(),
        }
    }
}

/// Execution configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct ExecutionConfig {
    /// Parallel target scanning
    pub parallel_targets: usize,
    /// Parallel template execution per target
    pub parallel_templates: usize,
    /// Maximum retries
    pub max_retries: u32,
    /// Retry delay (seconds)
    pub retry_delay_secs: u64,
    /// Aggressive mode
    pub aggressive_mode: bool,
    /// Stealth mode
    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() {
        // Only `network` is present, and only one of its keys. Every other
        // section — and every unspecified network key — must fall back to its
        // compiled-in default rather than erroring.
        let yaml = "network:\n  timeout_secs: 20\n";
        let config: Config = serde_yaml::from_str(yaml).expect("partial config should load");

        // Specified value wins.
        assert_eq!(config.network.timeout_secs, 20);
        // Unspecified network key falls back to NetworkConfig::default().
        assert_eq!(
            config.network.connection_pool_size,
            NetworkConfig::default().connection_pool_size
        );
        // Omitted sections equal their Default impls.
        assert_eq!(
            config.templates.timeout_secs,
            TemplateConfig::default().timeout_secs
        );
        assert_eq!(
            config.execution.parallel_targets,
            ExecutionConfig::default().parallel_targets
        );

        // A partial config still validates.
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_legacy_sandbox_section_still_loads_and_is_reported() {
        // A config written against the old `sandbox` section must keep loading —
        // breaking the load would be a worse outcome than the dead keys were.
        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());

        // ...but it must never load quietly.
        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() {
        // `cxg config generate` serializes Config::default(); it must not put the
        // section back into every new config file.
        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() {
        // An empty document is a valid config equal to Config::default().
        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());
    }
}