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
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
//! Common validators that apply to all template languages

use super::TemplateDiagnostic;
use crate::engine::common::parse_metadata_from_comments;
use crate::types::TemplateLanguage;
use anyhow::Result;

/// Common validation logic across all languages
#[derive(Debug)]
pub struct CommonValidator;

impl CommonValidator {
    /// Create a new CommonValidator instance
    pub fn new() -> Self {
        Self
    }

    /// Run all common validators
    pub fn validate(
        &self,
        code: &str,
        language: TemplateLanguage,
    ) -> Result<Vec<TemplateDiagnostic>> {
        let mut diagnostics = Vec::new();

        // Check for empty code
        if let Some(diag) = self.check_empty(code) {
            diagnostics.push(diag);
            // If empty, skip other checks
            return Ok(diagnostics);
        }

        // Check for skeleton placeholders
        diagnostics.extend(self.check_skeleton_placeholders(code));

        // Check for missing CERT_X_GEN_TARGET_HOST in non-YAML templates
        if language != TemplateLanguage::Yaml {
            if let Some(diag) = self.check_missing_target_host(code) {
                diagnostics.push(diag);
            }
        }

        // Check for hardcoded IPs/credentials
        diagnostics.extend(self.check_hardcoded_secrets(code));

        // Check for proper JSON output (for non-YAML templates)
        if language != TemplateLanguage::Yaml {
            if let Some(diag) = self.check_json_output(code, language) {
                diagnostics.push(diag);
            }
        }

        // Check for metadata completeness (for non-YAML templates)
        if language != TemplateLanguage::Yaml {
            diagnostics.extend(self.check_metadata_completeness(code));
        }

        Ok(diagnostics)
    }

    /// Check if code is empty or only whitespace
    fn check_empty(&self, code: &str) -> Option<TemplateDiagnostic> {
        if code.trim().is_empty() {
            Some(TemplateDiagnostic::error(
                "common.empty_code",
                "Template code is empty or contains only whitespace",
            ))
        } else {
            None
        }
    }

    /// Check for skeleton template placeholders
    fn check_skeleton_placeholders(&self, code: &str) -> Vec<TemplateDiagnostic> {
        let mut diagnostics = Vec::new();

        let skeleton_patterns = vec![
            ("YOUR_TEMPLATE_NAME", "Template name not customized"),
            (
                "YOUR_VULNERABILITY_CHECK",
                "Vulnerability check not implemented",
            ),
            ("YOUR_LOGIC_HERE", "Logic placeholder not replaced"),
            ("TODO:", "TODO comment found"),
            ("FIXME:", "FIXME comment found"),
            ("XXX", "XXX placeholder found"),
        ];

        for (pattern, msg) in skeleton_patterns {
            let hit = code
                .lines()
                .position(|line| line.contains(pattern) && !is_exempt_placeholder(pattern, line));
            if let Some(line_num) = hit {
                diagnostics.push(
                    TemplateDiagnostic::warning(
                        "common.skeleton_placeholder",
                        format!("{}: '{}'", msg, pattern),
                    )
                    .with_location(line_num + 1, None),
                );
            }
        }

        diagnostics
    }

    /// Check for missing CERT_X_GEN_TARGET_HOST usage
    fn check_missing_target_host(&self, code: &str) -> Option<TemplateDiagnostic> {
        // Check if template uses the target host env var
        if !reaches_its_target(code) {
            // Find where to suggest adding it (after shebang/imports)
            let suggest_line = code
                .lines()
                .enumerate()
                .find(|(_, line)| {
                    !line.trim().is_empty()
                        && !line.starts_with("#!")
                        && !line.starts_with("import ")
                        && !line.starts_with("from ")
                        && !line.starts_with("//")
                })
                .map(|(idx, _)| idx + 1)
                .unwrap_or(1);

            Some(
                TemplateDiagnostic::warning(
                    "common.missing_target_host",
                    "Template does not appear to use CERT_X_GEN_TARGET_HOST or target variables. \
                     Templates should be dynamic. \
                     Add: HOST = os.environ.get('CERT_X_GEN_TARGET_HOST') or similar",
                )
                .with_location(suggest_line, None),
            )
        } else {
            None
        }
    }

    /// Check for hardcoded secrets or IP addresses
    fn check_hardcoded_secrets(&self, code: &str) -> Vec<TemplateDiagnostic> {
        let mut diagnostics = Vec::new();

        // Check for hardcoded IP addresses (common mistake)
        let ip_regex = regex::Regex::new(
            r"\b(?:192\.168|10\.|172\.1[6-9]\.|172\.2[0-9]\.|172\.3[0-1]\.)\d{1,3}\.\d{1,3}\b",
        )
        .unwrap();

        for (line_num, line) in code.lines().enumerate() {
            if ip_regex.is_match(line)
                && !line.trim_start().starts_with('#')
                && !line.trim_start().starts_with("//")
            {
                diagnostics.push(
                    TemplateDiagnostic::warning(
                        "common.hardcoded_ip",
                        "Found hardcoded private IP address. Templates should be dynamic.",
                    )
                    .with_location(line_num + 1, None),
                );
            }
        }

        // Check for common password/secret patterns
        let secret_patterns = vec![
            (r#"password\s*=\s*["'][^"']+["']"#, "hardcoded password"),
            (r#"api[_-]?key\s*=\s*["'][^"']+["']"#, "hardcoded API key"),
            (r#"secret\s*=\s*["'][^"']+["']"#, "hardcoded secret"),
            (r#"token\s*=\s*["'][^"']+["']"#, "hardcoded token"),
        ];

        for (pattern_str, msg) in secret_patterns {
            if let Ok(pattern) = regex::Regex::new(pattern_str) {
                for (line_num, line) in code.lines().enumerate() {
                    if pattern.is_match(line)
                        && !line.trim_start().starts_with('#')
                        && !line.trim_start().starts_with("//")
                    {
                        diagnostics.push(
                            TemplateDiagnostic::warning(
                                "common.hardcoded_secret",
                                format!("Possible {}: avoid hardcoding credentials", msg),
                            )
                            .with_location(line_num + 1, None),
                        );
                    }
                }
            }
        }

        diagnostics
    }

    /// Check if template outputs JSON findings
    fn check_json_output(
        &self,
        code: &str,
        language: TemplateLanguage,
    ) -> Option<TemplateDiagnostic> {
        // Skip check for compiled languages (they typically use native Finding struct)
        if matches!(
            language,
            TemplateLanguage::Rust
                | TemplateLanguage::C
                | TemplateLanguage::Cpp
                | TemplateLanguage::Java
                | TemplateLanguage::Go
        ) {
            return None;
        }

        let has_json_output = code.contains("json.dumps")
            || code.contains("JSON.stringify")
            || code.contains("to_json")
            || code.contains("json_encode")
            || code.contains("<<EOF") // Shell heredoc for JSON
            || code.contains("print(json")
            || code.contains("echo '{")
            || code.contains("cat <<");

        if !has_json_output {
            Some(TemplateDiagnostic::info(
                "common.no_json_output",
                "Template does not appear to output JSON. CERT-X-GEN expects JSON findings format.",
            ))
        } else {
            None
        }
    }

    /// Check for metadata completeness using @field: annotations
    fn check_metadata_completeness(&self, code: &str) -> Vec<TemplateDiagnostic> {
        let mut diagnostics = Vec::new();
        let parsed = parse_metadata_from_comments(code);

        // Check if template has any metadata at all
        if !parsed.has_metadata() {
            diagnostics.push(
                TemplateDiagnostic::warning(
                    "common.missing_metadata",
                    "Template is missing metadata annotations. Add @field: annotations at the top of the file. \
                     Required: @id, @name, @author, @severity, @description, @tags",
                )
                .with_location(1, None)
            );
            return diagnostics;
        }

        // Check for specific missing required fields
        let missing = parsed.missing_required_fields();
        if !missing.is_empty() {
            let missing_list = missing.join(", ");
            diagnostics.push(
                TemplateDiagnostic::warning(
                    "common.incomplete_metadata",
                    format!(
                        "Template is missing required metadata fields: {}. \
                         Add these as @field: annotations (e.g., @severity: high)",
                        missing_list
                    ),
                )
                .with_location(1, None),
            );
        }

        // Check for empty tags
        if parsed.tags.is_empty() {
            diagnostics.push(
                TemplateDiagnostic::warning(
                    "common.missing_tags",
                    "Template has no tags. Add @tags: with comma-separated values for filtering \
                     (e.g., @tags: redis, database, unauthenticated, cwe-306)",
                )
                .with_location(1, None),
            );
        } else if parsed.tags.len() < 2 {
            diagnostics.push(
                TemplateDiagnostic::info(
                    "common.few_tags",
                    format!(
                        "Template has only {} tag(s). Consider adding more descriptive tags for better filtering.",
                        parsed.tags.len()
                    ),
                )
                .with_location(1, None)
            );
        }

        // Check severity validity
        if let Some(ref severity) = parsed.severity {
            let valid_severities = ["critical", "high", "medium", "low", "info", "informational"];
            if !valid_severities.contains(&severity.to_lowercase().as_str()) {
                diagnostics.push(
                    TemplateDiagnostic::error(
                        "common.invalid_severity",
                        format!(
                            "Invalid severity '{}'. Must be one of: critical, high, medium, low, info",
                            severity
                        ),
                    )
                    .with_location(1, None)
                );
            }
        }

        // Optional: Check for CWE reference (recommended)
        if parsed.cwe.is_empty() {
            diagnostics.push(
                TemplateDiagnostic::info(
                    "common.missing_cwe",
                    "Consider adding a CWE reference with @cwe: for vulnerability classification \
                     (e.g., @cwe: CWE-306)",
                )
                .with_location(1, None),
            );
        }

        diagnostics
    }
}

impl Default for CommonValidator {
    fn default() -> Self {
        Self::new()
    }
}

/// Does this template appear to reach its target at all?
///
/// A network template reaches it through `CERT_X_GEN_TARGET_HOST` (or a
/// templating placeholder). A template that declares `@target_kinds: cli` is
/// driving a **local binary**, and cxg hands it the probe input separately, so
/// `CERT_X_GEN_ARGV` / `CERT_X_GEN_STDIN_FILE` / `CERT_X_GEN_INPUT_DIR` are
/// equally valid ways for it to reach its target. Warning such a template that
/// it "should be dynamic" is wrong advice.
pub(super) fn reaches_its_target(code: &str) -> bool {
    const NETWORK_HANDLES: &[&str] = &[
        "CERT_X_GEN_TARGET_HOST",
        "target.address",
        "{{Hostname}}",
        "{{BaseURL}}",
    ];
    const CLI_PROBE_HANDLES: &[&str] = &[
        "CERT_X_GEN_ARGV",
        "CERT_X_GEN_STDIN_FILE",
        "CERT_X_GEN_INPUT_DIR",
    ];

    if NETWORK_HANDLES.iter().any(|h| code.contains(h)) {
        return true;
    }

    declares_cli_target_kind(code) && CLI_PROBE_HANDLES.iter().any(|h| code.contains(h))
}

/// Is this occurrence of a skeleton-placeholder pattern a false positive?
///
/// `XXX` is both an unreplaced-placeholder marker and, as a run of six, the
/// trailing template `mktemp` requires:
///
/// ```sh
/// LAB="$(mktemp -d "${TMPDIR:-/tmp}/cxg-b01.XXXXXX")"
/// ```
///
/// That is the standard hermetic-lab idiom, and every CLI probe template needs
/// it -- each has to build its own sandbox before it touches the target. The
/// `X`s there are an argument to `mktemp`, not a reminder to edit the file.
fn is_exempt_placeholder(pattern: &str, line: &str) -> bool {
    pattern == "XXX" && line.contains("mktemp")
}

/// Does this template declare `@target_kinds: cli`?
///
/// The declaration changes what several validation rules *mean*, because a CLI
/// template drives a local binary rather than a network service:
///
/// * an `ESC`-introduced sequence is the **probe payload** of a
///   terminal-escape check (baseline class B08), not decorative colour;
/// * a `mktemp -d …XXXXXX` template is a **hermetic lab**, not an unreplaced
///   skeleton placeholder.
///
/// Rules that would otherwise fire on those two idioms consult this helper, in
/// the same shape as the `@target_kinds`-aware exemption in
/// [`reaches_its_target`].
pub(super) fn declares_cli_target_kind(code: &str) -> bool {
    parse_metadata_from_comments(code)
        .target_kinds
        .iter()
        .any(|k| k.trim().eq_ignore_ascii_case("cli"))
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A cli:// probe drives a local binary and cxg hands it the input
    /// separately, so telling it to "be dynamic" with CERT_X_GEN_TARGET_HOST is
    /// wrong advice. It is only exempt when it actually declares that kind.
    #[test]
    fn a_declared_cli_template_is_not_warned_for_reaching_its_target_by_argv() {
        let validator = CommonValidator::new();

        let cli_probe =
            "#!/bin/bash\n# @id: probe\n# @target_kinds: cli\nrun \"$CERT_X_GEN_ARGV\"\n";
        assert!(
            validator.check_missing_target_host(cli_probe).is_none(),
            "a @target_kinds: cli template reaching its target via CERT_X_GEN_ARGV \
             must not be told to use CERT_X_GEN_TARGET_HOST"
        );

        // Without the declaration the same code is still warned: the exemption
        // is earned by the declaration, not by the variable.
        let undeclared = "#!/bin/bash\n# @id: probe\nrun \"$CERT_X_GEN_ARGV\"\n";
        assert!(validator.check_missing_target_host(undeclared).is_some());

        // And a network template that reaches nothing is still warned.
        let inert = "#!/bin/bash\n# @id: probe\necho hello\n";
        assert!(validator.check_missing_target_host(inert).is_some());
    }

    #[test]
    fn a_cli_template_using_the_target_path_is_not_warned() {
        let validator = CommonValidator::new();
        let code = "#!/bin/bash\n# @id: probe\n# @target_kinds: cli\n\"$CERT_X_GEN_TARGET_HOST\" --label x\n";
        assert!(validator.check_missing_target_host(code).is_none());
    }

    /// `mktemp` wants a run of `X`s, and every CLI probe template needs a
    /// hermetic lab, so the placeholder scan fired on the whole category.
    #[test]
    fn a_mktemp_template_is_not_an_unreplaced_placeholder() {
        let validator = CommonValidator::new();
        let code = concat!(
            "#!/bin/bash\n",
            "# @id: cli-baseline-b01-argument-injection\n",
            "# @target_kinds: cli\n",
            "LAB=\"$(mktemp -d \"${TMPDIR:-/tmp}/cxg-b01.XXXXXX\")\"\n",
        );
        let diags = validator.check_skeleton_placeholders(code);
        assert!(
            diags.is_empty(),
            "the X run is an argument to mktemp: {:?}",
            diags.iter().map(|d| &d.message).collect::<Vec<_>>()
        );
    }

    /// The exemption is for `mktemp`'s argument only -- a real `XXX` marker on
    /// any other line is still reported, including in a template that also
    /// builds a lab.
    #[test]
    fn a_real_xxx_marker_is_still_reported_alongside_a_mktemp_lab() {
        let validator = CommonValidator::new();
        let code = concat!(
            "#!/bin/bash\n",
            "LAB=\"$(mktemp -d /tmp/cxg.XXXXXX)\"\n",
            "# XXX decide what to probe here\n",
        );
        let diags = validator.check_skeleton_placeholders(code);
        assert_eq!(diags.len(), 1, "{:?}", diags);
        assert_eq!(diags[0].line, Some(3), "should point at the real marker");
    }

    #[test]
    fn test_empty_code_detection() {
        let validator = CommonValidator::new();
        assert!(validator.check_empty("").is_some());
        assert!(validator.check_empty("   \n  \t  ").is_some());
        assert!(validator.check_empty("import sys").is_none());
    }

    #[test]
    fn test_skeleton_placeholder_detection() {
        let validator = CommonValidator::new();
        let code_with_placeholder = "def scan():\n    # TODO: implement\n    pass";
        let diags = validator.check_skeleton_placeholders(code_with_placeholder);
        assert!(!diags.is_empty());
        assert_eq!(diags[0].code, "common.skeleton_placeholder");
    }

    #[test]
    fn test_hardcoded_ip_detection() {
        let validator = CommonValidator::new();
        // Test with explicit IP in code
        let code = "target = 10.0.0.1";
        let diags = validator.check_hardcoded_secrets(code);
        // Note: IP detection may not trigger on simple assignments
        // This test verifies the check runs without error
        let _ = diags.len(); // Just verify it runs
    }
}