cc-audit 3.6.0

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
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
//! AI coding client detection and configuration paths.
//!
//! This module provides functionality to detect installed AI coding clients
//! (Claude, Cursor, Windsurf, VS Code) and locate their configuration files.

use crate::rules::ParseEnumError;
use clap::ValueEnum;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// Supported AI coding clients.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ClientType {
    /// Claude Desktop / Claude Code
    Claude,
    /// Cursor IDE
    Cursor,
    /// Windsurf IDE
    Windsurf,
    /// VS Code with MCP extensions
    Vscode,
}

impl std::str::FromStr for ClientType {
    type Err = ParseEnumError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().replace(['-', '_'], "").as_str() {
            "claude" | "claudecode" | "claudedesktop" => Ok(ClientType::Claude),
            "cursor" => Ok(ClientType::Cursor),
            "windsurf" => Ok(ClientType::Windsurf),
            "vscode" | "code" => Ok(ClientType::Vscode),
            _ => Err(ParseEnumError::invalid("ClientType", s)),
        }
    }
}

impl ClientType {
    /// Returns all supported client types.
    pub fn all() -> &'static [ClientType] {
        &[
            ClientType::Claude,
            ClientType::Cursor,
            ClientType::Windsurf,
            ClientType::Vscode,
        ]
    }

    /// Returns the display name of the client.
    pub fn display_name(&self) -> &'static str {
        match self {
            ClientType::Claude => "Claude",
            ClientType::Cursor => "Cursor",
            ClientType::Windsurf => "Windsurf",
            ClientType::Vscode => "VS Code",
        }
    }

    /// Returns the home directory path for this client.
    /// Returns `None` if the home directory cannot be determined.
    pub fn home_dir(&self) -> Option<PathBuf> {
        match self {
            ClientType::Claude => Self::claude_home_dir(),
            ClientType::Cursor => Self::cursor_home_dir(),
            ClientType::Windsurf => Self::windsurf_home_dir(),
            ClientType::Vscode => Self::vscode_home_dir(),
        }
    }

    #[cfg(target_os = "windows")]
    fn claude_home_dir() -> Option<PathBuf> {
        dirs::data_dir().map(|d| d.join("Claude"))
    }

    #[cfg(not(target_os = "windows"))]
    fn claude_home_dir() -> Option<PathBuf> {
        dirs::home_dir().map(|d| d.join(".claude"))
    }

    #[cfg(target_os = "windows")]
    fn cursor_home_dir() -> Option<PathBuf> {
        dirs::data_dir().map(|d| d.join("Cursor"))
    }

    #[cfg(not(target_os = "windows"))]
    fn cursor_home_dir() -> Option<PathBuf> {
        dirs::home_dir().map(|d| d.join(".cursor"))
    }

    #[cfg(target_os = "windows")]
    fn windsurf_home_dir() -> Option<PathBuf> {
        // Windsurf may not be available on Windows yet
        dirs::data_dir().map(|d| d.join("Windsurf"))
    }

    #[cfg(not(target_os = "windows"))]
    fn windsurf_home_dir() -> Option<PathBuf> {
        dirs::home_dir().map(|d| d.join(".windsurf"))
    }

    #[cfg(target_os = "windows")]
    fn vscode_home_dir() -> Option<PathBuf> {
        dirs::data_dir().map(|d| d.join("Code"))
    }

    #[cfg(not(target_os = "windows"))]
    fn vscode_home_dir() -> Option<PathBuf> {
        dirs::home_dir().map(|d| d.join(".vscode"))
    }

    /// Returns the MCP configuration file paths for this client.
    pub fn mcp_config_paths(&self) -> Vec<PathBuf> {
        let Some(home) = self.home_dir() else {
            return Vec::new();
        };

        match self {
            ClientType::Claude => vec![
                home.join("mcp.json"),
                home.join("claude_desktop_config.json"),
            ],
            ClientType::Cursor => vec![home.join("mcp.json")],
            ClientType::Windsurf => vec![home.join("mcp_config.json")],
            ClientType::Vscode => {
                // VS Code MCP extensions store config in globalStorage
                let mut paths = Vec::new();
                if let Some(data_dir) = dirs::data_dir() {
                    // Roo-Cline extension
                    paths.push(
                        data_dir
                            .join("Code")
                            .join("User")
                            .join("globalStorage")
                            .join("rooveterinaryinc.roo-cline")
                            .join("settings")
                            .join("cline_mcp_settings.json"),
                    );
                    // Claude Dev extension
                    paths.push(
                        data_dir
                            .join("Code")
                            .join("User")
                            .join("globalStorage")
                            .join("saoudrizwan.claude-dev")
                            .join("settings")
                            .join("cline_mcp_settings.json"),
                    );
                }
                paths
            }
        }
    }

    /// Returns the settings/hooks configuration file paths for this client.
    pub fn settings_config_paths(&self) -> Vec<PathBuf> {
        let Some(home) = self.home_dir() else {
            return Vec::new();
        };

        match self {
            ClientType::Claude => vec![home.join("settings.json")],
            ClientType::Cursor => vec![home.join("settings.json")],
            ClientType::Windsurf => vec![home.join("settings.json")],
            ClientType::Vscode => vec![],
        }
    }

    /// Checks if this client is installed on the system.
    pub fn is_installed(&self) -> bool {
        self.home_dir().map(|p| p.exists()).unwrap_or(false)
    }

    /// Returns all scannable paths for this client (MCP + settings).
    pub fn all_config_paths(&self) -> Vec<PathBuf> {
        let mut paths = self.mcp_config_paths();
        paths.extend(self.settings_config_paths());
        paths
    }
}

impl std::fmt::Display for ClientType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.display_name())
    }
}

/// Information about a detected AI coding client.
#[derive(Debug, Clone)]
pub struct DetectedClient {
    /// The type of client.
    pub client_type: ClientType,
    /// The home directory of the client.
    pub home_dir: PathBuf,
    /// Existing MCP configuration files.
    pub mcp_configs: Vec<PathBuf>,
    /// Existing settings configuration files.
    pub settings_configs: Vec<PathBuf>,
}

impl DetectedClient {
    /// Returns all existing configuration files for this client.
    pub fn all_configs(&self) -> Vec<PathBuf> {
        let mut configs = self.mcp_configs.clone();
        configs.extend(self.settings_configs.clone());
        configs
    }

    /// Returns true if any configuration files exist.
    pub fn has_configs(&self) -> bool {
        !self.mcp_configs.is_empty() || !self.settings_configs.is_empty()
    }
}

/// Detects all installed AI coding clients on the system.
///
/// Returns a list of detected clients with their configuration file paths.
/// Only clients with at least one existing configuration file are returned.
pub fn detect_installed_clients() -> Vec<DetectedClient> {
    ClientType::all()
        .iter()
        .filter_map(|ct| detect_client(*ct))
        .collect()
}

/// Detects a specific client type.
///
/// Returns `None` if the client is not installed or has no configuration files.
pub fn detect_client(client_type: ClientType) -> Option<DetectedClient> {
    let home = client_type.home_dir()?;

    if !home.exists() {
        return None;
    }

    let mcp_configs: Vec<PathBuf> = client_type
        .mcp_config_paths()
        .into_iter()
        .filter(|p| p.exists())
        .collect();

    let settings_configs: Vec<PathBuf> = client_type
        .settings_config_paths()
        .into_iter()
        .filter(|p| p.exists())
        .collect();

    // Only return if at least one config file exists
    if mcp_configs.is_empty() && settings_configs.is_empty() {
        return None;
    }

    Some(DetectedClient {
        client_type,
        home_dir: home,
        mcp_configs,
        settings_configs,
    })
}

/// Lists all installed clients (even without configuration files).
pub fn list_installed_clients() -> Vec<ClientType> {
    ClientType::all()
        .iter()
        .filter(|ct| ct.is_installed())
        .copied()
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_client_type_display_name() {
        assert_eq!(ClientType::Claude.display_name(), "Claude");
        assert_eq!(ClientType::Cursor.display_name(), "Cursor");
        assert_eq!(ClientType::Windsurf.display_name(), "Windsurf");
        assert_eq!(ClientType::Vscode.display_name(), "VS Code");
    }

    #[test]
    fn test_client_type_all() {
        let all = ClientType::all();
        assert_eq!(all.len(), 4);
        assert!(all.contains(&ClientType::Claude));
        assert!(all.contains(&ClientType::Cursor));
        assert!(all.contains(&ClientType::Windsurf));
        assert!(all.contains(&ClientType::Vscode));
    }

    #[test]
    fn test_client_type_display() {
        assert_eq!(format!("{}", ClientType::Claude), "Claude");
        assert_eq!(format!("{}", ClientType::Cursor), "Cursor");
    }

    #[test]
    fn test_home_dir_returns_some() {
        // Home dir should always be resolvable on a real system
        for ct in ClientType::all() {
            let home = ct.home_dir();
            assert!(home.is_some(), "home_dir() should return Some for {:?}", ct);
        }
    }

    #[cfg(not(target_os = "windows"))]
    #[test]
    fn test_claude_home_dir_unix() {
        let home = ClientType::Claude.home_dir();
        assert!(home.is_some());
        let path = home.unwrap();
        assert!(path.to_string_lossy().contains(".claude"));
    }

    #[cfg(not(target_os = "windows"))]
    #[test]
    fn test_cursor_home_dir_unix() {
        let home = ClientType::Cursor.home_dir();
        assert!(home.is_some());
        let path = home.unwrap();
        assert!(path.to_string_lossy().contains(".cursor"));
    }

    #[test]
    fn test_mcp_config_paths_not_empty() {
        // All clients should have at least one potential MCP config path
        for ct in ClientType::all() {
            let paths = ct.mcp_config_paths();
            assert!(
                !paths.is_empty() || *ct == ClientType::Vscode,
                "mcp_config_paths() should not be empty for {:?}",
                ct
            );
        }
    }

    #[test]
    fn test_detected_client_has_configs() {
        let client = DetectedClient {
            client_type: ClientType::Claude,
            home_dir: PathBuf::from("/tmp/claude"),
            mcp_configs: vec![PathBuf::from("/tmp/claude/mcp.json")],
            settings_configs: vec![],
        };
        assert!(client.has_configs());

        let empty_client = DetectedClient {
            client_type: ClientType::Claude,
            home_dir: PathBuf::from("/tmp/claude"),
            mcp_configs: vec![],
            settings_configs: vec![],
        };
        assert!(!empty_client.has_configs());
    }

    #[test]
    fn test_detected_client_all_configs() {
        let client = DetectedClient {
            client_type: ClientType::Claude,
            home_dir: PathBuf::from("/tmp/claude"),
            mcp_configs: vec![PathBuf::from("/tmp/claude/mcp.json")],
            settings_configs: vec![PathBuf::from("/tmp/claude/settings.json")],
        };
        let all = client.all_configs();
        assert_eq!(all.len(), 2);
    }

    #[test]
    fn test_client_type_serialize() {
        let json = serde_json::to_string(&ClientType::Claude).unwrap();
        assert_eq!(json, "\"claude\"");

        let json = serde_json::to_string(&ClientType::Vscode).unwrap();
        assert_eq!(json, "\"vscode\"");
    }

    #[test]
    fn test_client_type_deserialize() {
        let ct: ClientType = serde_json::from_str("\"claude\"").unwrap();
        assert_eq!(ct, ClientType::Claude);

        let ct: ClientType = serde_json::from_str("\"vscode\"").unwrap();
        assert_eq!(ct, ClientType::Vscode);
    }

    #[test]
    fn test_client_type_from_str() {
        use std::str::FromStr;

        // Standard names
        assert_eq!(
            <ClientType as FromStr>::from_str("claude").unwrap(),
            ClientType::Claude
        );
        assert_eq!(
            <ClientType as FromStr>::from_str("cursor").unwrap(),
            ClientType::Cursor
        );
        assert_eq!(
            <ClientType as FromStr>::from_str("windsurf").unwrap(),
            ClientType::Windsurf
        );
        assert_eq!(
            <ClientType as FromStr>::from_str("vscode").unwrap(),
            ClientType::Vscode
        );

        // Alternate names
        assert_eq!(
            <ClientType as FromStr>::from_str("claudecode").unwrap(),
            ClientType::Claude
        );
        assert_eq!(
            <ClientType as FromStr>::from_str("claude-code").unwrap(),
            ClientType::Claude
        );
        assert_eq!(
            <ClientType as FromStr>::from_str("claude_desktop").unwrap(),
            ClientType::Claude
        );
        assert_eq!(
            <ClientType as FromStr>::from_str("code").unwrap(),
            ClientType::Vscode
        );

        // Case insensitive
        assert_eq!(
            <ClientType as FromStr>::from_str("CLAUDE").unwrap(),
            ClientType::Claude
        );
        assert_eq!(
            <ClientType as FromStr>::from_str("Cursor").unwrap(),
            ClientType::Cursor
        );

        // Invalid
        assert!(<ClientType as FromStr>::from_str("invalid").is_err());
        assert!(<ClientType as FromStr>::from_str("").is_err());
    }

    #[test]
    fn test_client_type_all_variants() {
        let all = ClientType::all();
        assert_eq!(all.len(), 4);
        assert!(all.contains(&ClientType::Claude));
        assert!(all.contains(&ClientType::Cursor));
        assert!(all.contains(&ClientType::Windsurf));
        assert!(all.contains(&ClientType::Vscode));
    }

    #[test]
    fn test_client_type_home_dir() {
        // Home dir should return Some on most systems
        let claude_home = ClientType::Claude.home_dir();
        let cursor_home = ClientType::Cursor.home_dir();
        let windsurf_home = ClientType::Windsurf.home_dir();
        let vscode_home = ClientType::Vscode.home_dir();

        // These should all succeed on a normal system
        assert!(claude_home.is_some());
        assert!(cursor_home.is_some());
        assert!(windsurf_home.is_some());
        assert!(vscode_home.is_some());
    }

    #[test]
    fn test_client_type_display_name_all() {
        assert_eq!(ClientType::Claude.display_name(), "Claude");
        assert_eq!(ClientType::Cursor.display_name(), "Cursor");
        assert_eq!(ClientType::Windsurf.display_name(), "Windsurf");
        assert_eq!(ClientType::Vscode.display_name(), "VS Code");
    }

    #[test]
    fn test_windsurf_home_dir() {
        // Specific test for windsurf
        let home = ClientType::Windsurf.home_dir();
        assert!(home.is_some());
        #[cfg(not(target_os = "windows"))]
        {
            let path = home.unwrap();
            assert!(path.to_string_lossy().contains(".windsurf"));
        }
    }

    #[test]
    fn test_vscode_home_dir() {
        // Specific test for vscode
        let home = ClientType::Vscode.home_dir();
        assert!(home.is_some());
    }

    #[test]
    fn test_is_installed_checks_path_exists() {
        // is_installed should return false for non-existent path
        // Since we can't mock home_dir, we just verify the method works
        for ct in ClientType::all() {
            let _ = ct.is_installed();
        }
    }

    #[test]
    fn test_all_config_paths_combines_mcp_and_settings() {
        // all_config_paths should return both MCP and settings paths
        for ct in ClientType::all() {
            let all = ct.all_config_paths();
            let mcp = ct.mcp_config_paths();
            let settings = ct.settings_config_paths();
            assert_eq!(all.len(), mcp.len() + settings.len());
        }
    }

    #[test]
    fn test_settings_config_paths() {
        // Claude, Cursor, Windsurf should have settings paths
        assert!(!ClientType::Claude.settings_config_paths().is_empty());
        assert!(!ClientType::Cursor.settings_config_paths().is_empty());
        assert!(!ClientType::Windsurf.settings_config_paths().is_empty());
        // VS Code doesn't have a settings path
        assert!(ClientType::Vscode.settings_config_paths().is_empty());
    }

    #[test]
    fn test_list_installed_clients() {
        // This function should return a vector of installed clients
        // We can't guarantee which clients are installed, but we can verify it doesn't panic
        let installed = list_installed_clients();
        // All returned clients should have is_installed() == true
        for client in &installed {
            assert!(client.is_installed());
        }
    }

    #[test]
    fn test_vscode_mcp_config_paths() {
        // VS Code MCP config paths should include Roo-Cline and Claude Dev extensions
        let paths = ClientType::Vscode.mcp_config_paths();
        // The paths depend on whether dirs::data_dir() returns Some
        // On most systems this should return paths
        if !paths.is_empty() {
            for path in &paths {
                assert!(path.to_string_lossy().contains("cline_mcp_settings.json"));
            }
        }
    }
}