agentsec-core 0.3.0

AgentSec core library — scan / web / paste logic, pure Rust
Documentation
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
//! BlackList check: classify installed MCP server names against a
//! [`crate::registry::Registry`] of known-good entries.
//!
//! This is a **demo-level** signature check: it reads MCP server names
//! from `.mcp.json` (cwd) and `.claude.json` (under [`crate::Paths::user_home`])
//! at call time, then assigns each name one of three verdicts:
//!
//! - [`UnknownVerdictKind::KnownGood`] — exact name match in the registry.
//! - [`UnknownVerdictKind::LikelyTyposquat`] — Levenshtein distance == 1 to a
//!   registry entry (not identical).  High-signal, warn severity.
//! - [`UnknownVerdictKind::InformationalTyposquat`] — Levenshtein distance == 2
//!   to a registry entry (not identical, and name length > 3).  Lower-signal,
//!   info severity.
//! - [`UnknownVerdictKind::Unknown`] — no match; the user has installed
//!   an MCP server AgentSec doesn't recognise. This is **neutral**, not a
//!   block — it just surfaces "we've never seen this".
//!
//! The classify step does not consume the inventory snapshot — it
//! re-reads the source JSON because the snapshot only stores hashes, not
//! the full structure.
//!
//! [`classify`] groups rows by `name`, deduplicates across files, and
//! returns one [`UnknownVerdict`] per distinct server name with `count`
//! (number of config files it appeared in) and `paths` (sorted list of
//! those files).

use std::collections::{BTreeMap, BTreeSet};
use std::fs;

use serde::{Deserialize, Serialize};

use crate::Paths;
use crate::platform::PlatformProbe;
use crate::registry::Registry;

const TYPOSQUAT_DISTANCE_MAX: usize = 2;

/// One row of [`classify`]'s aggregated output.
///
/// A single `name` may appear in multiple config files.  `count` is the
/// number of distinct files and `paths` lists them in sorted order.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct UnknownVerdict {
    /// MCP server name (the key under `mcpServers.<name>`).
    pub name: String,
    /// Classification of `name` against the registry.
    pub verdict: UnknownVerdictKind,
    /// Free-text rationale, suitable for one-line surface in the Markdown
    /// report. Echoes the registry [`crate::registry::RegistryEntry::source`]
    /// when relevant.
    pub reason: String,
    /// Number of config files this name was found in.
    pub count: usize,
    /// Sorted list of config files this name was found in.
    pub paths: Vec<String>,
}

/// Classification levels.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum UnknownVerdictKind {
    /// Exact match in the registry.
    KnownGood,
    /// Levenshtein distance == 1 to a registry entry (warn severity).
    LikelyTyposquat { closest: String, distance: usize },
    /// Levenshtein distance == 2 to a registry entry (info severity).
    /// Suppressed for names with ≤ 3 characters.
    InformationalTyposquat { closest: String, distance: usize },
    /// No match at any distance ≤ 2 (or suppressed short-name case).
    Unknown,
}

/// Read MCP server names from `.mcp.json` and `.claude.json`, classify each
/// one against `registry`, and return one aggregated row per distinct name.
///
/// File-read failures are silently skipped — a missing `.mcp.json` is the
/// common case, and a malformed `.claude.json` shouldn't fail the whole
/// classification.
///
/// Output is sorted by `name` for stable reporting.
pub fn classify(
    paths: &Paths,
    registry: &Registry,
    probes: &[&dyn PlatformProbe],
) -> Vec<UnknownVerdict> {
    // Collect (path, name) pairs — BTreeSet gives dedup + sorted order.
    let mut named: BTreeSet<(String, String)> = BTreeSet::new();

    // Enumerate MCP config files via each registered platform probe; for
    // each existing file, parse its content with that probe's
    // decomposition rules. File-read / JSON-parse failures are silently
    // swallowed so a single bad config does not fail the whole
    // classification (a missing `.mcp.json` is the common case).
    for probe in probes {
        for path in probe.mcp_config_paths(paths) {
            let Ok(body) = fs::read_to_string(&path) else {
                continue;
            };
            let Ok(entries) = probe.extract_mcp_servers(&body, &path) else {
                continue;
            };
            for entry in entries {
                named.insert((entry.display_path, entry.name));
            }
        }
    }

    // Group by name: BTreeMap keeps names sorted.
    let mut by_name: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
    for (path, name) in named {
        by_name.entry(name).or_default().insert(path);
    }

    by_name
        .into_iter()
        .map(|(name, path_set)| {
            let paths_vec: Vec<String> = path_set.into_iter().collect();
            let count = paths_vec.len();
            // Use the first path as the representative for classification;
            // the verdict depends only on the name + registry.
            let rep = paths_vec.first().map_or("", String::as_str);
            let mut v = classify_one(rep, &name, registry);
            v.count = count;
            v.paths = paths_vec;
            v
        })
        .collect()
}

fn classify_one(path: &str, name: &str, registry: &Registry) -> UnknownVerdict {
    if let Some(entry) = registry.get(name) {
        return UnknownVerdict {
            name: name.to_string(),
            verdict: UnknownVerdictKind::KnownGood,
            reason: format!("registered (source: {})", entry.source),
            count: 1,
            paths: vec![path.to_string()],
        };
    }
    if let Some((entry, distance)) = registry.closest(name, TYPOSQUAT_DISTANCE_MAX) {
        // Suppress distance-2 hits for very short names (≤ 3 chars) to avoid
        // false positives like "git" → "filesystem" (not a real threat model).
        let char_count = name.chars().count();
        if distance == 2 && char_count <= 3 {
            // Fall through to Unknown below.
        } else {
            let verdict = if distance == 1 {
                UnknownVerdictKind::LikelyTyposquat {
                    closest: entry.name.clone(),
                    distance,
                }
            } else {
                UnknownVerdictKind::InformationalTyposquat {
                    closest: entry.name.clone(),
                    distance,
                }
            };
            return UnknownVerdict {
                name: name.to_string(),
                verdict,
                reason: format!("close to `{}` (distance {})", entry.name, distance),
                count: 1,
                paths: vec![path.to_string()],
            };
        }
    }
    UnknownVerdict {
        name: name.to_string(),
        verdict: UnknownVerdictKind::Unknown,
        reason: "not in registry".to_string(),
        count: 1,
        paths: vec![path.to_string()],
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::platform::McpServerEntry;
    use crate::registry::RegistryEntry;
    use std::path::{Path, PathBuf};

    /// Test-only Claude Code probe stub. Mirrors just the bits these
    /// `classify`-based tests exercise: it points at the home-rooted
    /// `.claude.json` and parses `mcpServers` + per-project blocks.
    /// The real probe lives in `agentsec-platform-claude`; we don't
    /// take it as a dev-dependency here because the cargo cycle
    /// (platform-claude → core → platform-claude) makes cargo treat
    /// the trait as two separate instances at lib-test build time.
    struct TestClaudeProbe;

    impl PlatformProbe for TestClaudeProbe {
        fn id(&self) -> &'static str {
            "claude-code-test"
        }
        fn target_roots(&self, _paths: &Paths) -> Vec<(String, PathBuf)> {
            Vec::new()
        }
        fn mcp_config_paths(&self, paths: &Paths) -> Vec<PathBuf> {
            vec![
                paths.user_home.join(".claude.json"),
                PathBuf::from(".mcp.json"),
            ]
        }
        fn extract_mcp_servers(
            &self,
            content: &str,
            path: &Path,
        ) -> crate::error::Result<Vec<McpServerEntry>> {
            let json: serde_json::Value = serde_json::from_str(content)?;
            let display = path.display().to_string();
            let mut out = Vec::new();
            if let Some(top) = json
                .get("mcpServers")
                .and_then(serde_json::Value::as_object)
            {
                for name in top.keys() {
                    out.push(McpServerEntry {
                        name: name.clone(),
                        display_path: display.clone(),
                    });
                }
            }
            if path
                .file_name()
                .and_then(|f| f.to_str())
                .is_some_and(|n| n == ".claude.json")
            {
                if let Some(projects) = json.get("projects").and_then(serde_json::Value::as_object)
                {
                    for (proj_name, proj_val) in projects {
                        if let Some(servers) = proj_val
                            .get("mcpServers")
                            .and_then(serde_json::Value::as_object)
                        {
                            for name in servers.keys() {
                                let scoped = format!("{display}#projects.{proj_name}.mcpServers");
                                out.push(McpServerEntry {
                                    name: name.clone(),
                                    display_path: scoped,
                                });
                            }
                        }
                    }
                }
            }
            Ok(out)
        }
    }

    fn claude_only(probe: &TestClaudeProbe) -> [&dyn PlatformProbe; 1] {
        [probe as &dyn PlatformProbe]
    }

    fn write_claude_json(tmp: &tempfile::TempDir, body: &str) {
        std::fs::write(tmp.path().join(".claude.json"), body).unwrap();
    }

    fn paths_for(tmp: &tempfile::TempDir) -> Paths {
        Paths {
            home: tmp.path().to_path_buf(),
            user_home: tmp.path().to_path_buf(),
        }
    }

    fn small_registry() -> Registry {
        Registry::from_entries(vec![
            RegistryEntry {
                name: "filesystem".into(),
                source: "test-source".into(),
            },
            RegistryEntry {
                name: "github".into(),
                source: "test-source".into(),
            },
        ])
    }

    #[test]
    fn known_good_is_recognised() {
        let tmp = tempfile::tempdir().unwrap();
        write_claude_json(&tmp, r#"{"mcpServers":{"filesystem":{"command":"x"}}}"#);
        let verdicts = classify(
            &paths_for(&tmp),
            &small_registry(),
            &claude_only(&TestClaudeProbe),
        );
        assert_eq!(verdicts.len(), 1);
        assert_eq!(verdicts[0].name, "filesystem");
        assert_eq!(verdicts[0].verdict, UnknownVerdictKind::KnownGood);
    }

    #[test]
    fn typosquat_is_flagged() {
        let tmp = tempfile::tempdir().unwrap();
        // "filesystme" — transposition of last two chars from "filesystem",
        // Levenshtein distance 2 → InformationalTyposquat.
        write_claude_json(&tmp, r#"{"mcpServers":{"filesystme":{"command":"x"}}}"#);
        let verdicts = classify(
            &paths_for(&tmp),
            &small_registry(),
            &claude_only(&TestClaudeProbe),
        );
        assert_eq!(verdicts.len(), 1);
        match &verdicts[0].verdict {
            UnknownVerdictKind::InformationalTyposquat { closest, distance } => {
                assert_eq!(closest, "filesystem");
                assert_eq!(*distance, 2);
            }
            other => panic!("expected InformationalTyposquat, got {other:?}"),
        }
    }

    #[test]
    fn unknown_name_is_unknown() {
        let tmp = tempfile::tempdir().unwrap();
        write_claude_json(
            &tmp,
            r#"{"mcpServers":{"my-private-tool":{"command":"x"}}}"#,
        );
        let verdicts = classify(
            &paths_for(&tmp),
            &small_registry(),
            &claude_only(&TestClaudeProbe),
        );
        assert_eq!(verdicts.len(), 1);
        assert_eq!(verdicts[0].verdict, UnknownVerdictKind::Unknown);
    }

    #[test]
    fn per_project_servers_are_classified() {
        let tmp = tempfile::tempdir().unwrap();
        write_claude_json(
            &tmp,
            r#"{
                "mcpServers": {"github": {"command":"x"}},
                "projects": {
                    "/some/proj": {
                        "mcpServers": {"my-tool": {"command":"y"}}
                    }
                }
            }"#,
        );
        let verdicts = classify(
            &paths_for(&tmp),
            &small_registry(),
            &claude_only(&TestClaudeProbe),
        );
        assert_eq!(verdicts.len(), 2);
        let names: Vec<&str> = verdicts.iter().map(|v| v.name.as_str()).collect();
        assert!(names.contains(&"github"));
        assert!(names.contains(&"my-tool"));
    }

    #[test]
    fn missing_claude_json_yields_empty_result() {
        let tmp = tempfile::tempdir().unwrap();
        let verdicts = classify(
            &paths_for(&tmp),
            &small_registry(),
            &claude_only(&TestClaudeProbe),
        );
        assert!(verdicts.is_empty());
    }

    #[test]
    fn malformed_json_silently_skipped() {
        let tmp = tempfile::tempdir().unwrap();
        write_claude_json(&tmp, "not even close to JSON");
        let verdicts = classify(
            &paths_for(&tmp),
            &small_registry(),
            &claude_only(&TestClaudeProbe),
        );
        assert!(verdicts.is_empty());
    }

    // ── A-2 heuristic tests ───────────────────────────────────────────────

    #[test]
    fn classify_one_short_name_distance_2_suppressed() {
        // "git" has length 3, closest registry entry is "github" (distance 3)
        // or "filesystem" (distance > 2). Use a custom registry with a
        // 2-distance match to exercise the short-name suppression.
        let reg = Registry::from_entries(vec![RegistryEntry {
            name: "liv".into(),
            source: "test".into(),
        }]);
        // "git" → "liv": levenshtein("git","liv") = 3 → actually no match at max 2.
        // Use "gi" (2 chars) and "github" (distance 5) — too far.
        // Directly test classify_one: name "gxt", registry has "git", distance 2.
        let reg2 = Registry::from_entries(vec![RegistryEntry {
            name: "git".into(),
            source: "test".into(),
        }]);
        // "gxt" vs "git" = distance 1 (transposition: x→i) — actually 1, not 2.
        // Use "ab" vs "cd": distance 2, both len 2 → suppressed.
        let reg3 = Registry::from_entries(vec![RegistryEntry {
            name: "ab".into(),
            source: "test".into(),
        }]);
        let v = classify_one("path", "cd", &reg3);
        // "cd" len=2 ≤ 3, distance 2 → suppressed to Unknown
        assert_eq!(v.verdict, UnknownVerdictKind::Unknown);

        // Sanity: if registry has "ab", "abc" (len 3 ≤ 3) close at distance 2
        // to "axy" → suppressed
        let reg4 = Registry::from_entries(vec![RegistryEntry {
            name: "abc".into(),
            source: "test".into(),
        }]);
        // "axz" vs "abc" = 2 subs → distance 2, len("axz")=3 ≤ 3 → suppressed
        let v2 = classify_one("path", "axz", &reg4);
        assert_eq!(v2.verdict, UnknownVerdictKind::Unknown);

        // Suppress unused-var warning for reg, reg2 by using them.
        let _ = reg;
        let _ = reg2;
    }

    #[test]
    fn classify_severity_split() {
        let reg = Registry::from_entries(vec![RegistryEntry {
            name: "filesystem".into(),
            source: "test".into(),
        }]);
        // distance 1 → LikelyTyposquat
        let v1 = classify_one("p", "filesytem", &reg); // filesytem vs filesystem: 1 missing char
        match &v1.verdict {
            UnknownVerdictKind::LikelyTyposquat { distance, .. } => assert_eq!(*distance, 1),
            other => panic!("expected LikelyTyposquat, got {other:?}"),
        }
        // distance 2, long name → InformationalTyposquat
        let v2 = classify_one("p", "filesystXY", &reg); // 2 substitutions
        match &v2.verdict {
            UnknownVerdictKind::InformationalTyposquat { distance, .. } => {
                assert_eq!(*distance, 2);
            }
            other => panic!("expected InformationalTyposquat, got {other:?}"),
        }
    }

    #[test]
    fn classify_aggregates_dedup_count() {
        use std::fs;
        let tmp = tempfile::tempdir().unwrap();
        let paths = paths_for(&tmp);
        // Write .claude.json with "my-tool" in top-level and in two projects.
        write_claude_json(
            &tmp,
            r#"{
                "mcpServers": {"my-tool": {"command":"x"}},
                "projects": {
                    "/proj1": {"mcpServers": {"my-tool": {"command":"y"}}},
                    "/proj2": {"mcpServers": {"my-tool": {"command":"z"}}}
                }
            }"#,
        );
        let reg = Registry::from_entries(vec![]);
        let verdicts = classify(&paths, &reg, &claude_only(&TestClaudeProbe));
        // All occurrences of "my-tool" are aggregated into one row.
        let v = verdicts.iter().find(|v| v.name == "my-tool").unwrap();
        // 3 occurrences: top-level + 2 project scopes
        assert_eq!(v.count, 3);
        assert_eq!(v.paths.len(), 3);
        // paths are sorted
        assert!(v.paths.windows(2).all(|w| w[0] <= w[1]));

        // Suppress unused import warning
        let _ = fs::metadata(tmp.path());
    }
}