dart-decimate 0.0.9 → 0.0.10
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 +1 -1
- package/Cargo.toml +1 -1
- package/README.md +41 -58
- package/package.json +1 -1
- package/src/cli/analyze.rs +6 -3
- package/src/cli/entry_points.rs +18 -2
- package/src/cli/list_run.rs +1 -0
- package/src/cli/tests.rs +74 -0
- package/src/config/rules.rs +50 -13
- package/src/dependencies/analyze.rs +7 -1
- package/src/dependencies/tests.rs +29 -0
- package/src/dependencies.rs +19 -0
- package/src/dependency_scripts.rs +2 -0
- package/src/dupes/packages.rs +71 -0
- package/src/dupes/tests.rs +27 -0
- package/src/dupes.rs +6 -1
- package/src/extract/reference_tests.rs +106 -0
- package/src/extract/references.rs +264 -2
- package/src/extract/tests.rs +25 -0
- package/src/extract.rs +6 -3
- package/src/generated.rs +96 -3
- package/src/graph/package_config_tests.rs +123 -0
- package/src/graph/tests.rs +125 -0
- package/src/graph.rs +6 -5
- package/src/output/feature_flag_findings.rs +63 -1
- package/src/output/graph_findings.rs +23 -21
- package/src/output/security_findings.rs +15 -2
- package/src/output/tests.rs +104 -1
- package/src/package_map.rs +220 -28
- package/src/routes.rs +6 -1
- package/src/scan.rs +15 -5
- package/src/security/detect.rs +140 -6
- package/src/security/tests.rs +96 -0
- package/src/trace.rs +1 -1
package/src/package_map.rs
CHANGED
|
@@ -9,14 +9,18 @@ use crate::graph::{GraphError, normalize_path};
|
|
|
9
9
|
|
|
10
10
|
#[derive(Debug, Clone, Default)]
|
|
11
11
|
pub(crate) struct PackageMap {
|
|
12
|
-
by_name: BTreeMap<String, PackageRoot
|
|
12
|
+
by_name: BTreeMap<String, Vec<PackageRoot>>,
|
|
13
|
+
by_owner_dependency: BTreeMap<PathBuf, BTreeMap<String, PackageRoot>>,
|
|
14
|
+
uses_package_config: bool,
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
16
18
|
struct PackageRoot {
|
|
19
|
+
name: String,
|
|
17
20
|
root: PathBuf,
|
|
18
21
|
package_path: PathBuf,
|
|
19
22
|
local: bool,
|
|
23
|
+
from_package_config: bool,
|
|
20
24
|
}
|
|
21
25
|
|
|
22
26
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
@@ -28,11 +32,11 @@ pub(crate) struct PackageResolution {
|
|
|
28
32
|
impl PackageMap {
|
|
29
33
|
pub(crate) fn discover(root: &Path) -> Result<Self, GraphError> {
|
|
30
34
|
if let Some(config) = PackageConfigFile::read(root)? {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
)
|
|
35
|
+
let mut packages = Self::from_package_config(root, &config.config_dir, &config.config);
|
|
36
|
+
let mut visited = BTreeSet::new();
|
|
37
|
+
packages.discover_pubspec(root, &mut visited)?;
|
|
38
|
+
packages.discover_nested_pubspecs(root, &mut visited)?;
|
|
39
|
+
return Ok(packages);
|
|
36
40
|
}
|
|
37
41
|
|
|
38
42
|
let mut packages = Self::default();
|
|
@@ -49,16 +53,70 @@ impl PackageMap {
|
|
|
49
53
|
pub(crate) fn local_roots(&self) -> BTreeSet<PathBuf> {
|
|
50
54
|
self.by_name
|
|
51
55
|
.values()
|
|
56
|
+
.flatten()
|
|
52
57
|
.filter(|package| package.local)
|
|
53
58
|
.map(|package| package.root.clone())
|
|
54
59
|
.collect()
|
|
55
60
|
}
|
|
56
61
|
|
|
57
|
-
pub(crate) fn
|
|
58
|
-
self
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
+
pub(crate) fn resolve_from(
|
|
63
|
+
&self,
|
|
64
|
+
from_path: &Path,
|
|
65
|
+
package: &str,
|
|
66
|
+
path: &str,
|
|
67
|
+
) -> Option<PackageResolution> {
|
|
68
|
+
let roots = self.by_name.get(package)?;
|
|
69
|
+
let owner_package = self.owner_package(from_path);
|
|
70
|
+
let owner_root = owner_package.map(|package| package.root.as_path());
|
|
71
|
+
if let Some(owner_package) = owner_package {
|
|
72
|
+
if owner_package.name == package {
|
|
73
|
+
return Some(package_resolution(owner_package, path));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
let configured_root = roots.iter().find(|root| root.from_package_config);
|
|
77
|
+
if owner_package.is_some_and(|package| package.from_package_config) {
|
|
78
|
+
if let Some(root) = configured_root {
|
|
79
|
+
return Some(package_resolution(root, path));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if let Some(owner_root) = owner_root {
|
|
83
|
+
if let Some(root) = self
|
|
84
|
+
.by_owner_dependency
|
|
85
|
+
.get(owner_root)
|
|
86
|
+
.and_then(|dependencies| dependencies.get(package))
|
|
87
|
+
{
|
|
88
|
+
return Some(package_resolution(root, path));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if let Some(owner_package) = owner_package {
|
|
92
|
+
if owner_package.from_package_config || self.uses_package_config {
|
|
93
|
+
return None;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if let Some(root) = configured_root {
|
|
97
|
+
return Some(package_resolution(root, path));
|
|
98
|
+
}
|
|
99
|
+
if roots.len() == 1 {
|
|
100
|
+
return roots.first().map(|root| package_resolution(root, path));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
owner_root
|
|
104
|
+
.and_then(|owner_root| {
|
|
105
|
+
roots
|
|
106
|
+
.iter()
|
|
107
|
+
.filter(|candidate| candidate.root.starts_with(owner_root))
|
|
108
|
+
.min_by_key(|candidate| candidate.root.components().count())
|
|
109
|
+
})
|
|
110
|
+
.or_else(|| roots.first())
|
|
111
|
+
.map(|root| package_resolution(root, path))
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
fn owner_package(&self, path: &Path) -> Option<&PackageRoot> {
|
|
115
|
+
self.by_name
|
|
116
|
+
.values()
|
|
117
|
+
.flatten()
|
|
118
|
+
.filter(|package| path.starts_with(&package.root))
|
|
119
|
+
.max_by_key(|package| package.root.components().count())
|
|
62
120
|
}
|
|
63
121
|
|
|
64
122
|
fn from_package_config(root: &Path, config_dir: &Path, config: &PackageConfig) -> Self {
|
|
@@ -75,16 +133,22 @@ impl PackageMap {
|
|
|
75
133
|
is_local_package_config_root(root, config_root, root_uri, &package_root);
|
|
76
134
|
Some((
|
|
77
135
|
package.name.clone(),
|
|
78
|
-
PackageRoot {
|
|
136
|
+
vec![PackageRoot {
|
|
137
|
+
name: package.name.clone(),
|
|
79
138
|
root: package_root,
|
|
80
139
|
package_path,
|
|
81
140
|
local,
|
|
82
|
-
|
|
141
|
+
from_package_config: true,
|
|
142
|
+
}],
|
|
83
143
|
))
|
|
84
144
|
})
|
|
85
145
|
.collect();
|
|
86
146
|
|
|
87
|
-
Self {
|
|
147
|
+
Self {
|
|
148
|
+
by_name,
|
|
149
|
+
by_owner_dependency: BTreeMap::new(),
|
|
150
|
+
uses_package_config: true,
|
|
151
|
+
}
|
|
88
152
|
}
|
|
89
153
|
|
|
90
154
|
fn discover_pubspec(
|
|
@@ -102,7 +166,7 @@ impl PackageMap {
|
|
|
102
166
|
};
|
|
103
167
|
|
|
104
168
|
if let Some(name) = pubspec.name {
|
|
105
|
-
self.insert(name, package_root
|
|
169
|
+
self.insert(name, &package_root, PathBuf::from("lib"));
|
|
106
170
|
}
|
|
107
171
|
|
|
108
172
|
for dependency in pubspec.path_dependencies {
|
|
@@ -110,11 +174,15 @@ impl PackageMap {
|
|
|
110
174
|
if !dependency_root.join("pubspec.yaml").is_file() {
|
|
111
175
|
continue;
|
|
112
176
|
}
|
|
113
|
-
self.insert(
|
|
114
|
-
dependency.name,
|
|
115
|
-
dependency_root
|
|
177
|
+
let package = self.insert(
|
|
178
|
+
dependency.name.clone(),
|
|
179
|
+
&dependency_root,
|
|
116
180
|
PathBuf::from("lib"),
|
|
117
181
|
);
|
|
182
|
+
self.by_owner_dependency
|
|
183
|
+
.entry(package_root.clone())
|
|
184
|
+
.or_default()
|
|
185
|
+
.insert(dependency.name, package);
|
|
118
186
|
self.discover_pubspec(&dependency_root, visited)?;
|
|
119
187
|
}
|
|
120
188
|
|
|
@@ -157,7 +225,7 @@ impl PackageMap {
|
|
|
157
225
|
&& path.file_name().is_some_and(|name| name == "pubspec.yaml")
|
|
158
226
|
{
|
|
159
227
|
if let Some(package_root) = path.parent() {
|
|
160
|
-
self.
|
|
228
|
+
self.discover_optional_pubspec(package_root, visited)?;
|
|
161
229
|
}
|
|
162
230
|
}
|
|
163
231
|
}
|
|
@@ -165,18 +233,60 @@ impl PackageMap {
|
|
|
165
233
|
Ok(())
|
|
166
234
|
}
|
|
167
235
|
|
|
168
|
-
fn
|
|
169
|
-
self
|
|
236
|
+
fn discover_optional_pubspec(
|
|
237
|
+
&mut self,
|
|
238
|
+
package_root: &Path,
|
|
239
|
+
visited: &mut BTreeSet<PathBuf>,
|
|
240
|
+
) -> Result<(), GraphError> {
|
|
241
|
+
let previous_packages = self.clone();
|
|
242
|
+
let previous_visited = visited.clone();
|
|
243
|
+
match self.discover_pubspec(package_root, visited) {
|
|
244
|
+
Ok(()) => Ok(()),
|
|
245
|
+
Err(error) if is_optional_pubspec_error(&error) => {
|
|
246
|
+
*self = previous_packages;
|
|
247
|
+
*visited = previous_visited;
|
|
248
|
+
Ok(())
|
|
249
|
+
}
|
|
250
|
+
Err(error) => Err(error),
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
fn insert(&mut self, name: String, root: &Path, package_uri: PathBuf) -> PackageRoot {
|
|
255
|
+
let packages = self.by_name.entry(name.clone()).or_default();
|
|
256
|
+
let root = normalize_path(root);
|
|
257
|
+
if let Some(package) = packages.iter().find(|package| package.root == root) {
|
|
258
|
+
return package.clone();
|
|
259
|
+
}
|
|
260
|
+
let package = PackageRoot {
|
|
170
261
|
name,
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
);
|
|
262
|
+
package_path: normalize_path(&root.join(package_uri)),
|
|
263
|
+
root,
|
|
264
|
+
local: true,
|
|
265
|
+
from_package_config: false,
|
|
266
|
+
};
|
|
267
|
+
packages.push(package.clone());
|
|
268
|
+
packages.sort_by(|left, right| left.root.cmp(&right.root));
|
|
269
|
+
package
|
|
177
270
|
}
|
|
178
271
|
}
|
|
179
272
|
|
|
273
|
+
fn package_resolution(root: &PackageRoot, path: &str) -> PackageResolution {
|
|
274
|
+
PackageResolution {
|
|
275
|
+
path: normalize_path(&root.package_path.join(path)),
|
|
276
|
+
local: root.local,
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
fn is_optional_pubspec_error(error: &GraphError) -> bool {
|
|
281
|
+
matches!(
|
|
282
|
+
error,
|
|
283
|
+
GraphError::ReadPubspec { .. }
|
|
284
|
+
| GraphError::ParsePubspec { .. }
|
|
285
|
+
| GraphError::WorkspacePattern { .. }
|
|
286
|
+
| GraphError::WorkspaceGlob { .. }
|
|
287
|
+
)
|
|
288
|
+
}
|
|
289
|
+
|
|
180
290
|
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
|
181
291
|
struct PackageConfig {
|
|
182
292
|
#[serde(default)]
|
|
@@ -363,9 +473,17 @@ fn path_dependencies(value: &Value, overrides: Option<&Value>) -> Vec<PathDepend
|
|
|
363
473
|
.filter_map(|section| mapping_field(value, section))
|
|
364
474
|
.flat_map(path_dependencies_from_mapping)
|
|
365
475
|
.collect::<Vec<_>>();
|
|
476
|
+
|
|
366
477
|
let override_source = overrides
|
|
367
478
|
.filter(|value| has_top_level_field(value, "dependency_overrides"))
|
|
368
479
|
.unwrap_or(value);
|
|
480
|
+
if let Some(overrides) = mapping_field(override_source, "dependency_overrides") {
|
|
481
|
+
let override_names = overrides
|
|
482
|
+
.keys()
|
|
483
|
+
.filter_map(Value::as_str)
|
|
484
|
+
.collect::<BTreeSet<_>>();
|
|
485
|
+
dependencies.retain(|dependency| !override_names.contains(dependency.name.as_str()));
|
|
486
|
+
}
|
|
369
487
|
dependencies.extend(
|
|
370
488
|
mapping_field(override_source, "dependency_overrides")
|
|
371
489
|
.into_iter()
|
|
@@ -481,7 +599,81 @@ mod tests {
|
|
|
481
599
|
let packages = PackageMap::discover(fixture.path())?;
|
|
482
600
|
|
|
483
601
|
assert_eq!(packages.names(), vec!["example", "hydrated_bloc"]);
|
|
484
|
-
assert!(
|
|
602
|
+
assert!(
|
|
603
|
+
packages
|
|
604
|
+
.resolve_from(
|
|
605
|
+
&fixture.path().join("lib/hydrated_bloc.dart"),
|
|
606
|
+
"bloc",
|
|
607
|
+
"bloc.dart"
|
|
608
|
+
)
|
|
609
|
+
.is_none()
|
|
610
|
+
);
|
|
611
|
+
|
|
612
|
+
Ok(())
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
#[test]
|
|
616
|
+
fn optional_nested_pubspec_parse_failure_does_not_poison_later_strict_discovery()
|
|
617
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
618
|
+
let fixture = TempDir::new()?;
|
|
619
|
+
write(
|
|
620
|
+
&fixture,
|
|
621
|
+
"app/pubspec.yaml",
|
|
622
|
+
"name: app\ndependencies:\n bad:\n path: ../scratch/bad\n",
|
|
623
|
+
)?;
|
|
624
|
+
write(&fixture, "scratch/bad/pubspec.yaml", "name: [\n")?;
|
|
625
|
+
|
|
626
|
+
let mut packages = PackageMap::default();
|
|
627
|
+
let mut visited = BTreeSet::new();
|
|
628
|
+
packages.discover_nested_pubspecs(&fixture.path().join("scratch"), &mut visited)?;
|
|
629
|
+
|
|
630
|
+
match packages.discover_pubspec(&fixture.path().join("app"), &mut visited) {
|
|
631
|
+
Err(GraphError::ParsePubspec { path, .. }) => {
|
|
632
|
+
assert_eq!(path, fixture.path().join("scratch/bad/pubspec.yaml"));
|
|
633
|
+
}
|
|
634
|
+
other => panic!("expected strict parse error, got {other:?}"),
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
Ok(())
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
#[test]
|
|
641
|
+
fn optional_nested_pubspec_workspace_errors_are_ignored_without_partial_owner()
|
|
642
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
643
|
+
let fixture = TempDir::new()?;
|
|
644
|
+
write(&fixture, "pubspec.yaml", "name: app\n")?;
|
|
645
|
+
write(
|
|
646
|
+
&fixture,
|
|
647
|
+
"fixtures/bad/pubspec.yaml",
|
|
648
|
+
"name: bad\nworkspace:\n - '[bad'\n",
|
|
649
|
+
)?;
|
|
650
|
+
|
|
651
|
+
let packages = PackageMap::discover(fixture.path())?;
|
|
652
|
+
|
|
653
|
+
assert_eq!(packages.names(), vec!["app"]);
|
|
654
|
+
|
|
655
|
+
Ok(())
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
#[test]
|
|
659
|
+
fn declared_path_dependency_workspace_errors_stay_strict()
|
|
660
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
661
|
+
let fixture = TempDir::new()?;
|
|
662
|
+
write(
|
|
663
|
+
&fixture,
|
|
664
|
+
"pubspec.yaml",
|
|
665
|
+
"name: app\ndependencies:\n bad:\n path: fixtures/bad\n",
|
|
666
|
+
)?;
|
|
667
|
+
write(
|
|
668
|
+
&fixture,
|
|
669
|
+
"fixtures/bad/pubspec.yaml",
|
|
670
|
+
"name: bad\nworkspace:\n - '[bad'\n",
|
|
671
|
+
)?;
|
|
672
|
+
|
|
673
|
+
match PackageMap::discover(fixture.path()) {
|
|
674
|
+
Err(GraphError::WorkspacePattern { .. }) => {}
|
|
675
|
+
other => panic!("expected strict workspace pattern error, got {other:?}"),
|
|
676
|
+
}
|
|
485
677
|
|
|
486
678
|
Ok(())
|
|
487
679
|
}
|
package/src/routes.rs
CHANGED
|
@@ -349,6 +349,11 @@ final router = GoRouter(routes: [
|
|
|
349
349
|
.map(|(path, source)| extract_dart_source(root.join(path), source))
|
|
350
350
|
.collect::<Result<Vec<_>, _>>()?;
|
|
351
351
|
let graph = crate::build_module_graph(&root, &files)?;
|
|
352
|
-
Ok(ScannedProject {
|
|
352
|
+
Ok(ScannedProject {
|
|
353
|
+
scan_roots: vec![root.clone()],
|
|
354
|
+
root,
|
|
355
|
+
files,
|
|
356
|
+
graph,
|
|
357
|
+
})
|
|
353
358
|
}
|
|
354
359
|
}
|
package/src/scan.rs
CHANGED
|
@@ -17,8 +17,10 @@ use crate::{
|
|
|
17
17
|
/// Parsed Dart files plus their resolved module graph.
|
|
18
18
|
#[derive(Debug)]
|
|
19
19
|
pub struct ScannedProject {
|
|
20
|
-
///
|
|
20
|
+
/// Primary root used for dependency resolution and root-relative output.
|
|
21
21
|
pub root: PathBuf,
|
|
22
|
+
/// Local roots included in discovery, sorted for deterministic entrypoint analysis.
|
|
23
|
+
pub scan_roots: Vec<PathBuf>,
|
|
22
24
|
/// Extracted Dart files, sorted by path for deterministic output.
|
|
23
25
|
pub files: Vec<DartFile>,
|
|
24
26
|
/// Directed graph built from the extracted files.
|
|
@@ -115,7 +117,8 @@ pub enum ScanError {
|
|
|
115
117
|
Graph(#[from] GraphError),
|
|
116
118
|
}
|
|
117
119
|
|
|
118
|
-
/// Discover, parse, and graph all `.dart` files under `root
|
|
120
|
+
/// Discover, parse, and graph all `.dart` files under `root` and discovered
|
|
121
|
+
/// local package roots.
|
|
119
122
|
///
|
|
120
123
|
/// Parsing is parallelized with Rayon after a deterministic filesystem walk.
|
|
121
124
|
///
|
|
@@ -126,7 +129,8 @@ pub fn scan_project(root: impl AsRef<Path>) -> Result<ScannedProject, ScanError>
|
|
|
126
129
|
scan_project_with_options(root, &ScanOptions::default())
|
|
127
130
|
}
|
|
128
131
|
|
|
129
|
-
/// Discover, parse, and graph all non-ignored `.dart` files under `root
|
|
132
|
+
/// Discover, parse, and graph all non-ignored `.dart` files under `root` and
|
|
133
|
+
/// discovered local package roots.
|
|
130
134
|
///
|
|
131
135
|
/// Parsing is parallelized with Rayon after a deterministic filesystem walk.
|
|
132
136
|
///
|
|
@@ -139,9 +143,10 @@ pub fn scan_project_with_options(
|
|
|
139
143
|
) -> Result<ScannedProject, ScanError> {
|
|
140
144
|
let root = normalize_scan_root(root.as_ref())?;
|
|
141
145
|
let ignore_matcher = IgnoreMatcher::new(&options.ignore_patterns)?;
|
|
146
|
+
let package_map = PackageMap::discover(&root)?;
|
|
142
147
|
let mut scan_roots = BTreeSet::new();
|
|
143
148
|
scan_roots.insert(root.clone());
|
|
144
|
-
scan_roots.extend(
|
|
149
|
+
scan_roots.extend(package_map.local_roots());
|
|
145
150
|
|
|
146
151
|
let mut paths = BTreeSet::new();
|
|
147
152
|
for scan_root in &scan_roots {
|
|
@@ -163,7 +168,12 @@ pub fn scan_project_with_options(
|
|
|
163
168
|
},
|
|
164
169
|
)?;
|
|
165
170
|
|
|
166
|
-
Ok(ScannedProject {
|
|
171
|
+
Ok(ScannedProject {
|
|
172
|
+
root,
|
|
173
|
+
scan_roots: scan_roots.into_iter().collect(),
|
|
174
|
+
files,
|
|
175
|
+
graph,
|
|
176
|
+
})
|
|
167
177
|
}
|
|
168
178
|
|
|
169
179
|
fn normalize_scan_root(root: &Path) -> Result<PathBuf, ScanError> {
|
package/src/security/detect.rs
CHANGED
|
@@ -2,7 +2,7 @@ use std::path::Path;
|
|
|
2
2
|
|
|
3
3
|
use super::{DetectedSecurityCandidate, SecurityCategory, SecurityConfidence, SecurityOccurrence};
|
|
4
4
|
use crate::Location;
|
|
5
|
-
use crate::generated::is_generated_dart_path;
|
|
5
|
+
use crate::generated::{is_flutterfire_options_path, is_generated_dart_path};
|
|
6
6
|
|
|
7
7
|
pub(super) fn detect_in_source(path: &Path, source: &str) -> Vec<DetectedSecurityCandidate> {
|
|
8
8
|
let mut candidates = Vec::new();
|
|
@@ -24,7 +24,7 @@ pub(super) fn is_ignored_path(path: &Path) -> bool {
|
|
|
24
24
|
}) {
|
|
25
25
|
return true;
|
|
26
26
|
}
|
|
27
|
-
is_generated_dart_path(path)
|
|
27
|
+
is_generated_dart_path(path) && !is_flutterfire_options_path(path)
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
fn detect_hardcoded_secrets(
|
|
@@ -59,9 +59,14 @@ fn detect_hardcoded_secrets(
|
|
|
59
59
|
} else {
|
|
60
60
|
has_secret_like_name(line)
|
|
61
61
|
};
|
|
62
|
+
let secret_binding_name = if firebase_options_literal {
|
|
63
|
+
secret_name
|
|
64
|
+
} else {
|
|
65
|
+
literal_secret_binding_context(source, literal.index)
|
|
66
|
+
};
|
|
62
67
|
if is_module_uri_directive_line(line)
|
|
63
68
|
|| firebase_api_key_context(source, literal.index)
|
|
64
|
-
|| (!secret_value && benign_secret_named_literal(&literal.value))
|
|
69
|
+
|| (!secret_value && benign_secret_named_literal(&literal.value, secret_binding_name))
|
|
65
70
|
|| (!secret_value && firebase_options_literal && !secret_name)
|
|
66
71
|
|| google_app_id_context(source, literal.index)
|
|
67
72
|
|| (!secret_value && literal_looks_like_storage_key(&literal.value))
|
|
@@ -539,6 +544,48 @@ fn firebase_secret_name_context(source: &str, index: usize) -> bool {
|
|
|
539
544
|
literal_argument_context(source, index).is_some_and(has_secret_like_name)
|
|
540
545
|
}
|
|
541
546
|
|
|
547
|
+
fn literal_secret_binding_context(source: &str, index: usize) -> bool {
|
|
548
|
+
literal_binding_identifier(source, index).is_some_and(secret_binding_identifier)
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
fn literal_binding_identifier(source: &str, index: usize) -> Option<&str> {
|
|
552
|
+
let context = literal_context(source, index)?;
|
|
553
|
+
for separator in ['=', ':'] {
|
|
554
|
+
let Some((candidate, rest)) = context.rsplit_once(separator) else {
|
|
555
|
+
continue;
|
|
556
|
+
};
|
|
557
|
+
if rest.trim().is_empty() {
|
|
558
|
+
return trailing_identifier(candidate);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
None
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
fn secret_binding_identifier(identifier: &str) -> bool {
|
|
565
|
+
let lower = identifier.to_ascii_lowercase();
|
|
566
|
+
[
|
|
567
|
+
"token",
|
|
568
|
+
"secret",
|
|
569
|
+
"jwt",
|
|
570
|
+
"privatekey",
|
|
571
|
+
"private_key",
|
|
572
|
+
"clientsecret",
|
|
573
|
+
"client_secret",
|
|
574
|
+
"refreshtoken",
|
|
575
|
+
"refresh_token",
|
|
576
|
+
"accesstoken",
|
|
577
|
+
"access_token",
|
|
578
|
+
"bearer",
|
|
579
|
+
"authorization",
|
|
580
|
+
"apikey",
|
|
581
|
+
"api_key",
|
|
582
|
+
"signingkey",
|
|
583
|
+
"signing_key",
|
|
584
|
+
]
|
|
585
|
+
.iter()
|
|
586
|
+
.any(|suffix| lower == *suffix || lower.ends_with(suffix))
|
|
587
|
+
}
|
|
588
|
+
|
|
542
589
|
fn named_argument_context(source: &str, index: usize, name: &str) -> bool {
|
|
543
590
|
let Some(context) = literal_argument_context(source, index) else {
|
|
544
591
|
return false;
|
|
@@ -1026,13 +1073,16 @@ const RESET_OR_RECOVERY_PATH_MARKERS: &[&str] = &[
|
|
|
1026
1073
|
"recover-password",
|
|
1027
1074
|
];
|
|
1028
1075
|
|
|
1029
|
-
fn benign_secret_named_literal(value: &str) -> bool {
|
|
1076
|
+
fn benign_secret_named_literal(value: &str, secret_binding_name: bool) -> bool {
|
|
1030
1077
|
let route_or_reset_url =
|
|
1031
1078
|
literal_looks_like_route_path(value) || literal_looks_like_reset_or_recovery_url(value);
|
|
1079
|
+
let benign_copy = literal_looks_like_user_facing_copy(value)
|
|
1080
|
+
|| literal_looks_like_validation_copy(value)
|
|
1081
|
+
|| literal_looks_like_operational_copy(value);
|
|
1032
1082
|
(route_or_reset_url
|
|
1033
1083
|
&& !literal_has_secret_like_url_parameter(value)
|
|
1034
1084
|
&& !literal_has_secret_like_reset_path_segment(value))
|
|
1035
|
-
||
|
|
1085
|
+
|| (benign_copy && !secret_binding_name && !literal_has_concrete_token_like_segment(value))
|
|
1036
1086
|
}
|
|
1037
1087
|
|
|
1038
1088
|
fn literal_looks_like_route_path(value: &str) -> bool {
|
|
@@ -1141,15 +1191,26 @@ fn literal_looks_like_user_facing_copy(value: &str) -> bool {
|
|
|
1141
1191
|
.any(|word| lower.contains(word))
|
|
1142
1192
|
&& [
|
|
1143
1193
|
"change password",
|
|
1194
|
+
"current password",
|
|
1195
|
+
"new password",
|
|
1144
1196
|
"invalid email or password",
|
|
1145
1197
|
"forgot password",
|
|
1198
|
+
"forgot your password",
|
|
1146
1199
|
"reset password",
|
|
1200
|
+
"password reset",
|
|
1147
1201
|
"enter password",
|
|
1148
1202
|
"confirm password",
|
|
1149
1203
|
"password must",
|
|
1150
1204
|
"password is",
|
|
1205
|
+
"password should",
|
|
1206
|
+
"password cannot",
|
|
1207
|
+
"passwords do not match",
|
|
1208
|
+
"password do not match",
|
|
1209
|
+
"password field",
|
|
1210
|
+
"password requirements",
|
|
1151
1211
|
"token expired",
|
|
1152
1212
|
"invalid token",
|
|
1213
|
+
"cloud function error",
|
|
1153
1214
|
]
|
|
1154
1215
|
.iter()
|
|
1155
1216
|
.any(|phrase| lower.contains(phrase))
|
|
@@ -1158,9 +1219,81 @@ fn literal_looks_like_user_facing_copy(value: &str) -> bool {
|
|
|
1158
1219
|
|| character.is_ascii_whitespace()
|
|
1159
1220
|
|| matches!(
|
|
1160
1221
|
character,
|
|
1161
|
-
'.' | ','
|
|
1222
|
+
'.' | ','
|
|
1223
|
+
| '!'
|
|
1224
|
+
| '?'
|
|
1225
|
+
| ':'
|
|
1226
|
+
| ';'
|
|
1227
|
+
| '\''
|
|
1228
|
+
| '"'
|
|
1229
|
+
| '-'
|
|
1230
|
+
| '_'
|
|
1231
|
+
| '/'
|
|
1232
|
+
| '|'
|
|
1233
|
+
| '('
|
|
1234
|
+
| ')'
|
|
1235
|
+
)
|
|
1236
|
+
})
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
fn literal_looks_like_validation_copy(value: &str) -> bool {
|
|
1240
|
+
let trimmed = value.trim();
|
|
1241
|
+
let lower = trimmed.to_ascii_lowercase();
|
|
1242
|
+
trimmed.len() <= 120
|
|
1243
|
+
&& trimmed.contains(char::is_whitespace)
|
|
1244
|
+
&& ((lower.contains("use at least") && lower.contains("characters"))
|
|
1245
|
+
|| (lower.contains("reset link") && lower.contains("invalid or expired")))
|
|
1246
|
+
&& trimmed.chars().all(|character| {
|
|
1247
|
+
character.is_ascii_alphanumeric()
|
|
1248
|
+
|| character.is_ascii_whitespace()
|
|
1249
|
+
|| matches!(
|
|
1250
|
+
character,
|
|
1251
|
+
'.' | ',' | '!' | '?' | ':' | ';' | '\'' | '"' | '-' | '_' | '/' | '|'
|
|
1252
|
+
)
|
|
1253
|
+
})
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
fn literal_looks_like_operational_copy(value: &str) -> bool {
|
|
1257
|
+
let trimmed = value.trim();
|
|
1258
|
+
let lower = trimmed.to_ascii_lowercase();
|
|
1259
|
+
trimmed.len() <= 120
|
|
1260
|
+
&& trimmed.contains(char::is_whitespace)
|
|
1261
|
+
&& [
|
|
1262
|
+
"token issued",
|
|
1263
|
+
"pre-fetch token",
|
|
1264
|
+
"token notifications active",
|
|
1265
|
+
"updating password",
|
|
1266
|
+
]
|
|
1267
|
+
.iter()
|
|
1268
|
+
.any(|phrase| lower.contains(phrase))
|
|
1269
|
+
&& trimmed.chars().all(|character| !character.is_control())
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
fn literal_has_concrete_token_like_segment(value: &str) -> bool {
|
|
1273
|
+
value
|
|
1274
|
+
.split(|character: char| {
|
|
1275
|
+
character.is_ascii_whitespace()
|
|
1276
|
+
|| matches!(
|
|
1277
|
+
character,
|
|
1278
|
+
':' | ';'
|
|
1279
|
+
| ','
|
|
1280
|
+
| '='
|
|
1281
|
+
| '&'
|
|
1282
|
+
| '?'
|
|
1283
|
+
| '#'
|
|
1284
|
+
| '\''
|
|
1285
|
+
| '"'
|
|
1286
|
+
| '('
|
|
1287
|
+
| ')'
|
|
1288
|
+
| '['
|
|
1289
|
+
| ']'
|
|
1290
|
+
| '{'
|
|
1291
|
+
| '}'
|
|
1292
|
+
| '<'
|
|
1293
|
+
| '>'
|
|
1162
1294
|
)
|
|
1163
1295
|
})
|
|
1296
|
+
.any(concrete_secret_like_path_segment)
|
|
1164
1297
|
}
|
|
1165
1298
|
|
|
1166
1299
|
fn has_secret_shape(value: &str) -> bool {
|
|
@@ -1178,6 +1311,7 @@ fn has_secret_shape(value: &str) -> bool {
|
|
|
1178
1311
|
fn jwt_like(value: &str) -> bool {
|
|
1179
1312
|
let parts = value.split('.').collect::<Vec<_>>();
|
|
1180
1313
|
parts.len() == 3
|
|
1314
|
+
&& parts[0].starts_with("eyJ")
|
|
1181
1315
|
&& parts
|
|
1182
1316
|
.iter()
|
|
1183
1317
|
.all(|part| part.len() >= 10 && is_base64ish(part))
|