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.
- package/Cargo.lock +572 -0
- package/Cargo.toml +41 -0
- package/LICENSE +21 -0
- package/README.md +617 -0
- package/assets/dart-decimate-banner.png +0 -0
- package/npm/bin/dart-decimate-mcp.js +48 -0
- package/npm/bin/dart-decimate.js +48 -0
- package/npm/scripts/postinstall.js +46 -0
- package/npm/scripts/test-npx-local.js +54 -0
- package/npm/scripts/test-npx-mcp-local.js +56 -0
- package/package.json +45 -0
- package/src/baseline.rs +643 -0
- package/src/bin/dart-decimate-mcp.rs +11 -0
- package/src/boundaries/tests.rs +60 -0
- package/src/boundaries.rs +317 -0
- package/src/changed_scope.rs +117 -0
- package/src/ci_template.rs +399 -0
- package/src/cli/analyze.rs +462 -0
- package/src/cli/analyzer_options.rs +82 -0
- package/src/cli/audit_run.rs +327 -0
- package/src/cli/boundary_args.rs +44 -0
- package/src/cli/ci_template_run.rs +196 -0
- package/src/cli/command_name.rs +20 -0
- package/src/cli/common_args.rs +167 -0
- package/src/cli/config_run.rs +78 -0
- package/src/cli/coverage_run.rs +393 -0
- package/src/cli/decision_surface_run.rs +99 -0
- package/src/cli/default_command.rs +69 -0
- package/src/cli/dupes_args.rs +150 -0
- package/src/cli/entry_points.rs +121 -0
- package/src/cli/error.rs +149 -0
- package/src/cli/error_output.rs +68 -0
- package/src/cli/explain_run.rs +72 -0
- package/src/cli/fix_run.rs +162 -0
- package/src/cli/flags_args.rs +23 -0
- package/src/cli/health_args.rs +200 -0
- package/src/cli/hooks_run.rs +191 -0
- package/src/cli/impact_run.rs +95 -0
- package/src/cli/init_run.rs +49 -0
- package/src/cli/inspect_args.rs +20 -0
- package/src/cli/inspect_run.rs +66 -0
- package/src/cli/issue_filter_args.rs +240 -0
- package/src/cli/list_run.rs +311 -0
- package/src/cli/mode_args.rs +72 -0
- package/src/cli/output_format.rs +62 -0
- package/src/cli/regression_args.rs +156 -0
- package/src/cli/request_args.rs +175 -0
- package/src/cli/schema_commands.rs +72 -0
- package/src/cli/scope_args.rs +174 -0
- package/src/cli/security_args.rs +175 -0
- package/src/cli/security_gate_run.rs +66 -0
- package/src/cli/security_summary_run.rs +43 -0
- package/src/cli/summary_args.rs +12 -0
- package/src/cli/tests.rs +516 -0
- package/src/cli/trace_args.rs +67 -0
- package/src/cli/trace_run.rs +131 -0
- package/src/cli/trace_tests.rs +235 -0
- package/src/cli/unsupported_run.rs +116 -0
- package/src/cli/watch_run.rs +153 -0
- package/src/cli.rs +603 -0
- package/src/config/boundary_config.rs +235 -0
- package/src/config/cache.rs +25 -0
- package/src/config/dependencies.rs +114 -0
- package/src/config/health.rs +237 -0
- package/src/config/jsonc.rs +66 -0
- package/src/config/policy.rs +91 -0
- package/src/config/rule_aliases.rs +487 -0
- package/src/config/rules.rs +339 -0
- package/src/config.rs +635 -0
- package/src/coverage.rs +469 -0
- package/src/dart_parser.rs +673 -0
- package/src/decision_surface.rs +405 -0
- package/src/dependencies/analyze.rs +218 -0
- package/src/dependencies/codegen.rs +118 -0
- package/src/dependencies/discovery.rs +60 -0
- package/src/dependencies/overrides.rs +76 -0
- package/src/dependencies/private_src_imports.rs +32 -0
- package/src/dependencies/pubspec_document.rs +86 -0
- package/src/dependencies/pubspec_entry.rs +96 -0
- package/src/dependencies/tests.rs +544 -0
- package/src/dependencies/usage.rs +33 -0
- package/src/dependencies.rs +553 -0
- package/src/dependency_scripts.rs +91 -0
- package/src/dupes/lex.rs +236 -0
- package/src/dupes/tests.rs +119 -0
- package/src/dupes.rs +625 -0
- package/src/explain.rs +692 -0
- package/src/extract/directives.rs +252 -0
- package/src/extract/error_tests.rs +25 -0
- package/src/extract/members.rs +210 -0
- package/src/extract/primary_constructors.rs +421 -0
- package/src/extract/references.rs +151 -0
- package/src/extract/routes.rs +691 -0
- package/src/extract/signatures.rs +266 -0
- package/src/extract/string_tests.rs +18 -0
- package/src/extract/strings.rs +107 -0
- package/src/extract/tests.rs +697 -0
- package/src/extract.rs +680 -0
- package/src/feature_flags/tests.rs +76 -0
- package/src/feature_flags.rs +453 -0
- package/src/finding_identity.rs +60 -0
- package/src/fix.rs +551 -0
- package/src/generated.rs +53 -0
- package/src/graph/augment_tests.rs +212 -0
- package/src/graph/conditionals.rs +74 -0
- package/src/graph/package_config_tests.rs +100 -0
- package/src/graph/parts.rs +248 -0
- package/src/graph/pubspec_tests.rs +119 -0
- package/src/graph/tests.rs +837 -0
- package/src/graph/uri_tests.rs +102 -0
- package/src/graph.rs +644 -0
- package/src/health/coverage.rs +309 -0
- package/src/health/ownership.rs +175 -0
- package/src/health/runtime_coverage.rs +616 -0
- package/src/health/runtime_intelligence.rs +511 -0
- package/src/health/scores.rs +296 -0
- package/src/health/tests.rs +240 -0
- package/src/health/threshold_types.rs +163 -0
- package/src/health/thresholds.rs +229 -0
- package/src/health/types.rs +636 -0
- package/src/health.rs +597 -0
- package/src/hooks/agent_settings.rs +175 -0
- package/src/hooks/templates.rs +51 -0
- package/src/hooks.rs +569 -0
- package/src/impact.rs +287 -0
- package/src/init.rs +237 -0
- package/src/inspect.rs +142 -0
- package/src/intelligence/tests.rs +232 -0
- package/src/intelligence.rs +305 -0
- package/src/lib.rs +198 -0
- package/src/main.rs +5 -0
- package/src/manifest/mcp.rs +565 -0
- package/src/manifest.rs +552 -0
- package/src/mcp/cli_args/value_args.rs +175 -0
- package/src/mcp/cli_args.rs +658 -0
- package/src/mcp/cli_args_tests.rs +522 -0
- package/src/mcp/code_execute.rs +671 -0
- package/src/mcp/tools.rs +660 -0
- package/src/mcp.rs +325 -0
- package/src/output/audit_risk.rs +135 -0
- package/src/output/dependency_findings.rs +327 -0
- package/src/output/duplication_findings.rs +114 -0
- package/src/output/feature_flag_findings.rs +122 -0
- package/src/output/format.rs +31 -0
- package/src/output/graph_findings.rs +461 -0
- package/src/output/health_findings.rs +612 -0
- package/src/output/human.rs +27 -0
- package/src/output/next_steps.rs +92 -0
- package/src/output/route_findings.rs +76 -0
- package/src/output/runtime_coverage.rs +515 -0
- package/src/output/scope.rs +183 -0
- package/src/output/security_findings.rs +390 -0
- package/src/output/security_sarif.rs +150 -0
- package/src/output/suppressions.rs +323 -0
- package/src/output/symbol_findings.rs +257 -0
- package/src/output/tests.rs +114 -0
- package/src/output/types.rs +604 -0
- package/src/output/widget_findings.rs +213 -0
- package/src/output.rs +682 -0
- package/src/package_map.rs +495 -0
- package/src/policy/packs.rs +232 -0
- package/src/policy/tests.rs +134 -0
- package/src/policy.rs +640 -0
- package/src/project_list.rs +376 -0
- package/src/report_schema/inventory.rs +418 -0
- package/src/report_schema.rs +488 -0
- package/src/routes.rs +354 -0
- package/src/scan/tests.rs +250 -0
- package/src/scan.rs +275 -0
- package/src/security/detect.rs +639 -0
- package/src/security/tests.rs +178 -0
- package/src/security.rs +373 -0
- package/src/security_gate.rs +555 -0
- package/src/symbols/path_filters.rs +27 -0
- package/src/symbols/private_type_leaks.rs +125 -0
- package/src/symbols/riverpod.rs +113 -0
- package/src/symbols/tests.rs +692 -0
- package/src/symbols.rs +693 -0
- package/src/trace/tests.rs +225 -0
- package/src/trace.rs +690 -0
- package/src/unsupported.rs +47 -0
- package/src/widgets/lifecycle.rs +484 -0
- package/src/widgets/params.rs +236 -0
- package/src/widgets/tests.rs +430 -0
- package/src/widgets/top_level.rs +110 -0
- package/src/widgets/unrendered.rs +374 -0
- package/src/widgets.rs +590 -0
- package/src/workspace_scope.rs +358 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
use std::fs;
|
|
2
|
+
|
|
3
|
+
use tempfile::TempDir;
|
|
4
|
+
|
|
5
|
+
use crate::{SecurityOptions, analyze_security, scan_project};
|
|
6
|
+
|
|
7
|
+
#[test]
|
|
8
|
+
fn detects_dart_and_flutter_security_candidate_patterns() -> Result<(), Box<dyn std::error::Error>>
|
|
9
|
+
{
|
|
10
|
+
let fixture = tempfile::tempdir()?;
|
|
11
|
+
write(&fixture, "pubspec.yaml", "name: app\n")?;
|
|
12
|
+
write(
|
|
13
|
+
&fixture,
|
|
14
|
+
"lib/main.dart",
|
|
15
|
+
"import 'dart:io';
|
|
16
|
+
|
|
17
|
+
const accessToken = 'dart_decimate_fixture_value_1234567890';
|
|
18
|
+
|
|
19
|
+
Future<void> main(dynamic db, dynamic prefs, dynamic controller, String command, String id, String token) async {
|
|
20
|
+
final uri = Uri.parse('http://api.example.com/login');
|
|
21
|
+
final client = HttpClient();
|
|
22
|
+
client.badCertificateCallback = (cert, host, port) => true;
|
|
23
|
+
controller.setJavaScriptMode(JavaScriptMode.unrestricted);
|
|
24
|
+
await Process.run(command, ['-c', id]);
|
|
25
|
+
await db.rawQuery('SELECT * FROM users WHERE id = $id');
|
|
26
|
+
await prefs.setString('access_token', token);
|
|
27
|
+
print(uri);
|
|
28
|
+
}
|
|
29
|
+
",
|
|
30
|
+
)?;
|
|
31
|
+
|
|
32
|
+
let project = scan_project(fixture.path())?;
|
|
33
|
+
let report = analyze_security(
|
|
34
|
+
&project,
|
|
35
|
+
&SecurityOptions {
|
|
36
|
+
top: None,
|
|
37
|
+
surface: true,
|
|
38
|
+
..SecurityOptions::default()
|
|
39
|
+
},
|
|
40
|
+
None,
|
|
41
|
+
)?;
|
|
42
|
+
let rules = report
|
|
43
|
+
.candidates
|
|
44
|
+
.iter()
|
|
45
|
+
.map(|candidate| candidate.rule_id.as_str())
|
|
46
|
+
.collect::<Vec<_>>();
|
|
47
|
+
|
|
48
|
+
assert_eq!(report.analyzed_files, 1);
|
|
49
|
+
assert_eq!(report.total_occurrences, 7);
|
|
50
|
+
assert_eq!(report.attack_surface.len(), 7);
|
|
51
|
+
assert!(rules.contains(&"dart-decimate/security-hardcoded-secret"));
|
|
52
|
+
assert!(rules.contains(&"dart-decimate/security-insecure-transport"));
|
|
53
|
+
assert!(rules.contains(&"dart-decimate/security-tls-bypass"));
|
|
54
|
+
assert!(rules.contains(&"dart-decimate/security-webview-risk"));
|
|
55
|
+
assert!(rules.contains(&"dart-decimate/security-process-execution"));
|
|
56
|
+
assert!(rules.contains(&"dart-decimate/security-raw-sql"));
|
|
57
|
+
assert!(rules.contains(&"dart-decimate/security-plain-secret-storage"));
|
|
58
|
+
assert!(
|
|
59
|
+
report
|
|
60
|
+
.candidates
|
|
61
|
+
.iter()
|
|
62
|
+
.flat_map(|candidate| &candidate.occurrences)
|
|
63
|
+
.all(|occurrence| !occurrence
|
|
64
|
+
.evidence
|
|
65
|
+
.contains("dart_decimate_fixture_value_1234567890"))
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
Ok(())
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
#[test]
|
|
72
|
+
fn skips_comments_generated_tests_and_placeholders() -> Result<(), Box<dyn std::error::Error>> {
|
|
73
|
+
let fixture = tempfile::tempdir()?;
|
|
74
|
+
write(&fixture, "pubspec.yaml", "name: app\n")?;
|
|
75
|
+
write(
|
|
76
|
+
&fixture,
|
|
77
|
+
"lib/main.dart",
|
|
78
|
+
"// const accessToken = 'dart_decimate_fixture_value_1234567890';
|
|
79
|
+
const apiKey = 'YOUR_API_KEY';
|
|
80
|
+
const firebase = FirebaseOptions(apiKey: 'AIzaPublicMobileConfigValue');
|
|
81
|
+
final uri = Uri.parse('http://localhost:8080');
|
|
82
|
+
Future<void> run() => Process.run('git', ['status']);
|
|
83
|
+
Future<void> query(dynamic db, String id) => db.rawQuery('SELECT * FROM users WHERE id = ?', [id]);
|
|
84
|
+
",
|
|
85
|
+
)?;
|
|
86
|
+
write(
|
|
87
|
+
&fixture,
|
|
88
|
+
"lib/generated.g.dart",
|
|
89
|
+
"const accessToken = 'dart_decimate_fixture_value_1234567890';\n",
|
|
90
|
+
)?;
|
|
91
|
+
write(
|
|
92
|
+
&fixture,
|
|
93
|
+
"test/security_test.dart",
|
|
94
|
+
"const accessToken = 'dart_decimate_fixture_value_1234567890';\n",
|
|
95
|
+
)?;
|
|
96
|
+
|
|
97
|
+
let project = scan_project(fixture.path())?;
|
|
98
|
+
let report = analyze_security(&project, &SecurityOptions::default(), None)?;
|
|
99
|
+
|
|
100
|
+
assert!(report.candidates.is_empty());
|
|
101
|
+
assert_eq!(report.total_occurrences, 0);
|
|
102
|
+
|
|
103
|
+
Ok(())
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
#[test]
|
|
107
|
+
fn skips_flutter_commands_logs_and_interpolated_bearer_headers()
|
|
108
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
109
|
+
let fixture = tempfile::tempdir()?;
|
|
110
|
+
write(&fixture, "pubspec.yaml", "name: app\n")?;
|
|
111
|
+
write(
|
|
112
|
+
&fixture,
|
|
113
|
+
"lib/main.dart",
|
|
114
|
+
"/// This shouldn't shift later string positions.
|
|
115
|
+
Future<void> main(dynamic viewModel, dynamic prefs, String token) async {
|
|
116
|
+
viewModel.load.execute();
|
|
117
|
+
viewModel.login.execute((email: 'user@example.com', password: 'not-a-secret'));
|
|
118
|
+
_log.severe(
|
|
119
|
+
'Failed to fetch Token from SharedPreferences',
|
|
120
|
+
);
|
|
121
|
+
_log.warning('Failed to set token');
|
|
122
|
+
if (request.headers['Authorization'] != 'Bearer $token') {}
|
|
123
|
+
final header = 'Bearer $token';
|
|
124
|
+
await prefs.setString('access_token', token);
|
|
125
|
+
print(header);
|
|
126
|
+
}
|
|
127
|
+
",
|
|
128
|
+
)?;
|
|
129
|
+
|
|
130
|
+
let project = scan_project(fixture.path())?;
|
|
131
|
+
let report = analyze_security(&project, &SecurityOptions::default(), None)?;
|
|
132
|
+
|
|
133
|
+
assert_eq!(report.total_occurrences, 1);
|
|
134
|
+
assert_eq!(
|
|
135
|
+
report.candidates[0].rule_id,
|
|
136
|
+
"dart-decimate/security-plain-secret-storage"
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
Ok(())
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
#[test]
|
|
143
|
+
fn top_limits_grouped_candidates_but_preserves_total_occurrences()
|
|
144
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
145
|
+
let fixture = tempfile::tempdir()?;
|
|
146
|
+
write(&fixture, "pubspec.yaml", "name: app\n")?;
|
|
147
|
+
write(
|
|
148
|
+
&fixture,
|
|
149
|
+
"lib/main.dart",
|
|
150
|
+
"const accessToken = 'dart_decimate_fixture_value_1234567890';
|
|
151
|
+
final uri = Uri.parse('http://api.example.com/login');
|
|
152
|
+
",
|
|
153
|
+
)?;
|
|
154
|
+
|
|
155
|
+
let project = scan_project(fixture.path())?;
|
|
156
|
+
let report = analyze_security(
|
|
157
|
+
&project,
|
|
158
|
+
&SecurityOptions {
|
|
159
|
+
top: Some(1),
|
|
160
|
+
surface: false,
|
|
161
|
+
..SecurityOptions::default()
|
|
162
|
+
},
|
|
163
|
+
None,
|
|
164
|
+
)?;
|
|
165
|
+
|
|
166
|
+
assert_eq!(report.candidates.len(), 1);
|
|
167
|
+
assert_eq!(report.total_occurrences, 2);
|
|
168
|
+
|
|
169
|
+
Ok(())
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
fn write(fixture: &TempDir, path: &str, source: &str) -> Result<(), std::io::Error> {
|
|
173
|
+
let path = fixture.path().join(path);
|
|
174
|
+
if let Some(parent) = path.parent() {
|
|
175
|
+
fs::create_dir_all(parent)?;
|
|
176
|
+
}
|
|
177
|
+
fs::write(path, source)
|
|
178
|
+
}
|
package/src/security.rs
ADDED
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
use std::collections::{BTreeMap, BTreeSet};
|
|
2
|
+
use std::fs;
|
|
3
|
+
use std::path::PathBuf;
|
|
4
|
+
|
|
5
|
+
use serde::{Deserialize, Serialize};
|
|
6
|
+
use thiserror::Error;
|
|
7
|
+
|
|
8
|
+
use crate::graph::normalize_against;
|
|
9
|
+
use crate::{DeadCodeReport, Location, ScannedProject};
|
|
10
|
+
|
|
11
|
+
mod detect;
|
|
12
|
+
use detect::{detect_in_source, is_ignored_path};
|
|
13
|
+
|
|
14
|
+
/// Security candidate detector options.
|
|
15
|
+
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
|
16
|
+
pub struct SecurityOptions {
|
|
17
|
+
/// Limit output to the N most frequently reported candidate groups.
|
|
18
|
+
pub top: Option<usize>,
|
|
19
|
+
/// Include attack-surface inventory entries.
|
|
20
|
+
pub surface: bool,
|
|
21
|
+
/// Enabled candidate categories. Empty means all categories.
|
|
22
|
+
pub categories: BTreeSet<SecurityCategory>,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/// Security candidate report.
|
|
26
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
27
|
+
pub struct SecurityReport {
|
|
28
|
+
/// Options used to compute this report.
|
|
29
|
+
pub options: SecurityOptions,
|
|
30
|
+
/// Dart files included in security detection.
|
|
31
|
+
pub analyzed_files: usize,
|
|
32
|
+
/// Grouped unverified security candidates.
|
|
33
|
+
pub candidates: Vec<SecurityCandidate>,
|
|
34
|
+
/// Raw security candidate occurrence count before `--top` truncation.
|
|
35
|
+
pub total_occurrences: usize,
|
|
36
|
+
/// Optional attack-surface inventory.
|
|
37
|
+
pub attack_surface: Vec<AttackSurfaceEntry>,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/// One grouped unverified security candidate.
|
|
41
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
42
|
+
pub struct SecurityCandidate {
|
|
43
|
+
/// Stable rule id.
|
|
44
|
+
pub rule_id: String,
|
|
45
|
+
/// Candidate category.
|
|
46
|
+
pub category: SecurityCategory,
|
|
47
|
+
/// API surface or sink family.
|
|
48
|
+
pub sink: String,
|
|
49
|
+
/// Detection confidence.
|
|
50
|
+
pub confidence: SecurityConfidence,
|
|
51
|
+
/// Candidate occurrences.
|
|
52
|
+
pub occurrences: Vec<SecurityOccurrence>,
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// Security candidate category.
|
|
56
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
|
57
|
+
#[serde(rename_all = "kebab-case")]
|
|
58
|
+
pub enum SecurityCategory {
|
|
59
|
+
/// Secret-shaped literal or secret-named assignment.
|
|
60
|
+
HardcodedSecret,
|
|
61
|
+
/// Remote cleartext HTTP transport.
|
|
62
|
+
InsecureTransport,
|
|
63
|
+
/// TLS validation bypass.
|
|
64
|
+
TlsBypass,
|
|
65
|
+
/// `WebView` JavaScript or file access exposure.
|
|
66
|
+
WebViewRisk,
|
|
67
|
+
/// Process execution with shell or dynamic command material.
|
|
68
|
+
ProcessExecution,
|
|
69
|
+
/// Raw SQL with interpolation or dynamic query text.
|
|
70
|
+
RawSql,
|
|
71
|
+
/// Secret-like material written to plain local storage.
|
|
72
|
+
PlainSecretStorage,
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
impl SecurityCategory {
|
|
76
|
+
const fn rule_id(self) -> &'static str {
|
|
77
|
+
match self {
|
|
78
|
+
Self::HardcodedSecret => "dart-decimate/security-hardcoded-secret",
|
|
79
|
+
Self::InsecureTransport => "dart-decimate/security-insecure-transport",
|
|
80
|
+
Self::TlsBypass => "dart-decimate/security-tls-bypass",
|
|
81
|
+
Self::WebViewRisk => "dart-decimate/security-webview-risk",
|
|
82
|
+
Self::ProcessExecution => "dart-decimate/security-process-execution",
|
|
83
|
+
Self::RawSql => "dart-decimate/security-raw-sql",
|
|
84
|
+
Self::PlainSecretStorage => "dart-decimate/security-plain-secret-storage",
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
impl SecurityOptions {
|
|
90
|
+
fn includes_category(&self, category: SecurityCategory) -> bool {
|
|
91
|
+
self.categories.is_empty() || self.categories.contains(&category)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/// Detection confidence.
|
|
96
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
|
97
|
+
#[serde(rename_all = "kebab-case")]
|
|
98
|
+
pub enum SecurityConfidence {
|
|
99
|
+
/// Low-confidence heuristic.
|
|
100
|
+
Low,
|
|
101
|
+
/// Medium-confidence heuristic.
|
|
102
|
+
Medium,
|
|
103
|
+
/// High-confidence known risky surface.
|
|
104
|
+
High,
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/// One security candidate occurrence.
|
|
108
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
109
|
+
pub struct SecurityOccurrence {
|
|
110
|
+
/// Dart file path.
|
|
111
|
+
pub path: PathBuf,
|
|
112
|
+
/// Location of the candidate.
|
|
113
|
+
pub location: Location,
|
|
114
|
+
/// Matched expression or API surface.
|
|
115
|
+
pub expression: String,
|
|
116
|
+
/// Redacted source-line evidence.
|
|
117
|
+
pub evidence: String,
|
|
118
|
+
/// Optional module-level graph reachability context.
|
|
119
|
+
pub reachability: Option<SecurityReachability>,
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/// Module-level security reachability context.
|
|
123
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
124
|
+
pub struct SecurityReachability {
|
|
125
|
+
/// Whether this occurrence's module is reachable from a configured entry point.
|
|
126
|
+
pub reachable_from_entrypoint: bool,
|
|
127
|
+
/// Confidence tier for the reachability evidence.
|
|
128
|
+
pub taint_confidence: SecurityTaintConfidence,
|
|
129
|
+
/// Entry points that seeded the module graph traversal.
|
|
130
|
+
pub entry_points: Vec<PathBuf>,
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/// Security taint confidence tier.
|
|
134
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
135
|
+
#[serde(rename_all = "kebab-case")]
|
|
136
|
+
pub enum SecurityTaintConfidence {
|
|
137
|
+
/// Module is import-reachable from an entry point; value flow is not proven.
|
|
138
|
+
ModuleLevel,
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/// Attack-surface inventory entry.
|
|
142
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
143
|
+
pub struct AttackSurfaceEntry {
|
|
144
|
+
/// Candidate category exposed on this surface.
|
|
145
|
+
pub category: SecurityCategory,
|
|
146
|
+
/// Dart file path.
|
|
147
|
+
pub path: PathBuf,
|
|
148
|
+
/// Location of the surface.
|
|
149
|
+
pub location: Location,
|
|
150
|
+
/// API surface or boundary.
|
|
151
|
+
pub surface: String,
|
|
152
|
+
/// Verification prompt for downstream agents.
|
|
153
|
+
pub verification_prompt: String,
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/// Errors returned while detecting security candidates.
|
|
157
|
+
#[derive(Debug, Error)]
|
|
158
|
+
pub enum SecurityError {
|
|
159
|
+
/// A Dart file could not be read.
|
|
160
|
+
#[error("failed to read Dart file {path}: {source}")]
|
|
161
|
+
ReadFile {
|
|
162
|
+
/// File path.
|
|
163
|
+
path: PathBuf,
|
|
164
|
+
/// Underlying IO error.
|
|
165
|
+
source: std::io::Error,
|
|
166
|
+
},
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
170
|
+
struct CandidateGroup {
|
|
171
|
+
rule_id: String,
|
|
172
|
+
category: SecurityCategory,
|
|
173
|
+
sink: String,
|
|
174
|
+
confidence: SecurityConfidence,
|
|
175
|
+
occurrences: Vec<SecurityOccurrence>,
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
179
|
+
struct DetectedSecurityCandidate {
|
|
180
|
+
category: SecurityCategory,
|
|
181
|
+
sink: String,
|
|
182
|
+
confidence: SecurityConfidence,
|
|
183
|
+
occurrence: SecurityOccurrence,
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/// Detect unverified local security review candidates in Dart and Flutter code.
|
|
187
|
+
///
|
|
188
|
+
/// # Errors
|
|
189
|
+
///
|
|
190
|
+
/// Returns [`SecurityError`] if a scanned Dart file cannot be read.
|
|
191
|
+
pub fn analyze_security(
|
|
192
|
+
project: &ScannedProject,
|
|
193
|
+
options: &SecurityOptions,
|
|
194
|
+
dead_code: Option<&DeadCodeReport>,
|
|
195
|
+
) -> Result<SecurityReport, SecurityError> {
|
|
196
|
+
let mut groups = BTreeMap::<(SecurityCategory, String), CandidateGroup>::new();
|
|
197
|
+
let mut analyzed_files = 0;
|
|
198
|
+
let reachability = dead_code.map(SecurityReachabilityContext::from);
|
|
199
|
+
|
|
200
|
+
for file in &project.files {
|
|
201
|
+
let path = normalize_against(&project.root, &file.path);
|
|
202
|
+
if !path.starts_with(&project.root) || is_ignored_path(&path) {
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
analyzed_files += 1;
|
|
206
|
+
let source = fs::read_to_string(&path).map_err(|source| SecurityError::ReadFile {
|
|
207
|
+
path: path.clone(),
|
|
208
|
+
source,
|
|
209
|
+
})?;
|
|
210
|
+
for detected in detect_in_source(&path, &source)
|
|
211
|
+
.into_iter()
|
|
212
|
+
.filter(|candidate| options.includes_category(candidate.category))
|
|
213
|
+
{
|
|
214
|
+
let mut detected = detected;
|
|
215
|
+
detected.occurrence.reachability = reachability
|
|
216
|
+
.as_ref()
|
|
217
|
+
.and_then(|context| context.reachability_for(&detected.occurrence.path));
|
|
218
|
+
let key = (detected.category, detected.sink.clone());
|
|
219
|
+
let group = groups.entry(key).or_insert_with(|| CandidateGroup {
|
|
220
|
+
rule_id: detected.category.rule_id().to_owned(),
|
|
221
|
+
category: detected.category,
|
|
222
|
+
sink: detected.sink.clone(),
|
|
223
|
+
confidence: detected.confidence,
|
|
224
|
+
occurrences: Vec::new(),
|
|
225
|
+
});
|
|
226
|
+
group.confidence = group.confidence.max(detected.confidence);
|
|
227
|
+
group.occurrences.push(detected.occurrence);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
let total_occurrences = groups
|
|
232
|
+
.values()
|
|
233
|
+
.map(|group| group.occurrences.len())
|
|
234
|
+
.sum::<usize>();
|
|
235
|
+
let mut candidates = groups
|
|
236
|
+
.into_values()
|
|
237
|
+
.map(SecurityCandidate::from)
|
|
238
|
+
.collect::<Vec<_>>();
|
|
239
|
+
candidates.sort_by(|left, right| {
|
|
240
|
+
(
|
|
241
|
+
std::cmp::Reverse(left.occurrences.len()),
|
|
242
|
+
left.category,
|
|
243
|
+
&left.sink,
|
|
244
|
+
)
|
|
245
|
+
.cmp(&(
|
|
246
|
+
std::cmp::Reverse(right.occurrences.len()),
|
|
247
|
+
right.category,
|
|
248
|
+
&right.sink,
|
|
249
|
+
))
|
|
250
|
+
});
|
|
251
|
+
if let Some(top) = options.top {
|
|
252
|
+
candidates.truncate(top);
|
|
253
|
+
}
|
|
254
|
+
let attack_surface = if options.surface {
|
|
255
|
+
attack_surface_for(&candidates)
|
|
256
|
+
} else {
|
|
257
|
+
Vec::new()
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
Ok(SecurityReport {
|
|
261
|
+
options: options.clone(),
|
|
262
|
+
analyzed_files,
|
|
263
|
+
candidates,
|
|
264
|
+
total_occurrences,
|
|
265
|
+
attack_surface,
|
|
266
|
+
})
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
270
|
+
struct SecurityReachabilityContext {
|
|
271
|
+
reachable_files: BTreeSet<PathBuf>,
|
|
272
|
+
entry_points: Vec<PathBuf>,
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
impl SecurityReachabilityContext {
|
|
276
|
+
fn reachability_for(&self, path: &PathBuf) -> Option<SecurityReachability> {
|
|
277
|
+
self.reachable_files
|
|
278
|
+
.contains(path)
|
|
279
|
+
.then(|| SecurityReachability {
|
|
280
|
+
reachable_from_entrypoint: true,
|
|
281
|
+
taint_confidence: SecurityTaintConfidence::ModuleLevel,
|
|
282
|
+
entry_points: self.entry_points.clone(),
|
|
283
|
+
})
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
impl From<&DeadCodeReport> for SecurityReachabilityContext {
|
|
288
|
+
fn from(report: &DeadCodeReport) -> Self {
|
|
289
|
+
Self {
|
|
290
|
+
reachable_files: report.reachable_files.iter().cloned().collect(),
|
|
291
|
+
entry_points: report.entry_points.clone(),
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
impl From<CandidateGroup> for SecurityCandidate {
|
|
297
|
+
fn from(group: CandidateGroup) -> Self {
|
|
298
|
+
let mut seen = BTreeSet::new();
|
|
299
|
+
let mut occurrences = group
|
|
300
|
+
.occurrences
|
|
301
|
+
.into_iter()
|
|
302
|
+
.filter(|occurrence| {
|
|
303
|
+
seen.insert((
|
|
304
|
+
occurrence.path.clone(),
|
|
305
|
+
occurrence.location.line,
|
|
306
|
+
occurrence.location.column,
|
|
307
|
+
occurrence.expression.clone(),
|
|
308
|
+
))
|
|
309
|
+
})
|
|
310
|
+
.collect::<Vec<_>>();
|
|
311
|
+
occurrences.sort_by(|left, right| {
|
|
312
|
+
(&left.path, left.location.line, left.location.column).cmp(&(
|
|
313
|
+
&right.path,
|
|
314
|
+
right.location.line,
|
|
315
|
+
right.location.column,
|
|
316
|
+
))
|
|
317
|
+
});
|
|
318
|
+
Self {
|
|
319
|
+
rule_id: group.rule_id,
|
|
320
|
+
category: group.category,
|
|
321
|
+
sink: group.sink,
|
|
322
|
+
confidence: group.confidence,
|
|
323
|
+
occurrences,
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
fn attack_surface_for(candidates: &[SecurityCandidate]) -> Vec<AttackSurfaceEntry> {
|
|
329
|
+
candidates
|
|
330
|
+
.iter()
|
|
331
|
+
.flat_map(|candidate| {
|
|
332
|
+
candidate
|
|
333
|
+
.occurrences
|
|
334
|
+
.iter()
|
|
335
|
+
.map(|occurrence| AttackSurfaceEntry {
|
|
336
|
+
category: candidate.category,
|
|
337
|
+
path: occurrence.path.clone(),
|
|
338
|
+
location: occurrence.location,
|
|
339
|
+
surface: candidate.sink.clone(),
|
|
340
|
+
verification_prompt: verification_prompt(candidate.category).to_owned(),
|
|
341
|
+
})
|
|
342
|
+
})
|
|
343
|
+
.collect()
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const fn verification_prompt(category: SecurityCategory) -> &'static str {
|
|
347
|
+
match category {
|
|
348
|
+
SecurityCategory::HardcodedSecret => {
|
|
349
|
+
"Verify whether this literal is a real secret and rotate it if confirmed."
|
|
350
|
+
}
|
|
351
|
+
SecurityCategory::InsecureTransport => {
|
|
352
|
+
"Verify whether this remote HTTP endpoint can expose sensitive traffic."
|
|
353
|
+
}
|
|
354
|
+
SecurityCategory::TlsBypass => {
|
|
355
|
+
"Verify whether certificate validation can be bypassed outside trusted development code."
|
|
356
|
+
}
|
|
357
|
+
SecurityCategory::WebViewRisk => {
|
|
358
|
+
"Verify whether untrusted content can execute JavaScript or access local files."
|
|
359
|
+
}
|
|
360
|
+
SecurityCategory::ProcessExecution => {
|
|
361
|
+
"Verify whether attacker-controlled input can influence the executable, arguments, or shell."
|
|
362
|
+
}
|
|
363
|
+
SecurityCategory::RawSql => {
|
|
364
|
+
"Verify whether untrusted input can alter SQL text instead of using parameters."
|
|
365
|
+
}
|
|
366
|
+
SecurityCategory::PlainSecretStorage => {
|
|
367
|
+
"Verify whether secret material is persisted outside secure storage."
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
#[cfg(test)]
|
|
373
|
+
mod tests;
|