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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//! Supplied-only probe dispatch for ordinary scans, including mixed template packs.
use crate::cli::{self, Cli, Commands, PentestAction, PentestCommand};
use cert_x_gen::{
    error::{Error, Result},
    template::{PathResolver, Template},
    types::{ExecutionRecord, ExecutionStatus, Finding, ScanResults, Severity},
};
use serde_json::{json, Value};
use std::{collections::HashSet, fs};

pub async fn execute(
    args: &cli::ScanArgs,
    templates: Vec<Box<dyn Template>>,
    results: &mut ScanResults,
) -> Result<i32> {
    let mut reports = Vec::new();
    let mut refused = Vec::new();
    let mut exit = 0;
    let mut executed_targets = HashSet::new();
    let root = args.probe_inputs.session_dir.clone().unwrap_or_else(|| {
        PathResolver::user_config_dir()
            .join("sessions")
            .join(format!("scan-{}", results.scan_id))
    });
    let root = if root.is_absolute() {
        root
    } else {
        std::env::current_dir()?.join(root)
    };
    let inputs = root.join("probe-inputs").join(results.scan_id.to_string());
    let mut sources = Vec::new();
    let mut seen = HashSet::new();
    for template in templates {
        let path = template.metadata().file_path.clone();
        if seen.insert(path.clone()) {
            sources.push((template.id().to_string(), path));
        }
    }
    if let Err(error) = fs::create_dir_all(&inputs) {
        for (id, path) in &sources {
            refused.push(json!({"template": id, "path": path, "reason": error.to_string()}));
        }
        exit = 4;
    } else {
        let mut mappings = Vec::new();
        for (index, (id, path)) in sources.iter().enumerate() {
            let directory = inputs.join(index.to_string());
            if let Err(error) = fs::create_dir_all(&directory) {
                refused.push(json!({"template": id, "path": path, "reason": error.to_string()}));
                exit = 4;
                continue;
            }
            let staged = directory.join(path.file_name().unwrap_or_default());
            match fs::copy(path, &staged) {
                Ok(_) => mappings.push(json!({"template": id, "source": path, "staged": staged})),
                Err(error) => {
                    refused
                        .push(json!({"template": id, "path": path, "reason": error.to_string()}));
                    exit = 4;
                }
            }
        }
        let mut targets = Vec::new();
        let mut stack = HashSet::new();
        for scope in &args.scope {
            let direct = url::Url::parse(scope).ok().is_some_and(|u| {
                matches!(u.scheme(), "http" | "https")
                    && u.host_str().is_some_and(|h| !h.contains(','))
            });
            if direct {
                targets.push(scope.clone());
            } else {
                crate::expand_scope_entry(scope, &mut targets, &mut stack)?;
            }
        }
        let mut unique = HashSet::new();
        for (index, raw) in targets
            .into_iter()
            .filter(|s| unique.insert(s.clone()))
            .enumerate()
        {
            let url = if raw.starts_with("http://") || raw.starts_with("https://") {
                Some(raw.clone())
            } else if raw.starts_with("cli:") || raw.contains('/') {
                None
            } else {
                Some(crate::parse_target_string(&raw).url())
            };
            let Some(target) = url else {
                for (id, path) in &sources {
                    refused.push(json!({"template": id, "path": path, "target": raw, "reason": "JS probes require an HTTP(S) target; CLI and CIDR scopes cannot be honoured by the probe engine"}));
                }
                exit = 4;
                continue;
            };
            if mappings.is_empty() {
                continue;
            }
            let session = root.join(format!("engine-{}-{index}", results.scan_id));
            if let Err(error) = fs::create_dir_all(&session) {
                for (id, path) in &sources {
                    refused.push(json!({"template": id, "path": path, "target": target, "reason": error.to_string()}));
                }
                exit = 4;
                continue;
            }
            let report_path = session.join("report.json");
            let mut run = if let Some(run) = &args.probe_run {
                run.as_ref().clone()
            } else {
                let parsed = Cli::try_parse_from([
                    "cxg",
                    "pentest",
                    "run",
                    "--codebase",
                    ".",
                    "--target",
                    &target,
                ])
                .map_err(|e| Error::config(e.to_string()))?;
                let Some(Commands::Pentest(PentestCommand {
                    action: PentestAction::Run(run),
                })) = parsed.command
                else {
                    unreachable!()
                };
                run
            };
            run.target = Some(target.clone());
            run.probe_inputs = args.probe_inputs.clone();
            run.probe_inputs.session_dir = Some(session.clone());
            run.template_dir = Some(inputs.clone());
            run.probe_inputs.no_author = true;
            run.mutation_retries = 0;
            run.ai = false;
            run.deterministic_templates = false;
            run.output = Some(report_path.clone());
            // Shared options retain scanner proxy semantics. All other controls
            // come from the engine's own parser, including its defaults.
            run.proxy = args.proxy.clone();
            let outcome = crate::run_pentest_process(
                PentestCommand {
                    action: PentestAction::Run(run),
                },
                true,
            )
            .await;
            let (code, error) = match outcome {
                Ok(result) => (result.code, result.error),
                Err(error) => (4, error.to_string()),
            };
            if code != 0 {
                exit = if code == 2 && exit == 0 {
                    2
                } else if code != 2 {
                    code
                } else {
                    exit
                };
            }
            let report: Option<Value> = fs::read_to_string(&report_path)
                .ok()
                .and_then(|text| serde_json::from_str(&text).ok());
            if let Some(report) = &report {
                let executed = report
                    .get("templates_executed")
                    .and_then(Value::as_u64)
                    .unwrap_or(0) as usize;
                results.statistics.templates_executed += executed;
                if executed > 0 {
                    executed_targets.insert(target.clone());
                }
                for key in [
                    "probe_refusals",
                    "auth_skipped",
                    "chain_skipped",
                    "rejected_templates",
                    "template_errors",
                    "not_run",
                ] {
                    if let Some(entries) = report.get(key).and_then(Value::as_array) {
                        for entry in entries {
                            let id = entry
                                .get("template")
                                .or_else(|| entry.get("template_id"))
                                .or_else(|| entry.get("id"))
                                .cloned()
                                .unwrap_or(Value::Null);
                            let source = entry
                                .get("path")
                                .and_then(|path| {
                                    mappings.iter().find(|m| Some(path) == m.get("staged"))
                                })
                                .or_else(|| {
                                    let mut candidates =
                                        mappings.iter().filter(|m| m.get("template") == Some(&id));
                                    let first = candidates.next();
                                    if candidates.next().is_none() {
                                        first
                                    } else {
                                        None
                                    }
                                })
                                .and_then(|m| m.get("source"))
                                .cloned()
                                .unwrap_or(Value::Null);
                            let reason = entry.get("reason").cloned().unwrap_or_else(|| {
                                json!(entry.get("errors").unwrap_or(entry).to_string())
                            });
                            refused.push(json!({"target": target, "template": id, "path": source,
                                "status": "refused", "reason": reason, "details": entry}));
                        }
                    }
                }
                if let Some(findings) = report.get("confirmed_findings").and_then(Value::as_array) {
                    for finding in findings {
                        let severity = match finding
                            .get("severity")
                            .and_then(Value::as_str)
                            .unwrap_or("info")
                        {
                            "critical" => Severity::Critical,
                            "high" => Severity::High,
                            "medium" => Severity::Medium,
                            "low" => Severity::Low,
                            _ => Severity::Info,
                        };
                        let mut converted = Finding::new(
                            target.clone(),
                            finding
                                .get("template_id")
                                .or_else(|| finding.get("hypothesis_id"))
                                .or_else(|| finding.get("id"))
                                .and_then(Value::as_str)
                                .unwrap_or("probe")
                                .to_string(),
                            severity,
                            format!(
                                "[engine-adjudicated] {}",
                                finding
                                    .get("vuln_class")
                                    .and_then(Value::as_str)
                                    .unwrap_or("probe finding")
                            ),
                            finding
                                .get("description")
                                .and_then(Value::as_str)
                                .unwrap_or("Engine-confirmed probe finding")
                                .to_string(),
                        );
                        converted.verdict_source = Some("engine-adjudicated".into());
                        converted
                            .evidence
                            .add_data("engine_finding", finding.clone());
                        results.add_finding(converted);
                    }
                }
            } else {
                for (id, path) in &sources {
                    refused.push(json!({"template": id, "path": path, "target": target, "reason": if error.trim().is_empty() { format!("probe engine did not produce a report (exit {code})") } else { error.chars().take(4000).collect::<String>() }}));
                }
                if exit == 0 {
                    exit = 4;
                }
            }
            let audited_requests = audited_http_requests(&session.join("audit.jsonl"));
            results.statistics.network_requests_measured += audited_requests;
            // The durable audit counts observed HTTP attempts, not arbitrary
            // networking a probe can cause. Do not present that subset as a total.
            if audited_requests > 0
                || report
                    .as_ref()
                    .and_then(|r| r.get("templates_executed"))
                    .and_then(Value::as_u64)
                    .unwrap_or(0)
                    > 0
            {
                results.statistics.network_requests = None;
            }
            append_executions(results, &target, &mappings, report.as_ref(), code, &refused);
            reports.push(json!({"target": target, "session_dir": session, "audit_log": session.join("audit.jsonl"),
                "report_path": report_path, "exit_code": code, "templates": mappings,
                "request_counts": {"audited_http_attempts": audited_requests, "probe_budget_requests": report.as_ref().and_then(|r| r.pointer("/scope_stats/total_requests")).and_then(Value::as_u64).unwrap_or(0)},
                "report": report}));
        }
    }
    if !refused.is_empty() && !matches!(exit, 3 | 5) {
        exit = 4;
    }
    // Refusals before engine dispatch still need a gateable execution row.
    for entry in &refused {
        let target = entry.get("target").and_then(Value::as_str).unwrap_or("");
        let id = entry
            .get("template")
            .and_then(Value::as_str)
            .unwrap_or("probe");
        if !results
            .executions
            .iter()
            .any(|e| e.target == target && e.template_id == id)
        {
            results.executions.push(probe_execution(
                target,
                id,
                ExecutionStatus::Skipped,
                0,
                4,
                &format!(
                    "probe refused: {}",
                    entry.get("reason").unwrap_or(&Value::Null)
                ),
            ));
        }
    }
    let mut seen_refusals = HashSet::new();
    refused.retain(|entry| {
        seen_refusals.insert(
            json!([
                entry.get("target"),
                entry.get("template"),
                entry.get("path"),
                entry.get("reason")
            ])
            .to_string(),
        )
    });
    results.statistics.targets_scanned = results
        .statistics
        .targets_scanned
        .max(executed_targets.len());
    for refusal in &refused {
        eprintln!("[REFUSED] {}", refusal);
        results.errors.push(format!("Probe refused: {refusal}"));
    }
    results.probe_results = Some(
        json!({"verdict_source": "engine-adjudicated", "authoring": {"mode": "no_author", "model_calls": 0, "ranking": false, "generation": false, "mutation_retries": 0},
        "sessions": reports, "refused_probes": refused}),
    );
    Ok(exit)
}

fn audited_http_requests(path: &std::path::Path) -> usize {
    fs::read_to_string(path)
        .unwrap_or_default()
        .lines()
        .filter_map(|line| serde_json::from_str::<Value>(line).ok())
        .filter(|row| {
            row.get("_event").and_then(Value::as_str) == Some("request")
                && row
                    .get("url")
                    .and_then(Value::as_str)
                    .is_some_and(|url| url.starts_with("http://") || url.starts_with("https://"))
        })
        .count()
}

fn probe_execution(
    target: &str,
    id: &str,
    status: ExecutionStatus,
    findings: usize,
    _engine_code: i32,
    reason: &str,
) -> ExecutionRecord {
    ExecutionRecord {
        target: target.into(),
        target_kind: "http".into(),
        template_id: id.into(),
        status,
        declared_by_template: false,
        findings,
        // Probes execute inside a shared engine, without individual processes.
        exit_code: None,
        detail: Some(format!("engine-adjudicated: {reason}")),
        duration_ms: 0,
    }
}

fn append_executions(
    results: &mut ScanResults,
    target: &str,
    mappings: &[Value],
    report: Option<&Value>,
    code: i32,
    refusals: &[Value],
) {
    for mapping in mappings {
        let id = mapping["template"].as_str().unwrap_or("probe");
        let count = |key: &str| {
            report
                .and_then(|r| r.get(key))
                .and_then(Value::as_array)
                .map_or(0, |rows| {
                    rows.iter()
                        .filter(|r| {
                            r.get("template_id")
                                .or_else(|| r.get("hypothesis_id"))
                                .or_else(|| r.get("template"))
                                .and_then(Value::as_str)
                                == Some(id)
                        })
                        .count()
                })
        };
        let findings = count("confirmed_findings");
        let refusal = refusals.iter().find(|r| {
            r.get("target").and_then(Value::as_str) == Some(target)
                && if r.get("path").is_some_and(|p| !p.is_null()) {
                    r.get("path") == mapping.get("source")
                } else {
                    r.get("template").and_then(Value::as_str) == Some(id)
                }
        });
        let native = report
            .and_then(|r| r.get("probe_executions"))
            .and_then(Value::as_array)
            .and_then(|rows| {
                rows.iter()
                    .find(|row| row.get("path") == mapping.get("staged"))
            });
        if let Some(row) = native {
            let mut execution = probe_execution(
                target,
                id,
                row.get("status")
                    .and_then(Value::as_str)
                    .and_then(ExecutionStatus::parse)
                    .unwrap_or(ExecutionStatus::Errored),
                row.get("findings").and_then(Value::as_u64).unwrap_or(0) as usize,
                code,
                row.get("reason")
                    .and_then(Value::as_str)
                    .unwrap_or("native probe execution"),
            );
            execution.duration_ms = row.get("duration_ms").and_then(Value::as_u64).unwrap_or(0);
            results.executions.push(execution);
            continue;
        }
        let (status, detail) = if let Some(refusal) = refusal {
            (
                ExecutionStatus::Skipped,
                format!(
                    "probe refused: {}",
                    refusal.get("reason").unwrap_or(&Value::Null)
                ),
            )
        } else if findings > 0 {
            (
                ExecutionStatus::Confirmed,
                format!("{findings} engine-confirmed finding(s)"),
            )
        } else if count("template_errors") > 0 {
            (
                ExecutionStatus::Errored,
                "probe runtime error; see native report".into(),
            )
        } else if count("mitigation_verifications") > 0 {
            (
                ExecutionStatus::Refuted,
                "engine verified mitigation/control".into(),
            )
        } else {
            (
                ExecutionStatus::Errored,
                "no conclusive engine verdict; see native report (silence is not refutation)"
                    .into(),
            )
        };
        results
            .executions
            .push(probe_execution(target, id, status, findings, code, &detail));
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn probe_verdicts_are_gateable_and_silence_is_never_refutation() {
        for (report, expected) in [
            (
                json!({"confirmed_findings":[{"template_id":"p"}]}),
                ExecutionStatus::Confirmed,
            ),
            (
                json!({"mitigation_verifications":[{"template_id":"p"}]}),
                ExecutionStatus::Refuted,
            ),
            (
                json!({"ambiguous":[{"template_id":"p"}]}),
                ExecutionStatus::Errored,
            ),
            (json!({}), ExecutionStatus::Errored),
        ] {
            let mut results = ScanResults::new(uuid::Uuid::new_v4());
            append_executions(
                &mut results,
                "http://target",
                &[json!({"template":"p","source":"probe.js"})],
                Some(&report),
                0,
                &[],
            );
            assert_eq!(results.executions.len(), 1);
            assert_eq!(results.executions[0].status, expected);
            assert!(!results.executions[0].declared_by_template);
            assert!(results.executions[0]
                .detail
                .as_ref()
                .unwrap()
                .starts_with("engine-adjudicated:"));
        }
    }
    #[test]
    fn audit_counts_engine_and_probe_http_attempts_without_budget_or_event_rows() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("audit.jsonl");
        let rows = [
            json!({"_event":"request","actor":"engine","url":"http://target/","method":"GET"}),
            json!({"_event":"request","actor":"engine","url":"http://target/api/me","method":"GET"}),
            json!({"_event":"request","template_id":"probe","url":"http://target/check","method":"POST"}),
            json!({"_event":"blocked","url":"http://target/blocked"}),
            json!({"_event":"request","method":"TEMPLATE_START","url":""}),
        ];
        fs::write(
            &file,
            rows.iter()
                .map(Value::to_string)
                .collect::<Vec<_>>()
                .join("\n"),
        )
        .unwrap();
        assert_eq!(audited_http_requests(&file), 3);
    }
}