dart-decimate 0.0.23 → 0.0.25
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/package.json +1 -1
- package/src/output/suppressions.rs +82 -17
package/Cargo.lock
CHANGED
package/Cargo.toml
CHANGED
package/package.json
CHANGED
|
@@ -13,6 +13,7 @@ pub(super) fn filter_suppressed_findings(
|
|
|
13
13
|
require_reasons: bool,
|
|
14
14
|
) -> Vec<Finding> {
|
|
15
15
|
let mut state = SuppressionState::new(root, files);
|
|
16
|
+
state.record(&findings);
|
|
16
17
|
let mut filtered = findings
|
|
17
18
|
.into_iter()
|
|
18
19
|
.filter(|finding| !state.is_suppressed(finding))
|
|
@@ -30,6 +31,17 @@ struct SuppressionState {
|
|
|
30
31
|
cache: BTreeMap<PathBuf, Option<Vec<String>>>,
|
|
31
32
|
directives: BTreeMap<SuppressionKey, SuppressionDirective>,
|
|
32
33
|
used: BTreeSet<SuppressionKey>,
|
|
34
|
+
reported: Vec<ReportedFinding>,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// The identity a directive needs to decide whether it merely sits on the
|
|
38
|
+
/// wrong line rather than covering nothing at all.
|
|
39
|
+
#[derive(Debug)]
|
|
40
|
+
struct ReportedFinding {
|
|
41
|
+
path: String,
|
|
42
|
+
line: usize,
|
|
43
|
+
rule_id: String,
|
|
44
|
+
kind: FindingKind,
|
|
33
45
|
}
|
|
34
46
|
|
|
35
47
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
|
@@ -43,6 +55,7 @@ struct SuppressionDirective {
|
|
|
43
55
|
column: usize,
|
|
44
56
|
text: String,
|
|
45
57
|
has_reason: bool,
|
|
58
|
+
rules: Vec<String>,
|
|
46
59
|
}
|
|
47
60
|
|
|
48
61
|
impl SuppressionState {
|
|
@@ -62,9 +75,22 @@ impl SuppressionState {
|
|
|
62
75
|
cache,
|
|
63
76
|
directives,
|
|
64
77
|
used: BTreeSet::new(),
|
|
78
|
+
reported: Vec::new(),
|
|
65
79
|
}
|
|
66
80
|
}
|
|
67
81
|
|
|
82
|
+
fn record(&mut self, findings: &[Finding]) {
|
|
83
|
+
self.reported = findings
|
|
84
|
+
.iter()
|
|
85
|
+
.map(|finding| ReportedFinding {
|
|
86
|
+
path: display_path(&self.root, &finding_path(&self.root, &finding.path)),
|
|
87
|
+
line: finding.line,
|
|
88
|
+
rule_id: finding.rule_id.clone(),
|
|
89
|
+
kind: finding.kind,
|
|
90
|
+
})
|
|
91
|
+
.collect();
|
|
92
|
+
}
|
|
93
|
+
|
|
68
94
|
fn is_suppressed(&mut self, finding: &Finding) -> bool {
|
|
69
95
|
let Some(previous_line) = finding.line.checked_sub(2) else {
|
|
70
96
|
return false;
|
|
@@ -94,10 +120,36 @@ impl SuppressionState {
|
|
|
94
120
|
self.directives
|
|
95
121
|
.iter()
|
|
96
122
|
.filter(|(key, _)| !self.used.contains(key))
|
|
97
|
-
.map(|(key, directive)|
|
|
123
|
+
.map(|(key, directive)| {
|
|
124
|
+
stale_finding(key, directive, self.nearest_match(key, directive))
|
|
125
|
+
})
|
|
98
126
|
.collect()
|
|
99
127
|
}
|
|
100
128
|
|
|
129
|
+
/// Where the directive's own rules still fire in the same file. A clone
|
|
130
|
+
/// group anchors on the common token run, so a directive can miss the line
|
|
131
|
+
/// it was written for while the finding it names is very much alive.
|
|
132
|
+
fn nearest_match(
|
|
133
|
+
&self,
|
|
134
|
+
key: &SuppressionKey,
|
|
135
|
+
directive: &SuppressionDirective,
|
|
136
|
+
) -> Option<usize> {
|
|
137
|
+
self.reported
|
|
138
|
+
.iter()
|
|
139
|
+
.filter(|finding| finding.path == key.path)
|
|
140
|
+
.filter(|finding| {
|
|
141
|
+
directive.rules.is_empty()
|
|
142
|
+
|| directive
|
|
143
|
+
.rules
|
|
144
|
+
.iter()
|
|
145
|
+
.any(|rule| rule_matches_kind(rule, &finding.rule_id, finding.kind))
|
|
146
|
+
})
|
|
147
|
+
.map(|finding| finding.line)
|
|
148
|
+
// Ties break toward the lower line, so the message does not depend
|
|
149
|
+
// on the order findings happen to arrive in.
|
|
150
|
+
.min_by_key(|line| (line.abs_diff(key.line), *line))
|
|
151
|
+
}
|
|
152
|
+
|
|
101
153
|
fn missing_reason_findings(&self) -> Vec<Finding> {
|
|
102
154
|
self.directives
|
|
103
155
|
.iter()
|
|
@@ -125,22 +177,35 @@ fn collect_directives(
|
|
|
125
177
|
column: line.find("//").unwrap_or_default(),
|
|
126
178
|
text: line.trim().to_owned(),
|
|
127
179
|
has_reason: suppression.has_reason,
|
|
180
|
+
rules: suppression.rules,
|
|
128
181
|
},
|
|
129
182
|
);
|
|
130
183
|
}
|
|
131
184
|
}
|
|
132
185
|
}
|
|
133
186
|
|
|
134
|
-
fn stale_finding(
|
|
187
|
+
fn stale_finding(
|
|
188
|
+
key: &SuppressionKey,
|
|
189
|
+
directive: &SuppressionDirective,
|
|
190
|
+
nearest: Option<usize>,
|
|
191
|
+
) -> Finding {
|
|
192
|
+
let message = match nearest {
|
|
193
|
+
Some(line) => format!(
|
|
194
|
+
"Suppression covers line {}, but the finding it names is reported on line {line}: {}",
|
|
195
|
+
key.line + 1,
|
|
196
|
+
directive.text
|
|
197
|
+
),
|
|
198
|
+
None => format!(
|
|
199
|
+
"Suppression no longer matches a finding: {}",
|
|
200
|
+
directive.text
|
|
201
|
+
),
|
|
202
|
+
};
|
|
135
203
|
Finding {
|
|
136
204
|
rule_id: "dart-decimate/stale-suppression".to_owned(),
|
|
137
205
|
fingerprint: Some(format!("stale-suppression:{}:{}", key.path, key.line)),
|
|
138
206
|
kind: FindingKind::StaleSuppression,
|
|
139
207
|
severity: Severity::Error,
|
|
140
|
-
message
|
|
141
|
-
"Suppression no longer matches a finding: {}",
|
|
142
|
-
directive.text
|
|
143
|
-
),
|
|
208
|
+
message,
|
|
144
209
|
path: key.path.clone(),
|
|
145
210
|
line: key.line,
|
|
146
211
|
column: directive.column,
|
|
@@ -259,21 +324,21 @@ fn split_reason(rest: &str) -> (&str, Option<&str>) {
|
|
|
259
324
|
}
|
|
260
325
|
|
|
261
326
|
fn rule_matches(rule: &str, finding: &Finding) -> bool {
|
|
327
|
+
rule_matches_kind(rule, &finding.rule_id, finding.kind)
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
fn rule_matches_kind(rule: &str, rule_id: &str, kind: FindingKind) -> bool {
|
|
262
331
|
rule == "all"
|
|
263
|
-
|| rule ==
|
|
264
|
-
||
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
.next()
|
|
268
|
-
.is_some_and(|rule_id| rule == rule_id)
|
|
269
|
-
|| rule == kind_key(finding.kind)
|
|
270
|
-
|| security_rule_matches(rule, finding)
|
|
332
|
+
|| rule == rule_id
|
|
333
|
+
|| rule_id.rsplit('/').next().is_some_and(|tail| rule == tail)
|
|
334
|
+
|| rule == kind_key(kind)
|
|
335
|
+
|| security_rule_matches(rule, rule_id, kind)
|
|
271
336
|
}
|
|
272
337
|
|
|
273
|
-
fn security_rule_matches(rule: &str,
|
|
274
|
-
|
|
338
|
+
fn security_rule_matches(rule: &str, rule_id: &str, kind: FindingKind) -> bool {
|
|
339
|
+
kind == FindingKind::SecurityCandidate
|
|
275
340
|
&& (rule == "security-sink"
|
|
276
|
-
|| (rule == "hardcoded-secret" &&
|
|
341
|
+
|| (rule == "hardcoded-secret" && rule_id.ends_with("hardcoded-secret")))
|
|
277
342
|
}
|
|
278
343
|
|
|
279
344
|
const fn kind_key(kind: FindingKind) -> &'static str {
|