Repository navigation
Expand file tree
/
Copy pathrules.rs
More file actions
708 lines (665 loc) · 26.4 KB
/
Copy pathrules.rs
File metadata and controls
708 lines (665 loc) · 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//! Deterministic rules database — the safety authority.
//!
//! Rules are data, not code: shipped as JSON, versioned, embedded at build
//! time, updatable independently of the binary later. A rule matches path
//! patterns and yields a Category + base Verdict. The risk module may
//! DOWNGRADE a verdict (Safe -> Review) based on evidence; it may never
//! upgrade one (Risky -> Safe). Protected is final.
//!
//! Patterns are globs, matched against the normalized path (lowercased,
//! `\` rewritten to `/`) with `*` stopping at a path separator. That
//! anchoring is the point: a plain substring test made `/tmp/` fire on
//! `/home/user/tmp/tax-return.pdf`, which is user data, not scratch space.
use crate::{Category, GenomeError, Result, Verdict};
use globset::{Glob, GlobBuilder, GlobSet, GlobSetBuilder};
use serde::{Deserialize, Serialize};
use std::sync::OnceLock;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Rule {
pub id: String,
/// Globs matched against the normalized path. `*` matches within one
/// path component, `**` spans components, so `**/node_modules/**`
/// reaches any depth while `/tmp/**` stays at the filesystem root.
pub patterns: Vec<String>,
pub category: Category,
pub verdict: Verdict,
/// Shown to the user as evidence.
pub description: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct RulesDb {
pub version: u32,
pub rules: Vec<Rule>,
/// One compiled matcher per rule, in rule order. Built on the first
/// `classify` and reused after: a home directory is millions of
/// entries, and compiling a glob per entry would cost more than the
/// walk that found them.
///
/// Skipped by serde and rebuilt on demand, so a db that arrives over
/// the wire behaves exactly like one built here.
#[serde(skip)]
matchers: OnceLock<Vec<GlobSet>>,
}
/// Hand-written because `OnceLock<GlobSet>` isn't `Clone`. A clone starts
/// with an empty cache rather than sharing one — the rules are the value,
/// the compiled form is an optimization.
impl Clone for RulesDb {
fn clone(&self) -> Self {
Self {
version: self.version,
rules: self.rules.clone(),
matchers: OnceLock::new(),
}
}
}
impl RulesDb {
/// Starter rules embedded from rules/base.json. Deliberately tiny —
/// growing this database (per-platform, per-app) IS the product work.
pub fn embedded() -> Self {
serde_json::from_str(include_str!("../rules/base.json"))
.expect("embedded rules db must parse")
}
/// Build a db from rules held in memory (tests, future remote rule
/// updates). Same matching path as [`RulesDb::embedded`].
pub fn new(version: u32, rules: Vec<Rule>) -> Self {
Self {
version,
rules,
matchers: OnceLock::new(),
}
}
/// Reject malformed patterns before an externally supplied database is
/// used. `compile` remains tolerant for the embedded matcher path, but a
/// caller loading rules from outside the binary must not report success
/// for a rule that can never match.
pub fn validate(&self) -> Result<()> {
for rule in &self.rules {
// An empty pattern list compiles cleanly into a GlobSet that
// matches nothing, so it fails the same way a bad glob does —
// silently, and only for the rule the author cared about.
if rule.patterns.is_empty() {
return Err(GenomeError::Rules(format!(
"rule '{}' has no patterns, so it can never match",
rule.id
)));
}
for pattern in &rule.patterns {
build_glob(pattern).map_err(|error| {
GenomeError::Rules(format!(
"rule '{}' has invalid glob pattern '{}': {error}",
rule.id, pattern
))
})?;
}
}
Ok(())
}
/// Put the embedded protected rules ahead of an externally supplied
/// database. First-match-wins makes this ordering a safety boundary:
/// custom rules may add coverage, but cannot shadow the system-critical
/// rules shipped with the application.
pub fn with_embedded_protected_rules(self) -> Self {
let mut rules = RulesDb::embedded()
.rules
.into_iter()
.filter(|rule| rule.verdict == Verdict::Protected)
.collect::<Vec<_>>();
rules.extend(self.rules);
Self::new(self.version, rules)
}
/// First matching rule wins; order in the db is priority order.
/// Protected rules are listed first for exactly that reason.
pub fn classify(&self, path: &std::path::Path) -> (Category, Verdict, Option<&Rule>) {
let p = normalize(path);
let candidate = std::path::Path::new(&p);
for (rule, matcher) in self.rules.iter().zip(self.matchers()) {
if matcher.is_match(candidate) {
return (rule.category, rule.verdict, Some(rule));
}
}
(Category::Unknown, Verdict::Review, None)
}
fn matchers(&self) -> &[GlobSet] {
self.matchers
.get_or_init(|| self.rules.iter().map(compile).collect())
}
}
/// Lowercase, `/`-separated. Windows hands us `C:\Users\...`; the rules
/// are written once, in one shape, and the path is bent to fit them.
pub(crate) fn normalize(path: &std::path::Path) -> String {
path.to_string_lossy().replace('\\', "/").to_lowercase()
}
/// `literal_separator` is the whole reason this module compiles globs at
/// all: without it `*` swallows `/`, and `**/target/*/**` would reach back
/// across directories the rule never named.
fn build_glob(pattern: &str) -> std::result::Result<Glob, globset::Error> {
GlobBuilder::new(pattern).literal_separator(true).build()
}
/// A pattern that doesn't compile is dropped, not fatal. The alternative —
/// panicking inside `classify` — would take the whole scan down over one
/// bad line in a rules file that may not even be ours. `patterns_compile`
/// below keeps the shipped db honest.
fn compile(rule: &Rule) -> GlobSet {
let mut builder = GlobSetBuilder::new();
for pattern in &rule.patterns {
match build_glob(pattern) {
Ok(glob) => {
builder.add(glob);
}
Err(e) => tracing::warn!(
rule = %rule.id,
pattern = %pattern,
"ignoring rule pattern that is not a valid glob: {e}"
),
}
}
builder.build().unwrap_or_else(|e| {
tracing::warn!(rule = %rule.id, "rule matches nothing: {e}");
GlobSet::empty()
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn embedded_db_parses_and_protects_driverstore() {
let db = RulesDb::embedded();
let (cat, verdict, _) = db.classify(std::path::Path::new(
"C:/Windows/System32/DriverStore/x.inf",
));
assert_eq!(cat, Category::SystemCritical);
assert_eq!(verdict, Verdict::Protected);
}
#[test]
fn unknown_paths_default_to_review_not_safe() {
let db = RulesDb::embedded();
let (_, verdict, _) = db.classify(std::path::Path::new("/home/user/mystery.bin"));
assert_eq!(verdict, Verdict::Review);
}
#[test]
fn logs_are_reachable() {
let db = RulesDb::embedded();
for path in [
"/var/log/syslog.1",
"/Users/x/Library/Logs/SomeApp/app.log",
"C:\\Users\\x\\AppData\\Local\\CrashDumps\\a.dmp",
] {
let (cat, verdict, _) = db.classify(std::path::Path::new(path));
assert_eq!(cat, Category::Log, "{path}");
assert_eq!(verdict, Verdict::Review, "{path}");
}
}
#[test]
fn installers_are_reachable() {
let db = RulesDb::embedded();
for path in [
"/home/u/Downloads/Diskern_0.1.0_amd64.dmg",
"/home/u/Downloads/node-v22.msi",
"/home/u/Downloads/tool.pkg",
] {
let (cat, verdict, _) = db.classify(std::path::Path::new(path));
assert_eq!(cat, Category::Installer, "{path}");
assert_eq!(verdict, Verdict::Review, "{path}");
}
}
/// The installer patterns are bare extensions, so they match anywhere in
/// a path. First-match-wins is the only thing keeping them from
/// classifying an OS component as a reclaimable download — which makes
/// rule order a safety property, not a stylistic one.
///
/// The installer-cache entries are the ones that bite hardest: `review`
/// is an actionable verdict in the UI, so without a protected rule above
/// `installer-packages` the app would offer to quarantine the files
/// Windows needs to uninstall or repair installed software.
#[test]
fn protected_rules_still_win_over_installer_patterns() {
let db = RulesDb::embedded();
for path in [
"C:/Windows/System32/DriverStore/setup.exe",
"C:/Windows/WinSxS/component.msi",
"C:/Windows/Installer/1a2b3c.msi",
"C:/Windows/Installer/$PatchCache$/Managed/x.msi",
"C:/ProgramData/Package Cache/{guid}/vs_setup.exe",
"C:/Users/x/AppData/Local/Package Cache/{guid}/dotnet.msi",
] {
let (cat, verdict, _) = db.classify(std::path::Path::new(path));
assert_eq!(cat, Category::SystemCritical, "{path}");
assert_eq!(verdict, Verdict::Protected, "{path}");
}
}
/// Every shipped pattern has to compile, because `compile` drops the
/// ones that don't. A protected rule that silently matches nothing is
/// the worst failure this module has.
#[test]
fn every_shipped_pattern_is_a_valid_glob() {
for rule in &RulesDb::embedded().rules {
for pattern in &rule.patterns {
assert!(
build_glob(pattern).is_ok(),
"{}: {pattern} is not a valid glob",
rule.id
);
}
}
}
#[test]
fn invalid_patterns_are_rejected_before_external_use() {
let db = RulesDb::new(
1,
vec![Rule {
id: "broken".into(),
patterns: vec!["[".into()],
category: Category::Unknown,
verdict: Verdict::Review,
description: "invalid test rule".into(),
}],
);
let error = db
.validate()
.expect_err("invalid glob must fail validation");
assert!(error.to_string().contains("broken"));
assert!(error.to_string().contains("invalid glob pattern"));
}
#[test]
fn rules_without_patterns_are_rejected() {
let db = RulesDb::new(
1,
vec![Rule {
id: "empty".into(),
patterns: vec![],
category: Category::Unknown,
verdict: Verdict::Review,
description: "rule with no patterns".into(),
}],
);
let error = db
.validate()
.expect_err("a rule with no patterns must fail validation");
assert!(error.to_string().contains("empty"));
assert!(error.to_string().contains("can never match"));
}
#[test]
fn embedded_protected_rules_precede_external_rules() {
let external = RulesDb::new(
1,
vec![Rule {
id: "unsafe-override".into(),
patterns: vec!["**/*.msi".into()],
category: Category::Installer,
verdict: Verdict::Safe,
description: "must not shadow protected rules".into(),
}],
)
.with_embedded_protected_rules();
let (category, verdict, rule) =
external.classify(std::path::Path::new("/tmp/windows/installer/setup.msi"));
assert_eq!(category, Category::SystemCritical);
assert_eq!(verdict, Verdict::Protected);
assert_eq!(
rule.map(|rule| rule.id.as_str()),
Some("windows-installer-cache")
);
}
/// Issue #41. Under substring matching every one of these matched a
/// rule written for somewhere else on the disk, and `review` is an
/// actionable verdict — the app offered to move them.
#[test]
fn rules_do_not_reach_outside_the_paths_they_name() {
let db = RulesDb::embedded();
for path in [
"/home/user/tmp/tax-return.pdf", // not /tmp
"/home/user/var/log/notes.txt", // not /var/log
"/home/user/var/tmp/notes.txt", // not /var/tmp
"/home/user/project/var/tmp/session.dat",
"/opt/homebrew/var/tmp/formula.lock",
"/home/user/Downloads/holiday.dmgx", // not a .dmg
"/home/user/mytmp/scratch.bin",
] {
let (cat, _, rule) = db.classify(std::path::Path::new(path));
assert_eq!(cat, Category::Unknown, "{path} matched {rule:?}");
}
}
/// The same rules still have to fire where they were meant to.
#[test]
fn rules_still_reach_the_paths_they_do_name() {
let db = RulesDb::embedded();
for (path, expected) in [
("/tmp/build-9a2f/out.o", Category::TempFile),
("/var/tmp/systemd-private/x", Category::TempFile),
("/private/var/tmp/com.apple.launchd/x", Category::TempFile),
(
"C:\\Users\\x\\AppData\\Local\\Temp\\a.tmp",
Category::TempFile,
),
(
"/home/u/proj/node_modules/react/index.js",
Category::BuildArtifact,
),
("/home/u/proj/target/debug/app", Category::BuildArtifact),
(
"/home/u/.cache/pip/wheels/a.whl",
Category::PackageManagerCache,
),
] {
let (cat, _, _) = db.classify(std::path::Path::new(path));
assert_eq!(cat, expected, "{path}");
}
}
#[test]
fn chrome_cache_is_safe_to_remove_on_each_platform() {
let db = RulesDb::embedded();
for path in [
"/home/u/.cache/google-chrome/Default/Cache/data_0",
"C:\\Users\\x\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Cache\\data_0",
"/Users/x/Library/Caches/Google/Chrome/Default/Cache/data_0",
"/Users/x/Library/Caches/Google/Chrome/Default/Code Cache/js/index",
] {
let (cat, verdict, rule) = db.classify(std::path::Path::new(path));
assert_eq!(cat, Category::BrowserCache, "{path} matched {rule:?}");
assert_eq!(verdict, Verdict::Safe, "{path} matched {rule:?}");
}
}
#[test]
fn chrome_profile_data_is_not_safe_to_remove() {
let db = RulesDb::embedded();
let path = "/Users/x/Library/Application Support/Google/Chrome/Default/History";
let (cat, verdict, rule) = db.classify(std::path::Path::new(path));
assert_eq!(cat, Category::Unknown, "{path} matched {rule:?}");
assert_ne!(verdict, Verdict::Safe, "{path} matched {rule:?}");
}
/// Issue #40. `/mozilla/firefox/profiles` covered the whole profile
/// directory, so bookmarks, history, logins and cookies were listed
/// under "Safe to remove" and `quarantine_finding` accepted them.
/// Only cache2 is cache; everything else in the profile is user data.
#[test]
fn firefox_profile_data_is_not_safe_to_remove() {
let db = RulesDb::embedded();
for path in [
"C:\\Users\\x\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\ab12.default\\logins.json",
"C:\\Users\\x\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\ab12.default\\places.sqlite",
"C:\\Users\\x\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\ab12.default\\cookies.sqlite",
"/home/u/.mozilla/firefox/ab12.default/key4.db",
] {
let (_, verdict, rule) = db.classify(std::path::Path::new(path));
assert_ne!(verdict, Verdict::Safe, "{path} matched {rule:?}");
}
}
/// The cache the rule is actually named after still classifies.
#[test]
fn firefox_cache_is_still_safe_to_remove() {
let db = RulesDb::embedded();
for path in [
"/home/u/.cache/mozilla/firefox/ab12.default/cache2/entries/A1B2",
"C:\\Users\\x\\AppData\\Local\\Mozilla\\Firefox\\Profiles\\ab12.default\\cache2\\entries\\A1B2",
"/Users/x/Library/Caches/Firefox/Profiles/ab12.default/cache2/entries/A1B2",
] {
let (cat, verdict, _) = db.classify(std::path::Path::new(path));
assert_eq!(cat, Category::BrowserCache, "{path}");
assert_eq!(verdict, Verdict::Safe, "{path}");
}
}
#[test]
fn npm_cache_is_safe_to_remove() {
let db = RulesDb::embedded();
for path in [
"/home/u/.npm/_cacache/content-v2/sha512/aa/package",
"/Users/u/.npm/_cacache/index-v5/ab/cd",
"C:\\Users\\u\\AppData\\Local\\npm-cache\\_cacache\\content-v2\\sha512\\aa\\package",
] {
let (cat, verdict, rule) = db.classify(std::path::Path::new(path));
assert_eq!(
cat,
Category::PackageManagerCache,
"{path} matched {rule:?}"
);
assert_eq!(verdict, Verdict::Safe, "{path} matched {rule:?}");
}
}
#[test]
fn npm_state_outside_the_download_cache_is_not_marked_safe() {
let db = RulesDb::embedded();
for path in [
"/home/u/.npmrc",
"/home/u/.npm/_logs/2026-09-12-debug.log",
"C:\\Users\\u\\AppData\\Roaming\\npm\\npmrc",
] {
let (cat, verdict, rule) = db.classify(std::path::Path::new(path));
assert_eq!(cat, Category::Unknown, "{path} matched {rule:?}");
assert_ne!(verdict, Verdict::Safe, "{path} matched {rule:?}");
}
}
#[test]
fn pnpm_store_is_safe_to_remove() {
let db = RulesDb::embedded();
for path in [
"/home/u/.local/share/pnpm/store/v3/files/aa/bb",
"/Users/u/Library/pnpm/store/v3/files/aa/bb",
"C:\\Users\\u\\AppData\\Local\\pnpm\\store\\v3\\files\\aa\\bb",
"/home/u/.pnpm-store/v3/files/aa/bb",
] {
let (cat, verdict, rule) = db.classify(std::path::Path::new(path));
assert_eq!(
cat,
Category::PackageManagerCache,
"{path} matched {rule:?}"
);
assert_eq!(verdict, Verdict::Safe, "{path} matched {rule:?}");
}
}
/// The pnpm home directory holds the store, but also the pnpm binary
/// itself and globally installed packages — neither is a download
/// cache.
#[test]
fn pnpm_home_outside_the_store_is_not_marked_safe() {
let db = RulesDb::embedded();
for path in [
"/Users/u/Library/pnpm/pnpm",
"/home/u/.local/share/pnpm/global/5/.npmrc",
] {
let (cat, verdict, rule) = db.classify(std::path::Path::new(path));
assert_eq!(cat, Category::Unknown, "{path} matched {rule:?}");
assert_ne!(verdict, Verdict::Safe, "{path} matched {rule:?}");
}
}
#[test]
fn yarn_cache_is_safe_to_remove() {
let db = RulesDb::embedded();
for path in [
"/home/u/.cache/yarn/v6/npm-react-18.2.0.zip",
"/Users/u/Library/Caches/Yarn/v6/npm-react-18.2.0.zip",
"C:\\Users\\u\\AppData\\Local\\Yarn\\Cache\\v6\\npm-react-18.2.0.zip",
] {
let (cat, verdict, rule) = db.classify(std::path::Path::new(path));
assert_eq!(
cat,
Category::PackageManagerCache,
"{path} matched {rule:?}"
);
assert_eq!(verdict, Verdict::Safe, "{path} matched {rule:?}");
}
}
/// Yarn Berry's Plug'n'Play loader reads packages straight out of the
/// cache archives — a project's `.yarn/cache` or the global `.yarn/berry/cache`
/// — so these paths are `review`, not `safe`: quarantining them breaks
/// imports until `yarn install` restores the cache.
#[test]
fn yarn_pnp_caches_are_review_not_safe() {
let db = RulesDb::embedded();
for path in [
"/home/u/proj/.yarn/cache/react-npm-18.2.0.zip",
"/home/u/.yarn/berry/cache/react-npm-18.2.0.zip",
] {
let (cat, verdict, rule) = db.classify(std::path::Path::new(path));
assert_eq!(
cat,
Category::PackageManagerCache,
"{path} matched {rule:?}"
);
assert_eq!(verdict, Verdict::Review, "{path} matched {rule:?}");
}
}
/// A project's `.yarn` directory keeps the Yarn release binary,
/// plugins and editor SDKs next to `cache` — only `cache` is
/// re-downloaded packages.
#[test]
fn yarn_support_files_outside_the_cache_are_not_marked_safe() {
let db = RulesDb::embedded();
for path in [
"/home/u/proj/.yarn/releases/yarn-4.5.0.cjs",
"/home/u/proj/.yarn/sdks/typescript/bin/tsc",
"/home/u/proj/.yarnrc.yml",
] {
let (cat, verdict, rule) = db.classify(std::path::Path::new(path));
assert_eq!(cat, Category::Unknown, "{path} matched {rule:?}");
assert_ne!(verdict, Verdict::Safe, "{path} matched {rule:?}");
}
}
/// An installed application's own repair binary is not a reclaimable
/// download. `unknown` is the right answer: report::build drops those,
/// so it never reaches the user as an actionable row.
#[test]
fn installed_application_binaries_are_not_installers() {
let db = RulesDb::embedded();
let (cat, _, rule) =
db.classify(std::path::Path::new("C:/Program Files/SomeApp/setup.exe"));
assert_eq!(cat, Category::Unknown);
assert!(rule.is_none());
}
/// Issue #159. One common path per embedded rule, in base.json order —
/// the fixture that fails loudly when a rules edit stops a rule matching
/// where it used to. Asserting the rule id (not just the category)
/// proves the named rule fired rather than some other pattern that
/// happens to produce the same verdict.
///
/// The paths are chosen to reach only their own rule: the WinSxS and
/// Installer-cache fixtures use extensions the installer rule doesn't
/// claim, so they can't pass through `installer-packages` by accident.
const EMBEDDED_RULE_FIXTURES: &[(&str, &str, Category, Verdict)] = &[
(
"C:/Windows/System32/DriverStore/FileRepository/nv_dispi.inf",
"windows-driverstore",
Category::SystemCritical,
Verdict::Protected,
),
(
"C:/Windows/WinSxS/amd64_microsoft-windows-netfx/component.dll",
"windows-winsxs",
Category::SystemCritical,
Verdict::Protected,
),
(
"C:/Windows/Installer/9f3ab2.msp",
"windows-installer-cache",
Category::SystemCritical,
Verdict::Protected,
),
(
"/home/u/.cache/google-chrome/Default/Cache/f_000001",
"chrome-cache",
Category::BrowserCache,
Verdict::Safe,
),
(
"/home/u/.cache/mozilla/firefox/ab12.default/cache2/entries/9F3AB",
"firefox-cache",
Category::BrowserCache,
Verdict::Safe,
),
(
"/home/u/proj/target/release/diskern",
"rust-target",
Category::BuildArtifact,
Verdict::Review,
),
(
"/home/u/proj/node_modules/react/index.js",
"node-modules",
Category::BuildArtifact,
Verdict::Review,
),
(
"C:/Users/u/AppData/Local/pip/cache/wheels/aa/pkg.whl",
"pip-cache",
Category::PackageManagerCache,
Verdict::Safe,
),
(
"/home/u/.npm/_cacache/index-v5/ab/cd/entry",
"npm-cache",
Category::PackageManagerCache,
Verdict::Safe,
),
(
"/home/u/.local/share/pnpm/store/v3/files/aa/bb",
"pnpm-store",
Category::PackageManagerCache,
Verdict::Safe,
),
(
"/Users/u/Library/Caches/Yarn/v6/npm-react-18.2.0.zip",
"yarn-cache",
Category::PackageManagerCache,
Verdict::Safe,
),
(
"/home/u/proj/.yarn/cache/react-npm-18.2.0.zip",
"yarn-pnp-cache",
Category::PackageManagerCache,
Verdict::Review,
),
(
"/var/log/apt/history.log",
"unix-system-logs",
Category::Log,
Verdict::Review,
),
(
"/Users/u/Library/Logs/SomeApp/app.log",
"macos-user-logs",
Category::Log,
Verdict::Review,
),
(
"C:/Users/u/AppData/Local/Microsoft/Windows/WER/ReportQueue/x.wer",
"windows-crash-logs",
Category::Log,
Verdict::Review,
),
(
"/home/u/Downloads/Diskern_0.1.0_amd64.dmg",
"installer-packages",
Category::Installer,
Verdict::Review,
),
(
"C:/Users/u/AppData/Local/Temp/scratch.tmp",
"temp-dirs",
Category::TempFile,
Verdict::Review,
),
];
#[test]
fn each_embedded_rule_classifies_a_common_path() {
let db = RulesDb::embedded();
for &(path, id, category, verdict) in EMBEDDED_RULE_FIXTURES {
let (cat, actual_verdict, rule) = db.classify(std::path::Path::new(path));
assert_eq!(rule.map(|rule| rule.id.as_str()), Some(id), "{path}");
assert_eq!(cat, category, "{path}");
assert_eq!(actual_verdict, verdict, "{path}");
}
}
/// The fixture table only protects the rules it names: a rule added to
/// base.json without a row here is coverage that silently doesn't exist.
#[test]
fn every_embedded_rule_has_a_fixture() {
for rule in &RulesDb::embedded().rules {
assert!(
EMBEDDED_RULE_FIXTURES
.iter()
.any(|(_, id, _, _)| *id == rule.id),
"no fixture path for rule {}",
rule.id
);
}
}
}