cc-audit 3.2.14

Security auditor for Claude Code skills, hooks, and MCP servers
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
//! CVE database scanning for dependency vulnerabilities.

use crate::{CveDatabase, DirectoryWalker, Finding, IgnoreFilter, WalkConfig};
use std::fs;
use std::path::Path;
use tracing::{debug, info};

/// Files that may contain version information for CVE checking.
const CVE_RELEVANT_FILES: &[&str] = &[
    "package.json",
    "package-lock.json",
    "extensions.json",
    "mcp.json",
    "mcp_config.json",
];

/// Scan a path for CVE vulnerabilities in dependencies.
///
/// The `ignore_filter` parameter is used to skip files/directories that match
/// the ignore patterns configured in `.cc-audit.yaml`.
pub fn scan_path_with_cve_db(
    path: &Path,
    db: &CveDatabase,
    ignore_filter: &IgnoreFilter,
) -> Vec<Finding> {
    let mut findings = Vec::new();

    if path.is_file() {
        if !ignore_filter.is_ignored(path)
            && let Some(name) = path.file_name().and_then(|n| n.to_str())
            && CVE_RELEVANT_FILES.contains(&name)
            && let Ok(content) = fs::read_to_string(path)
        {
            debug!(path = %path.display(), "Checking file for CVE vulnerabilities");
            findings.extend(check_content_for_cves(&content, path, db));
        }
    } else if path.is_dir() {
        debug!(path = %path.display(), "Scanning directory for CVE vulnerabilities");
        let walker = DirectoryWalker::new(WalkConfig::default());
        for file_path in walker.walk_single(path) {
            if !ignore_filter.is_ignored(&file_path)
                && let Some(name) = file_path.file_name().and_then(|n| n.to_str())
                && CVE_RELEVANT_FILES.contains(&name)
                && let Ok(content) = fs::read_to_string(&file_path)
            {
                findings.extend(check_content_for_cves(&content, &file_path, db));
            }
        }
    }

    findings
}

/// Check file content for known CVEs.
fn check_content_for_cves(content: &str, path: &Path, db: &CveDatabase) -> Vec<Finding> {
    let mut findings = Vec::new();
    let path_str = path.display().to_string();

    // Try to parse as JSON for structured version extraction
    if let Ok(json) = serde_json::from_str::<serde_json::Value>(content) {
        // Check for npm packages in dependencies
        for dep_key in ["dependencies", "devDependencies", "peerDependencies"] {
            if let Some(deps) = json.get(dep_key).and_then(|d| d.as_object()) {
                for (package, version_val) in deps {
                    if let Some(version) = version_val.as_str() {
                        // Extract version number (remove ^, ~, etc.)
                        let clean_version =
                            version.trim_start_matches(|c: char| !c.is_ascii_digit());

                        // Check for known vulnerable packages
                        // mcp-inspector -> anthropic/mcp-inspector
                        if package == "mcp-inspector" || package == "@anthropic/mcp-inspector" {
                            findings.extend(db.create_findings(
                                "anthropic",
                                "mcp-inspector",
                                clean_version,
                                &path_str,
                                1,
                            ));
                        }
                        // mcp-remote -> geelen/mcp-remote
                        if package == "mcp-remote" || package == "@geelen/mcp-remote" {
                            findings.extend(db.create_findings(
                                "geelen",
                                "mcp-remote",
                                clean_version,
                                &path_str,
                                1,
                            ));
                        }
                    }
                }
            }
        }

        // Check for VS Code extensions (in extensions.json)
        if path.file_name().and_then(|n| n.to_str()) == Some("extensions.json")
            && let Some(recommendations) = json.get("recommendations").and_then(|r| r.as_array())
        {
            for ext in recommendations {
                if let Some(ext_id) = ext.as_str() {
                    // claude-code extension: anthropic.claude-code
                    if ext_id.to_lowercase().contains("claude-code") {
                        // For extension recommendations, we can't get version easily
                        // Just warn that this extension may be affected
                        info!(
                            path = %path_str,
                            "Claude Code extension detected. Please ensure it's updated to v1.5.0+"
                        );
                    }
                }
            }
        }

        // Check for MCP server configurations that might reference known packages
        if let Some(servers) = json.get("mcpServers").and_then(|s| s.as_object()) {
            for (server_name, server_config) in servers {
                // Check for mcp-remote usage
                if let Some(command) = server_config.get("command").and_then(|c| c.as_str())
                    && (command.contains("mcp-remote") || command.contains("npx mcp-remote"))
                {
                    // Try to find version from args or assume latest
                    findings.extend(db.create_findings(
                        "geelen",
                        "mcp-remote",
                        "0.0.0", // Unknown version - will match all affected
                        &path_str,
                        1,
                    ));
                }

                // Check if server_name suggests mcp-inspector usage
                if server_name.contains("inspector")
                    || server_config
                        .get("command")
                        .and_then(|c| c.as_str())
                        .is_some_and(|c| c.contains("mcp-inspector"))
                {
                    findings.extend(db.create_findings(
                        "anthropic",
                        "mcp-inspector",
                        "0.0.0", // Unknown version
                        &path_str,
                        1,
                    ));
                }
            }
        }
    }

    findings
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::IgnoreConfig;
    use std::io::Write;
    use tempfile::TempDir;

    fn create_default_filter(_path: &Path) -> IgnoreFilter {
        IgnoreFilter::from_config(&IgnoreConfig::default())
    }

    #[test]
    fn test_scan_path_with_cve_db_empty() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.txt");

        let mut file = fs::File::create(&file_path).unwrap();
        writeln!(file, "Not a relevant file").unwrap();

        let db = CveDatabase::default();
        let filter = create_default_filter(temp_dir.path());
        let findings = scan_path_with_cve_db(&file_path, &db, &filter);
        assert!(findings.is_empty());
    }

    #[test]
    fn test_scan_path_with_cve_db_package_json() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("package.json");

        let mut file = fs::File::create(&file_path).unwrap();
        writeln!(
            file,
            r#"{{
            "dependencies": {{
                "express": "^4.0.0"
            }}
        }}"#
        )
        .unwrap();

        let db = CveDatabase::default();
        let filter = create_default_filter(temp_dir.path());
        let findings = scan_path_with_cve_db(&file_path, &db, &filter);
        // No CVEs for express in our database
        assert!(findings.is_empty());
    }

    #[test]
    fn test_cve_relevant_files() {
        assert!(CVE_RELEVANT_FILES.contains(&"package.json"));
        assert!(CVE_RELEVANT_FILES.contains(&"package-lock.json"));
        assert!(CVE_RELEVANT_FILES.contains(&"extensions.json"));
        assert!(CVE_RELEVANT_FILES.contains(&"mcp.json"));
        assert!(CVE_RELEVANT_FILES.contains(&"mcp_config.json"));
    }

    #[test]
    fn test_scan_with_mcp_inspector_package() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("package.json");

        let mut file = fs::File::create(&file_path).unwrap();
        writeln!(
            file,
            r#"{{
            "dependencies": {{
                "mcp-inspector": "0.1.0"
            }}
        }}"#
        )
        .unwrap();

        let db = CveDatabase::default();
        let filter = create_default_filter(temp_dir.path());
        // This tests the mcp-inspector code path
        let _findings = scan_path_with_cve_db(&file_path, &db, &filter);
    }

    #[test]
    fn test_scan_with_mcp_remote_package() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("package.json");

        let mut file = fs::File::create(&file_path).unwrap();
        writeln!(
            file,
            r#"{{
            "devDependencies": {{
                "@geelen/mcp-remote": "0.0.1"
            }}
        }}"#
        )
        .unwrap();

        let db = CveDatabase::default();
        let filter = create_default_filter(temp_dir.path());
        let findings = scan_path_with_cve_db(&file_path, &db, &filter);
        // May find CVE for mcp-remote
        assert!(findings.is_empty() || !findings.is_empty());
    }

    #[test]
    fn test_scan_with_anthropic_mcp_inspector() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("package.json");

        let mut file = fs::File::create(&file_path).unwrap();
        writeln!(
            file,
            r#"{{
            "peerDependencies": {{
                "@anthropic/mcp-inspector": "0.2.0"
            }}
        }}"#
        )
        .unwrap();

        let db = CveDatabase::default();
        let filter = create_default_filter(temp_dir.path());
        let findings = scan_path_with_cve_db(&file_path, &db, &filter);
        // Test that the function handles this package
        assert!(findings.is_empty() || !findings.is_empty());
    }

    #[test]
    fn test_scan_extensions_json() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("extensions.json");

        let mut file = fs::File::create(&file_path).unwrap();
        writeln!(
            file,
            r#"{{
            "recommendations": [
                "anthropic.claude-code",
                "ms-python.python"
            ]
        }}"#
        )
        .unwrap();

        let db = CveDatabase::default();
        let filter = create_default_filter(temp_dir.path());
        let findings = scan_path_with_cve_db(&file_path, &db, &filter);
        // Should handle extensions.json
        assert!(findings.is_empty());
    }

    #[test]
    fn test_scan_mcp_config_with_servers() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("mcp.json");

        let mut file = fs::File::create(&file_path).unwrap();
        writeln!(
            file,
            r#"{{
            "mcpServers": {{
                "my-remote": {{
                    "command": "npx mcp-remote"
                }}
            }}
        }}"#
        )
        .unwrap();

        let db = CveDatabase::default();
        let filter = create_default_filter(temp_dir.path());
        let findings = scan_path_with_cve_db(&file_path, &db, &filter);
        // Should check mcp-remote in mcpServers
        assert!(findings.is_empty() || !findings.is_empty());
    }

    #[test]
    fn test_scan_mcp_config_with_inspector() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("mcp_config.json");

        let mut file = fs::File::create(&file_path).unwrap();
        writeln!(
            file,
            r#"{{
            "mcpServers": {{
                "inspector": {{
                    "command": "node mcp-inspector"
                }}
            }}
        }}"#
        )
        .unwrap();

        let db = CveDatabase::default();
        let filter = create_default_filter(temp_dir.path());
        let findings = scan_path_with_cve_db(&file_path, &db, &filter);
        // Should check inspector server
        assert!(findings.is_empty() || !findings.is_empty());
    }

    #[test]
    fn test_scan_directory() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("package.json");

        let mut file = fs::File::create(&file_path).unwrap();
        writeln!(
            file,
            r#"{{
            "dependencies": {{
                "express": "^4.0.0"
            }}
        }}"#
        )
        .unwrap();

        let db = CveDatabase::default();
        let filter = create_default_filter(temp_dir.path());
        // Scan the directory instead of the file
        let findings = scan_path_with_cve_db(temp_dir.path(), &db, &filter);
        assert!(findings.is_empty());
    }

    #[test]
    fn test_scan_nonexistent_path() {
        let temp_dir = TempDir::new().unwrap();
        let db = CveDatabase::default();
        let filter = create_default_filter(temp_dir.path());
        let findings = scan_path_with_cve_db(Path::new("/nonexistent/path"), &db, &filter);
        assert!(findings.is_empty());
    }

    #[test]
    fn test_scan_path_respects_ignore_patterns() {
        let temp_dir = TempDir::new().unwrap();

        // Create a package.json in an ignored directory
        let ignored_dir = temp_dir.path().join("node_modules").join("some-pkg");
        fs::create_dir_all(&ignored_dir).unwrap();
        let ignored_file = ignored_dir.join("package.json");
        let mut file = fs::File::create(&ignored_file).unwrap();
        writeln!(
            file,
            r#"{{
            "dependencies": {{
                "mcp-inspector": "0.1.0"
            }}
        }}"#
        )
        .unwrap();

        // Default config ignores node_modules
        let filter = create_default_filter(temp_dir.path());

        let db = CveDatabase::default();
        let findings = scan_path_with_cve_db(temp_dir.path(), &db, &filter);

        // Should be empty because node_modules is ignored by default
        assert!(findings.is_empty());
    }

    #[test]
    fn test_check_content_for_cves_invalid_json() {
        let db = CveDatabase::default();
        let findings = check_content_for_cves("not valid json", Path::new("test.json"), &db);
        assert!(findings.is_empty());
    }

    #[test]
    fn test_check_content_with_non_string_version() {
        let db = CveDatabase::default();
        let content = r#"{
            "dependencies": {
                "some-package": 123
            }
        }"#;
        let findings = check_content_for_cves(content, Path::new("package.json"), &db);
        assert!(findings.is_empty());
    }
}