1use crate::report::sink::outln;
13use std::cmp::Ordering;
14use std::collections::BTreeMap;
15use std::io::Write;
16use std::path::{Path, PathBuf};
17use std::process::ExitCode;
18use std::time::Instant;
19
20use fallow_config::{OutputFormat, ProductionAnalysis, Severity};
21use fallow_core::analyze::derive_security_severity;
22use fallow_core::results::{
23 AnalysisResults, SecurityAttackSurfaceEntry, SecurityDeadCodeKind, SecurityFinding,
24 SecurityFindingKind, TraceHop, TraceHopRole,
25};
26use fallow_types::discover::DiscoveredFile;
27use fallow_types::envelope::{ElapsedMs, Meta, ToolVersion};
28use fallow_types::extract::ModuleInfo;
29use fallow_types::results::{
30 SecurityRuntimeContext, SecurityRuntimeState, SecuritySeverity,
31 SecurityUnresolvedCalleeDiagnostic, TaintConfidence,
32};
33use rustc_hash::FxHashSet;
34use serde::Serialize;
35use xxhash_rust::xxh3::xxh3_64;
36
37use crate::base_worktree::{BaseWorktree, git_rev_parse};
38use crate::error::emit_error;
39use crate::health::{HealthOptions, SharedParseData, SortBy};
40use crate::health_types::{
41 RuntimeCoverageFinding, RuntimeCoverageHotPath, RuntimeCoverageReport, RuntimeCoverageVerdict,
42};
43use crate::load_config_for_analysis;
44
45const UNRESOLVED_CALLEE_SAMPLE_LIMIT: usize = 25;
46const UNRESOLVED_CALLEE_TOP_FILES_LIMIT: usize = 10;
47
48#[derive(Debug, Clone, Copy, Serialize)]
51#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
52pub enum SecuritySchemaVersion {
53 #[allow(
55 dead_code,
56 reason = "kept so the generated schema documents historical v1"
57 )]
58 #[serde(rename = "1")]
59 V1,
60 #[allow(
62 dead_code,
63 reason = "kept so the generated schema documents historical v2"
64 )]
65 #[serde(rename = "2")]
66 V2,
67 #[allow(
69 dead_code,
70 reason = "kept so the generated schema documents historical v3"
71 )]
72 #[serde(rename = "3")]
73 V3,
74 #[allow(
76 dead_code,
77 reason = "kept so the generated schema documents historical v4"
78 )]
79 #[serde(rename = "4")]
80 V4,
81 #[allow(
83 dead_code,
84 reason = "kept so the generated schema documents historical v5"
85 )]
86 #[serde(rename = "5")]
87 V5,
88 #[serde(rename = "6")]
90 V6,
91}
92
93#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, clap::ValueEnum)]
95#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
96#[serde(rename_all = "kebab-case")]
97pub enum SecurityGateMode {
98 New,
103 NewlyReachable,
106}
107
108#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
112#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
113#[serde(rename_all = "kebab-case")]
114pub enum SecurityGateVerdict {
115 Pass,
117 Fail,
119}
120
121#[derive(Debug, Clone, Copy, Serialize)]
124#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
125pub struct SecurityGate {
126 pub mode: SecurityGateMode,
128 pub verdict: SecurityGateVerdict,
130 pub new_count: usize,
132}
133
134#[derive(Debug, Clone, Serialize)]
136#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
137#[cfg_attr(
138 feature = "schema",
139 schemars(extend("required" = ["rules", "categories_include", "categories_exclude"]))
140)]
141pub struct SecurityOutputConfig {
142 pub rules: SecurityOutputRulesConfig,
145 pub categories_include: Option<Vec<String>>,
148 pub categories_exclude: Option<Vec<String>>,
151}
152
153#[derive(Debug, Clone, Copy, Serialize)]
154#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
155pub struct SecurityOutputRulesConfig {
156 pub security_client_server_leak: SecurityRuleSeverityConfig,
157 pub security_sink: SecurityRuleSeverityConfig,
158}
159
160#[derive(Debug, Clone, Copy, Serialize)]
161#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
162pub struct SecurityRuleSeverityConfig {
163 pub configured: Severity,
166 pub effective: Severity,
168}
169
170#[derive(Debug, Clone, Serialize)]
174#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
175pub struct SecurityOutput {
176 pub schema_version: SecuritySchemaVersion,
178 pub version: ToolVersion,
180 pub elapsed_ms: ElapsedMs,
182 pub config: SecurityOutputConfig,
184 #[serde(rename = "_meta", default, skip_serializing_if = "Option::is_none")]
186 pub meta: Option<Meta>,
187 #[serde(default, skip_serializing_if = "Option::is_none")]
191 pub gate: Option<SecurityGate>,
192 pub security_findings: Vec<SecurityFinding>,
194 #[serde(default, skip_serializing_if = "Option::is_none")]
197 pub attack_surface: Option<Vec<SecurityAttackSurfaceEntry>>,
198 pub unresolved_edge_files: usize,
203 pub unresolved_callee_sites: usize,
208 #[serde(default, skip_serializing_if = "Option::is_none")]
210 pub unresolved_callee_diagnostics: Option<SecurityUnresolvedCalleeDiagnostics>,
211}
212
213#[derive(Debug, Clone, Serialize)]
215#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
216pub struct SecurityUnresolvedCalleeDiagnostics {
217 pub sampled: Vec<SecurityUnresolvedCalleeSample>,
219 pub top_files: Vec<SecurityUnresolvedCalleeTopFile>,
221 pub by_reason: Vec<SecurityUnresolvedCalleeReasonCount>,
223 pub sample_limit: usize,
225 pub top_files_limit: usize,
227}
228
229#[derive(Debug, Clone, Serialize)]
231#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
232pub struct SecurityUnresolvedCalleeSample {
233 pub path: String,
235 pub line: u32,
237 pub col: u32,
239 pub reason: fallow_types::extract::SkippedSecurityCalleeReason,
241 pub expression_kind: fallow_types::extract::SkippedSecurityCalleeExpressionKind,
243}
244
245#[derive(Debug, Clone, Serialize)]
247#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
248pub struct SecurityUnresolvedCalleeTopFile {
249 pub path: String,
251 pub count: usize,
253}
254
255#[derive(Debug, Clone, Serialize)]
257#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
258pub struct SecurityUnresolvedCalleeReasonCount {
259 pub reason: fallow_types::extract::SkippedSecurityCalleeReason,
261 pub count: usize,
263}
264
265#[derive(Debug, Clone, Serialize)]
269#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
270pub struct SecuritySummaryOutput {
271 pub schema_version: SecuritySchemaVersion,
273 pub version: ToolVersion,
275 pub elapsed_ms: ElapsedMs,
277 pub config: SecurityOutputConfig,
279 #[serde(rename = "_meta", default, skip_serializing_if = "Option::is_none")]
281 pub meta: Option<Meta>,
282 #[serde(default, skip_serializing_if = "Option::is_none")]
284 pub gate: Option<SecurityGate>,
285 pub summary: SecuritySummary,
287}
288
289#[derive(Debug, Clone, Serialize)]
291#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
292pub struct SecuritySummary {
293 pub security_findings: usize,
295 pub by_severity: SecuritySeverityCounts,
297 pub by_category: BTreeMap<String, usize>,
300 pub by_reachability: SecurityReachabilityCounts,
302 pub by_runtime_state: SecurityRuntimeStateCounts,
304 pub unresolved_edge_files: usize,
306 pub unresolved_callee_sites: usize,
308 pub attack_surface_entries: usize,
310}
311
312#[derive(Debug, Clone, Copy, Default, Serialize)]
314#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
315pub struct SecuritySeverityCounts {
316 pub high: usize,
317 pub medium: usize,
318 pub low: usize,
319}
320
321#[derive(Debug, Clone, Copy, Default, Serialize)]
323#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
324pub struct SecurityReachabilityCounts {
325 pub entry_reachable: usize,
326 pub untrusted_source_reachable: usize,
327 pub arg_level: usize,
328 pub module_level: usize,
329 pub crosses_boundary: usize,
330 pub source_backed: usize,
331}
332
333#[derive(Debug, Clone, Copy, Default, Serialize)]
335#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
336pub struct SecurityRuntimeStateCounts {
337 pub runtime_hot: usize,
338 pub runtime_cold: usize,
339 pub never_executed: usize,
340 pub low_traffic: usize,
341 pub coverage_unavailable: usize,
342 pub runtime_unknown: usize,
343 pub not_collected: usize,
344}
345
346pub struct SecurityOptions<'a> {
348 pub root: &'a Path,
350 pub config_path: &'a Option<PathBuf>,
352 pub output: OutputFormat,
354 pub no_cache: bool,
356 pub threads: usize,
358 pub quiet: bool,
360 pub fail_on_issues: bool,
362 pub sarif_file: Option<&'a Path>,
364 pub summary: bool,
366 pub changed_since: Option<&'a str>,
368 pub use_shared_diff_index: bool,
370 pub workspace: Option<&'a [String]>,
372 pub changed_workspaces: Option<&'a str>,
374 pub file: &'a [PathBuf],
376 pub surface: bool,
378 pub gate: Option<SecurityGateMode>,
383 pub runtime_coverage: Option<&'a Path>,
385 pub min_invocations_hot: u64,
387 pub explain: bool,
389}
390
391pub fn run(opts: &SecurityOptions<'_>) -> ExitCode {
396 let started = Instant::now();
397 if !matches!(
398 opts.output,
399 OutputFormat::Human | OutputFormat::Json | OutputFormat::Sarif
400 ) {
401 return emit_error(
402 "fallow security supports --format human, json, or sarif only.",
403 2,
404 opts.output,
405 );
406 }
407
408 let mut config = match load_config_for_analysis(
409 opts.root,
410 opts.config_path,
411 opts.output,
412 opts.no_cache,
413 opts.threads,
414 None,
415 opts.quiet,
416 ProductionAnalysis::DeadCode,
417 ) {
418 Ok(config) => config,
419 Err(code) => return code,
420 };
421
422 let configured_severity = config.rules.security_client_server_leak;
423 let configured_sink_severity = config.rules.security_sink;
424 force_security_rules(&mut config);
425 let effective_severity = config.rules.security_client_server_leak;
426 let effective_sink_severity = config.rules.security_sink;
427
428 let mut analysis = match analyze_security_candidates(opts, &config) {
429 Ok(analysis) => analysis,
430 Err(code) => return code,
431 };
432
433 let ws_roots = match crate::check::filtering::resolve_workspace_scope(
435 opts.root,
436 opts.workspace,
437 opts.changed_workspaces,
438 opts.output,
439 ) {
440 Ok(roots) => roots,
441 Err(code) => return code,
442 };
443 if let Some(ref roots) = ws_roots {
444 crate::check::filtering::filter_to_workspaces(&mut analysis.results, roots);
445 }
446
447 if !matches!(opts.gate, Some(SecurityGateMode::NewlyReachable)) {
448 apply_changed_scope(opts, &mut analysis.results);
449 }
450 filter_to_files(&mut analysis.results, opts.root, opts.file, opts.quiet);
451
452 let gate_mode = match apply_security_gate(opts, &config, &mut analysis.results) {
453 Ok(mode) => mode,
454 Err(code) => return code,
455 };
456
457 let unresolved_edge_files = analysis.results.security_unresolved_edge_files;
458 let unresolved_callee_sites = analysis.results.security_unresolved_callee_sites;
459 let unresolved_callee_diagnostics = unresolved_callee_diagnostics(
460 &analysis.results.security_unresolved_callee_diagnostics,
461 &config.root,
462 );
463 let runtime_report = match security_runtime_report(opts, &mut analysis) {
464 Ok(report) => report,
465 Err(code) => return code,
466 };
467 let mut findings: Vec<SecurityFinding> =
468 std::mem::take(&mut analysis.results.security_findings)
469 .into_iter()
470 .map(|f| relativize_finding(f, &config.root))
471 .collect();
472 if let (Some(report), Some(modules), Some(files)) = (
473 runtime_report.as_ref(),
474 analysis.modules.as_ref(),
475 analysis.files.as_ref(),
476 ) {
477 apply_runtime_context(&mut findings, modules, files, &config.root, report);
478 }
479 apply_security_severity(&mut findings);
480 sort_by_security_severity(&mut findings);
481 for finding in &mut findings {
482 finding.finding_id = security_finding_id(finding);
485 }
486 let (findings, attack_surface) = prepare_findings(findings, &config.root, opts.surface);
487
488 let gate = gate_mode.map(|mode| {
493 let new_count = findings.len();
494 SecurityGate {
495 mode,
496 verdict: if new_count > 0 {
497 SecurityGateVerdict::Fail
498 } else {
499 SecurityGateVerdict::Pass
500 },
501 new_count,
502 }
503 });
504
505 let advisory_fail = (opts.fail_on_issues
506 || effective_severity == Severity::Error
507 || effective_sink_severity == Severity::Error)
508 && !findings.is_empty();
509
510 let output = SecurityOutput {
511 schema_version: SecuritySchemaVersion::V6,
512 version: ToolVersion(env!("CARGO_PKG_VERSION").to_string()),
513 elapsed_ms: ElapsedMs(started.elapsed().as_millis() as u64),
514 config: security_output_config(
515 &config,
516 configured_severity,
517 effective_severity,
518 configured_sink_severity,
519 effective_sink_severity,
520 ),
521 meta: opts.explain.then(crate::explain::security_meta),
522 gate,
523 security_findings: findings,
524 attack_surface,
525 unresolved_edge_files,
526 unresolved_callee_sites,
527 unresolved_callee_diagnostics,
528 };
529 crate::telemetry::note_result_count(output.security_findings.len());
530
531 if let Some(path) = opts.sarif_file
532 && let Err(message) = write_sarif_file(&output, path)
533 {
534 return emit_error(&message, 2, opts.output);
535 }
536
537 let rendered = match opts.output {
538 OutputFormat::Json if opts.summary => render_json_summary(&output),
539 OutputFormat::Json => render_json(&output),
540 OutputFormat::Sarif => render_sarif(&output),
541 _ if opts.summary => render_human_summary(&output),
542 _ => render_human(&output),
543 };
544 outln!("{rendered}");
545
546 if let Some(gate) = &output.gate {
552 if gate.verdict == SecurityGateVerdict::Fail {
553 ExitCode::from(8)
554 } else {
555 ExitCode::SUCCESS
556 }
557 } else if advisory_fail {
558 ExitCode::from(1)
559 } else {
560 ExitCode::SUCCESS
561 }
562}
563
564fn force_security_rules(config: &mut fallow_config::ResolvedConfig) {
565 if config.rules.security_client_server_leak == Severity::Off {
568 config.rules.security_client_server_leak = Severity::Warn;
569 }
570 if config.rules.security_sink == Severity::Off {
571 config.rules.security_sink = Severity::Warn;
572 }
573}
574
575fn security_output_config(
576 config: &fallow_config::ResolvedConfig,
577 configured_severity: Severity,
578 effective_severity: Severity,
579 configured_sink_severity: Severity,
580 effective_sink_severity: Severity,
581) -> SecurityOutputConfig {
582 let categories = config.security.categories.as_ref();
583 SecurityOutputConfig {
584 rules: SecurityOutputRulesConfig {
585 security_client_server_leak: SecurityRuleSeverityConfig {
586 configured: configured_severity,
587 effective: effective_severity,
588 },
589 security_sink: SecurityRuleSeverityConfig {
590 configured: configured_sink_severity,
591 effective: effective_sink_severity,
592 },
593 },
594 categories_include: categories.and_then(|categories| categories.include.clone()),
595 categories_exclude: categories.and_then(|categories| categories.exclude.clone()),
596 }
597}
598
599fn apply_changed_scope(opts: &SecurityOptions<'_>, results: &mut AnalysisResults) {
600 if let Some(git_ref) = opts.changed_since
601 && let Some(changed) = fallow_core::changed_files::get_changed_files(opts.root, git_ref)
602 {
603 fallow_core::changed_files::filter_results_by_changed_files(results, &changed);
604 }
605 if opts.use_shared_diff_index
606 && let Some(diff_index) = crate::report::ci::diff_filter::shared_diff_index()
607 {
608 crate::check::filtering::filter_results_by_diff(results, diff_index, opts.root);
609 }
610}
611
612fn apply_security_gate(
613 opts: &SecurityOptions<'_>,
614 config: &fallow_config::ResolvedConfig,
615 results: &mut AnalysisResults,
616) -> Result<Option<SecurityGateMode>, ExitCode> {
617 let Some(mode) = opts.gate else {
618 return Ok(None);
619 };
620
621 if matches!(mode, SecurityGateMode::NewlyReachable) {
622 retain_gate_newly_reachable(opts, config, results)?;
623 return Ok(Some(mode));
624 }
625
626 let mut owned_gate_diff: Option<crate::report::ci::diff_filter::DiffIndex> = None;
631 let gate_diff: &crate::report::ci::diff_filter::DiffIndex =
632 if let Some(shared) = crate::report::ci::diff_filter::shared_diff_index() {
633 shared
634 } else if let Some(git_ref) = opts.changed_since {
635 match fallow_core::changed_files::try_get_changed_diff(opts.root, git_ref) {
636 Ok(text) => owned_gate_diff
637 .insert(crate::report::ci::diff_filter::DiffIndex::from_unified_diff(&text)),
638 Err(err) => {
639 return Err(emit_error(
640 &format!(
641 "fallow security --gate could not compute the diff for '{git_ref}': {}",
642 err.describe()
643 ),
644 2,
645 opts.output,
646 ));
647 }
648 }
649 } else {
650 return Err(emit_error(
651 "fallow security --gate requires a diff source: --changed-since <ref>, \
652 --diff-file <path>, or --diff-stdin.",
653 2,
654 opts.output,
655 ));
656 };
657 crate::check::filtering::retain_gate_new(results, gate_diff, opts.root);
658 Ok(Some(mode))
659}
660
661const SECURITY_BASE_SNAPSHOT_CACHE_VERSION: u8 = 1;
662const MAX_SECURITY_BASE_SNAPSHOT_CACHE_SIZE: usize = 8 * 1024 * 1024;
663
664#[derive(Debug, Clone)]
665struct SecurityKeySnapshot {
666 reachable: FxHashSet<String>,
667}
668
669struct SecurityBaseSnapshotCacheKey {
670 hash: u64,
671 base_sha: String,
672}
673
674#[derive(bitcode::Encode, bitcode::Decode)]
675struct CachedSecurityKeySnapshot {
676 version: u8,
677 cli_version: String,
678 key_hash: u64,
679 base_sha: String,
680 reachable: Vec<String>,
681}
682
683fn retain_gate_newly_reachable(
684 opts: &SecurityOptions<'_>,
685 config: &fallow_config::ResolvedConfig,
686 results: &mut AnalysisResults,
687) -> Result<(), ExitCode> {
688 let Some(base_ref) = opts.changed_since else {
689 return Err(emit_error(
690 "fallow security --gate newly-reachable requires --changed-since <ref>; \
691 --diff-file and --diff-stdin do not identify a base tree.",
692 2,
693 opts.output,
694 ));
695 };
696 let Some(base_sha) = git_rev_parse(opts.root, base_ref) else {
697 return Err(emit_error(
698 &format!(
699 "fallow security --gate newly-reachable could not resolve base ref '{base_ref}'."
700 ),
701 2,
702 opts.output,
703 ));
704 };
705 let cache_key = security_base_snapshot_cache_key(opts, config, &base_sha)?;
706 let base = if let Some(snapshot) = load_cached_security_base_snapshot(config, &cache_key) {
707 snapshot
708 } else {
709 let snapshot = compute_base_security_snapshot(opts, config, base_ref, &base_sha)?;
710 save_cached_security_base_snapshot(config, &cache_key, &snapshot);
711 snapshot
712 };
713 results.security_findings.retain(|finding| {
714 security_reachability_key(finding, opts.root)
715 .is_some_and(|key| !base.reachable.contains(&key))
716 });
717 Ok(())
718}
719
720fn compute_base_security_snapshot(
721 opts: &SecurityOptions<'_>,
722 config: &fallow_config::ResolvedConfig,
723 base_ref: &str,
724 base_sha: &str,
725) -> Result<SecurityKeySnapshot, ExitCode> {
726 let Some(worktree) = BaseWorktree::create(opts.root, base_ref, Some(base_sha)) else {
727 return Err(emit_error(
728 &format!("could not create a temporary worktree for base ref '{base_ref}'"),
729 2,
730 opts.output,
731 ));
732 };
733 let base_root = base_analysis_root(opts.root, worktree.path());
734 let current_config_path = opts
735 .config_path
736 .clone()
737 .or_else(|| fallow_config::FallowConfig::find_config_path(opts.root));
738 let mut base_config = load_config_for_analysis(
739 &base_root,
740 ¤t_config_path,
741 opts.output,
742 opts.no_cache,
743 opts.threads,
744 None,
745 true,
746 ProductionAnalysis::DeadCode,
747 )?;
748 base_config.cache_dir =
749 remap_cache_dir_for_base_worktree(opts.root, &base_root, &config.cache_dir);
750 force_security_rules(&mut base_config);
751 let mut base_analysis = analyze_security_candidates(
752 &SecurityOptions {
753 root: &base_root,
754 config_path: ¤t_config_path,
755 output: opts.output,
756 no_cache: opts.no_cache,
757 threads: opts.threads,
758 quiet: true,
759 fail_on_issues: false,
760 sarif_file: None,
761 summary: false,
762 changed_since: None,
763 use_shared_diff_index: false,
764 workspace: opts.workspace,
765 changed_workspaces: None,
766 file: &[],
767 surface: false,
768 gate: None,
769 runtime_coverage: None,
770 min_invocations_hot: opts.min_invocations_hot,
771 explain: false,
772 },
773 &base_config,
774 )?;
775 if let Some(ref roots) = crate::check::filtering::resolve_workspace_scope(
776 &base_root,
777 opts.workspace,
778 None,
779 opts.output,
780 )? {
781 crate::check::filtering::filter_to_workspaces(&mut base_analysis.results, roots);
782 }
783 Ok(SecurityKeySnapshot {
784 reachable: security_reachable_keys(&base_analysis.results.security_findings, &base_root),
785 })
786}
787
788fn security_reachable_keys(findings: &[SecurityFinding], root: &Path) -> FxHashSet<String> {
789 findings
790 .iter()
791 .filter_map(|finding| security_reachability_key(finding, root))
792 .collect()
793}
794
795fn security_reachability_key(finding: &SecurityFinding, root: &Path) -> Option<String> {
796 if !finding
797 .reachability
798 .as_ref()
799 .is_some_and(|reachability| reachability.reachable_from_entry)
800 {
801 return None;
802 }
803 let category = finding.category.as_deref().unwrap_or("none");
804 Some(format!(
805 "security-reach:{}:{}:{}",
806 relative_key(&finding.path, root),
807 security_kind_key(finding.kind),
808 category,
809 ))
810}
811
812fn security_kind_key(kind: SecurityFindingKind) -> &'static str {
813 match kind {
814 SecurityFindingKind::ClientServerLeak => "client-server-leak",
815 SecurityFindingKind::TaintedSink => "tainted-sink",
816 }
817}
818
819fn security_base_snapshot_cache_key(
820 opts: &SecurityOptions<'_>,
821 config: &fallow_config::ResolvedConfig,
822 base_sha: &str,
823) -> Result<SecurityBaseSnapshotCacheKey, ExitCode> {
824 let payload = serde_json::json!({
825 "cache_version": SECURITY_BASE_SNAPSHOT_CACHE_VERSION,
826 "cli_version": env!("CARGO_PKG_VERSION"),
827 "base_sha": base_sha,
828 "config_hash": format!("{:016x}", config.cache_config_hash),
829 "security_client_server_leak": format!("{:?}", config.rules.security_client_server_leak),
830 "security_sink": format!("{:?}", config.rules.security_sink),
831 "workspace": opts.workspace,
832 "changed_workspaces": opts.changed_workspaces,
833 });
834 let bytes = serde_json::to_vec(&payload).map_err(|err| {
835 emit_error(
836 &format!("failed to build security gate cache key: {err}"),
837 2,
838 opts.output,
839 )
840 })?;
841 Ok(SecurityBaseSnapshotCacheKey {
842 hash: xxh3_64(&bytes),
843 base_sha: base_sha.to_owned(),
844 })
845}
846
847fn security_base_snapshot_cache_dir(config: &fallow_config::ResolvedConfig) -> PathBuf {
848 config.cache_dir.join("cache").join(format!(
849 "security-base-v{SECURITY_BASE_SNAPSHOT_CACHE_VERSION}"
850 ))
851}
852
853fn security_base_snapshot_cache_file(
854 config: &fallow_config::ResolvedConfig,
855 key: &SecurityBaseSnapshotCacheKey,
856) -> PathBuf {
857 security_base_snapshot_cache_dir(config).join(format!("{:016x}.bin", key.hash))
858}
859
860fn ensure_security_base_snapshot_cache_dir(dir: &Path) -> Result<(), std::io::Error> {
861 std::fs::create_dir_all(dir)?;
862 let gitignore = dir.join(".gitignore");
863 if std::fs::read_to_string(&gitignore).ok().as_deref() != Some("*\n") {
864 std::fs::write(gitignore, "*\n")?;
865 }
866 Ok(())
867}
868
869fn load_cached_security_base_snapshot(
870 config: &fallow_config::ResolvedConfig,
871 key: &SecurityBaseSnapshotCacheKey,
872) -> Option<SecurityKeySnapshot> {
873 if config.no_cache {
874 return None;
875 }
876 let path = security_base_snapshot_cache_file(config, key);
877 let data = std::fs::read(path).ok()?;
878 if data.len() > MAX_SECURITY_BASE_SNAPSHOT_CACHE_SIZE {
879 return None;
880 }
881 let cached: CachedSecurityKeySnapshot = bitcode::decode(&data).ok()?;
882 if cached.version != SECURITY_BASE_SNAPSHOT_CACHE_VERSION
883 || cached.cli_version != env!("CARGO_PKG_VERSION")
884 || cached.key_hash != key.hash
885 || cached.base_sha != key.base_sha
886 {
887 return None;
888 }
889 Some(SecurityKeySnapshot {
890 reachable: cached.reachable.into_iter().collect(),
891 })
892}
893
894fn save_cached_security_base_snapshot(
895 config: &fallow_config::ResolvedConfig,
896 key: &SecurityBaseSnapshotCacheKey,
897 snapshot: &SecurityKeySnapshot,
898) {
899 if config.no_cache {
900 return;
901 }
902 let dir = security_base_snapshot_cache_dir(config);
903 if ensure_security_base_snapshot_cache_dir(&dir).is_err() {
904 return;
905 }
906 let mut reachable = snapshot.reachable.iter().cloned().collect::<Vec<_>>();
907 reachable.sort_unstable();
908 let data = bitcode::encode(&CachedSecurityKeySnapshot {
909 version: SECURITY_BASE_SNAPSHOT_CACHE_VERSION,
910 cli_version: env!("CARGO_PKG_VERSION").to_owned(),
911 key_hash: key.hash,
912 base_sha: key.base_sha.clone(),
913 reachable,
914 });
915 let Ok(mut tmp) = tempfile::NamedTempFile::new_in(&dir) else {
916 return;
917 };
918 if tmp.write_all(&data).is_err() {
919 return;
920 }
921 let _ = tmp.persist(security_base_snapshot_cache_file(config, key));
922}
923
924fn base_analysis_root(current_root: &Path, base_worktree_root: &Path) -> PathBuf {
925 if current_root.is_absolute()
926 && let Some(git_root) = crate::base_worktree::git_toplevel(current_root)
927 && let Ok(relative) = current_root.strip_prefix(git_root)
928 {
929 return base_worktree_root.join(relative);
930 }
931 base_worktree_root.to_path_buf()
932}
933
934fn remap_cache_dir_for_base_worktree(
935 current_root: &Path,
936 base_worktree_root: &Path,
937 cache_dir: &Path,
938) -> PathBuf {
939 if cache_dir.is_absolute()
940 && let Ok(relative) = cache_dir.strip_prefix(current_root)
941 {
942 return base_worktree_root.join(relative);
943 }
944 cache_dir.to_path_buf()
945}
946
947struct SecurityAnalysisState {
948 results: AnalysisResults,
949 modules: Option<Vec<ModuleInfo>>,
950 files: Option<Vec<DiscoveredFile>>,
951 analysis_output: Option<fallow_core::AnalysisOutput>,
952}
953
954#[expect(
955 deprecated,
956 reason = "ADR-008 deprecates fallow_core::analyze APIs externally; the CLI uses the workspace path dependency"
957)]
958fn analyze_security_candidates(
959 opts: &SecurityOptions<'_>,
960 config: &fallow_config::ResolvedConfig,
961) -> Result<SecurityAnalysisState, ExitCode> {
962 if opts.runtime_coverage.is_none() {
963 return fallow_core::analyze(config)
964 .map(|results| SecurityAnalysisState {
965 results,
966 modules: None,
967 files: None,
968 analysis_output: None,
969 })
970 .map_err(|err| emit_error(&format!("Analysis error: {err}"), 2, opts.output));
971 }
972
973 fallow_core::analyze_retaining_modules(config, true, true)
974 .map(|mut output| {
975 let modules = output.modules.take();
976 let files = output.files.take();
977 let results = output.results.clone();
978 SecurityAnalysisState {
979 results,
980 modules,
981 files,
982 analysis_output: Some(output),
983 }
984 })
985 .map_err(|err| emit_error(&format!("Analysis error: {err}"), 2, opts.output))
986}
987
988fn security_runtime_report(
989 opts: &SecurityOptions<'_>,
990 analysis: &mut SecurityAnalysisState,
991) -> Result<Option<RuntimeCoverageReport>, ExitCode> {
992 let Some(path) = opts.runtime_coverage else {
993 return Ok(None);
994 };
995 let (Some(modules), Some(files), Some(analysis_output)) = (
996 analysis.modules.as_ref(),
997 analysis.files.as_ref(),
998 analysis.analysis_output.take(),
999 ) else {
1000 return Ok(None);
1001 };
1002 analyze_security_runtime(opts, path, modules.clone(), files.clone(), analysis_output)
1003}
1004
1005fn analyze_security_runtime(
1006 opts: &SecurityOptions<'_>,
1007 path: &Path,
1008 modules: Vec<ModuleInfo>,
1009 files: Vec<DiscoveredFile>,
1010 analysis_output: fallow_core::AnalysisOutput,
1011) -> Result<Option<RuntimeCoverageReport>, ExitCode> {
1012 let runtime_coverage = crate::health::coverage::prepare_options(
1013 path,
1014 opts.min_invocations_hot,
1015 None,
1016 None,
1017 opts.output,
1018 )?;
1019 let result = crate::health::execute_health_with_shared_parse(
1020 &HealthOptions {
1021 root: opts.root,
1022 config_path: opts.config_path,
1023 output: opts.output,
1024 no_cache: opts.no_cache,
1025 threads: opts.threads,
1026 quiet: opts.quiet,
1027 max_cyclomatic: None,
1028 max_cognitive: None,
1029 max_crap: None,
1030 top: None,
1031 sort: SortBy::Cyclomatic,
1032 production: true,
1033 production_override: Some(true),
1034 changed_since: opts.changed_since,
1035 diff_index: None,
1036 use_shared_diff_index: opts.use_shared_diff_index,
1037 workspace: opts.workspace,
1038 changed_workspaces: opts.changed_workspaces,
1039 baseline: None,
1040 save_baseline: None,
1041 complexity: false,
1042 complexity_breakdown: false,
1043 file_scores: false,
1044 coverage_gaps: false,
1045 config_activates_coverage_gaps: false,
1046 hotspots: false,
1047 ownership: false,
1048 ownership_emails: None,
1049 targets: false,
1050 force_full: false,
1051 score_only_output: false,
1052 enforce_coverage_gap_gate: false,
1053 effort: None,
1054 score: false,
1055 min_score: None,
1056 since: None,
1057 min_commits: None,
1058 explain: false,
1059 summary: false,
1060 save_snapshot: None,
1061 trend: false,
1062 group_by: None,
1063 coverage: None,
1064 coverage_root: None,
1065 performance: false,
1066 min_severity: None,
1067 report_only: false,
1068 runtime_coverage: Some(runtime_coverage),
1069 churn_file: None,
1070 },
1071 SharedParseData {
1072 files,
1073 modules,
1074 analysis_output: Some(analysis_output),
1075 },
1076 )?;
1077 Ok(result.report.runtime_coverage)
1078}
1079
1080#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1081struct RuntimeFunctionKey {
1082 path: String,
1083 function: String,
1084 line: u32,
1085}
1086
1087#[derive(Debug, Clone)]
1088struct FunctionSpan {
1089 key: RuntimeFunctionKey,
1090 end_line: u32,
1091}
1092
1093fn apply_runtime_context(
1094 findings: &mut Vec<SecurityFinding>,
1095 modules: &[ModuleInfo],
1096 files: &[fallow_types::discover::DiscoveredFile],
1097 root: &Path,
1098 report: &RuntimeCoverageReport,
1099) {
1100 let spans = function_spans(modules, files, root);
1101 let runtime = SecurityRuntimeIndex::new(report);
1102 let mut indexed = findings.drain(..).enumerate().collect::<Vec<_>>();
1103 for (_, finding) in &mut indexed {
1104 if !matches!(finding.kind, SecurityFindingKind::TaintedSink) {
1105 continue;
1106 }
1107 finding.runtime = runtime_context_for_finding(finding, &spans, &runtime);
1108 }
1109 indexed.sort_by(|(left_index, left), (right_index, right)| {
1110 runtime_rank(left)
1111 .cmp(&runtime_rank(right))
1112 .then_with(|| left_index.cmp(right_index))
1113 });
1114 findings.extend(indexed.into_iter().map(|(_, finding)| finding));
1115}
1116
1117fn function_spans(
1118 modules: &[ModuleInfo],
1119 files: &[fallow_types::discover::DiscoveredFile],
1120 root: &Path,
1121) -> Vec<FunctionSpan> {
1122 let paths_by_id = files
1123 .iter()
1124 .map(|file| (file.id, &file.path))
1125 .collect::<rustc_hash::FxHashMap<_, _>>();
1126 let mut spans = Vec::new();
1127 for module in modules {
1128 let Some(path) = paths_by_id.get(&module.file_id) else {
1129 continue;
1130 };
1131 let path = relative_key(path, root);
1132 for function in &module.complexity {
1133 spans.push(FunctionSpan {
1134 key: RuntimeFunctionKey {
1135 path: path.clone(),
1136 function: function.name.clone(),
1137 line: function.line,
1138 },
1139 end_line: function.line.saturating_add(function.line_count),
1140 });
1141 }
1142 }
1143 spans
1144}
1145
1146struct SecurityRuntimeIndex {
1147 hot_paths: Vec<(RuntimeFunctionKey, u32, SecurityRuntimeContext)>,
1148 findings: rustc_hash::FxHashMap<RuntimeFunctionKey, SecurityRuntimeContext>,
1149}
1150
1151impl SecurityRuntimeIndex {
1152 fn new(report: &RuntimeCoverageReport) -> Self {
1153 let hot_paths = report
1154 .hot_paths
1155 .iter()
1156 .map(|hot| {
1157 (
1158 runtime_hot_key(hot),
1159 hot.end_line.max(hot.line),
1160 SecurityRuntimeContext {
1161 state: SecurityRuntimeState::RuntimeHot,
1162 function: hot.function.clone(),
1163 line: hot.line,
1164 invocations: Some(hot.invocations),
1165 stable_id: hot.stable_id.clone(),
1166 evidence: Some(format!(
1167 "production hot path observed with {} invocation{}",
1168 hot.invocations,
1169 crate::report::plural(hot.invocations as usize)
1170 )),
1171 },
1172 )
1173 })
1174 .collect();
1175 let findings = report
1176 .findings
1177 .iter()
1178 .map(runtime_finding_context)
1179 .collect();
1180 Self {
1181 hot_paths,
1182 findings,
1183 }
1184 }
1185}
1186
1187fn runtime_context_for_finding(
1188 finding: &SecurityFinding,
1189 spans: &[FunctionSpan],
1190 runtime: &SecurityRuntimeIndex,
1191) -> Option<SecurityRuntimeContext> {
1192 let path = path_key(&finding.path);
1193 let span = spans
1194 .iter()
1195 .filter(|span| {
1196 span.key.path == path && span.key.line <= finding.line && finding.line <= span.end_line
1197 })
1198 .min_by_key(|span| span.end_line.saturating_sub(span.key.line))?;
1199 if let Some((_, _, context)) = runtime.hot_paths.iter().find(|(key, end_line, _)| {
1200 key == &span.key && key.line <= finding.line && finding.line <= *end_line
1201 }) {
1202 return Some(context.clone());
1203 }
1204 runtime.findings.get(&span.key).cloned().or_else(|| {
1205 Some(SecurityRuntimeContext {
1206 state: SecurityRuntimeState::RuntimeUnknown,
1207 function: span.key.function.clone(),
1208 line: span.key.line,
1209 invocations: None,
1210 stable_id: None,
1211 evidence: Some("runtime coverage carried no matching function evidence".to_owned()),
1212 })
1213 })
1214}
1215
1216fn runtime_rank(finding: &SecurityFinding) -> u8 {
1217 match finding.runtime.as_ref().map(|runtime| runtime.state) {
1218 Some(SecurityRuntimeState::RuntimeHot) => 0,
1219 Some(SecurityRuntimeState::LowTraffic) => 1,
1220 None | Some(SecurityRuntimeState::RuntimeUnknown) => 2,
1221 Some(SecurityRuntimeState::CoverageUnavailable) => 3,
1222 Some(SecurityRuntimeState::RuntimeCold) => 4,
1223 Some(SecurityRuntimeState::NeverExecuted) => 5,
1224 }
1225}
1226
1227fn apply_security_severity(findings: &mut [SecurityFinding]) {
1228 for finding in findings {
1229 finding.severity = derive_security_severity(finding);
1230 }
1231}
1232
1233fn sort_by_security_severity(findings: &mut [SecurityFinding]) {
1234 findings.sort_by(compare_security_priority);
1235}
1236
1237fn compare_security_priority(left: &SecurityFinding, right: &SecurityFinding) -> Ordering {
1238 security_severity_rank(left.severity)
1239 .cmp(&security_severity_rank(right.severity))
1240 .then_with(|| runtime_rank(left).cmp(&runtime_rank(right)))
1241 .then_with(|| {
1242 right
1243 .reachability
1244 .as_ref()
1245 .is_some_and(|reach| reach.reachable_from_entry)
1246 .cmp(
1247 &left
1248 .reachability
1249 .as_ref()
1250 .is_some_and(|reach| reach.reachable_from_entry),
1251 )
1252 })
1253 .then_with(|| taint_rank(left).cmp(&taint_rank(right)))
1254 .then_with(|| security_blast_radius(right).cmp(&security_blast_radius(left)))
1255 .then_with(|| security_crosses_boundary(right).cmp(&security_crosses_boundary(left)))
1256 .then_with(|| left.dead_code.is_some().cmp(&right.dead_code.is_some()))
1257 .then_with(|| left.path.cmp(&right.path))
1258 .then_with(|| left.line.cmp(&right.line))
1259 .then_with(|| left.col.cmp(&right.col))
1260 .then_with(|| left.category.cmp(&right.category))
1261}
1262
1263fn taint_rank(finding: &SecurityFinding) -> u8 {
1264 match finding
1265 .reachability
1266 .as_ref()
1267 .and_then(|reach| reach.taint_confidence)
1268 {
1269 Some(TaintConfidence::ArgLevel) => 0,
1270 Some(TaintConfidence::ModuleLevel) => 1,
1271 None if finding.source_backed => 0,
1272 None if finding
1273 .reachability
1274 .as_ref()
1275 .is_some_and(|reach| reach.reachable_from_untrusted_source) =>
1276 {
1277 1
1278 }
1279 None => 2,
1280 }
1281}
1282
1283fn security_blast_radius(finding: &SecurityFinding) -> u32 {
1284 finding
1285 .reachability
1286 .as_ref()
1287 .map_or(0, |reach| reach.blast_radius)
1288}
1289
1290fn security_crosses_boundary(finding: &SecurityFinding) -> bool {
1291 finding
1292 .reachability
1293 .as_ref()
1294 .is_some_and(|reach| reach.crosses_boundary)
1295}
1296
1297const fn security_severity_rank(severity: SecuritySeverity) -> u8 {
1298 match severity {
1299 SecuritySeverity::High => 0,
1300 SecuritySeverity::Medium => 1,
1301 SecuritySeverity::Low => 2,
1302 }
1303}
1304
1305fn runtime_hot_key(hot: &RuntimeCoverageHotPath) -> RuntimeFunctionKey {
1306 RuntimeFunctionKey {
1307 path: path_key(&hot.path),
1308 function: hot.function.clone(),
1309 line: hot.line,
1310 }
1311}
1312
1313fn runtime_finding_context(
1314 finding: &RuntimeCoverageFinding,
1315) -> (RuntimeFunctionKey, SecurityRuntimeContext) {
1316 let state = match finding.verdict {
1317 RuntimeCoverageVerdict::SafeToDelete => SecurityRuntimeState::NeverExecuted,
1318 RuntimeCoverageVerdict::ReviewRequired if finding.invocations.unwrap_or(0) == 0 => {
1319 SecurityRuntimeState::RuntimeCold
1320 }
1321 RuntimeCoverageVerdict::LowTraffic => SecurityRuntimeState::LowTraffic,
1322 RuntimeCoverageVerdict::CoverageUnavailable | RuntimeCoverageVerdict::Unknown => {
1323 SecurityRuntimeState::CoverageUnavailable
1324 }
1325 RuntimeCoverageVerdict::ReviewRequired | RuntimeCoverageVerdict::Active => {
1326 SecurityRuntimeState::RuntimeUnknown
1327 }
1328 };
1329 (
1330 RuntimeFunctionKey {
1331 path: path_key(&finding.path),
1332 function: finding.function.clone(),
1333 line: finding.line,
1334 },
1335 SecurityRuntimeContext {
1336 state,
1337 function: finding.function.clone(),
1338 line: finding.line,
1339 invocations: finding.invocations,
1340 stable_id: finding.stable_id.clone(),
1341 evidence: Some(format!("runtime coverage verdict: {}", finding.verdict)),
1342 },
1343 )
1344}
1345
1346fn relative_key(path: &Path, root: &Path) -> String {
1347 path_key(path.strip_prefix(root).unwrap_or(path))
1348}
1349
1350fn path_key(path: &Path) -> String {
1351 path.to_string_lossy().replace('\\', "/")
1352}
1353
1354fn unresolved_callee_diagnostics(
1355 diagnostics: &[SecurityUnresolvedCalleeDiagnostic],
1356 root: &Path,
1357) -> Option<SecurityUnresolvedCalleeDiagnostics> {
1358 if diagnostics.is_empty() {
1359 return None;
1360 }
1361
1362 let mut sorted = diagnostics.to_vec();
1363 sorted.sort_by(|a, b| {
1364 a.path
1365 .cmp(&b.path)
1366 .then(a.line.cmp(&b.line))
1367 .then(a.col.cmp(&b.col))
1368 .then(a.reason.cmp(&b.reason))
1369 .then(a.expression_kind.cmp(&b.expression_kind))
1370 });
1371
1372 let sampled = sorted
1373 .iter()
1374 .take(UNRESOLVED_CALLEE_SAMPLE_LIMIT)
1375 .map(|diagnostic| SecurityUnresolvedCalleeSample {
1376 path: relative_key(&diagnostic.path, root),
1377 line: diagnostic.line,
1378 col: diagnostic.col,
1379 reason: diagnostic.reason,
1380 expression_kind: diagnostic.expression_kind,
1381 })
1382 .collect();
1383
1384 let mut by_file: BTreeMap<String, usize> = BTreeMap::new();
1385 let mut by_reason: BTreeMap<fallow_types::extract::SkippedSecurityCalleeReason, usize> =
1386 BTreeMap::new();
1387 for diagnostic in &sorted {
1388 *by_file
1389 .entry(relative_key(&diagnostic.path, root))
1390 .or_insert(0) += 1;
1391 *by_reason.entry(diagnostic.reason).or_insert(0) += 1;
1392 }
1393
1394 let mut top_files: Vec<_> = by_file
1395 .into_iter()
1396 .map(|(path, count)| SecurityUnresolvedCalleeTopFile { path, count })
1397 .collect();
1398 top_files.sort_by(|a, b| b.count.cmp(&a.count).then(a.path.cmp(&b.path)));
1399 top_files.truncate(UNRESOLVED_CALLEE_TOP_FILES_LIMIT);
1400
1401 let mut by_reason: Vec<_> = by_reason
1402 .into_iter()
1403 .map(|(reason, count)| SecurityUnresolvedCalleeReasonCount { reason, count })
1404 .collect();
1405 by_reason.sort_by(|a, b| b.count.cmp(&a.count).then(a.reason.cmp(&b.reason)));
1406
1407 Some(SecurityUnresolvedCalleeDiagnostics {
1408 sampled,
1409 top_files,
1410 by_reason,
1411 sample_limit: UNRESOLVED_CALLEE_SAMPLE_LIMIT,
1412 top_files_limit: UNRESOLVED_CALLEE_TOP_FILES_LIMIT,
1413 })
1414}
1415
1416fn filter_to_files(
1417 results: &mut fallow_core::results::AnalysisResults,
1418 root: &Path,
1419 files: &[PathBuf],
1420 quiet: bool,
1421) {
1422 if files.is_empty() {
1423 return;
1424 }
1425
1426 let resolved_files: Vec<PathBuf> = files
1427 .iter()
1428 .map(|path| {
1429 if crate::path_util::is_absolute_path_any_platform(path) {
1430 path.clone()
1431 } else {
1432 root.join(path)
1433 }
1434 })
1435 .collect();
1436
1437 if !quiet {
1438 for (original, resolved) in files.iter().zip(&resolved_files) {
1439 if !resolved.exists() {
1440 eprintln!(
1441 "Warning: --file '{}' (resolved to '{}') was not found in the project",
1442 original.display(),
1443 resolved.display()
1444 );
1445 }
1446 }
1447 }
1448
1449 let file_set: rustc_hash::FxHashSet<PathBuf> = resolved_files.into_iter().collect();
1450 fallow_core::changed_files::filter_results_by_changed_files(results, &file_set);
1451}
1452
1453fn prepare_findings(
1454 findings: Vec<SecurityFinding>,
1455 root: &Path,
1456 include_surface: bool,
1457) -> (
1458 Vec<SecurityFinding>,
1459 Option<Vec<SecurityAttackSurfaceEntry>>,
1460) {
1461 let mut findings: Vec<SecurityFinding> = findings
1462 .into_iter()
1463 .map(|f| {
1464 let mut f = relativize_finding(f, root);
1465 f.finding_id = security_finding_id(&f);
1466 f
1467 })
1468 .collect();
1469 let attack_surface = include_surface.then(|| {
1470 findings
1471 .iter()
1472 .filter_map(|finding| finding.attack_surface.clone())
1473 .collect()
1474 });
1475 for finding in &mut findings {
1476 finding.attack_surface = None;
1477 }
1478 (findings, attack_surface)
1479}
1480
1481fn relativize_finding(mut finding: SecurityFinding, root: &Path) -> SecurityFinding {
1484 finding.path = relativize(&finding.path, root);
1485 for hop in &mut finding.trace {
1486 hop.path = relativize(&hop.path, root);
1487 }
1488 if let Some(reachability) = &mut finding.reachability {
1489 for hop in &mut reachability.untrusted_source_trace {
1490 hop.path = relativize(&hop.path, root);
1491 }
1492 }
1493 finding.candidate.sink.path = relativize(&finding.candidate.sink.path, root);
1494 if let Some(flow) = &mut finding.taint_flow {
1495 flow.source.path = relativize(&flow.source.path, root);
1496 flow.sink.path = relativize(&flow.sink.path, root);
1497 }
1498 if let Some(surface) = &mut finding.attack_surface {
1499 surface.source.path = relativize(&surface.source.path, root);
1500 surface.sink.path = relativize(&surface.sink.path, root);
1501 for hop in &mut surface.path {
1502 hop.path = relativize(&hop.path, root);
1503 }
1504 for control in &mut surface.defensive_boundary.controls {
1505 control.path = relativize(&control.path, root);
1506 }
1507 }
1508 finding
1509}
1510
1511fn relativize(path: &Path, root: &Path) -> PathBuf {
1512 path.strip_prefix(root)
1513 .map_or_else(|_| path.to_path_buf(), Path::to_path_buf)
1514}
1515
1516#[must_use]
1518pub fn render_json(output: &SecurityOutput) -> String {
1519 let Ok(value) = crate::output_envelope::serialize_root_output(
1520 crate::output_envelope::FallowOutput::Security(output.clone()),
1521 ) else {
1522 return "{\"error\":\"failed to serialize security output\"}".to_owned();
1523 };
1524 serde_json::to_string_pretty(&value)
1525 .unwrap_or_else(|_| "{\"error\":\"failed to serialize security output\"}".to_owned())
1526}
1527
1528#[must_use]
1530pub fn render_json_summary(output: &SecurityOutput) -> String {
1531 let summary = SecuritySummaryOutput {
1532 schema_version: output.schema_version,
1533 version: output.version.clone(),
1534 elapsed_ms: output.elapsed_ms,
1535 config: output.config.clone(),
1536 meta: output.meta.clone(),
1537 gate: output.gate,
1538 summary: security_summary(output),
1539 };
1540 let Ok(value) = crate::output_envelope::serialize_root_output_without_telemetry(
1541 crate::output_envelope::FallowOutput::SecuritySummary(summary),
1542 ) else {
1543 return "{\"error\":\"failed to serialize security summary output\"}".to_owned();
1544 };
1545 serde_json::to_string_pretty(&value).unwrap_or_else(|_| {
1546 "{\"error\":\"failed to serialize security summary output\"}".to_owned()
1547 })
1548}
1549
1550fn security_summary(output: &SecurityOutput) -> SecuritySummary {
1551 let mut by_severity = SecuritySeverityCounts::default();
1552 let mut by_reachability = SecurityReachabilityCounts::default();
1553 let mut by_runtime_state = SecurityRuntimeStateCounts::default();
1554 let mut by_category = BTreeMap::new();
1555
1556 for finding in &output.security_findings {
1557 match finding.severity {
1558 SecuritySeverity::High => by_severity.high += 1,
1559 SecuritySeverity::Medium => by_severity.medium += 1,
1560 SecuritySeverity::Low => by_severity.low += 1,
1561 }
1562 let category = finding
1563 .category
1564 .clone()
1565 .unwrap_or_else(|| security_kind_key(finding.kind).to_owned());
1566 *by_category.entry(category).or_insert(0) += 1;
1567
1568 if finding.source_backed {
1569 by_reachability.source_backed += 1;
1570 }
1571 if let Some(reachability) = &finding.reachability {
1572 if reachability.reachable_from_entry {
1573 by_reachability.entry_reachable += 1;
1574 }
1575 if reachability.reachable_from_untrusted_source {
1576 by_reachability.untrusted_source_reachable += 1;
1577 }
1578 if reachability.crosses_boundary {
1579 by_reachability.crosses_boundary += 1;
1580 }
1581 match reachability.taint_confidence {
1582 Some(TaintConfidence::ArgLevel) => by_reachability.arg_level += 1,
1583 Some(TaintConfidence::ModuleLevel) => by_reachability.module_level += 1,
1584 None => {}
1585 }
1586 }
1587
1588 match finding.runtime.as_ref().map(|runtime| runtime.state) {
1589 Some(SecurityRuntimeState::RuntimeHot) => by_runtime_state.runtime_hot += 1,
1590 Some(SecurityRuntimeState::RuntimeCold) => by_runtime_state.runtime_cold += 1,
1591 Some(SecurityRuntimeState::NeverExecuted) => by_runtime_state.never_executed += 1,
1592 Some(SecurityRuntimeState::LowTraffic) => by_runtime_state.low_traffic += 1,
1593 Some(SecurityRuntimeState::CoverageUnavailable) => {
1594 by_runtime_state.coverage_unavailable += 1;
1595 }
1596 Some(SecurityRuntimeState::RuntimeUnknown) => by_runtime_state.runtime_unknown += 1,
1597 None => by_runtime_state.not_collected += 1,
1598 }
1599 }
1600
1601 SecuritySummary {
1602 security_findings: output.security_findings.len(),
1603 by_severity,
1604 by_category,
1605 by_reachability,
1606 by_runtime_state,
1607 unresolved_edge_files: output.unresolved_edge_files,
1608 unresolved_callee_sites: output.unresolved_callee_sites,
1609 attack_surface_entries: output.attack_surface.as_ref().map_or(0, Vec::len),
1610 }
1611}
1612
1613fn write_sarif_file(output: &SecurityOutput, path: &Path) -> Result<(), String> {
1614 if let Some(parent) = path.parent()
1615 && !parent.as_os_str().is_empty()
1616 {
1617 std::fs::create_dir_all(parent).map_err(|err| {
1618 format!(
1619 "Failed to create directory for SARIF file {}: {err}",
1620 path.display()
1621 )
1622 })?;
1623 }
1624 std::fs::write(path, render_sarif(output))
1625 .map_err(|err| format!("Failed to write SARIF file {}: {err}", path.display()))
1626}
1627
1628fn gate_human_header(gate: &SecurityGate) -> String {
1633 use crate::report::plural;
1634 let checked = match gate.mode {
1635 SecurityGateMode::New => "in changed lines",
1636 SecurityGateMode::NewlyReachable => "newly reachable from entry points",
1637 };
1638 match gate.verdict {
1639 SecurityGateVerdict::Fail => format!(
1640 "Gate: REVIEW REQUIRED, {} new security item{} {checked}. fallow has not confirmed a vulnerability.",
1641 gate.new_count,
1642 plural(gate.new_count),
1643 ),
1644 SecurityGateVerdict::Pass => {
1645 format!("Gate: PASS, no new security items {checked}.")
1646 }
1647 }
1648}
1649
1650fn unresolved_callee_human_hint(output: &SecurityOutput) -> Option<String> {
1651 let diagnostics = output.unresolved_callee_diagnostics.as_ref()?;
1652 let top_reason = diagnostics.by_reason.first()?;
1653 let top_file = diagnostics.top_files.first()?;
1654 Some(format!(
1655 "Most unresolved callees: {} in {}.",
1656 unresolved_callee_reason_label(top_reason.reason),
1657 top_file.path
1658 ))
1659}
1660
1661fn unresolved_callee_reason_label(
1662 reason: fallow_types::extract::SkippedSecurityCalleeReason,
1663) -> &'static str {
1664 match reason {
1665 fallow_types::extract::SkippedSecurityCalleeReason::ComputedMember => "computed-member",
1666 fallow_types::extract::SkippedSecurityCalleeReason::DynamicDispatch => "dynamic-dispatch",
1667 fallow_types::extract::SkippedSecurityCalleeReason::UnsupportedAssignmentObject => {
1668 "unsupported-assignment-object"
1669 }
1670 }
1671}
1672
1673#[must_use]
1674fn render_human_summary(output: &SecurityOutput) -> String {
1675 use crate::report::plural;
1676 use std::fmt::Write as _;
1677
1678 let mut out = String::new();
1679 if let Some(gate) = &output.gate {
1680 out.push_str(&gate_human_header(gate));
1681 out.push('\n');
1682 }
1683 let count = output.security_findings.len();
1684 if count == 0 {
1685 out.push_str("Security review: no items to check in the scanned code.\n");
1686 } else {
1687 let _ = writeln!(
1688 out,
1689 "Security review: {count} item{} to check. These are unverified security candidates, not confirmed vulnerabilities.",
1690 plural(count),
1691 );
1692 out.push_str(
1693 "Next: check whether the listed code can run with unsafe input, secrets, or settings, and whether anything blocks the risk.\n",
1694 );
1695 }
1696 if output.unresolved_edge_files > 0 {
1697 let n = output.unresolved_edge_files;
1698 let verb = if n == 1 { "uses" } else { "use" };
1699 let _ = writeln!(
1700 out,
1701 "Blind spot: {n} client file{} {verb} dynamic imports that fallow could not follow.",
1702 plural(n)
1703 );
1704 }
1705 if output.unresolved_callee_sites > 0 {
1706 let n = output.unresolved_callee_sites;
1707 let verb = if n == 1 { "uses" } else { "use" };
1708 let _ = writeln!(
1709 out,
1710 "Blind spot: {n} call site{} {verb} code patterns that fallow could not resolve.",
1711 plural(n)
1712 );
1713 if let Some(hint) = unresolved_callee_human_hint(output) {
1714 let _ = writeln!(out, "{hint}");
1715 }
1716 }
1717 out
1718}
1719
1720#[must_use]
1723#[expect(
1724 clippy::format_push_string,
1725 reason = "small report renderer; readability over avoiding the extra allocation"
1726)]
1727pub fn render_human(output: &SecurityOutput) -> String {
1728 use crate::report::plural;
1729 use colored::Colorize;
1730
1731 let mut out = String::new();
1732 if let Some(gate) = &output.gate {
1733 out.push_str(&gate_human_header(gate));
1734 out.push_str("\n\n");
1735 }
1736 let count = output.security_findings.len();
1737 out.push_str(&format!("Security review: {count} item{}", plural(count)));
1738 if count == 0 {
1739 out.push_str(" to check in the scanned code.\n");
1740 } else {
1741 out.push_str(" to check.\n");
1742 out.push_str(
1743 "These are unverified security candidates, not confirmed vulnerabilities. Check whether the listed code can run with unsafe input, secrets, or settings, and whether anything blocks the risk.\n",
1744 );
1745 }
1746 out.push('\n');
1747
1748 if output.security_findings.is_empty() {
1749 out.push_str("No security details to show.\n");
1750 } else {
1751 for finding in &output.security_findings {
1752 let kind = security_finding_label(finding);
1753 let (glyph, label) = human_severity_marker(finding.severity);
1754 out.push_str(&format!(
1755 "{} {label} {kind} {}:{}\n",
1756 glyph,
1757 finding.path.to_string_lossy().replace('\\', "/").bold(),
1758 finding.line,
1759 ));
1760 out.push_str(&format!(" evidence: {}\n", finding.evidence));
1761 if let Some(hint) = dead_code_hint(finding) {
1762 out.push_str(&format!(" dead-code: {hint}\n"));
1763 }
1764 if let Some(runtime) = finding.runtime.as_ref() {
1765 out.push_str(&format!(" runtime: {}\n", runtime_hint_text(runtime)));
1766 }
1767 if let Some(reach) = finding.reachability.as_ref() {
1768 let entry = if reach.reachable_from_entry {
1769 "reachable from a runtime entry point"
1770 } else {
1771 "not reached from any runtime entry point"
1772 };
1773 let boundary = if reach.crosses_boundary {
1774 "; crosses an architecture boundary"
1775 } else {
1776 ""
1777 };
1778 out.push_str(&format!(
1779 " code path: {entry} (blast radius {}){boundary}\n",
1780 reach.blast_radius,
1781 ));
1782 if reach.reachable_from_untrusted_source {
1783 let hops = reach.untrusted_source_hop_count.unwrap_or(0);
1784 out.push_str(&format!(
1785 " input path: this module is reachable from a module that receives \
1786 untrusted input via {hops} import hop{}\n",
1787 crate::report::plural(hops as usize),
1788 ));
1789 if !reach.untrusted_source_trace.is_empty() {
1790 out.push_str(" input import trace:\n");
1791 for hop in &reach.untrusted_source_trace {
1792 out.push_str(&format!(
1793 " {}:{} ({})\n",
1794 hop.path.to_string_lossy().replace('\\', "/"),
1795 hop.line,
1796 hop_role_label(hop.role),
1797 ));
1798 }
1799 }
1800 }
1801 }
1802 if !finding.trace.is_empty() {
1803 out.push_str(" import trace:\n");
1804 for hop in &finding.trace {
1805 out.push_str(&format!(
1806 " {}:{} ({})\n",
1807 hop.path.to_string_lossy().replace('\\', "/"),
1808 hop.line,
1809 hop_role_label(hop.role),
1810 ));
1811 }
1812 }
1813 if matches!(finding.kind, SecurityFindingKind::ClientServerLeak) {
1814 out.push_str(
1815 " Next: check whether this import can ship a secret to the browser. If \
1816 it is type-only, server-only, or removed at build time, mark it as a false \
1817 positive.\n",
1818 );
1819 } else if finding.dead_code.is_some() {
1820 out.push_str(
1821 " Next: first verify the dead-code finding. If the code is safe to \
1822 remove, delete it. Otherwise check and harden the risky call.\n",
1823 );
1824 } else {
1825 out.push_str(
1826 " Next: check whether unsafe input, secrets, or settings can reach this \
1827 risky call without a safe guard. If not, mark it as a false positive.\n",
1828 );
1829 }
1830 out.push('\n');
1831 }
1832 }
1833
1834 if output.unresolved_edge_files > 0 {
1835 let n = output.unresolved_edge_files;
1836 let verb = if n == 1 { "uses" } else { "use" };
1837 out.push_str(&format!(
1838 "{} Blind spot: {n} client file{} {verb} dynamic imports that fallow could not \
1839 follow. Code behind those imports may be missing from this report.\n",
1840 "[I]".blue().bold(),
1841 plural(n),
1842 ));
1843 }
1844
1845 if output.unresolved_callee_sites > 0 {
1846 let n = output.unresolved_callee_sites;
1847 let verb = if n == 1 { "uses" } else { "use" };
1848 out.push_str(&format!(
1849 "{} Blind spot: {n} call site{} {verb} code patterns that fallow could not resolve, \
1850 such as dynamic dispatch, computed members, or aliased bindings.\n",
1851 "[I]".blue().bold(),
1852 plural(n),
1853 ));
1854 if let Some(hint) = unresolved_callee_human_hint(output) {
1855 out.push_str(&format!(" {hint}\n"));
1856 }
1857 }
1858
1859 out.push_str(&format!(
1860 "\nResult: {count} security item{} to check.",
1861 plural(count),
1862 ));
1863 if count > 0 {
1864 out.push_str(" Review the listed evidence and trace before changing code.");
1865 }
1866 out.push('\n');
1867 out
1868}
1869
1870fn security_finding_label(finding: &SecurityFinding) -> String {
1874 match finding.kind {
1875 SecurityFindingKind::ClientServerLeak => "client-server-leak".to_string(),
1876 SecurityFindingKind::TaintedSink => {
1877 let title = finding
1878 .category
1879 .as_deref()
1880 .and_then(fallow_core::analyze::security_catalogue_title)
1881 .or(finding.category.as_deref())
1882 .unwrap_or("tainted-sink");
1883 match finding.cwe {
1884 Some(cwe) => format!("{title} (CWE-{cwe})"),
1885 None => title.to_string(),
1886 }
1887 }
1888 }
1889}
1890
1891fn human_severity_marker(severity: SecuritySeverity) -> (colored::ColoredString, &'static str) {
1892 use colored::Colorize;
1893 match severity {
1894 SecuritySeverity::High => ("[H]".red().bold(), "high"),
1895 SecuritySeverity::Medium => ("[M]".yellow().bold(), "medium"),
1896 SecuritySeverity::Low => ("[L]".blue().bold(), "low"),
1897 }
1898}
1899
1900fn dead_code_hint(finding: &SecurityFinding) -> Option<String> {
1901 let context = finding.dead_code.as_ref()?;
1902 match context.kind {
1903 SecurityDeadCodeKind::UnusedFile => Some(
1904 "also reported as unused-file; delete this file instead of hardening the sink"
1905 .to_string(),
1906 ),
1907 SecurityDeadCodeKind::UnusedExport => Some(format!(
1908 "also reported as unused-export{}; remove the export instead of hardening the sink",
1909 context
1910 .export_name
1911 .as_ref()
1912 .map_or(String::new(), |name| format!(" `{name}`"))
1913 )),
1914 }
1915}
1916
1917const fn hop_role_label(role: TraceHopRole) -> &'static str {
1918 match role {
1919 TraceHopRole::ClientBoundary => "client boundary",
1920 TraceHopRole::UntrustedSource => "untrusted source",
1921 TraceHopRole::ModuleSource => "source module",
1922 TraceHopRole::Intermediate => "intermediate",
1923 TraceHopRole::SecretSource => "secret source",
1924 TraceHopRole::Sink => "sink site",
1925 }
1926}
1927
1928fn source_reachability_hint(finding: &SecurityFinding) -> Option<&'static str> {
1929 finding
1930 .reachability
1931 .as_ref()
1932 .filter(|reach| reach.reachable_from_untrusted_source)
1933 .map(|_| {
1934 "Module-level context: the sink module is reachable from an untrusted-source module; fallow does not prove value flow."
1935 })
1936}
1937
1938fn runtime_hint_text(runtime: &SecurityRuntimeContext) -> String {
1939 use std::fmt::Write as _;
1940
1941 let mut text = format!(
1942 "{} in {}:{}",
1943 runtime_state_label(runtime.state),
1944 runtime.function,
1945 runtime.line
1946 );
1947 if let Some(invocations) = runtime.invocations {
1948 let _ = write!(
1949 text,
1950 " ({} invocation{})",
1951 invocations,
1952 crate::report::plural(invocations as usize)
1953 );
1954 }
1955 if let Some(evidence) = runtime.evidence.as_deref() {
1956 text.push_str("; ");
1957 text.push_str(evidence);
1958 }
1959 text
1960}
1961
1962const fn runtime_state_label(state: SecurityRuntimeState) -> &'static str {
1963 match state {
1964 SecurityRuntimeState::RuntimeHot => "runtime-hot",
1965 SecurityRuntimeState::RuntimeCold => "runtime-cold",
1966 SecurityRuntimeState::NeverExecuted => "never-executed",
1967 SecurityRuntimeState::LowTraffic => "low-traffic",
1968 SecurityRuntimeState::CoverageUnavailable => "coverage-unavailable",
1969 SecurityRuntimeState::RuntimeUnknown => "runtime-unknown",
1970 }
1971}
1972
1973fn sarif_rule_id(finding: &SecurityFinding) -> String {
1978 match finding.kind {
1979 SecurityFindingKind::ClientServerLeak => "security/client-server-leak".to_owned(),
1980 SecurityFindingKind::TaintedSink => {
1981 format!(
1982 "security/{}",
1983 finding.category.as_deref().unwrap_or("tainted-sink")
1984 )
1985 }
1986 }
1987}
1988
1989fn security_help_text(title: &str) -> String {
1990 format!(
1991 "Verify this unverified {title} candidate before acting. Review the source, sink, \
1992 SARIF code flow, and any runtime or dead-code context. fallow does not prove \
1993 exploitability, attacker control, or missing sanitization."
1994 )
1995}
1996
1997fn security_help_markdown(title: &str) -> String {
1998 format!(
1999 "Verify this unverified **{title}** candidate before acting.\n\n\
2000 1. Review the source and sink in the SARIF code flow.\n\
2001 2. Confirm whether attacker-controlled data can reach the sink unsanitized.\n\
2002 3. Use runtime and dead-code context only as triage signals."
2003 )
2004}
2005
2006fn cwe_taxon_id(cwe: u32) -> String {
2007 format!("CWE-{cwe}")
2008}
2009
2010fn cwe_taxon(cwe: u32) -> serde_json::Value {
2011 let id = cwe_taxon_id(cwe);
2012 serde_json::json!({
2013 "id": id,
2014 "name": id,
2015 "shortDescription": { "text": format!("Common Weakness Enumeration {id}") },
2016 "fullDescription": { "text": format!("MITRE Common Weakness Enumeration {id}") },
2017 "helpUri": format!("https://cwe.mitre.org/data/definitions/{cwe}.html")
2018 })
2019}
2020
2021fn cwe_relationship(cwe: u32, taxon_index: usize) -> serde_json::Value {
2022 serde_json::json!({
2023 "target": {
2024 "id": cwe_taxon_id(cwe),
2025 "index": taxon_index,
2026 "toolComponent": {
2027 "name": "CWE",
2028 "index": 0
2029 }
2030 },
2031 "kinds": ["superset"]
2032 })
2033}
2034
2035fn collect_cwes(findings: &[SecurityFinding]) -> Vec<u32> {
2036 let mut cwes: Vec<u32> = findings.iter().filter_map(|finding| finding.cwe).collect();
2037 cwes.sort_unstable();
2038 cwes.dedup();
2039 cwes
2040}
2041
2042fn cwe_index(cwes: &[u32], cwe: u32) -> Option<usize> {
2043 cwes.iter().position(|existing| *existing == cwe)
2044}
2045
2046fn cwe_taxonomy(cwes: &[u32]) -> Option<serde_json::Value> {
2047 if cwes.is_empty() {
2048 return None;
2049 }
2050 let taxa = cwes.iter().map(|cwe| cwe_taxon(*cwe)).collect::<Vec<_>>();
2051 Some(serde_json::json!({
2052 "name": "CWE",
2053 "fullName": "Common Weakness Enumeration",
2054 "organization": "MITRE",
2055 "informationUri": "https://cwe.mitre.org/",
2056 "taxa": taxa
2057 }))
2058}
2059
2060fn sarif_rule_def(
2064 rule_id: &str,
2065 finding: &SecurityFinding,
2066 cwe_taxon_index: Option<usize>,
2067) -> serde_json::Value {
2068 match finding.kind {
2069 SecurityFindingKind::ClientServerLeak => {
2070 let title = "Client-server secret leak";
2071 serde_json::json!({
2072 "id": rule_id,
2073 "name": title,
2074 "shortDescription": { "text": "Client-server secret leak candidate (unverified)" },
2075 "fullDescription": { "text":
2076 "Unverified candidate, requires verification: a \"use client\" file \
2077 transitively imports a module that reads a non-public process.env \
2078 secret. fallow does not prove the secret reaches client-bundled code." },
2079 "help": {
2080 "text": security_help_text(title),
2081 "markdown": security_help_markdown(title)
2082 },
2083 "helpUri": "https://github.com/fallow-rs/fallow",
2084 "defaultConfiguration": { "level": "note" }
2085 })
2086 }
2087 SecurityFindingKind::TaintedSink => {
2088 let title = finding
2089 .category
2090 .as_deref()
2091 .and_then(fallow_core::analyze::security_catalogue_title)
2092 .or(finding.category.as_deref())
2093 .unwrap_or("tainted-sink");
2094 let mut rule = serde_json::json!({
2095 "id": rule_id,
2096 "name": title,
2097 "shortDescription": { "text": format!("{title} candidate (unverified)") },
2098 "fullDescription": { "text": format!(
2099 "Unverified candidate, requires verification: {title}. fallow flags a \
2100 syntactic sink reached by a non-literal argument; it does not prove the \
2101 value is attacker-controlled or reaches the sink unsanitized."
2102 ) },
2103 "help": {
2104 "text": security_help_text(title),
2105 "markdown": security_help_markdown(title)
2106 },
2107 "helpUri": "https://github.com/fallow-rs/fallow",
2108 "defaultConfiguration": { "level": "note" }
2109 });
2110 if let Some(cwe) = finding.cwe {
2111 rule["properties"] = serde_json::json!({
2112 "tags": [format!("external/cwe/cwe-{cwe}")]
2113 });
2114 if let Some(taxon_index) = cwe_taxon_index {
2115 rule["relationships"] = serde_json::json!([cwe_relationship(cwe, taxon_index)]);
2116 }
2117 }
2118 rule
2119 }
2120 }
2121}
2122
2123fn hop_role_token(role: TraceHopRole) -> &'static str {
2124 match role {
2125 TraceHopRole::ClientBoundary => "client-boundary",
2126 TraceHopRole::UntrustedSource => "untrusted-source",
2127 TraceHopRole::ModuleSource => "module-source",
2128 TraceHopRole::Intermediate => "intermediate",
2129 TraceHopRole::SecretSource => "secret-source",
2130 TraceHopRole::Sink => "sink",
2131 }
2132}
2133
2134fn sarif_thread_flow_location(hop: &TraceHop) -> serde_json::Value {
2135 let role = hop_role_token(hop.role);
2136 serde_json::json!({
2137 "location": sarif_location(&hop.path, hop.line, hop.col),
2138 "kinds": [role],
2139 "properties": { "fallowTraceRole": role }
2140 })
2141}
2142
2143fn primary_code_flow_hops(finding: &SecurityFinding) -> &[TraceHop] {
2144 if let Some(reachability) = finding.reachability.as_ref()
2145 && !reachability.untrusted_source_trace.is_empty()
2146 {
2147 return &reachability.untrusted_source_trace;
2148 }
2149 &finding.trace
2150}
2151
2152fn sarif_code_flows(finding: &SecurityFinding) -> Option<serde_json::Value> {
2153 let hops = primary_code_flow_hops(finding);
2154 if hops.is_empty() {
2155 return None;
2156 }
2157 let locations = hops
2158 .iter()
2159 .map(sarif_thread_flow_location)
2160 .collect::<Vec<_>>();
2161 Some(serde_json::json!([
2162 {
2163 "threadFlows": [
2164 { "locations": locations }
2165 ]
2166 }
2167 ]))
2168}
2169
2170fn push_related_location(related: &mut Vec<serde_json::Value>, hop: &TraceHop) {
2171 let location = sarif_location(&hop.path, hop.line, hop.col);
2172 if !related.iter().any(|existing| existing == &location) {
2173 related.push(location);
2174 }
2175}
2176
2177fn sarif_related_locations(finding: &SecurityFinding) -> Vec<serde_json::Value> {
2178 let mut related = Vec::new();
2179 for hop in &finding.trace {
2180 push_related_location(&mut related, hop);
2181 }
2182 if let Some(reachability) = finding.reachability.as_ref() {
2183 for hop in &reachability.untrusted_source_trace {
2184 push_related_location(&mut related, hop);
2185 }
2186 }
2187 related
2188}
2189
2190const fn sarif_level(severity: SecuritySeverity) -> &'static str {
2191 match severity {
2192 SecuritySeverity::High | SecuritySeverity::Medium => "warning",
2193 SecuritySeverity::Low => "note",
2194 }
2195}
2196
2197#[must_use]
2204fn render_sarif(output: &SecurityOutput) -> String {
2205 let cwes = collect_cwes(&output.security_findings);
2206 let results: Vec<serde_json::Value> = output
2207 .security_findings
2208 .iter()
2209 .map(|finding| {
2210 let rule_id = sarif_rule_id(finding);
2211 let mut message = dead_code_hint(finding).map_or_else(
2212 || finding.evidence.clone(),
2213 |hint| format!("{} Dead-code cross-link: {hint}.", finding.evidence),
2214 );
2215 if let Some(hint) = source_reachability_hint(finding) {
2216 message.push(' ');
2217 message.push_str(hint);
2218 }
2219 if let Some(runtime) = finding.runtime.as_ref() {
2220 message.push_str(" Runtime context: ");
2221 message.push_str(&runtime_hint_text(runtime));
2222 message.push('.');
2223 }
2224 let related = sarif_related_locations(finding);
2225 let mut result = serde_json::json!({
2230 "ruleId": rule_id,
2231 "level": sarif_level(finding.severity),
2232 "message": { "text": message },
2233 "locations": [sarif_location(&finding.path, finding.line, finding.col)],
2234 "relatedLocations": related,
2235 "partialFingerprints": { "fallowSecurity/v1": security_finding_id(finding) },
2236 });
2237 if let Some(code_flows) = sarif_code_flows(finding) {
2238 result["codeFlows"] = code_flows;
2239 }
2240 result
2241 })
2242 .collect();
2243
2244 let mut seen: Vec<String> = Vec::new();
2246 let mut rules: Vec<serde_json::Value> = Vec::new();
2247 for finding in &output.security_findings {
2248 let rule_id = sarif_rule_id(finding);
2249 if seen.iter().any(|s| s == &rule_id) {
2250 continue;
2251 }
2252 seen.push(rule_id.clone());
2253 let cwe_taxon_index = finding.cwe.and_then(|cwe| cwe_index(&cwes, cwe));
2254 rules.push(sarif_rule_def(&rule_id, finding, cwe_taxon_index));
2255 }
2256
2257 let mut run = serde_json::json!({
2258 "tool": { "driver": {
2259 "name": "fallow",
2260 "version": env!("CARGO_PKG_VERSION"),
2261 "informationUri": "https://github.com/fallow-rs/fallow",
2262 "rules": rules,
2263 }},
2264 "results": results,
2265 });
2266 if let Some(taxonomy) = cwe_taxonomy(&cwes) {
2267 run["taxonomies"] = serde_json::json!([taxonomy]);
2268 run["tool"]["driver"]["supportedTaxonomies"] = serde_json::json!([
2269 { "name": "CWE", "index": 0 }
2270 ]);
2271 }
2272 if let Some(gate) = &output.gate
2276 && let Ok(gate_value) = serde_json::to_value(gate)
2277 {
2278 run["properties"] = serde_json::json!({ "fallowGate": gate_value });
2279 }
2280
2281 let sarif = serde_json::json!({
2282 "version": "2.1.0",
2283 "$schema": "https://json.schemastore.org/sarif-2.1.0.json",
2284 "runs": [run],
2285 });
2286 serde_json::to_string_pretty(&sarif)
2287 .unwrap_or_else(|_| "{\"error\":\"failed to serialize sarif\"}".to_owned())
2288}
2289
2290fn fnv_hex(input: &str) -> String {
2292 let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
2293 for byte in input.bytes() {
2294 hash ^= u64::from(byte);
2295 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
2296 }
2297 format!("{hash:016x}")
2298}
2299
2300fn security_finding_id(finding: &SecurityFinding) -> String {
2306 let fp = format!(
2307 "{}:{}:{}",
2308 sarif_rule_id(finding),
2309 finding.path.to_string_lossy().replace('\\', "/"),
2310 finding.line,
2311 );
2312 fnv_hex(&fp)
2313}
2314
2315fn sarif_location(path: &Path, line: u32, col: u32) -> serde_json::Value {
2316 serde_json::json!({
2317 "physicalLocation": {
2318 "artifactLocation": { "uri": path.to_string_lossy().replace('\\', "/") },
2319 "region": { "startLine": line.max(1), "startColumn": col.saturating_add(1) }
2320 }
2321 })
2322}
2323
2324#[cfg(test)]
2325mod tests {
2326 use super::*;
2327 use fallow_core::results::{
2328 SecurityCandidate, SecurityCandidateBoundary, SecurityCandidateSink,
2329 SecurityDeadCodeContext, SecurityDeadCodeKind, SecurityFinding, SecurityFindingKind,
2330 TraceHop, TraceHopRole,
2331 };
2332 use fallow_types::results::{
2333 SecurityReachability, SecurityTaintFlow, TaintEndpoint, TaintPath,
2334 };
2335
2336 fn sample_finding(root: &Path) -> SecurityFinding {
2338 SecurityFinding {
2339 kind: SecurityFindingKind::ClientServerLeak,
2340 path: root.join("src/app.tsx"),
2341 line: 12,
2342 col: 3,
2343 evidence: "reaches process.env.SECRET_KEY".to_owned(),
2344 source_backed: false,
2345 source_read: None,
2346 severity: SecuritySeverity::High,
2347 trace: vec![
2348 TraceHop {
2349 path: root.join("src/app.tsx"),
2350 line: 12,
2351 col: 3,
2352 role: TraceHopRole::ClientBoundary,
2353 },
2354 TraceHop {
2355 path: root.join("src/lib/util.ts"),
2356 line: 4,
2357 col: 0,
2358 role: TraceHopRole::Intermediate,
2359 },
2360 TraceHop {
2361 path: root.join("src/lib/secret.ts"),
2362 line: 8,
2363 col: 2,
2364 role: TraceHopRole::SecretSource,
2365 },
2366 ],
2367 actions: vec![],
2368 category: None,
2369 cwe: None,
2370 dead_code: None,
2371 reachability: None,
2372 finding_id: String::new(),
2373 candidate: SecurityCandidate {
2374 source_kind: None,
2375 sink: SecurityCandidateSink {
2376 path: root.join("src/app.tsx"),
2377 line: 12,
2378 col: 3,
2379 category: None,
2380 cwe: None,
2381 callee: None,
2382 url_shape: None,
2383 },
2384 boundary: SecurityCandidateBoundary {
2385 client_server: true,
2386 cross_module: false,
2387 architecture_zone: None,
2388 },
2389 network: None,
2390 },
2391 taint_flow: None,
2392 runtime: None,
2393 attack_surface: None,
2394 }
2395 }
2396
2397 fn output_with(findings: Vec<SecurityFinding>, unresolved_edge_files: usize) -> SecurityOutput {
2398 SecurityOutput {
2399 schema_version: SecuritySchemaVersion::V6,
2400 version: ToolVersion("test".to_string()),
2401 elapsed_ms: ElapsedMs(0),
2402 config: test_output_config(),
2403 meta: None,
2404 gate: None,
2405 security_findings: findings,
2406 attack_surface: None,
2407 unresolved_edge_files,
2408 unresolved_callee_sites: 0,
2409 unresolved_callee_diagnostics: None,
2410 }
2411 }
2412
2413 fn output_with_gate(verdict: SecurityGateVerdict, new_count: usize) -> SecurityOutput {
2414 SecurityOutput {
2415 schema_version: SecuritySchemaVersion::V6,
2416 version: ToolVersion("test".to_string()),
2417 elapsed_ms: ElapsedMs(0),
2418 config: test_output_config(),
2419 meta: None,
2420 gate: Some(SecurityGate {
2421 mode: SecurityGateMode::New,
2422 verdict,
2423 new_count,
2424 }),
2425 security_findings: vec![],
2426 attack_surface: None,
2427 unresolved_edge_files: 0,
2428 unresolved_callee_sites: 0,
2429 unresolved_callee_diagnostics: None,
2430 }
2431 }
2432
2433 fn sample_unresolved_callee_diagnostics(root: &Path) -> SecurityUnresolvedCalleeDiagnostics {
2434 unresolved_callee_diagnostics(
2435 &[
2436 SecurityUnresolvedCalleeDiagnostic {
2437 path: root.join("src/z.ts"),
2438 line: 9,
2439 col: 4,
2440 reason: fallow_types::extract::SkippedSecurityCalleeReason::ComputedMember,
2441 expression_kind:
2442 fallow_types::extract::SkippedSecurityCalleeExpressionKind::ComputedMemberExpression,
2443 },
2444 SecurityUnresolvedCalleeDiagnostic {
2445 path: root.join("src/a.ts"),
2446 line: 3,
2447 col: 2,
2448 reason: fallow_types::extract::SkippedSecurityCalleeReason::DynamicDispatch,
2449 expression_kind: fallow_types::extract::SkippedSecurityCalleeExpressionKind::Other,
2450 },
2451 SecurityUnresolvedCalleeDiagnostic {
2452 path: root.join("src/a.ts"),
2453 line: 4,
2454 col: 2,
2455 reason: fallow_types::extract::SkippedSecurityCalleeReason::DynamicDispatch,
2456 expression_kind: fallow_types::extract::SkippedSecurityCalleeExpressionKind::Other,
2457 },
2458 ],
2459 root,
2460 )
2461 .expect("diagnostics summarized")
2462 }
2463
2464 fn test_output_config() -> SecurityOutputConfig {
2465 SecurityOutputConfig {
2466 rules: SecurityOutputRulesConfig {
2467 security_client_server_leak: SecurityRuleSeverityConfig {
2468 configured: Severity::Off,
2469 effective: Severity::Warn,
2470 },
2471 security_sink: SecurityRuleSeverityConfig {
2472 configured: Severity::Off,
2473 effective: Severity::Warn,
2474 },
2475 },
2476 categories_include: None,
2477 categories_exclude: None,
2478 }
2479 }
2480
2481 fn tainted_with_runtime(root: &Path, state: Option<SecurityRuntimeState>) -> SecurityFinding {
2482 let mut finding = sample_finding(root);
2483 finding.kind = SecurityFindingKind::TaintedSink;
2484 finding.category = Some("dangerous-html".to_owned());
2485 finding.cwe = Some(79);
2486 finding.runtime = state.map(|state| SecurityRuntimeContext {
2487 state,
2488 function: "render".to_owned(),
2489 line: 10,
2490 invocations: Some(123),
2491 stable_id: Some("fallow:fn:test".to_owned()),
2492 evidence: Some("production runtime evidence".to_owned()),
2493 });
2494 finding
2495 }
2496
2497 #[test]
2498 fn runtime_rank_promotes_hot_and_demotes_never_executed() {
2499 let root = Path::new("/proj/root");
2500 let mut findings = [
2501 tainted_with_runtime(root, Some(SecurityRuntimeState::NeverExecuted)),
2502 tainted_with_runtime(root, None),
2503 tainted_with_runtime(root, Some(SecurityRuntimeState::RuntimeHot)),
2504 tainted_with_runtime(root, Some(SecurityRuntimeState::CoverageUnavailable)),
2505 ];
2506
2507 findings.sort_by_key(runtime_rank);
2508
2509 assert_eq!(
2510 findings
2511 .iter()
2512 .map(|finding| finding.runtime.as_ref().map(|runtime| runtime.state))
2513 .collect::<Vec<_>>(),
2514 vec![
2515 Some(SecurityRuntimeState::RuntimeHot),
2516 None,
2517 Some(SecurityRuntimeState::CoverageUnavailable),
2518 Some(SecurityRuntimeState::NeverExecuted),
2519 ]
2520 );
2521 }
2522
2523 #[test]
2524 fn severity_sort_orders_tiers_then_location() {
2525 let root = Path::new("/proj/root");
2526 let mut high = sample_finding(root);
2527 high.path = root.join("z.ts");
2528 high.severity = SecuritySeverity::High;
2529 let mut low = sample_finding(root);
2530 low.path = root.join("a.ts");
2531 low.severity = SecuritySeverity::Low;
2532 let mut medium_a = sample_finding(root);
2533 medium_a.path = root.join("a.ts");
2534 medium_a.severity = SecuritySeverity::Medium;
2535 medium_a.reachability = Some(fallow_types::results::SecurityReachability {
2536 reachable_from_entry: false,
2537 reachable_from_untrusted_source: true,
2538 taint_confidence: Some(TaintConfidence::ModuleLevel),
2539 untrusted_source_hop_count: Some(1),
2540 untrusted_source_trace: vec![],
2541 blast_radius: 10,
2542 crosses_boundary: false,
2543 });
2544 let mut medium_b = sample_finding(root);
2545 medium_b.path = root.join("b.ts");
2546 medium_b.severity = SecuritySeverity::Medium;
2547 medium_b.source_backed = true;
2548 medium_b.reachability = Some(fallow_types::results::SecurityReachability {
2549 reachable_from_entry: false,
2550 reachable_from_untrusted_source: true,
2551 taint_confidence: Some(TaintConfidence::ArgLevel),
2552 untrusted_source_hop_count: Some(0),
2553 untrusted_source_trace: vec![],
2554 blast_radius: 1,
2555 crosses_boundary: false,
2556 });
2557 let mut findings = vec![low, medium_b, high, medium_a];
2558
2559 sort_by_security_severity(&mut findings);
2560
2561 assert_eq!(
2562 findings
2563 .iter()
2564 .map(|finding| (finding.severity, finding.path.file_name().unwrap()))
2565 .collect::<Vec<_>>(),
2566 vec![
2567 (SecuritySeverity::High, std::ffi::OsStr::new("z.ts")),
2568 (SecuritySeverity::Medium, std::ffi::OsStr::new("b.ts")),
2569 (SecuritySeverity::Medium, std::ffi::OsStr::new("a.ts")),
2570 (SecuritySeverity::Low, std::ffi::OsStr::new("a.ts")),
2571 ]
2572 );
2573 }
2574
2575 #[test]
2576 fn human_render_includes_runtime_context_line() {
2577 let root = Path::new("/proj/root");
2578 let finding = relativize_finding(
2579 tainted_with_runtime(root, Some(SecurityRuntimeState::RuntimeHot)),
2580 root,
2581 );
2582 let out = render_human(&output_with(vec![finding], 0));
2583
2584 assert!(
2585 out.contains("runtime: runtime-hot in render:10"),
2586 "got: {out}"
2587 );
2588 assert!(out.contains("production runtime evidence"), "got: {out}");
2589 }
2590
2591 #[test]
2592 fn sarif_render_includes_runtime_context_in_message() {
2593 let root = Path::new("/proj/root");
2594 let finding = relativize_finding(
2595 tainted_with_runtime(root, Some(SecurityRuntimeState::RuntimeHot)),
2596 root,
2597 );
2598 let rendered = render_sarif(&output_with(vec![finding], 0));
2599 let sarif: serde_json::Value = serde_json::from_str(&rendered).expect("valid SARIF JSON");
2600 let message = sarif["runs"][0]["results"][0]["message"]["text"]
2601 .as_str()
2602 .expect("message text");
2603
2604 assert!(message.contains("Runtime context"), "got: {message}");
2605 assert!(
2606 message.contains("runtime-hot in render:10"),
2607 "got: {message}"
2608 );
2609 }
2610
2611 #[test]
2612 fn gate_human_header_fail_says_review_required_not_fail() {
2613 let gate = SecurityGate {
2614 mode: SecurityGateMode::New,
2615 verdict: SecurityGateVerdict::Fail,
2616 new_count: 2,
2617 };
2618 let header = gate_human_header(&gate);
2619 assert!(header.contains("REVIEW REQUIRED"));
2620 assert!(header.contains("2 new security items"));
2621 assert!(header.contains("not confirmed a vulnerability"));
2622 assert!(!header.to_uppercase().contains("GATE: FAIL"));
2623 }
2624
2625 #[test]
2626 fn gate_human_header_fail_singular_for_one_candidate() {
2627 let gate = SecurityGate {
2629 mode: SecurityGateMode::New,
2630 verdict: SecurityGateVerdict::Fail,
2631 new_count: 1,
2632 };
2633 let header = gate_human_header(&gate);
2634 assert!(header.contains("1 new security item in changed lines"));
2635 assert!(!header.contains("1 new security candidates"));
2636 }
2637
2638 #[test]
2639 fn gate_human_header_pass() {
2640 let gate = SecurityGate {
2641 mode: SecurityGateMode::New,
2642 verdict: SecurityGateVerdict::Pass,
2643 new_count: 0,
2644 };
2645 assert!(gate_human_header(&gate).contains("Gate: PASS"));
2646 }
2647
2648 #[test]
2649 fn gate_json_block_is_snake_case_and_present_on_pass() {
2650 let json = render_json(&output_with_gate(SecurityGateVerdict::Pass, 0));
2651 assert!(json.contains("\"gate\""));
2652 assert!(json.contains("\"mode\": \"new\""));
2653 assert!(json.contains("\"verdict\": \"pass\""));
2654 assert!(json.contains("\"new_count\": 0"));
2655 }
2656
2657 #[test]
2658 fn reachability_key_includes_path_kind_and_category() {
2659 let root = Path::new("/proj/root");
2660 let mut leak = sample_finding(root);
2661 leak.reachability = Some(SecurityReachability {
2662 reachable_from_entry: true,
2663 reachable_from_untrusted_source: false,
2664 taint_confidence: None,
2665 untrusted_source_hop_count: None,
2666 untrusted_source_trace: vec![],
2667 blast_radius: 0,
2668 crosses_boundary: false,
2669 });
2670 let mut sink = leak.clone();
2671 sink.kind = SecurityFindingKind::TaintedSink;
2672 sink.category = Some("dangerous-html".to_owned());
2673
2674 assert_eq!(
2675 security_reachability_key(&leak, root).as_deref(),
2676 Some("security-reach:src/app.tsx:client-server-leak:none")
2677 );
2678 assert_eq!(
2679 security_reachability_key(&sink, root).as_deref(),
2680 Some("security-reach:src/app.tsx:tainted-sink:dangerous-html")
2681 );
2682 }
2683
2684 #[test]
2685 fn reachability_key_ignores_unreachable_findings() {
2686 let root = Path::new("/proj/root");
2687 let finding = sample_finding(root);
2688
2689 assert!(security_reachability_key(&finding, root).is_none());
2690 }
2691
2692 #[test]
2693 fn gate_absent_from_json_when_no_gate_ran() {
2694 let json = render_json(&output_with(vec![], 0));
2695 assert!(!json.contains("\"gate\""));
2696 }
2697
2698 #[test]
2699 fn gate_sarif_is_a_run_property_not_result_severity() {
2700 let sarif = render_sarif(&output_with_gate(SecurityGateVerdict::Fail, 1));
2701 assert!(sarif.contains("fallowGate"));
2702 assert!(!sarif.contains("\"level\": \"error\""));
2704 assert!(!sarif.contains("\"level\": \"warning\""));
2705 }
2706
2707 fn add_untrusted_source_reachability(finding: &mut SecurityFinding, root: &Path) {
2708 finding.reachability = Some(SecurityReachability {
2709 reachable_from_entry: true,
2710 reachable_from_untrusted_source: true,
2711 taint_confidence: Some(fallow_core::results::TaintConfidence::ModuleLevel),
2713 untrusted_source_hop_count: Some(1),
2714 untrusted_source_trace: vec![
2715 TraceHop {
2716 path: root.join("src/routes/api.ts"),
2717 line: 3,
2718 col: 0,
2719 role: TraceHopRole::ModuleSource,
2720 },
2721 TraceHop {
2722 path: root.join("src/lib/sink.ts"),
2723 line: 9,
2724 col: 2,
2725 role: TraceHopRole::Sink,
2726 },
2727 ],
2728 blast_radius: 2,
2729 crosses_boundary: false,
2730 });
2731 }
2732
2733 fn add_taint_flow(finding: &mut SecurityFinding, root: &Path) {
2734 finding.taint_flow = Some(SecurityTaintFlow {
2735 source: TaintEndpoint {
2736 path: root.join("src/routes/api.ts"),
2737 line: 3,
2738 col: 0,
2739 },
2740 sink: TaintEndpoint {
2741 path: root.join("src/lib/sink.ts"),
2742 line: 9,
2743 col: 2,
2744 },
2745 path: TaintPath {
2746 intra_module: false,
2747 cross_module_hops: 1,
2748 },
2749 });
2750 }
2751
2752 #[test]
2753 fn relativize_strips_root_prefix() {
2754 let root = Path::new("/proj/root");
2755 let abs = root.join("src/app.tsx");
2756 let rel = relativize(&abs, root);
2757 assert_eq!(rel.to_string_lossy().replace('\\', "/"), "src/app.tsx");
2758 }
2759
2760 #[test]
2761 fn relativize_keeps_path_when_outside_root() {
2762 let root = Path::new("/proj/root");
2763 let outside = Path::new("/elsewhere/file.ts");
2764 assert_eq!(relativize(outside, root), outside.to_path_buf());
2766 }
2767
2768 #[test]
2769 fn relativize_finding_relativizes_anchor_and_every_hop() {
2770 let root = Path::new("/proj/root");
2771 let finding = relativize_finding(sample_finding(root), root);
2772 assert_eq!(
2773 finding.path.to_string_lossy().replace('\\', "/"),
2774 "src/app.tsx"
2775 );
2776 let hop_paths: Vec<String> = finding
2777 .trace
2778 .iter()
2779 .map(|h| h.path.to_string_lossy().replace('\\', "/"))
2780 .collect();
2781 assert_eq!(
2782 hop_paths,
2783 vec!["src/app.tsx", "src/lib/util.ts", "src/lib/secret.ts"]
2784 );
2785 }
2786
2787 #[test]
2788 fn relativize_finding_relativizes_untrusted_source_trace() {
2789 let root = Path::new("/proj/root");
2790 let mut finding = sample_finding(root);
2791 add_untrusted_source_reachability(&mut finding, root);
2792 let finding = relativize_finding(finding, root);
2793 let reach = finding.reachability.as_ref().expect("reachability");
2794 let hop_paths: Vec<String> = reach
2795 .untrusted_source_trace
2796 .iter()
2797 .map(|h| h.path.to_string_lossy().replace('\\', "/"))
2798 .collect();
2799 assert_eq!(hop_paths, vec!["src/routes/api.ts", "src/lib/sink.ts"]);
2800 }
2801
2802 #[test]
2803 fn fnv_hex_is_deterministic_and_16_hex_digits() {
2804 let a = fnv_hex("security/client-server-leak:src/app.tsx:12");
2805 let b = fnv_hex("security/client-server-leak:src/app.tsx:12");
2806 assert_eq!(a, b, "same input must hash identically");
2807 assert_eq!(a.len(), 16);
2808 assert!(a.chars().all(|c| c.is_ascii_hexdigit()));
2809 assert_ne!(a, fnv_hex("security/client-server-leak:src/app.tsx:13"));
2811 }
2812
2813 #[test]
2814 fn hop_role_labels_cover_every_role() {
2815 assert_eq!(
2816 hop_role_label(TraceHopRole::ClientBoundary),
2817 "client boundary"
2818 );
2819 assert_eq!(
2820 hop_role_label(TraceHopRole::UntrustedSource),
2821 "untrusted source"
2822 );
2823 assert_eq!(hop_role_label(TraceHopRole::ModuleSource), "source module");
2824 assert_eq!(hop_role_label(TraceHopRole::Intermediate), "intermediate");
2825 assert_eq!(hop_role_label(TraceHopRole::SecretSource), "secret source");
2826 assert_eq!(hop_role_label(TraceHopRole::Sink), "sink site");
2827 }
2828
2829 #[test]
2830 fn sarif_location_clamps_line_and_offsets_column() {
2831 let loc = sarif_location(Path::new("a\\b.ts"), 0, 0);
2833 let region = &loc["physicalLocation"]["region"];
2834 assert_eq!(region["startLine"], 1);
2835 assert_eq!(region["startColumn"], 1);
2836 assert_eq!(loc["physicalLocation"]["artifactLocation"]["uri"], "a/b.ts");
2838 }
2839
2840 #[test]
2841 fn human_summary_reports_zero_without_edge_line() {
2842 let out = render_human_summary(&output_with(vec![], 0));
2843 assert!(
2844 out.contains("Security review: no items to check in the scanned code."),
2845 "got: {out}"
2846 );
2847 assert!(!out.contains("Blind spot"));
2848 }
2849
2850 #[test]
2851 fn human_summary_pluralizes_and_surfaces_unresolved_edges() {
2852 let root = Path::new("/proj/root");
2853 let out = render_human_summary(&output_with(vec![sample_finding(root)], 2));
2854 assert!(
2855 out.contains("Security review: 1 item to check."),
2856 "got: {out}"
2857 );
2858 assert!(out.contains("not confirmed vulnerabilities"));
2859 assert!(out.contains("unsafe input, secrets, or settings"));
2860 assert!(out.contains("Blind spot: 2 client files use dynamic imports"));
2861 }
2862
2863 #[test]
2864 fn human_render_empty_states_no_candidates() {
2865 colored::control::set_override(false);
2866 let out = render_human(&output_with(vec![], 0));
2867 assert!(out.contains("Security review: 0 items to check"));
2868 assert!(out.contains("No security details to show."));
2869 assert!(out.contains("Result: 0 security items to check."));
2870 }
2871
2872 #[test]
2873 fn human_render_shows_finding_trace_and_next_action() {
2874 colored::control::set_override(false);
2875 let root = Path::new("/proj/root");
2876 let finding = relativize_finding(sample_finding(root), root);
2877 let out = render_human(&output_with(vec![finding], 0));
2878 assert!(out.contains("[H] high client-server-leak"));
2879 assert!(out.contains("client-server-leak"));
2880 assert!(out.contains("src/app.tsx:12"));
2881 assert!(out.contains("evidence: reaches process.env.SECRET_KEY"));
2882 assert!(out.contains("import trace:"));
2883 assert!(out.contains("src/lib/secret.ts:8 (secret source)"));
2884 assert!(out.contains("src/app.tsx:12 (client boundary)"));
2885 assert!(out.contains("Next: check whether this import can ship a secret to the browser"));
2886 assert!(out.contains("Result: 1 security item to check."));
2887 }
2888
2889 #[test]
2890 fn human_render_shows_dead_code_hint_and_delete_next_step() {
2891 colored::control::set_override(false);
2892 let root = Path::new("/proj/root");
2893 let mut finding = relativize_finding(sample_finding(root), root);
2894 finding.kind = SecurityFindingKind::TaintedSink;
2895 finding.dead_code = Some(SecurityDeadCodeContext {
2896 kind: SecurityDeadCodeKind::UnusedFile,
2897 export_name: None,
2898 line: None,
2899 guidance: "delete instead of harden".to_string(),
2900 });
2901 let out = render_human(&output_with(vec![finding], 0));
2902 assert!(
2903 out.contains("dead-code: also reported as unused-file"),
2904 "got: {out}"
2905 );
2906 assert!(
2907 out.contains("If the code is safe to remove, delete it"),
2908 "got: {out}"
2909 );
2910 }
2911
2912 #[test]
2913 fn human_render_shows_untrusted_source_path_as_module_context() {
2914 colored::control::set_override(false);
2915 let root = Path::new("/proj/root");
2916 let mut finding = sample_finding(root);
2917 finding.kind = SecurityFindingKind::TaintedSink;
2918 finding.category = Some("command-injection".to_string());
2919 add_untrusted_source_reachability(&mut finding, root);
2920 let finding = relativize_finding(finding, root);
2921
2922 let out = render_human(&output_with(vec![finding], 0));
2923
2924 assert!(
2925 out.contains("reachable from a module that receives untrusted input via 1 import hop"),
2926 "got: {out}"
2927 );
2928 assert!(out.contains("input import trace:"), "got: {out}");
2929 assert!(
2930 out.contains("src/routes/api.ts:3 (source module)"),
2931 "got: {out}"
2932 );
2933 }
2934
2935 #[test]
2936 fn human_render_surfaces_unresolved_edge_blind_spot() {
2937 colored::control::set_override(false);
2938 let out = render_human(&output_with(vec![], 3));
2939 assert!(out.contains("Blind spot: 3 client files use dynamic imports"));
2940 assert!(out.contains("Code behind those imports may be missing from this report."));
2941 }
2942
2943 #[test]
2944 fn human_render_blind_spots_use_singular_verbs() {
2945 colored::control::set_override(false);
2946 let mut output = output_with(vec![], 1);
2947 output.unresolved_callee_sites = 1;
2948
2949 let out = render_human(&output);
2950
2951 assert!(out.contains("Blind spot: 1 client file uses dynamic imports"));
2952 assert!(out.contains("Blind spot: 1 call site uses code patterns"));
2953 }
2954
2955 #[test]
2956 fn human_render_mentions_top_unresolved_callee_reason_and_file() {
2957 colored::control::set_override(false);
2958 let root = Path::new("/proj/root");
2959 let mut output = output_with(vec![], 0);
2960 output.unresolved_callee_sites = 3;
2961 output.unresolved_callee_diagnostics = Some(sample_unresolved_callee_diagnostics(root));
2962
2963 let out = render_human(&output);
2964
2965 assert!(
2966 out.contains("Most unresolved callees: dynamic-dispatch in src/a.ts."),
2967 "got: {out}"
2968 );
2969 }
2970
2971 #[test]
2972 fn json_render_carries_schema_version_and_findings() {
2973 let root = Path::new("/proj/root");
2974 let finding = relativize_finding(sample_finding(root), root);
2975 let rendered = render_json(&output_with(vec![finding], 1));
2976 let value: serde_json::Value = serde_json::from_str(&rendered).expect("valid JSON");
2977 assert_eq!(value["schema_version"], "6");
2978 assert_eq!(value["version"], "test");
2979 assert_eq!(value["elapsed_ms"], 0);
2980 assert_eq!(
2981 value["config"]["rules"]["security_client_server_leak"]["configured"],
2982 "off"
2983 );
2984 assert_eq!(
2985 value["config"]["rules"]["security_client_server_leak"]["effective"],
2986 "warn"
2987 );
2988 assert!(value["config"]["categories_include"].is_null());
2989 assert!(value["config"]["categories_exclude"].is_null());
2990 assert_eq!(value["unresolved_edge_files"], 1);
2991 let findings = value["security_findings"].as_array().expect("array");
2992 assert_eq!(findings.len(), 1);
2993 assert_eq!(findings[0]["kind"], "client-server-leak");
2994 assert_eq!(findings[0]["path"], "src/app.tsx");
2995 assert_eq!(findings[0]["severity"], "high");
2996 }
2997
2998 #[test]
2999 fn json_render_carries_bounded_unresolved_callee_diagnostics() {
3000 let root = Path::new("/proj/root");
3001 let mut output = output_with(vec![], 0);
3002 output.unresolved_callee_sites = 3;
3003 output.unresolved_callee_diagnostics = Some(sample_unresolved_callee_diagnostics(root));
3004
3005 let rendered = render_json(&output);
3006 let value: serde_json::Value = serde_json::from_str(&rendered).expect("valid JSON");
3007 let diagnostics = &value["unresolved_callee_diagnostics"];
3008
3009 assert_eq!(diagnostics["sample_limit"], 25);
3010 assert_eq!(diagnostics["top_files_limit"], 10);
3011 assert_eq!(diagnostics["sampled"][0]["path"], "src/a.ts");
3012 assert_eq!(diagnostics["sampled"][0]["reason"], "dynamic-dispatch");
3013 assert_eq!(diagnostics["sampled"][0]["expression_kind"], "other");
3014 assert_eq!(diagnostics["top_files"][0]["path"], "src/a.ts");
3015 assert_eq!(diagnostics["top_files"][0]["count"], 2);
3016 assert_eq!(diagnostics["by_reason"][0]["reason"], "dynamic-dispatch");
3017 assert_eq!(diagnostics["by_reason"][0]["count"], 2);
3018 }
3019
3020 #[test]
3021 fn json_summary_omits_finding_arrays_and_counts_security_findings() {
3022 let root = Path::new("/proj/root");
3023 let mut leak = relativize_finding(sample_finding(root), root);
3024 leak.severity = SecuritySeverity::High;
3025
3026 let mut sink = relativize_finding(sample_finding(root), root);
3027 sink.kind = SecurityFindingKind::TaintedSink;
3028 sink.category = Some("dangerous-html".to_string());
3029 sink.severity = SecuritySeverity::Medium;
3030 sink.source_backed = true;
3031 sink.reachability = Some(SecurityReachability {
3032 reachable_from_entry: true,
3033 reachable_from_untrusted_source: true,
3034 taint_confidence: Some(TaintConfidence::ArgLevel),
3035 untrusted_source_hop_count: Some(0),
3036 untrusted_source_trace: vec![],
3037 blast_radius: 3,
3038 crosses_boundary: true,
3039 });
3040 sink.runtime = Some(SecurityRuntimeContext {
3041 state: SecurityRuntimeState::RuntimeHot,
3042 function: "render".to_owned(),
3043 line: 10,
3044 invocations: Some(120),
3045 stable_id: Some("src/app.tsx::render:10".to_owned()),
3046 evidence: Some("production hot path observed".to_owned()),
3047 });
3048
3049 let mut output = output_with(vec![leak, sink], 2);
3050 output.elapsed_ms = ElapsedMs(17);
3051 output.unresolved_callee_sites = 3;
3052
3053 let rendered = render_json_summary(&output);
3054 let value: serde_json::Value = serde_json::from_str(&rendered).expect("valid JSON");
3055
3056 assert_eq!(value["kind"], "security");
3057 assert_eq!(value["schema_version"], "6");
3058 assert_eq!(value["version"], "test");
3059 assert_eq!(value["elapsed_ms"], 17);
3060 assert!(value.get("config").is_some());
3061 assert!(value.get("security_findings").is_none());
3062 assert!(value.get("attack_surface").is_none());
3063 assert!(value.get("_meta").is_none());
3064 assert_eq!(value["summary"]["security_findings"], 2);
3065 assert_eq!(value["summary"]["by_severity"]["high"], 1);
3066 assert_eq!(value["summary"]["by_severity"]["medium"], 1);
3067 assert_eq!(value["summary"]["by_severity"]["low"], 0);
3068 assert_eq!(value["summary"]["by_category"]["client-server-leak"], 1);
3069 assert_eq!(value["summary"]["by_category"]["dangerous-html"], 1);
3070 assert_eq!(value["summary"]["by_reachability"]["entry_reachable"], 1);
3071 assert_eq!(
3072 value["summary"]["by_reachability"]["untrusted_source_reachable"],
3073 1
3074 );
3075 assert_eq!(value["summary"]["by_reachability"]["arg_level"], 1);
3076 assert_eq!(value["summary"]["by_reachability"]["module_level"], 0);
3077 assert_eq!(value["summary"]["by_reachability"]["crosses_boundary"], 1);
3078 assert_eq!(value["summary"]["by_reachability"]["source_backed"], 1);
3079 assert_eq!(value["summary"]["by_runtime_state"]["runtime_hot"], 1);
3080 assert_eq!(value["summary"]["by_runtime_state"]["runtime_cold"], 0);
3081 assert_eq!(value["summary"]["by_runtime_state"]["never_executed"], 0);
3082 assert_eq!(value["summary"]["by_runtime_state"]["low_traffic"], 0);
3083 assert_eq!(
3084 value["summary"]["by_runtime_state"]["coverage_unavailable"],
3085 0
3086 );
3087 assert_eq!(value["summary"]["by_runtime_state"]["runtime_unknown"], 0);
3088 assert_eq!(value["summary"]["by_runtime_state"]["not_collected"], 1);
3089 assert_eq!(value["summary"]["unresolved_edge_files"], 2);
3090 assert_eq!(value["summary"]["unresolved_callee_sites"], 3);
3091 assert_eq!(value["summary"]["attack_surface_entries"], 0);
3092 }
3093
3094 #[test]
3095 fn json_summary_carries_security_meta_when_explain_requested() {
3096 let root = Path::new("/proj/root");
3097 let mut output = output_with(vec![relativize_finding(sample_finding(root), root)], 0);
3098 output.meta = Some(crate::explain::security_meta());
3099
3100 let rendered = render_json_summary(&output);
3101 let value: serde_json::Value = serde_json::from_str(&rendered).expect("valid JSON");
3102
3103 assert!(value.get("security_findings").is_none());
3104 assert!(value["_meta"]["field_definitions"]["security_findings[]"].is_string());
3105 assert!(value["_meta"]["field_definitions"]["summary.by_reachability"].is_string());
3106 assert!(value["_meta"]["field_definitions"]["summary.by_runtime_state"].is_string());
3107 assert!(value["_meta"]["field_definitions"]["unresolved_callee_sites"].is_string());
3108 }
3109
3110 #[test]
3111 fn json_summary_preserves_gate_block() {
3112 let output = output_with_gate(SecurityGateVerdict::Fail, 1);
3113 let rendered = render_json_summary(&output);
3114 let value: serde_json::Value = serde_json::from_str(&rendered).expect("valid JSON");
3115
3116 assert_eq!(value["kind"], "security");
3117 assert_eq!(value["gate"]["mode"], "new");
3118 assert_eq!(value["gate"]["verdict"], "fail");
3119 assert_eq!(value["gate"]["new_count"], 1);
3120 assert_eq!(value["summary"]["security_findings"], 0);
3121 }
3122
3123 #[test]
3124 fn json_render_carries_security_meta_when_explain_requested() {
3125 let mut output = output_with(vec![], 0);
3126 output.meta = Some(crate::explain::security_meta());
3127
3128 let rendered = render_json(&output);
3129 let value: serde_json::Value = serde_json::from_str(&rendered).expect("valid JSON");
3130
3131 assert_eq!(
3132 value["_meta"]["field_definitions"]["security_findings[]"],
3133 "Unverified security candidates for downstream human or agent verification."
3134 );
3135 assert!(value["_meta"]["rules"]["security/tainted-sink"].is_object());
3136 }
3137
3138 #[test]
3139 fn json_render_carries_candidate_record_and_omits_impact() {
3140 let root = Path::new("/proj/root");
3144 let finding = relativize_finding(sample_finding(root), root);
3145 let rendered = render_json(&output_with(vec![finding], 0));
3146 let value: serde_json::Value = serde_json::from_str(&rendered).expect("valid JSON");
3147 let finding = &value["security_findings"][0];
3148
3149 let candidate = &finding["candidate"];
3150 assert!(candidate.is_object(), "candidate record present");
3151 assert!(candidate["sink"].is_object(), "sink slot present");
3152 assert_eq!(candidate["boundary"]["client_server"], true);
3153 assert!(
3154 candidate.get("impact").is_none(),
3155 "impact must NOT be a wire field"
3156 );
3157 assert!(
3158 candidate.get("source_kind").is_none(),
3159 "client-server-leak has no source kind"
3160 );
3161 assert!(
3162 finding.get("taint_flow").is_none(),
3163 "no untrusted-source flow on a client-server-leak"
3164 );
3165 assert!(
3166 finding.get("finding_id").is_some(),
3167 "finding_id is on the wire"
3168 );
3169 }
3170
3171 #[test]
3172 fn finding_id_is_stable_and_matches_sarif_fingerprint() {
3173 let root = Path::new("/proj/root");
3176 let finding = relativize_finding(sample_finding(root), root);
3177 let id = security_finding_id(&finding);
3178 assert!(!id.is_empty());
3179 assert_eq!(
3180 id,
3181 security_finding_id(&finding),
3182 "deterministic across calls"
3183 );
3184
3185 let sarif: serde_json::Value =
3186 serde_json::from_str(&render_sarif(&output_with(vec![finding], 0)))
3187 .expect("valid SARIF");
3188 assert_eq!(
3189 sarif["runs"][0]["results"][0]["partialFingerprints"]["fallowSecurity/v1"],
3190 serde_json::Value::String(id)
3191 );
3192 }
3193
3194 #[test]
3195 fn json_render_carries_dead_code_context() {
3196 let root = Path::new("/proj/root");
3197 let mut finding = relativize_finding(sample_finding(root), root);
3198 finding.kind = SecurityFindingKind::TaintedSink;
3199 finding.dead_code = Some(SecurityDeadCodeContext {
3200 kind: SecurityDeadCodeKind::UnusedExport,
3201 export_name: Some("handler".to_string()),
3202 line: Some(12),
3203 guidance: "remove export instead of harden".to_string(),
3204 });
3205 let rendered = render_json(&output_with(vec![finding], 0));
3206 let value: serde_json::Value = serde_json::from_str(&rendered).expect("valid JSON");
3207 let context = &value["security_findings"][0]["dead_code"];
3208 assert_eq!(context["kind"], "unused-export");
3209 assert_eq!(context["export_name"], "handler");
3210 assert_eq!(context["line"], 12);
3211 }
3212
3213 #[test]
3214 fn sarif_render_emits_warning_level_with_fingerprint_and_related_locations() {
3215 let root = Path::new("/proj/root");
3216 let finding = relativize_finding(sample_finding(root), root);
3217 let rendered = render_sarif(&output_with(vec![finding], 0));
3218 let sarif: serde_json::Value = serde_json::from_str(&rendered).expect("valid SARIF JSON");
3219 assert_eq!(sarif["version"], "2.1.0");
3220 let run = &sarif["runs"][0];
3221 assert_eq!(run["tool"]["driver"]["name"], "fallow");
3222 let result = &run["results"][0];
3223 assert_eq!(result["level"], "warning");
3225 assert_eq!(result["ruleId"], "security/client-server-leak");
3226 assert_eq!(result["message"]["text"], "reaches process.env.SECRET_KEY");
3227 assert_eq!(result["relatedLocations"].as_array().unwrap().len(), 3);
3229 let flow_locations = result["codeFlows"][0]["threadFlows"][0]["locations"]
3230 .as_array()
3231 .expect("thread flow locations");
3232 assert_eq!(flow_locations.len(), 3);
3233 assert_eq!(
3234 flow_locations[0]["location"]["physicalLocation"]["artifactLocation"]["uri"],
3235 "src/app.tsx"
3236 );
3237 assert_eq!(
3238 flow_locations[2]["location"]["physicalLocation"]["artifactLocation"]["uri"],
3239 "src/lib/secret.ts"
3240 );
3241 assert_eq!(
3242 flow_locations[2]["kinds"][0],
3243 serde_json::json!("secret-source")
3244 );
3245 assert!(result["partialFingerprints"]["fallowSecurity/v1"].is_string());
3247
3248 let rules = run["tool"]["driver"]["rules"].as_array().unwrap();
3249 assert_eq!(rules[0]["name"], "Client-server secret leak");
3250 assert!(rules[0]["help"]["text"].is_string());
3251 assert!(rules[0].get("relationships").is_none());
3252 assert!(run.get("taxonomies").is_none());
3253 }
3254
3255 #[test]
3256 fn sarif_render_keeps_low_severity_as_note() {
3257 let root = Path::new("/proj/root");
3258 let mut finding = sample_finding(root);
3259 finding.severity = SecuritySeverity::Low;
3260 let rendered = render_sarif(&output_with(vec![relativize_finding(finding, root)], 0));
3261 let sarif: serde_json::Value = serde_json::from_str(&rendered).expect("valid SARIF JSON");
3262
3263 assert_eq!(sarif["runs"][0]["results"][0]["level"], "note");
3264 }
3265
3266 #[test]
3267 fn sarif_render_includes_dead_code_hint_in_message() {
3268 let root = Path::new("/proj/root");
3269 let mut finding = relativize_finding(sample_finding(root), root);
3270 finding.kind = SecurityFindingKind::TaintedSink;
3271 finding.dead_code = Some(SecurityDeadCodeContext {
3272 kind: SecurityDeadCodeKind::UnusedFile,
3273 export_name: None,
3274 line: None,
3275 guidance: "delete instead of harden".to_string(),
3276 });
3277 let rendered = render_sarif(&output_with(vec![finding], 0));
3278 let sarif: serde_json::Value = serde_json::from_str(&rendered).expect("valid SARIF JSON");
3279 let message = sarif["runs"][0]["results"][0]["message"]["text"]
3280 .as_str()
3281 .expect("message text");
3282 assert!(message.contains("Dead-code cross-link"), "got: {message}");
3283 assert!(
3284 message.contains("delete this file instead of hardening"),
3285 "got: {message}"
3286 );
3287 }
3288
3289 #[test]
3290 fn sarif_render_includes_untrusted_source_context_and_related_locations() {
3291 let root = Path::new("/proj/root");
3292 let mut finding = sample_finding(root);
3293 finding.kind = SecurityFindingKind::TaintedSink;
3294 finding.category = Some("command-injection".to_string());
3295 add_untrusted_source_reachability(&mut finding, root);
3296 add_taint_flow(&mut finding, root);
3297 finding.trace.push(TraceHop {
3298 path: root.join("src/lib/sink.ts"),
3299 line: 9,
3300 col: 2,
3301 role: TraceHopRole::Sink,
3302 });
3303 let rendered = render_sarif(&output_with(vec![relativize_finding(finding, root)], 0));
3304 let sarif: serde_json::Value = serde_json::from_str(&rendered).expect("valid SARIF JSON");
3305 let result = &sarif["runs"][0]["results"][0];
3306 let message = result["message"]["text"].as_str().expect("message text");
3307 assert!(message.contains("Module-level context"), "got: {message}");
3308 assert!(
3309 message.contains("does not prove value flow"),
3310 "got: {message}"
3311 );
3312 assert_eq!(result["relatedLocations"].as_array().unwrap().len(), 5);
3314 let flow_locations = result["codeFlows"][0]["threadFlows"][0]["locations"]
3315 .as_array()
3316 .expect("thread flow locations");
3317 assert_eq!(flow_locations.len(), 2);
3318 assert_eq!(
3319 flow_locations[0]["location"]["physicalLocation"]["artifactLocation"]["uri"],
3320 "src/routes/api.ts"
3321 );
3322 assert_eq!(
3323 flow_locations[1]["location"]["physicalLocation"]["artifactLocation"]["uri"],
3324 "src/lib/sink.ts"
3325 );
3326 }
3327
3328 #[test]
3329 fn sarif_tainted_sink_uses_per_category_rule_id_and_cwe_metadata() {
3330 let root = Path::new("/proj/root");
3331 let mut finding = sample_finding(root);
3332 finding.kind = SecurityFindingKind::TaintedSink;
3333 finding.category = Some("dangerous-html".to_owned());
3334 finding.cwe = Some(79);
3335 let rendered = render_sarif(&output_with(vec![relativize_finding(finding, root)], 0));
3336 let sarif: serde_json::Value = serde_json::from_str(&rendered).expect("valid SARIF JSON");
3337 let run = &sarif["runs"][0];
3338 let result = &run["results"][0];
3341 assert_eq!(result["level"], "warning");
3342 assert_eq!(result["ruleId"], "security/dangerous-html");
3343 let rules = run["tool"]["driver"]["rules"].as_array().unwrap();
3345 assert_eq!(rules.len(), 1);
3346 assert_eq!(rules[0]["id"], "security/dangerous-html");
3347 assert_eq!(rules[0]["name"], "Dangerous HTML sink");
3348 assert!(
3349 rules[0]["help"]["text"]
3350 .as_str()
3351 .expect("help text")
3352 .contains("Verify this unverified")
3353 );
3354 assert!(
3355 rules[0]["help"]["markdown"]
3356 .as_str()
3357 .expect("help markdown")
3358 .contains("**Dangerous HTML sink**")
3359 );
3360 let tags = rules[0]["properties"]["tags"].as_array().unwrap();
3361 assert!(tags.iter().any(|t| t == "external/cwe/cwe-79"));
3362 let relationship = &rules[0]["relationships"][0];
3363 assert_eq!(relationship["target"]["id"], "CWE-79");
3364 assert_eq!(relationship["target"]["index"], 0);
3365 assert_eq!(relationship["target"]["toolComponent"]["name"], "CWE");
3366 assert_eq!(relationship["kinds"][0], "superset");
3367
3368 let taxonomy = &run["taxonomies"][0];
3369 assert_eq!(taxonomy["name"], "CWE");
3370 assert_eq!(taxonomy["taxa"][0]["id"], "CWE-79");
3371 assert_eq!(
3372 run["tool"]["driver"]["supportedTaxonomies"][0]["name"],
3373 "CWE"
3374 );
3375 }
3376
3377 #[test]
3378 fn write_sarif_file_creates_parent_dir_and_writes_valid_sarif() {
3379 let root = Path::new("/proj/root");
3380 let finding = relativize_finding(sample_finding(root), root);
3381 let output = output_with(vec![finding], 0);
3382 let dir = tempfile::tempdir().expect("tempdir");
3383 let path = dir.path().join("nested/out.sarif");
3384 write_sarif_file(&output, &path).expect("write succeeds and creates parent dir");
3385 let written = std::fs::read_to_string(&path).expect("file exists");
3386 let sarif: serde_json::Value = serde_json::from_str(&written).expect("valid SARIF JSON");
3387 assert_eq!(sarif["version"], "2.1.0");
3388 }
3389
3390 const NO_CONFIG: Option<PathBuf> = None;
3392
3393 fn leak_fixture_root() -> PathBuf {
3394 Path::new(env!("CARGO_MANIFEST_DIR"))
3395 .join("../../tests/fixtures/security-client-server-leak")
3396 }
3397
3398 fn source_reachability_fixture_root() -> PathBuf {
3399 Path::new(env!("CARGO_MANIFEST_DIR"))
3400 .join("../../tests/fixtures/security-source-reachability-885")
3401 }
3402
3403 fn run_opts(root: &Path, output: OutputFormat, fail_on_issues: bool) -> SecurityOptions<'_> {
3404 SecurityOptions {
3405 root,
3406 config_path: &NO_CONFIG,
3407 output,
3408 no_cache: true,
3409 threads: 1,
3410 quiet: true,
3411 fail_on_issues,
3412 sarif_file: None,
3413 summary: false,
3414 changed_since: None,
3415 use_shared_diff_index: false,
3416 workspace: None,
3417 changed_workspaces: None,
3418 file: &[],
3419 surface: false,
3420 gate: None,
3421 runtime_coverage: None,
3422 min_invocations_hot: 100,
3423 explain: false,
3424 }
3425 }
3426
3427 #[test]
3428 #[expect(
3429 deprecated,
3430 reason = "CLI fixture test uses the same workspace path dependency boundary as `run`"
3431 )]
3432 fn source_reachability_fixture_marks_cross_module_sink() {
3433 let root = source_reachability_fixture_root();
3434 let mut config = load_config_for_analysis(
3435 &root,
3436 &NO_CONFIG,
3437 OutputFormat::Json,
3438 true,
3439 1,
3440 None,
3441 true,
3442 ProductionAnalysis::DeadCode,
3443 )
3444 .expect("fixture config loads");
3445 config.rules.security_sink = Severity::Warn;
3446
3447 let results = fallow_core::analyze(&config).expect("fixture analyzes");
3448 let finding = results
3449 .security_findings
3450 .iter()
3451 .find(|finding| finding.path.ends_with("src/runner.ts"))
3452 .expect("runner sink finding");
3453 let reach = finding.reachability.as_ref().expect("reachability");
3454
3455 assert!(reach.reachable_from_untrusted_source);
3456 assert_eq!(reach.untrusted_source_hop_count, Some(1));
3457 assert_eq!(
3461 reach.taint_confidence,
3462 Some(fallow_core::results::TaintConfidence::ModuleLevel)
3463 );
3464 assert_eq!(
3465 reach
3466 .untrusted_source_trace
3467 .iter()
3468 .map(|hop| hop.role)
3469 .collect::<Vec<_>>(),
3470 vec![TraceHopRole::ModuleSource, TraceHopRole::Sink]
3471 );
3472 assert!(
3473 reach.untrusted_source_trace[0]
3474 .path
3475 .ends_with("src/route.ts")
3476 );
3477
3478 assert!(
3482 finding.candidate.boundary.cross_module,
3483 "a sink reached across a module hop crosses a module boundary"
3484 );
3485 let flow = finding.taint_flow.as_ref().expect("taint_flow present");
3486 assert!(!flow.path.intra_module);
3487 assert_eq!(flow.path.cross_module_hops, 1);
3488 assert!(flow.source.path.ends_with("src/route.ts"));
3489 assert!(flow.sink.path.ends_with("src/runner.ts"));
3490 }
3491
3492 #[test]
3493 fn file_scope_keeps_security_finding_when_anchor_matches() {
3494 let root = Path::new("/proj/root");
3495 let mut results = fallow_core::results::AnalysisResults::default();
3496 results.security_findings.push(sample_finding(root));
3497
3498 filter_to_files(&mut results, root, &[PathBuf::from("src/app.tsx")], true);
3499
3500 assert_eq!(results.security_findings.len(), 1);
3501 }
3502
3503 #[test]
3504 fn file_scope_keeps_security_finding_when_trace_hop_matches() {
3505 let root = Path::new("/proj/root");
3506 let mut results = fallow_core::results::AnalysisResults::default();
3507 results.security_findings.push(sample_finding(root));
3508
3509 filter_to_files(
3510 &mut results,
3511 root,
3512 &[PathBuf::from("src/lib/secret.ts")],
3513 true,
3514 );
3515
3516 assert_eq!(results.security_findings.len(), 1);
3517 }
3518
3519 #[test]
3520 fn file_scope_drops_unrelated_security_finding() {
3521 let root = Path::new("/proj/root");
3522 let mut results = fallow_core::results::AnalysisResults::default();
3523 results.security_findings.push(sample_finding(root));
3524
3525 filter_to_files(&mut results, root, &[PathBuf::from("src/other.ts")], true);
3526
3527 assert!(results.security_findings.is_empty());
3528 }
3529
3530 #[test]
3531 fn run_is_advisory_and_exits_zero_even_with_candidates() {
3532 let root = leak_fixture_root();
3535 let code = run(&run_opts(&root, OutputFormat::Json, false));
3536 assert_eq!(code, ExitCode::SUCCESS);
3537 }
3538
3539 #[test]
3540 fn run_with_fail_on_issues_exits_one_when_candidates_found() {
3541 let root = leak_fixture_root();
3543 let code = run(&run_opts(&root, OutputFormat::Human, true));
3544 assert_eq!(code, ExitCode::from(1));
3545 }
3546
3547 #[test]
3548 fn run_rejects_unsupported_output_format() {
3549 let root = leak_fixture_root();
3551 let code = run(&run_opts(&root, OutputFormat::Compact, false));
3552 assert_eq!(code, ExitCode::from(2));
3553 }
3554
3555 #[test]
3556 fn run_summary_mode_dispatches_compact_human_renderer() {
3557 let root = leak_fixture_root();
3558 let opts = SecurityOptions {
3559 summary: true,
3560 ..run_opts(&root, OutputFormat::Human, false)
3561 };
3562 assert_eq!(run(&opts), ExitCode::SUCCESS);
3563 }
3564
3565 #[test]
3566 fn run_sarif_format_dispatches_sarif_renderer() {
3567 let root = leak_fixture_root();
3568 assert_eq!(
3569 run(&run_opts(&root, OutputFormat::Sarif, false)),
3570 ExitCode::SUCCESS
3571 );
3572 }
3573
3574 #[test]
3575 fn run_writes_sarif_sidecar_file_when_requested() {
3576 let root = leak_fixture_root();
3577 let dir = tempfile::tempdir().expect("tempdir");
3578 let sidecar = dir.path().join("security.sarif");
3579 let opts = SecurityOptions {
3580 sarif_file: Some(&sidecar),
3581 ..run_opts(&root, OutputFormat::Human, false)
3582 };
3583 assert_eq!(run(&opts), ExitCode::SUCCESS);
3584 let written = std::fs::read_to_string(&sidecar).expect("sidecar SARIF written");
3585 let sarif: serde_json::Value = serde_json::from_str(&written).expect("valid SARIF JSON");
3586 assert_eq!(sarif["version"], "2.1.0");
3587 }
3588}