dart-decimate 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/Cargo.lock +572 -0
  2. package/Cargo.toml +41 -0
  3. package/LICENSE +21 -0
  4. package/README.md +617 -0
  5. package/assets/dart-decimate-banner.png +0 -0
  6. package/npm/bin/dart-decimate-mcp.js +48 -0
  7. package/npm/bin/dart-decimate.js +48 -0
  8. package/npm/scripts/postinstall.js +46 -0
  9. package/npm/scripts/test-npx-local.js +54 -0
  10. package/npm/scripts/test-npx-mcp-local.js +56 -0
  11. package/package.json +45 -0
  12. package/src/baseline.rs +643 -0
  13. package/src/bin/dart-decimate-mcp.rs +11 -0
  14. package/src/boundaries/tests.rs +60 -0
  15. package/src/boundaries.rs +317 -0
  16. package/src/changed_scope.rs +117 -0
  17. package/src/ci_template.rs +399 -0
  18. package/src/cli/analyze.rs +462 -0
  19. package/src/cli/analyzer_options.rs +82 -0
  20. package/src/cli/audit_run.rs +327 -0
  21. package/src/cli/boundary_args.rs +44 -0
  22. package/src/cli/ci_template_run.rs +196 -0
  23. package/src/cli/command_name.rs +20 -0
  24. package/src/cli/common_args.rs +167 -0
  25. package/src/cli/config_run.rs +78 -0
  26. package/src/cli/coverage_run.rs +393 -0
  27. package/src/cli/decision_surface_run.rs +99 -0
  28. package/src/cli/default_command.rs +69 -0
  29. package/src/cli/dupes_args.rs +150 -0
  30. package/src/cli/entry_points.rs +121 -0
  31. package/src/cli/error.rs +149 -0
  32. package/src/cli/error_output.rs +68 -0
  33. package/src/cli/explain_run.rs +72 -0
  34. package/src/cli/fix_run.rs +162 -0
  35. package/src/cli/flags_args.rs +23 -0
  36. package/src/cli/health_args.rs +200 -0
  37. package/src/cli/hooks_run.rs +191 -0
  38. package/src/cli/impact_run.rs +95 -0
  39. package/src/cli/init_run.rs +49 -0
  40. package/src/cli/inspect_args.rs +20 -0
  41. package/src/cli/inspect_run.rs +66 -0
  42. package/src/cli/issue_filter_args.rs +240 -0
  43. package/src/cli/list_run.rs +311 -0
  44. package/src/cli/mode_args.rs +72 -0
  45. package/src/cli/output_format.rs +62 -0
  46. package/src/cli/regression_args.rs +156 -0
  47. package/src/cli/request_args.rs +175 -0
  48. package/src/cli/schema_commands.rs +72 -0
  49. package/src/cli/scope_args.rs +174 -0
  50. package/src/cli/security_args.rs +175 -0
  51. package/src/cli/security_gate_run.rs +66 -0
  52. package/src/cli/security_summary_run.rs +43 -0
  53. package/src/cli/summary_args.rs +12 -0
  54. package/src/cli/tests.rs +516 -0
  55. package/src/cli/trace_args.rs +67 -0
  56. package/src/cli/trace_run.rs +131 -0
  57. package/src/cli/trace_tests.rs +235 -0
  58. package/src/cli/unsupported_run.rs +116 -0
  59. package/src/cli/watch_run.rs +153 -0
  60. package/src/cli.rs +603 -0
  61. package/src/config/boundary_config.rs +235 -0
  62. package/src/config/cache.rs +25 -0
  63. package/src/config/dependencies.rs +114 -0
  64. package/src/config/health.rs +237 -0
  65. package/src/config/jsonc.rs +66 -0
  66. package/src/config/policy.rs +91 -0
  67. package/src/config/rule_aliases.rs +487 -0
  68. package/src/config/rules.rs +339 -0
  69. package/src/config.rs +635 -0
  70. package/src/coverage.rs +469 -0
  71. package/src/dart_parser.rs +673 -0
  72. package/src/decision_surface.rs +405 -0
  73. package/src/dependencies/analyze.rs +218 -0
  74. package/src/dependencies/codegen.rs +118 -0
  75. package/src/dependencies/discovery.rs +60 -0
  76. package/src/dependencies/overrides.rs +76 -0
  77. package/src/dependencies/private_src_imports.rs +32 -0
  78. package/src/dependencies/pubspec_document.rs +86 -0
  79. package/src/dependencies/pubspec_entry.rs +96 -0
  80. package/src/dependencies/tests.rs +544 -0
  81. package/src/dependencies/usage.rs +33 -0
  82. package/src/dependencies.rs +553 -0
  83. package/src/dependency_scripts.rs +91 -0
  84. package/src/dupes/lex.rs +236 -0
  85. package/src/dupes/tests.rs +119 -0
  86. package/src/dupes.rs +625 -0
  87. package/src/explain.rs +692 -0
  88. package/src/extract/directives.rs +252 -0
  89. package/src/extract/error_tests.rs +25 -0
  90. package/src/extract/members.rs +210 -0
  91. package/src/extract/primary_constructors.rs +421 -0
  92. package/src/extract/references.rs +151 -0
  93. package/src/extract/routes.rs +691 -0
  94. package/src/extract/signatures.rs +266 -0
  95. package/src/extract/string_tests.rs +18 -0
  96. package/src/extract/strings.rs +107 -0
  97. package/src/extract/tests.rs +697 -0
  98. package/src/extract.rs +680 -0
  99. package/src/feature_flags/tests.rs +76 -0
  100. package/src/feature_flags.rs +453 -0
  101. package/src/finding_identity.rs +60 -0
  102. package/src/fix.rs +551 -0
  103. package/src/generated.rs +53 -0
  104. package/src/graph/augment_tests.rs +212 -0
  105. package/src/graph/conditionals.rs +74 -0
  106. package/src/graph/package_config_tests.rs +100 -0
  107. package/src/graph/parts.rs +248 -0
  108. package/src/graph/pubspec_tests.rs +119 -0
  109. package/src/graph/tests.rs +837 -0
  110. package/src/graph/uri_tests.rs +102 -0
  111. package/src/graph.rs +644 -0
  112. package/src/health/coverage.rs +309 -0
  113. package/src/health/ownership.rs +175 -0
  114. package/src/health/runtime_coverage.rs +616 -0
  115. package/src/health/runtime_intelligence.rs +511 -0
  116. package/src/health/scores.rs +296 -0
  117. package/src/health/tests.rs +240 -0
  118. package/src/health/threshold_types.rs +163 -0
  119. package/src/health/thresholds.rs +229 -0
  120. package/src/health/types.rs +636 -0
  121. package/src/health.rs +597 -0
  122. package/src/hooks/agent_settings.rs +175 -0
  123. package/src/hooks/templates.rs +51 -0
  124. package/src/hooks.rs +569 -0
  125. package/src/impact.rs +287 -0
  126. package/src/init.rs +237 -0
  127. package/src/inspect.rs +142 -0
  128. package/src/intelligence/tests.rs +232 -0
  129. package/src/intelligence.rs +305 -0
  130. package/src/lib.rs +198 -0
  131. package/src/main.rs +5 -0
  132. package/src/manifest/mcp.rs +565 -0
  133. package/src/manifest.rs +552 -0
  134. package/src/mcp/cli_args/value_args.rs +175 -0
  135. package/src/mcp/cli_args.rs +658 -0
  136. package/src/mcp/cli_args_tests.rs +522 -0
  137. package/src/mcp/code_execute.rs +671 -0
  138. package/src/mcp/tools.rs +660 -0
  139. package/src/mcp.rs +325 -0
  140. package/src/output/audit_risk.rs +135 -0
  141. package/src/output/dependency_findings.rs +327 -0
  142. package/src/output/duplication_findings.rs +114 -0
  143. package/src/output/feature_flag_findings.rs +122 -0
  144. package/src/output/format.rs +31 -0
  145. package/src/output/graph_findings.rs +461 -0
  146. package/src/output/health_findings.rs +612 -0
  147. package/src/output/human.rs +27 -0
  148. package/src/output/next_steps.rs +92 -0
  149. package/src/output/route_findings.rs +76 -0
  150. package/src/output/runtime_coverage.rs +515 -0
  151. package/src/output/scope.rs +183 -0
  152. package/src/output/security_findings.rs +390 -0
  153. package/src/output/security_sarif.rs +150 -0
  154. package/src/output/suppressions.rs +323 -0
  155. package/src/output/symbol_findings.rs +257 -0
  156. package/src/output/tests.rs +114 -0
  157. package/src/output/types.rs +604 -0
  158. package/src/output/widget_findings.rs +213 -0
  159. package/src/output.rs +682 -0
  160. package/src/package_map.rs +495 -0
  161. package/src/policy/packs.rs +232 -0
  162. package/src/policy/tests.rs +134 -0
  163. package/src/policy.rs +640 -0
  164. package/src/project_list.rs +376 -0
  165. package/src/report_schema/inventory.rs +418 -0
  166. package/src/report_schema.rs +488 -0
  167. package/src/routes.rs +354 -0
  168. package/src/scan/tests.rs +250 -0
  169. package/src/scan.rs +275 -0
  170. package/src/security/detect.rs +639 -0
  171. package/src/security/tests.rs +178 -0
  172. package/src/security.rs +373 -0
  173. package/src/security_gate.rs +555 -0
  174. package/src/symbols/path_filters.rs +27 -0
  175. package/src/symbols/private_type_leaks.rs +125 -0
  176. package/src/symbols/riverpod.rs +113 -0
  177. package/src/symbols/tests.rs +692 -0
  178. package/src/symbols.rs +693 -0
  179. package/src/trace/tests.rs +225 -0
  180. package/src/trace.rs +690 -0
  181. package/src/unsupported.rs +47 -0
  182. package/src/widgets/lifecycle.rs +484 -0
  183. package/src/widgets/params.rs +236 -0
  184. package/src/widgets/tests.rs +430 -0
  185. package/src/widgets/top_level.rs +110 -0
  186. package/src/widgets/unrendered.rs +374 -0
  187. package/src/widgets.rs +590 -0
  188. package/src/workspace_scope.rs +358 -0
@@ -0,0 +1,183 @@
1
+ use std::collections::BTreeSet;
2
+
3
+ use super::format;
4
+ use super::{
5
+ Finding, JsonAttackSurfaceEntry, JsonCloneGroup, JsonComplexityFinding, JsonFeatureFlag,
6
+ JsonFileHealthScore, JsonHealthHotspot, JsonRefactoringTarget, JsonSecurityCandidate,
7
+ };
8
+ use crate::{HealthReport, scan::ScannedProject};
9
+
10
+ pub(super) fn file_scope(
11
+ project: &ScannedProject,
12
+ paths: Option<&Vec<std::path::PathBuf>>,
13
+ ) -> Option<BTreeSet<String>> {
14
+ paths.map(|paths| {
15
+ paths
16
+ .iter()
17
+ .map(|path| format::display_path(&project.root, path))
18
+ .collect()
19
+ })
20
+ }
21
+
22
+ pub(super) fn finding_in_scope(finding: &Finding, scope: Option<&BTreeSet<String>>) -> bool {
23
+ scope.is_none_or(|scope| {
24
+ scope.contains(&finding.path)
25
+ || finding.files.iter().any(|file| scope.contains(file))
26
+ || finding
27
+ .edge
28
+ .as_ref()
29
+ .is_some_and(|edge| scope.contains(&edge.from) || scope.contains(&edge.to))
30
+ })
31
+ }
32
+
33
+ pub(super) fn scope_clone_groups(
34
+ groups: Vec<JsonCloneGroup>,
35
+ scope: Option<&BTreeSet<String>>,
36
+ ) -> Vec<JsonCloneGroup> {
37
+ groups
38
+ .into_iter()
39
+ .filter_map(|mut group| {
40
+ if let Some(scope) = scope {
41
+ group
42
+ .instances
43
+ .retain(|instance| scope.contains(&instance.path));
44
+ }
45
+ (!group.instances.is_empty()).then_some(group)
46
+ })
47
+ .collect()
48
+ }
49
+
50
+ pub(super) fn scope_complexity(
51
+ findings: Vec<JsonComplexityFinding>,
52
+ scope: Option<&BTreeSet<String>>,
53
+ ) -> Vec<JsonComplexityFinding> {
54
+ findings
55
+ .into_iter()
56
+ .filter(|finding| scope.is_none_or(|scope| scope.contains(&finding.path)))
57
+ .collect()
58
+ }
59
+
60
+ pub(super) fn scope_file_scores(
61
+ scores: Vec<JsonFileHealthScore>,
62
+ scope: Option<&BTreeSet<String>>,
63
+ ) -> Vec<JsonFileHealthScore> {
64
+ scores
65
+ .into_iter()
66
+ .filter(|score| scope.is_none_or(|scope| scope.contains(&score.path)))
67
+ .collect()
68
+ }
69
+
70
+ pub(super) fn scope_hotspots(
71
+ hotspots: Vec<JsonHealthHotspot>,
72
+ scope: Option<&BTreeSet<String>>,
73
+ ) -> Vec<JsonHealthHotspot> {
74
+ hotspots
75
+ .into_iter()
76
+ .filter(|hotspot| scope.is_none_or(|scope| scope.contains(&hotspot.path)))
77
+ .collect()
78
+ }
79
+
80
+ pub(super) fn scope_refactoring_targets(
81
+ targets: Vec<JsonRefactoringTarget>,
82
+ scope: Option<&BTreeSet<String>>,
83
+ ) -> Vec<JsonRefactoringTarget> {
84
+ targets
85
+ .into_iter()
86
+ .filter(|target| scope.is_none_or(|scope| scope.contains(&target.path)))
87
+ .collect()
88
+ }
89
+
90
+ pub(super) fn scope_feature_flags(
91
+ flags: Vec<JsonFeatureFlag>,
92
+ scope: Option<&BTreeSet<String>>,
93
+ ) -> Vec<JsonFeatureFlag> {
94
+ flags
95
+ .into_iter()
96
+ .filter_map(|mut flag| {
97
+ if let Some(scope) = scope {
98
+ flag.occurrences
99
+ .retain(|occurrence| scope.contains(&occurrence.path));
100
+ }
101
+ (!flag.occurrences.is_empty()).then_some(flag)
102
+ })
103
+ .collect()
104
+ }
105
+
106
+ pub(super) fn scope_security_candidates(
107
+ candidates: Vec<JsonSecurityCandidate>,
108
+ scope: Option<&BTreeSet<String>>,
109
+ ) -> Vec<JsonSecurityCandidate> {
110
+ candidates
111
+ .into_iter()
112
+ .filter_map(|mut candidate| {
113
+ if let Some(scope) = scope {
114
+ candidate
115
+ .occurrences
116
+ .retain(|occurrence| scope.contains(&occurrence.path));
117
+ }
118
+ (!candidate.occurrences.is_empty()).then_some(candidate)
119
+ })
120
+ .collect()
121
+ }
122
+
123
+ pub(super) fn scope_attack_surface(
124
+ entries: Vec<JsonAttackSurfaceEntry>,
125
+ scope: Option<&BTreeSet<String>>,
126
+ ) -> Vec<JsonAttackSurfaceEntry> {
127
+ entries
128
+ .into_iter()
129
+ .filter(|entry| scope.is_none_or(|scope| scope.contains(&entry.path)))
130
+ .collect()
131
+ }
132
+
133
+ pub(super) fn project_file_scope_count(
134
+ project: &ScannedProject,
135
+ scope: &BTreeSet<String>,
136
+ ) -> usize {
137
+ project
138
+ .files
139
+ .iter()
140
+ .filter(|file| scope.contains(&format::display_path(&project.root, &file.path)))
141
+ .count()
142
+ }
143
+
144
+ pub(super) fn health_file_score_count(
145
+ project: &ScannedProject,
146
+ scores: &[crate::FileHealthScore],
147
+ scope: Option<&BTreeSet<String>>,
148
+ ) -> usize {
149
+ scores
150
+ .iter()
151
+ .filter(|score| {
152
+ scope.is_none_or(|scope| {
153
+ scope.contains(&format::display_path(&project.root, &score.path))
154
+ })
155
+ })
156
+ .count()
157
+ }
158
+
159
+ pub(super) fn scoped_quality_score(
160
+ project: &ScannedProject,
161
+ report: &HealthReport,
162
+ scope: Option<&BTreeSet<String>>,
163
+ ) -> usize {
164
+ let Some(scope) = scope else {
165
+ return report.quality_score;
166
+ };
167
+ if report.file_scores.is_empty() {
168
+ return report.quality_score;
169
+ }
170
+ let mut total = 0usize;
171
+ let mut weight = 0usize;
172
+ for score in &report.file_scores {
173
+ if scope.contains(&format::display_path(&project.root, &score.path)) {
174
+ let file_weight = score.functions.max(1);
175
+ total = total.saturating_add(score.score.saturating_mul(file_weight));
176
+ weight = weight.saturating_add(file_weight);
177
+ }
178
+ }
179
+ if weight == 0 {
180
+ return 0;
181
+ }
182
+ total.saturating_add(weight / 2) / weight
183
+ }
@@ -0,0 +1,390 @@
1
+ use std::collections::BTreeSet;
2
+ use std::path::Path;
3
+
4
+ use serde::ser::{SerializeStruct, Serializer};
5
+ use serde::{Deserialize, Serialize};
6
+
7
+ use super::format::display_path;
8
+ use super::{Finding, FindingAction, FindingKind, Severity};
9
+ use crate::{
10
+ AttackSurfaceEntry, SecurityCandidate, SecurityCategory, SecurityConfidence, SecurityReport,
11
+ SecurityTaintConfidence,
12
+ };
13
+
14
+ /// Security candidate serialized in JSON reports.
15
+ #[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
16
+ pub struct JsonSecurityCandidate {
17
+ /// Stable rule id.
18
+ pub rule_id: String,
19
+ /// Stable finding id, equal to the SARIF/Dart Decimate fingerprint.
20
+ pub finding_id: String,
21
+ /// Stable candidate fingerprint.
22
+ pub fingerprint: String,
23
+ /// Candidate category.
24
+ pub category: SecurityCategory,
25
+ /// CWE ids associated with this candidate category.
26
+ pub cwe: Vec<String>,
27
+ /// Default review severity before config rule-level overrides.
28
+ pub severity: Severity,
29
+ /// Agent-actionable source/sink/boundary summary.
30
+ pub candidate: JsonSecurityCandidateDetails,
31
+ /// API surface or sink family.
32
+ pub sink: String,
33
+ /// Detection confidence.
34
+ pub confidence: SecurityConfidence,
35
+ /// Candidate occurrences.
36
+ pub occurrences: Vec<JsonSecurityOccurrence>,
37
+ /// Optional module-level graph reachability context.
38
+ pub reachability: Option<JsonSecurityReachability>,
39
+ }
40
+
41
+ impl Serialize for JsonSecurityCandidate {
42
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
43
+ where
44
+ S: Serializer,
45
+ {
46
+ let mut state = serializer.serialize_struct("JsonSecurityCandidate", 13)?;
47
+ state.serialize_field("rule_id", &self.rule_id)?;
48
+ state.serialize_field("finding_id", &self.finding_id)?;
49
+ state.serialize_field("fingerprint", &self.fingerprint)?;
50
+ state.serialize_field("category", &self.category)?;
51
+ state.serialize_field("cwe", &self.cwe)?;
52
+ state.serialize_field("severity", &self.severity)?;
53
+ state.serialize_field("candidate", &self.candidate)?;
54
+ state.serialize_field("sink", &self.sink)?;
55
+ state.serialize_field("confidence", &self.confidence)?;
56
+ state.serialize_field("occurrences", &self.occurrences)?;
57
+ if let Some(reachability) = &self.reachability {
58
+ state.serialize_field("reachability", reachability)?;
59
+ }
60
+ state.serialize_field(
61
+ "evidence",
62
+ &JsonSecurityEvidence {
63
+ occurrences: &self.occurrences,
64
+ },
65
+ )?;
66
+ state.serialize_field("trace", &trace_steps(self.category, &self.occurrences))?;
67
+ state.end()
68
+ }
69
+ }
70
+
71
+ /// Agent-actionable candidate context serialized in JSON reports.
72
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
73
+ pub struct JsonSecurityCandidateDetails {
74
+ /// Evidence source family.
75
+ pub source: String,
76
+ /// Risky sink family.
77
+ pub sink: String,
78
+ /// Trust or platform boundary involved.
79
+ pub boundary: String,
80
+ }
81
+
82
+ /// One security candidate occurrence serialized in JSON reports.
83
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
84
+ pub struct JsonSecurityOccurrence {
85
+ /// Dart file path, root-relative where possible.
86
+ pub path: String,
87
+ /// 1-based line.
88
+ pub line: usize,
89
+ /// 0-based byte column.
90
+ pub column: usize,
91
+ /// Matched expression or API surface.
92
+ pub expression: String,
93
+ /// Redacted source-line evidence.
94
+ pub evidence: String,
95
+ /// Optional module-level graph reachability context.
96
+ #[serde(skip_serializing_if = "Option::is_none")]
97
+ pub reachability: Option<JsonSecurityReachability>,
98
+ }
99
+
100
+ /// Module-level security reachability context serialized in JSON reports.
101
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
102
+ pub struct JsonSecurityReachability {
103
+ /// Whether this module is reachable from a configured entry point.
104
+ pub reachable_from_entrypoint: bool,
105
+ /// Confidence tier for the reachability evidence.
106
+ pub taint_confidence: SecurityTaintConfidence,
107
+ /// Root-relative entry points that seeded graph traversal.
108
+ pub entry_points: Vec<String>,
109
+ /// Candidate occurrence count covered by this reachability context.
110
+ pub reachable_occurrences: usize,
111
+ }
112
+
113
+ #[derive(Serialize)]
114
+ struct JsonSecurityEvidence<'a> {
115
+ occurrences: &'a [JsonSecurityOccurrence],
116
+ }
117
+
118
+ #[derive(Serialize)]
119
+ struct JsonSecurityTraceStep<'a> {
120
+ role: &'static str,
121
+ path: &'a str,
122
+ line: usize,
123
+ column: usize,
124
+ expression: &'a str,
125
+ evidence: &'a str,
126
+ }
127
+
128
+ /// Attack-surface entry serialized in JSON reports.
129
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
130
+ pub struct JsonAttackSurfaceEntry {
131
+ /// Candidate category exposed on this surface.
132
+ pub category: SecurityCategory,
133
+ /// Dart file path, root-relative where possible.
134
+ pub path: String,
135
+ /// 1-based line.
136
+ pub line: usize,
137
+ /// 0-based byte column.
138
+ pub column: usize,
139
+ /// API surface or boundary.
140
+ pub surface: String,
141
+ /// Verification prompt for downstream agents.
142
+ pub verification_prompt: String,
143
+ }
144
+
145
+ pub(super) fn add_security_findings(
146
+ root: &Path,
147
+ report: &SecurityReport,
148
+ findings: &mut Vec<Finding>,
149
+ ) {
150
+ findings.extend(
151
+ report
152
+ .candidates
153
+ .iter()
154
+ .map(|candidate| security_finding(root, candidate)),
155
+ );
156
+ }
157
+
158
+ pub(super) fn json_security_candidates(
159
+ root: &Path,
160
+ report: &SecurityReport,
161
+ ) -> Vec<JsonSecurityCandidate> {
162
+ report
163
+ .candidates
164
+ .iter()
165
+ .map(|candidate| {
166
+ let fingerprint = security_fingerprint(candidate);
167
+ let occurrences = candidate
168
+ .occurrences
169
+ .iter()
170
+ .map(|occurrence| JsonSecurityOccurrence {
171
+ path: display_path(root, &occurrence.path),
172
+ line: occurrence.location.line,
173
+ column: occurrence.location.column,
174
+ expression: occurrence.expression.clone(),
175
+ evidence: occurrence.evidence.clone(),
176
+ reachability: occurrence.reachability.as_ref().map(|reachability| {
177
+ JsonSecurityReachability {
178
+ reachable_from_entrypoint: reachability.reachable_from_entrypoint,
179
+ taint_confidence: reachability.taint_confidence,
180
+ entry_points: reachability
181
+ .entry_points
182
+ .iter()
183
+ .map(|entry| display_path(root, entry))
184
+ .collect(),
185
+ reachable_occurrences: 1,
186
+ }
187
+ }),
188
+ })
189
+ .collect::<Vec<_>>();
190
+ JsonSecurityCandidate {
191
+ rule_id: candidate.rule_id.clone(),
192
+ finding_id: fingerprint.clone(),
193
+ fingerprint,
194
+ category: candidate.category,
195
+ cwe: cwe_ids(candidate.category)
196
+ .iter()
197
+ .map(|cwe| (*cwe).to_owned())
198
+ .collect(),
199
+ severity: Severity::Error,
200
+ candidate: JsonSecurityCandidateDetails {
201
+ source: source_label(candidate.category).to_owned(),
202
+ sink: candidate.sink.clone(),
203
+ boundary: boundary_label(candidate.category).to_owned(),
204
+ },
205
+ sink: candidate.sink.clone(),
206
+ confidence: candidate.confidence,
207
+ reachability: candidate_reachability(&occurrences),
208
+ occurrences,
209
+ }
210
+ })
211
+ .collect()
212
+ }
213
+
214
+ fn candidate_reachability(
215
+ occurrences: &[JsonSecurityOccurrence],
216
+ ) -> Option<JsonSecurityReachability> {
217
+ let reachable = occurrences
218
+ .iter()
219
+ .filter_map(|occurrence| occurrence.reachability.as_ref())
220
+ .collect::<Vec<_>>();
221
+ let first = reachable.first()?;
222
+ let entry_points = reachable
223
+ .iter()
224
+ .flat_map(|reachability| reachability.entry_points.iter())
225
+ .cloned()
226
+ .collect::<BTreeSet<_>>()
227
+ .into_iter()
228
+ .collect::<Vec<_>>();
229
+
230
+ Some(JsonSecurityReachability {
231
+ reachable_from_entrypoint: true,
232
+ taint_confidence: first.taint_confidence,
233
+ entry_points,
234
+ reachable_occurrences: reachable.len(),
235
+ })
236
+ }
237
+
238
+ pub(super) fn json_attack_surface(
239
+ root: &Path,
240
+ report: &SecurityReport,
241
+ ) -> Vec<JsonAttackSurfaceEntry> {
242
+ report
243
+ .attack_surface
244
+ .iter()
245
+ .map(|entry| attack_surface_entry(root, entry))
246
+ .collect()
247
+ }
248
+
249
+ fn security_finding(root: &Path, candidate: &SecurityCandidate) -> Finding {
250
+ let first = &candidate.occurrences[0];
251
+ let path = display_path(root, &first.path);
252
+ let files = candidate
253
+ .occurrences
254
+ .iter()
255
+ .map(|occurrence| display_path(root, &occurrence.path))
256
+ .collect::<BTreeSet<_>>()
257
+ .into_iter()
258
+ .collect::<Vec<_>>();
259
+
260
+ Finding {
261
+ rule_id: candidate.rule_id.clone(),
262
+ fingerprint: Some(security_fingerprint(candidate)),
263
+ kind: FindingKind::SecurityCandidate,
264
+ severity: Severity::Error,
265
+ message: format!(
266
+ "Security review candidate for {} via {}",
267
+ category_name(candidate.category),
268
+ candidate.sink
269
+ ),
270
+ path: path.clone(),
271
+ line: first.location.line,
272
+ column: first.location.column,
273
+ safe_to_delete: false,
274
+ files,
275
+ edge: None,
276
+ actions: vec![
277
+ FindingAction::new(
278
+ "review-security-candidate",
279
+ "Verify source control, reachability, and defensive controls before editing",
280
+ false,
281
+ )
282
+ .with_target_path(path.clone())
283
+ .with_target_symbol(candidate.sink.clone())
284
+ .with_dart_decimate_args(["inspect", "--format", "json", "--file", path.as_str()])
285
+ .with_suppression_comment("// dart-decimate-ignore-next-line security-sink"),
286
+ ],
287
+ }
288
+ }
289
+
290
+ fn attack_surface_entry(root: &Path, entry: &AttackSurfaceEntry) -> JsonAttackSurfaceEntry {
291
+ JsonAttackSurfaceEntry {
292
+ category: entry.category,
293
+ path: display_path(root, &entry.path),
294
+ line: entry.location.line,
295
+ column: entry.location.column,
296
+ surface: entry.surface.clone(),
297
+ verification_prompt: entry.verification_prompt.clone(),
298
+ }
299
+ }
300
+
301
+ fn security_fingerprint(candidate: &SecurityCandidate) -> String {
302
+ let text = format!(
303
+ "{}:{}:{}",
304
+ candidate.rule_id,
305
+ category_name(candidate.category),
306
+ candidate.sink
307
+ );
308
+ let mut hash = 0xcbf2_9ce4_8422_2325_u64;
309
+ for byte in text.as_bytes() {
310
+ hash ^= u64::from(*byte);
311
+ hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
312
+ }
313
+ format!("sec:{:08x}", hash & 0xffff_ffff)
314
+ }
315
+
316
+ fn trace_steps(
317
+ category: SecurityCategory,
318
+ occurrences: &[JsonSecurityOccurrence],
319
+ ) -> Vec<JsonSecurityTraceStep<'_>> {
320
+ occurrences
321
+ .iter()
322
+ .map(|occurrence| JsonSecurityTraceStep {
323
+ role: trace_role(category),
324
+ path: &occurrence.path,
325
+ line: occurrence.line,
326
+ column: occurrence.column,
327
+ expression: &occurrence.expression,
328
+ evidence: &occurrence.evidence,
329
+ })
330
+ .collect()
331
+ }
332
+
333
+ const fn cwe_ids(category: SecurityCategory) -> &'static [&'static str] {
334
+ match category {
335
+ SecurityCategory::HardcodedSecret => &["CWE-798"],
336
+ SecurityCategory::InsecureTransport => &["CWE-319"],
337
+ SecurityCategory::TlsBypass => &["CWE-295"],
338
+ SecurityCategory::WebViewRisk => &["CWE-749"],
339
+ SecurityCategory::ProcessExecution => &["CWE-78"],
340
+ SecurityCategory::RawSql => &["CWE-89"],
341
+ SecurityCategory::PlainSecretStorage => &["CWE-922"],
342
+ }
343
+ }
344
+
345
+ const fn source_label(category: SecurityCategory) -> &'static str {
346
+ match category {
347
+ SecurityCategory::HardcodedSecret => "source-code-literal",
348
+ SecurityCategory::InsecureTransport => "http-url-literal",
349
+ SecurityCategory::TlsBypass => "tls-callback-or-context",
350
+ SecurityCategory::WebViewRisk => "webview-configuration",
351
+ SecurityCategory::ProcessExecution => "process-call-arguments",
352
+ SecurityCategory::RawSql => "query-text",
353
+ SecurityCategory::PlainSecretStorage => "secret-named-value",
354
+ }
355
+ }
356
+
357
+ const fn boundary_label(category: SecurityCategory) -> &'static str {
358
+ match category {
359
+ SecurityCategory::HardcodedSecret => "source-control",
360
+ SecurityCategory::InsecureTransport => "network-transport",
361
+ SecurityCategory::TlsBypass => "tls-validation",
362
+ SecurityCategory::WebViewRisk => "embedded-webview",
363
+ SecurityCategory::ProcessExecution => "operating-system-process",
364
+ SecurityCategory::RawSql => "database",
365
+ SecurityCategory::PlainSecretStorage => "local-storage",
366
+ }
367
+ }
368
+
369
+ const fn trace_role(category: SecurityCategory) -> &'static str {
370
+ match category {
371
+ SecurityCategory::HardcodedSecret => "source",
372
+ SecurityCategory::TlsBypass | SecurityCategory::WebViewRisk => "boundary",
373
+ SecurityCategory::InsecureTransport
374
+ | SecurityCategory::ProcessExecution
375
+ | SecurityCategory::RawSql
376
+ | SecurityCategory::PlainSecretStorage => "sink",
377
+ }
378
+ }
379
+
380
+ const fn category_name(category: SecurityCategory) -> &'static str {
381
+ match category {
382
+ SecurityCategory::HardcodedSecret => "hardcoded secret",
383
+ SecurityCategory::InsecureTransport => "insecure transport",
384
+ SecurityCategory::TlsBypass => "TLS bypass",
385
+ SecurityCategory::WebViewRisk => "WebView risk",
386
+ SecurityCategory::ProcessExecution => "process execution",
387
+ SecurityCategory::RawSql => "raw SQL",
388
+ SecurityCategory::PlainSecretStorage => "plain secret storage",
389
+ }
390
+ }